自己构建一些数据,来求Weights和Biases
#create data
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
#create tensorflow structure
Weights = tf.Variable(tf.random_uniform([1], -1.0, 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)
with tf.Session() as sess:
sess.run(init)
for step in range(201):
sess.run(train)
if step % 20 == 0:
print(step, sess.run(weights), sess.run(biases))
输出结果:
0 [-0.53141755] [0.01215398] 20 [-0.42150587] [0.19936536] 40 [-0.3514395] [0.31025246] 60 [-0.30549625] [0.37518197] 80 [-0.27419537] [0.41245845] 100 [-0.2518188] [0.4331152] 120 [-0.23491696] [0.44380206] 140 [-0.22140765] [0.4485264] 160 [-0.21003328] [0.44970292] 180 [-0.20003326] [0.44878575] 200 [-0.19094667] [0.44665036]