Python 程序生成了一些三维数据, 然后用一个平面拟合它.
import tensorflow as tf
import numpy as np
x_data = np.float32(np.random.rand(2,100)) #create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1).
y_data = np.dot([0.100, 0.200], x_data) + 0.300 # Dot product of two arrays
b = tf.Variable(tf.zeros([1]))
W = tf.Variable(tf.random_uniform([1, 2], -1.0, 1.0)) #return an array of the given shape and the value is between -0.1 and 1.0
y= tf.matmul(W, x_data) + b
loss = tf.reduce_mean(tf.square( y - y_data)) # get the value of mean
print loss
optimizer = tf.train.GradientDescentOptimizer(0.5) #0.5 is a learning rate
train = optimizer.minimize(loss)
init = tf.initialize_all_variables()
sess = tf.Session() #Launch the graph in a session.
sess.run(init) #Evaluate the tensor `c`
for step in xrange(0, 401):
sess.run(train)
if step%20 == 0:
print step, sess.run(W), sess.run(b)
0 [[ 0.26362666 0.36850226]] [ 0.27107275]
20 [[ 0.14066522 0.23561597]] [ 0.25837913]
40 [[ 0.11415388 0.21148048]] [ 0.28603336]
60 [[ 0.10485236 0.20377406]] [ 0.29530376]
80 [[ 0.10165044 0.20125468]] [ 0.29841915]
100 [[ 0.10055901 0.20041972]] [ 0.29946756]
120 [[ 0.1001889 0.20014086]] [ 0.29982063]
140 [[ 0.10006375 0.20004736]] [ 0.29993957]
160 [[ 0.1000215 0.20001593]] [ 0.29997963]
180 [[ 0.10000724 0.20000535]] [ 0.29999316]
200 [[ 0.10000244 0.20000181]] [ 0.29999769]
220 [[ 0.10000083 0.20000061]] [ 0.29999921]
240 [[ 0.10000027 0.20000021]] [ 0.29999974]
260 [[ 0.1000001 0.20000009]] [ 0.29999989]
280 [[ 0.1000001 0.20000009]] [ 0.29999989]
300 [[ 0.1000001 0.20000009]] [ 0.29999989]
320 [[ 0.1000001 0.20000009]] [ 0.29999989]
340 [[ 0.1000001 0.20000009]] [ 0.29999989]
360 [[ 0.1000001 0.20000009]] [ 0.29999989]
380 [[ 0.1000001 0.20000009]] [ 0.29999989]
400 [[ 0.1000001 0.20000009]] [ 0.29999989]