实验前
import tensorflow as tf import numpy as np #create data x_data=np.random.rand(100).astype(np.float32) y_data=x_data*0.1+0.3 ###create tensorflow structure start### 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) ###create tensorflow structure end### train = optimizer.minimize(loss) init = tf.initialize_all_variables() 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))
实验后:
import tensorflow as tf import numpy as np #create data x_data=np.random.rand(100).astype(np.float32) y_data=x_data*0.1+0.3 ###create tensorflow structure start### 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(input_tensor=tf.square(y-y_data)) optimizer = tf.compat.v1.train.GradientDescentOptimizer(0.5) ###create tensorflow structure end### train = optimizer.minimize(loss) init = tf.compat.v1.initialize_all_variables() sess = tf.compat.v1.Session() sess.run(init) for step in range(201): sess.run(train) if step % 20 == 0: print(step, sess.run(Weights), sess.run(biases))
代码对比可看出代码前后的变化
https://blog.csdn.net/u012223913/article/details/79097297
原文:https://www.cnblogs.com/2008nmj/p/11849957.html