安装tensorflow之后,先用命令查看一下相关版本及路径,然后再开始完成第一个梯度下降优化的小案例。
import tensorflow as tf
tf.__version__
'1.3.0'
import sys
print(sys.version)
3.5.2 |Anaconda 4.2.0 (64-bit)| (default, Jul 2 2016, 17:53:06)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]
tf.__path__
['/root/anaconda3/lib/python3.5/site-packages/tensorflow']
import numpy as np
x_data = np.random.rand(100).astype(np.float32) #tensorflow中大部分数据是float32格式
y_data = x_data*0.1+0.3 #weight接近0.1(一维),bias接近0.3
## create tensorflow structure start
#Weights参数变量初始值由随机数列生成[1]一维,范围(-1.0,1.0)
Weights = tf.Variable(tf.random_uniform([1],-1.0,1.0))
biases = tf.Variable(tf.zeros([1]))#biases初始值0
y = Weights*x_data +biases
loss = tf.reduce_mean(tf.square(y-y_data))
#GradientDescentOptimizer随机梯度下降优化器,learning rate一般<1,此处设为0.5
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss) #用优化器减小误差
#初始化所有变量
init = tf.global_variables_initializer() #Use `tf.global_variables_initializer` instead.
#激活session
sess = tf.Session()
sess.run(init)
#开始神经网络训练201次
for step in range(201):
sess.run(train)
if step % 20 == 0:
print(step, sess.run(Weights),sess.run(biases))
0 [ 0.14329368] [ 0.38283098]
20 [ 0.09969744] [ 0.3001636]
40 [ 0.09992074] [ 0.30004287]
60 [ 0.09997924] [ 0.30001125]
80 [ 0.09999458] [ 0.30000296]
100 [ 0.09999861] [ 0.30000076]
120 [ 0.09999964] [ 0.30000022]
140 [ 0.09999991] [ 0.30000007]
160 [ 0.09999991] [ 0.30000007]
180 [ 0.09999991] [ 0.30000007]
200 [ 0.09999991] [ 0.30000007]
来源:CSDN
作者:Zen of Data Analysis
链接:https://blog.csdn.net/gracejpw/article/details/104108011