【莫凡】
预测 y=0.1x+0.3
# -*- coding: utf-8 -*- """ Created on Thu May 10 15:52:58 2018 @author: sun_y """ 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 statr### 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) #learning rate is 0.5 train=optimizer.minimize(loss) init=tf.initialize_all_variables() ### create tensorflow structure end### 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.48835528] [0.12029712] 20 [0.19531727] [0.24877861] 40 [0.12414611] [0.28702444] 60 [0.10611676] [0.296713] 80 [0.10154952] [0.29916734] 100 [0.10039254] [0.29978907] 120 [0.10009944] [0.29994658] 140 [0.1000252] [0.29998648] 160 [0.10000637] [0.29999658] 180 [0.10000161] [0.29999915] 200 [0.10000043] [0.29999977]