Tensorflow 常用函数

一、神经网络实现过程

  1、准备数据集,提取特征,作为输入喂给神经网络。

  2、搭建NN结构,从输入到输出。(先搭建计算图,再用会话执行)

   (NN前向传播算法——>计算输出)

  3、大量特征喂给NN,迭代优化NN参数。

   (NN反向传播算法——>优化参数训练模型)

  4、使用训练好的模型预测和分类。

 

二、Tensorflow主要函数

 1、变量的声名函数 tf.Variable( initialize , name=  )

  i)参数initializer是初始化参数,name是可自定义的变量名称

  ii)函数作用:保存和更新神经网络中的参数

  iii)张量与变量的关系:tf.Variable是一个运算,其输出结果是一个张量。变量是一种特殊的张量。 

      iiii)参数shape 的一列表示一个变量的权重。

    2、生成张量的常见函数

  tf.zeros(shape, dtype=, name=)

  tf.zeros_like(tensor, dtype=, name=)

  tf.constant(value, dtype=, shape=, name=)

  tf.fill(dims, value, name=)

  tf.ones_like(tensor, dtype=, name=)

  tf.ones(shape, dtype=, name=)

  3、生成随机数函数

  tf.random_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)

  tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)

  tf.random_uniform(shape, minval=0.0, maxval=1.0, dtype=tf.float32, seed=None, name=None)

  tf.random_shuffle(value, seed=None, name=None)

 4、占位函数 tf.placeholder(dtype,shape=,name=)

x=tf.placeholder(tf.float32,shape=(None,3), name='input')
...
sess.run( y ,feed_dict={ x:[[1,2,3]]  } )

 

  i)参数dtype为数据类型,shape为维度,name为名称

  ii)通过会话执行计算时,通过feed_dict字典指定相应值

 5、矩阵乘法 tf.matmul( matrix1 , matrix2 )

  i)matrix1*matrix2 表示矩阵内每个元素对应相乘

 6、所有变量(参数)初始化 tf.global_variables_initializer() 

###第一种
init_op=tf.global_variable_initializer() sess.run(init_op)

###第二种
init_op=tf.initialize_all_variables()
sess.rund(init_op)

  i)由于变量之间可能存在依赖关系,单个调用方案比较麻烦?

 7、激活函数

  tf.nn.relu()
  tf.nn.sigmoid()
  tf.nn.tanh()
  tf.nn.elu()
  tf.nn.bias_add()
  tf.nn.crelu()
  tf.nn.relu6()
  tf.nn.softplus()
  tf.nn.softsign()
  tf.nn.dropout()#防止过拟合,用来舍弃某些神经元

 8、损失函数    

  i)均方根误差(MSE) —— 回归问题中最常用的损失函数 。优点是便于梯度下降,误差大时下降快,误差小时下降慢,有利于函数收敛。缺点是受明显偏离正常范围的离群样本的影响较大

    loss=tf.losses.mean_squared_error(y_true,y_pred)
    loss=tf.reduce_mean(  tf.square( y_true,y_pred ) )     上一函数的另一实现                                                        

  ii)平均绝对误差(MAE) —— 想格外增强对离群样本的健壮性时使用

    mae = tf.losses.absolute_difference(y_true,y_pred ) 
    mae.loss = tf.reduce_sum( mae)

 9、自定义损失函数

  tf.greater(x, y):判断 x 是否大于 y,当维度不一致时广播后比较
  tf.where(condition, x, y):当 condition 为 true 时返回 x,否则返回 y
  tf.reduce_mean():沿维度求平均
  tf.reduce_sum():沿维度相加
  tf.reduce_prod():沿维度相乘
  tf.reduce_min():沿维度找最小
  tf.reduce_max():沿维度找最大

 10、优化函数

  tf.train.GradientDescentOptimizer
  tf.train.AdadeltaOptimizer
  tf.train.AdagradOptimizer
  tf.train.AdagradDAOptimizer
  tf.train.MomentumOptimizer
  tf.train.AdamOptimizer
  tf.train.FtrlOptimizer
  tf.train.RMSPropOptimizer

 11、正则化函数 (在损失函数中引入模型复杂度指标,利用W加权值,弱化训练数据的噪声,一般不正则化 b 偏置 )

  tensorflow 中的两种正则化函数  

  i)L1正则化 : tf.contrib.layers.l1_regularizer(REGULARIZER)(w)    =》 lossl1( w )= Σ | wi |

  ii)L2正则化 : tf.contrib.layers.l2_regularizer(REGULARIZER)(w)   =》 lossl2( w )= ∑ | wi2 |

    使用例子:

w=tf.Variable(...)
y=tf.matmul( x,w )

loss=tf.reduce_mean(tf.square(y_ - y ))



