TensorFlow+实战Google深度学习框架学习笔记(5)----神经网络训练步骤

时间:2022-08-13 03:31:56

一、TensorFlow实战Google深度学习框架学习

1、步骤:

1、定义神经网络的结构和前向传播的输出结果。

2、定义损失函数以及选择反向传播优化的算法。

3、生成会话(session)并且在训练数据上反复运行反向传播优化算法。

2、代码:

来源:https://blog.csdn.net/longji/article/details/69472310

import tensorflow as tf
from numpy.random import RandomState # 1. 定义神经网络的参数,输入和输出节点
batch_size = 8
w1= tf.Variable(tf.random_normal([2, 3], stddev=1, seed=1))
w2= tf.Variable(tf.random_normal([3, 1], stddev=1, seed=1))
x = tf.placeholder(tf.float32, shape=(None, 2), name="x-input")
y_= tf.placeholder(tf.float32, shape=(None, 1), name='y-input') # 2. 定义前向传播过程,损失函数及反向传播算法
a = tf.matmul(x, w1)
y = tf.matmul(a, w2)
cross_entropy = -tf.reduce_mean(y_ * tf.log(tf.clip_by_value(y, 1e-10, 1.0)))
train_step = tf.train.AdamOptimizer(0.001).minimize(cross_entropy) # 3. 生成模拟数据集
rdm = RandomState(1)
X = rdm.rand(128,2)
Y = [[int(x1+x2 < 1)] for (x1, x2) in X] # 4. 创建一个会话来运行TensorFlow程序
with tf.Session() as sess:
init_op = tf.global_variables_initializer()
sess.run(init_op) # 输出目前(未经训练)的参数取值。
print("w1:", sess.run(w1))
print("w2:", sess.run(w2))
print("\n") # 训练模型。
STEPS = 5000
for i in range(STEPS):
start = (i * batch_size) % 128
end = (i * batch_size) % 128 + batch_size
sess.run(train_step, feed_dict={x: X[start:end], y_: Y[start:end]})
if i % 1000 == 0:
total_cross_entropy = sess.run(cross_entropy, feed_dict={x: X, y_: Y})
print("After %d training step(s), cross entropy on all data is %g" % (i, total_cross_entropy)) # 输出训练后的参数取值。
print("\n")
print("w1:", sess.run(w1))
print("w2:", sess.run(w2)) ''' 输出形式
w1: [[-0.81131822 1.48459876 0.06532937]
[-2.4427042 0.0992484 0.59122431]]
w2: [[-0.81131822]
[ 1.48459876]
[ 0.06532937]] After 0 training step(s), cross entropy on all data is 0.0674925
After 1000 training step(s), cross entropy on all data is 0.0163385
After 2000 training step(s), cross entropy on all data is 0.00907547
After 3000 training step(s), cross entropy on all data is 0.00714436
After 4000 training step(s), cross entropy on all data is 0.00578471 w1: [[-1.9618274 2.58235407 1.68203783]
[-3.46817183 1.06982327 2.11789012]]
w2: [[-1.82471502]
[ 2.68546653]
[ 1.41819513]]
'''

二、莫烦大大的神经网络训练步骤:

1、def add_layer()

添加神经网络层:

import tensorflow as tf
#输入、输入大小、输出大小、激活函数 def add_layer( inputs, in_size, out_size ,activation_function=None) : #weight初始化时生成一个随机变量矩阵比0矩阵效果要好 Weights = tf.Variable( tf.random_normal ( [in_size, out_size])) #biases初始值最好也不要都为0,则biases值全部等于0.1 biases = tf.Variable( tf.zeros([1,out_size]) + 0.1) #相当于Y_predict Wx_plus_b = tf.matmul ( inputs,Weights ) +biases
#如果为线性则outputs不用改变,如果不为线性则用激活函数
if activation_function is None:
outputs = Wx_plus_b
else:
outputs = activation_functions ( Wx_plus_b)
reeturn outputs

2、建立神经网络

#定义数据

x_data = np.linspace ( -1,1,300) [:,np.newaxis]
noise = np.random.normal ( 0,0.05 , x_data.shape)
y_data = np.square( x_data) - 0.5 +noise #建立第一层layer
#一个输入层、一个隐藏层、一个输出层
#输入层:输入多少data就多少个神经元,这里的x只有一个特征属性,则输入层有1个神经元
#隐藏层:自己定义10个
#输出层:输出y只有1个输出 #None表示无论给多少个样本都可以
xs = tf.placeholder(tf.float32, [None, 1])
ys = tf.placeholder(tf.float32, [None,1] ) #add_layer为上面自己建立的函数,这里建立隐藏层
l1 = add_layer( xs , 1 ,10 ,activation_function = tf.nn.relu)
#输出层
predition = add_layer( l1 ,10 , 1,activation_function = None) #算损失函数 , reduction_indices =[1] 按行求和
loss = tf.reduce_mean ( tf.square ( ys -prediction ),
reduction_indices =[1] ) #选择一个优化器,选择:梯度下降,需要给定一个学习率为0.1,通常要小于1
#优化器以0.1的学习效率要减少loss函数,使下一次结果更好
train_step = tf.train.GradientDecentOptimizer( 0.1).minimize (loss) #初始所有变量 init = tf.initialize_all_variables () sess = tf.Session() sess.run(init)
#重复学习1000次
for i in range(1000):
sess.run( train_step , feed_dict = {xs:x_data,ys:y_data})
#每50次打印loss
if i % 50 == 0:
print(sess.run(loss,feed_dict={x:x_data,ys:y_data})