tensorflow入门,完成1元1次方程拟合
该一元一次方程为:
x_data = np.float32(np.random.rand(2, 100)) # 随机输入
y_data = np.dot([0.100, 0.200], x_data) + 0.300
由于是x_data是随机数,故而无法画出图形,若我们定义x为等差数列[[0,1,2,...100],[101...200]]
则y=ax+b为分段方程;x在[1,100] 时,y=0.100x+0.3;x在[101,200]时,y=0.200x+0.3
我们今天要做的,就是使用tensorflow来求解系数0.100和0.200以及偏置0.3
以下是代码和详细描述:
import tensorflow as tf
import numpy as np
x_data=np.float32(np.random.rand(2,100))
y_data=np.dot([0.100,0.200],x_data)+0.300
# In [18]: y_data
# Out[18]:
# array([0.3775784 , 0.44246963, 0.54814347, 0.47920347, 0.44502148,
# 0.47671588, 0.52602917, 0.42399144, 0.55421975, 0.56123726,
# 0.43116061, 0.58739868, 0.56753602, 0.33052424, 0.51231386,
# 0.35698801, 0.41138735, 0.43066818, 0.45888491, 0.51811442,
# 0.37335225, 0.35873668, 0.49872529, 0.51496372, 0.4708255 ,
# 0.33057731, 0.49321586, 0.52910891, 0.48146467, 0.4564127 ,
# 0.49315754, 0.47433195, 0.41521251, 0.47231215, 0.36476854,
# 0.56163432, 0.48083746, 0.36155672, 0.45484892, 0.47454886,
# 0.35468251, 0.58545158, 0.52509505, 0.51380747, 0.40301575,
# 0.49873314, 0.41870016, 0.51702609, 0.38683276, 0.34510854,
# 0.43597745, 0.33068472, 0.45829301, 0.53734922, 0.37926108,
# 0.47578884, 0.50000341, 0.47377452, 0.33492953, 0.54954112,
# 0.49541566, 0.54095768, 0.4495868 , 0.4871654 , 0.51237076,
# 0.48808393, 0.48120754, 0.519952 , 0.50792828, 0.52414889,
# 0.4598966 , 0.53859273, 0.35056699, 0.52637514, 0.37148828,
# 0.52328432, 0.45714943, 0.39835291, 0.47680219, 0.48079917,
# 0.52709996, 0.58097438, 0.31558467, 0.55858573, 0.44779962,
# 0.46960804, 0.32776277, 0.40463255, 0.36816873, 0.40686887,
# 0.53873119, 0.491694 , 0.40990473, 0.48624806, 0.4489985 ,
# 0.49574559, 0.48093255, 0.38655084, 0.48854129, 0.46561137])
#真实数据x_data,y_data
#使用神经网络来进行拟合,假设我们不懂y_dat与x_data之间s的关系
#定义我们使用的weight
#定义一个一行两列的weight
#因为我们的参数就是1行两列
w=tf.Variable(tf.random_uniform([1,2],-1.0,1.0))
#定义我们的weight为1行两列,范围从-1.0到1.0之间
#定义我们的偏置,原方程只有1个偏置,故我们定义一个1行1列的偏置
b=tf.Variable(tf.zeros([1]))
# In [28]: w
# Out[28]: <tf.Variable 'Variable:0' shape=(1, 2) dtype=float32_ref>
# In [29]: b
# Out[29]: <tf.Variable 'Variable_1:0' shape=(1,) dtype=float32_ref>
#3.定义我们的损失函数,目标为最小化prediction与y_data之间的偏差
prediction=tf.matmul(w,x_data)+b
loss=tf.reduce_mean(tf.square(prediction-y_data))
#4.定义我们的优化器,其实影响的是反向c传播的梯度,该方法我们也可以自定义
optimizer=tf.train.GradientDescentOptimizer(0.5)
#0.1是学习率,就是一个变化系数,网络根据prediction-y_data的大小,进行求导后,w变化的值=(prediction-y_data)*学习率*梯度*梯度方向
train=optimizer.minimize(loss)
#定义训练为最小化误差loss
#开始tensorflow框架的运用
init=tf.initialize_all_variables()
with tf.Session() as sess:
sess.run(init)
for step in range(201):
sess.run(train)
if step%20==0:
print("step{0},w={1},b={2}".format(step,sess.run(w),sess.run(b)))
得到结果:
step0,w=[[0.09073986 0.25432885]],b=[0.06143282]
step20,w=[[0.14288965 0.30549458]],b=[0.2138709]
step40,w=[[0.13310951 0.28356478]],b=[0.2322896]
step60,w=[[0.12557037 0.2661502 ]],b=[0.24672177]
step80,w=[[0.11976979 0.25235054]],b=[0.2580708]
step100,w=[[0.11530145 0.24141914]],b=[0.26699725]
step120,w=[[0.11185528 0.23276275]],b=[0.2740195]
step140,w=[[0.10919438 0.22590993]],b=[0.27954486]
step160,w=[[0.10713752 0.2204864 ]],b=[0.2838931]
step180,w=[[0.10554589 0.21619518]],b=[0.28731555]
step200,w=[[0.10431294 0.21280068]],b=[0.29000968]
经过200次迭代后,w=[0.104,0.212] b=0.290 与真实值存在一定偏差,可通过调整学习率+loss+迭代次数获得更好的拟合效果