1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| import tensorflow as tf import numpy as np
x_data = np.random.rand(100).astype(np.float32) y_data = x_data*0.1 + 0.3
Weights = tf.Variable(tf.random_uniform([1],-1,0)) biases = tf.Variable(tf.zeros([1]))
y = Weights*x_data + biases
loss = tf.reduce_mean(tf.square(y-y_data)) optimizer = tf.train.GradientDescentOptimizer(0.5) train = optimizer.minimize(loss)
init = tf.global_variables_initializer()
sess = tf.Session() sess.run(init)
for step in range(401): sess.run(train) if step % 20 == 0: print(step,sess.run(Weights),sess.run(biases))
|