MoFan.代码
1、
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 # 创建 tensorflow结构体(开始) weights = tf.Variable(tf.random_uniform([1], -0.1, 0.1)) 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 ) init = tf.initialize_all_variables() # 创建 tensorflow结构体(结束) sess = tf.Session() sess.run(init) # 非常重要 for step in range(201): sess.run( train ) if step % 20 == 0: print(step, sess.run(weights), sess.run(biases))
2、
3、
4、
5、