vim test.py
[IN]
import tensorflow as tf
import numpy as np
x_data = np.float32(np.random.rand(2, 100))
y_data = np.dot([0.100, 0.200], x_data) + 0.300
b = tf.Variable(tf.zeros([1]))
W = tf.Variable(tf.random_uniform([1, 2], -1.0, 1.0))
y = tf.matmul(W, x_data) + b
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 step in xrange(0, 201):
sess.run(train)
if step % 20 == 0:
print step, sess.run(W), sess.run(b)
python test.py
[OUT]
0 [[ 0.51267505 0.66411674]] [-0.39591151]
20 [[ 0.34062019 0.38497368]] [ 0.0721921]
40 [[ 0.20607311 0.27231586]] [ 0.20419313]
60 [[ 0.14546445 0.22976567]] [ 0.25955138]
80 [[ 0.11931396 0.21247624]] [ 0.28290126]
100 [[ 0.10818116 0.20526144]] [ 0.29276887]
120 [[ 0.10346215 0.20222335]] [ 0.29694149]
140 [[ 0.10146467 0.20094013]] [ 0.29870632]
160 [[ 0.10061956 0.20039763]] [ 0.29945278]
180 [[ 0.10026208 0.20016818]] [ 0.29976854]
200 [[ 0.10011085 0.20007114]] [ 0.29990211]