#第一种
loss_total=loss+ tf.contrib.layers.l2_regularizer(regularizer_rate)(w)    #或者regularizer=tf.tf.contrib.layers.l2_regularizer(regularizer_rate) ; regular_loss=regularizer(w)

#第二种  将正则项加入名为losses 集合。
tf.add_to_collection ( 'losses' ,  tf.contrib.layers.l2_regularizer(regularizer_rate)(w) )
loss_total =loss+tf.add_n( tf.get_collection('losses') ) 

#!!常用的方法是第二种
#原因:1、第一种方式导致损失函数loss的定义很长
#          2、定义网络结构的部分和计算损失函数的部分可能不在同一函数中,此时使用第一种方式不方便1

 

  12、学习率设置函数: learning_rate = tf.train.exponential_decay( learning_rate_base,global_step,decay_step, decay_rate , staircase=True/False)

   i)该函数实现的是以下功能:learning_rate = learning_rate_base * decay_rate ^ (global_step / decay_step)

   ii)参数learning_rate_base表示初始学习率,global_step表示当前迭代次数,是不可训练参数,decay_step表示衰减速度,decay_rate表示衰减率,staircase=True表示每训练decay_step轮更新一次学习率,否则每轮更新一次。

     使用例子:

global_step=tf.Variable(0,trainable=False)
learning_rate = tf.train.exponential_decay( learning_rate_base,global_step,decay_step,decay_rate,staricase=True)  
train_=tf.train.GradientDescentOptimizer(learning_rate).minimize(...loss...,global_step=global_step)

  13、滑动平均模型   tf.train.ExponentialMovingAverage( decay,step )

   i)基本思想: 在使用梯度下降算法训练中,每次更新权重时,为每个权重维护一个影子变量,该影子变量随着训练的进行,最终会稳定在一个接近真实权重的值附近,那么在进行预测时,使用影子变量的值可以得到更好的结果

                         其中:shadowVariable=decay∗shadowVariable+(1−decay)∗variable ,具体可看 https://blog.csdn.net/u012436149/article/details/56484572        shadowVariable=decayshadowVariable+(1decay)variable

   ii)参数decay表示衰减率,step用于decay= min(decay,(1+step)/(10+step))来进行手动设置decay

   iii)decay通常设置为非常接近1的数(比如0.999或0.9999).

    使用例子:

 

step= tf.Variable( 0,trainable=False )  #用于模拟神经网络中迭代的轮数
v1=tf.Variable(0,dtype=tf.float32)       # 滑动平均模型变量

第一种
#定义一个滑动平均类。初始化给定衰减率和控制衰减率的变量
ema  =  tf.train.ExponentialMovingAverage( 0.99, step )

#定义一个更新变量滑动平均的操作。需要给定一个列表,每次执行此操作,列表的变量会更新
maintain_average_op = ema.apply( [v1] ) #实际应用中使用 maintain_average_op = ema.apply( tf.trainable_variables())  所有变量求滑动平均 ; tf.trainable_variables()将所有可训练变量形成列表
...
sess.run(maintain_average_op)  #每迭代优化一次v1 都要加上此语句。

第二种
ema= tf.train.ExponentialMovingAverage( 0.99, step )
maintain_average_op = ema.apply( tf.trainable_variables()) 
#将训练过程和滑动平均绑定在一起运行,合成一个节点
with tf.control_dependencies( [train_step,maintain_average_op] ): train_op=tf.no_op(name='train') #使用ema.average( 参数名 ) 查看某参数的滑动平均

 

  14、获取矩阵中某一元素值

    i) 主要用到的函数就是tf.gather_nd(A, B),其中A是一个矩阵,B是索引下标,A和B的维度需要一致。下面是使用样例:

import tensorflow as tf
####当矩阵A是一维, 我们取A中下标(从0开始)为2,4所对应的值,即3,5
A = tf.constant([1,2,3,4,5 ])

# B的表示如下:
B = tf.placeholder(dtype=tf.int32,shape=[None,1])
C = tf.gather_nd(A,B)
    #执行C
b = [ [2],[4] ]  
with tf.Session() as sess:
    c=sess.run([C],feed_dict={B:b})
    print(c)   ##其结果为 [array([3, 5])]


####当矩阵A是二维, 我们取A中下标(从0开始)为(0,2),(1,4)所对应的值,即3,10
A = tf.constant([ [1,2,3,4,5], [6,7,8,9,10] ])
# B的表示如下:
B = tf.placeholder(dtype=tf.int32,shape=[None,2])
C = tf.gather_nd(A,B)
    #执行C
b = [ [0,2],[1,4] ]  
with tf.Session() as sess:
    c=sess.run([C],feed_dict={B:b})
    print(c)   ##其结果为 [array([3, 10])]

####后面的维度以此类推

 

posted @ 2019-02-10 21:48  bear_ge  阅读(611)  评论(0编辑  收藏  举报