Tensorflow
搭建神经网络
-
用张量表示数据
-
用计算图搭建神经网络
-
用绘画执行计算图,优化线上的权重
张量(tensor): 多维数组(列表)
维数 | 阶 | 名字 | 例子 |
---|---|---|---|
0 | 0 | 标量 scalar | 单个数字 s=1,2,3 |
1 | 1 | 向量 vector | v=[1,2,3] |
2 | 2 | 矩阵 matrix | m=[[1,2,3],[4,5,6],[7,8,9]] |
3 | 3 | 张量 tensor | t=[[[… (几个括号是几阶) |
数据类型
tf.float32
tf.int32
计算图
计算图(Graph):搭建神经网络的计算过程,只搭建,不运算
搭建神经网络
-
前向传播:定义输入、参数和输出
x =
y_ =
w1 =
w2 =
a =
y =
-
反向传播:定义损失函数、反向传播方法
loss =
train_step =
-
生成会话,训练STEPS轮
with tf.session() as sess: Init_op = tf.global_variables_initializer() sess.run(init_op) STEPS = 3000 for i in range(STEPS): start = end = sess.run(train_step,feed_dict=)
神经网络的优化
学习率设置多少合适
学习率大了振荡不收敛,学习率小了收敛速度慢
-
指数衰减学习率
global_step = tf.Variable(0,trainable=False) learning_rate = tf.train.exponential_decay( LEARNING_RATE_BASE,#最开始的学习率 gloabal_step,#当前运行到第几轮的计数器 LEARNING_RATE_STEP,#学习率多少轮更新一次 #一般设定为 数据集总样本数/每次喂入多少数据 LEARNING_RATE_DECAY,#学习衰减率(0,1) staircase=True#取整 学习率阶梯型衰减 )
搭建模块化的神经网络八股:
#前向传播就是搭建网络,设计网络结构forward.py
def foward(x,regularizer):
w =
b =
y =
return y
def get_weight(shape,regularizer):
w = tf.Variable() #给w赋初值
tf.add_to_collection('losses',tf.contrib.layer.l2_regularizer(regularizer)(w))
return w
def get_bias(shape):
b = tf.Variable()
return b
#反向传播就是训练网络,优化网络参数 backward.py
def backward():
x = tf.placeholder()
y_ = tf.placeholder()
y = forward.forward(x,REGULARIZER)
global_step = tf.Variable(0,trainable=False)
loss =
''' loss可以是y与y_的差距 loss_mse = tf.reduce_mean(tf.square(y-y_)) 也可以是 交叉熵 ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y,labels=tf.argmax(y_,1)) cem = tf.reduce_mean(ce) 加入正则化后:q loss = y与y_的差距+tf.add(tf.get_collection('losses')) '''
''' 指数衰减学习率 learning_rate = tf.train.exponential_decay( LEARNING_RATE_BASE, global_step, 数据集总样本数/BATCH_SIZE, LEARNING_RATE_DECAY, staircase=True ) '''
''' 滑动平均学习率 ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY,global_step) ema_op = ema.apply(tf.trainable_variables()) with tf.control_dependencies([trains_step,ema_op]): train_op = tf.np_op(name='train') '''
train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss,global_step=global_step)
with tf.Session() as sess:
init_op = tf.global_variables_initializer()
sess.run(init_op)
for i in range(train_steps):
sess.run(train_step,feed_dict={x:,y_:})
if i%轮数 ==0:
print
if __name__ =='__main__':
backward()