二维的线性模型
打开ipython====》桌面建立bat文件:cmd /k "cd /d f:\DeepLearning\TensorFlow &&ipython notebook",双击运行,在网页内编辑运行
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
W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
b = tf.Variable(tf.zeros([1]))
y = 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.global_variables_initializer()
sess = tf.Session()
sess.run(init)
for step in range(201):
sess.run(train)
if step % 20 == 0:
print(step, sess.run(W), sess.run(b))
0 [-0.38000667] [ 0.77975672]
20 [-0.06334207] [ 0.38801551]
40 [ 0.05338327] [ 0.32511905]
60 [ 0.0866959] [ 0.30716881]
80 [ 0.09620309] [ 0.30204594]
100 [ 0.09891639] [ 0.3005839]
120 [ 0.09969076] [ 0.30016664]
140 [ 0.09991175] [ 0.30004758]
160 [ 0.09997481] [ 0.30001357]
180 [ 0.09999283] [ 0.30000389]
200 [ 0.09999795] [ 0.30000111]