import numpy as np
import tensorflow as tf
x_data = np.random.rand(100).astype(np.float32)
y_data = x_data*0.1 + 0.3
Weight = tf.Variable(tf.random_uniform([1],-1.0, 1.0))
biases = tf.Variable(tf.zeros([1]))
y = x_data*Weight + biases
loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
for i in range(201):
sess.run(train)
if i % 10 == 0:
print(i, sess.run(Weight), sess.run(biases))
0 [0.5913683] [0.02325284]
10 [0.33041072] [0.17002238]
20 [0.20808482] [0.23902799]
30 [0.1507022] [0.27139825]
40 [0.12378422] [0.28658304]
50 [0.1111571] [0.29370615]
60 [0.10523375] [0.2970476]
70 [0.10245514] [0.29861504]
80 [0.1011517] [0.29935032]
90 [0.10054026] [0.29969525]
100 [0.10025343] [0.29985705]
110 [0.10011888] [0.29993296]
120 [0.10005578] [0.29996854]
130 [0.10002616] [0.29998526]
140 [0.10001229] [0.2999931]
150 [0.10000578] [0.29999676]
160 [0.10000271] [0.2999985]
170 [0.10000128] [0.2999993]
180 [0.1000006] [0.29999968]
190 [0.10000029] [0.29999986]
200 [0.10000014] [0.29999995]
Process finished with exit code 0