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 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) 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)) 0 [0.7417692] [-0.07732911] 20 [0.30772722] [0.18689097] 40 [0.16603212] [0.26404503] 60 [0.12099022] [0.28857067] 80 [0.10667235] [0.29636687] 100 [0.10212099] [0.2988451] 120 [0.10067423] [0.29963288] 140 [0.10021434] [0.2998833] 160 [0.10006816] [0.2999629] 180 [0.10002167] [0.2999882] 200 [0.10000689] [0.29999626]
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:TensorFlow1.0 线性回归 - Python技术站