tensorflow基础知识学习(二)---深度前馈神经网络

激活函数

tf.nn.relu(x,name)
tf.nn.relu6(x,name)
tf.nn.softplus(x,name)
tf.nn.softmax(x,name)
tf.sigmoid(x,name)
tf.tanh(x,name)

损失函数

y为真实值
prediction为预测值
1.手动输入型

#不多做举例,不同的激活函数对应不同的损失函数
loss = tf.reduce_mean(tf.square(y-prediction))

2.调用tensorflow方法

#值得注意的是,这两种方法本身都包含了将数值转化成对应概率的步骤,因此前面不需要再添加softmax激活函数
#封装了交叉熵损失函数的softmax函数(封装了sigmoid函数,即直接用z带入即可)  (labels接受的是one-hot标签,类似于[1,0,0][0,1,0][0,0,1])
tf.nn.softmax_cross_entropy_with_logits(_sentinel="",labels="",logits="",dim="",name="")
#更适用于每个类别相互独立且排斥的情况,即一幅图片只属于一个类  (labels接受的是数字标签,类似于[0][1][2])
tf.nn.sparse_softmax_cross_entropy_with_logits(_sentinel="",labels="",logits="",name="")

3.附加

#clip_by_value:剪切张量的损失。如下例代码,将prediction中低于1e-10的数变为1e-10,将高于1的数变为1,防止log溢出
loss = -tf.reduce_mean(y * tf.log(tf.clip_by_value(prediction,1e-10,1.0)))

#分段函数的损失
a,b = 2,5
'''tf.greater(prediction,y)     比较y和prediction的大小,若prediction大则返回True,否则返回False
   tf.where(BOOL,x1,x2) 若BOOL值为True,则返回x1,否则返回x2
'''
loss = tf.reduce_sum(tf.where(tf.greater(prediction,y),(prediction-y)*a),(prediction-y)*b)

反向传播

tf.train.GradientDescentOptimizer()
tf.train.AdagradOptimizer()
tf.train.RMSPropOptimizer()
tf.train.AdamOptimizer()
tf.train.AdagradDAOptimizer()
tf.train.AdadeltaOptimizer()
tf.train.ProximalGradientDescentOptimizer()
tf.train.ProximalAdagradOptimizer()
tf.train.FtrlOptimizer()

自定义学习率

1.指数衰减学习率 (α = decay_rate*α) 其中α为学习率,decay_rate为衰减速率

#用于记录当前学习的轮数
training_rate = tf.Variable(0)
'''exponential_decay   用于学习率的自衰减
   0.8                 学习率的初始值
   training_rate       当前学习的轮数
   100,0.9             每训练100轮学习率更新为原来的0.9
   staircase           当为True时,该衰减才起作用
'''#在最小化时加上定义的training_rate :  .minimize(loss, global_step = training_rate)
decayed_learning_rate = tf.train.exponential_decay(0.8,training_rate,100,0.9,staircase=True)
train = tf.train.GradientDescentOptimizer(decayed_learning_rate).minimize(loss,global_step=training_rate)

#反时限学习率衰减 (α = α/(1+decay_rate*t), t = global_step/decay_steps)

tf.train.inverse_time_decay(learning_rate="",global_step="",decay_steps="",decay_rate="",staircase=True,name="")

#自然指数学习率衰减 (α = α * e^(-decay_rate * global_step))

tf.train.natural_exp_decay(learning_rate="",global_step="",decay_steps="",decay_rate="",staircase=True,name="")

#分片常数学习率衰减 (例:前5000次为0.8,5000~10000次为0.6,以后都用0.2作为学习率)

tf.train.piecewise_constant(x="",boundaries="",values="",name="")

#多项式学习率衰减(在一个周期内,学习率从峰值衰减到一个很低的end_learning_rate。第二个周期,学习率从略低的峰值同样衰减到end_learning_rate)

tf.train.polynomial_decay(learning_rate="",global_step="",decay_steps="",end_learning_rate="",power="",cycle="",name="")

正则化

一.正则表达式
#L1正则,λ为0.5

regular_l1 = tf.contrib.layers.l1_regularizer(.5)

#L2正则,λ为0.5

regular_l2 = tf.contrib.layers.l2_regularizer(.5)

#若只对特定超参做惩罚(正则),例:只对weight1和weight2做正则

weight1 = tf.get_variable("weight1",[10,100],initializer=tf.random_uniform_initializer)
weight2 = tf.get_variable("weight2",[20,50],initializer=tf.truncated_normal_initializer)

regulation = regular_l2(weight1) + regular_l1(weight2)

#将loss和正则化损失都加入到集合losses中
tf.add_to_collection("losses",loss)
tf.add_to_collection("losses",regulation)
'''get_collection获取指定集合中的所有个体(在这里就是获取loss和regulation)
   add_n 对所有个体进行加和操作
'''
loss = tf.add_n(tf.get_collection("losses"))

#二.dropout(选取随机部分数据进行下一次的训练) x为总数据,keep_prob为保留的概率

tf.nn.dropout(layer,keep_prob=0.9,noise_shape="",seed="",name="")
tf.contrib.layers.dropout(layer, keep_prob=0.9)
tf.contrib.rnn.DropoutWrapper(cell, output_keep_prob=keep_prob)   #专属于rnn的dropout

batch normalization

import functools
import tensorflow.contrib.slim as slim
bn = functools.partial(slim.batch_norm, scale=True, is_training=train,
                           decay=0.9, epsilon=1e-5, updates_collections=None)
#或者直接是:
slim.batch_norm("",decay=0.9,scale=True,is_training=train,epsilon=1e-5,updates_collections=None)

猜你喜欢

转载自blog.csdn.net/weixin_43968119/article/details/88817778