TensorFlow学习日记30

1.fit_generator
解析:(train) batches of samples.

2.random.choice(seq)
解析:seq可以是一个列表,元组或字符串。

3.log_device_placement=True)
解析:

# Creates a graph.
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
c = tf.matmul(a, b)
# Creates a session with log_device_placement set to True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# Runs the op.
print(sess.run(c))

结果输出,如下所示:

Device mapping:
/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: Tesla K40c, pci bus
id: 0000:05:00.0
b: /job:localhost/replica:0/task:0/device:GPU:0
a: /job:localhost/replica:0/task:0/device:GPU:0
MatMul: /job:localhost/replica:0/task:0/device:GPU:0
[[ 22.  28.]
 [ 49.  64.]]

4.allow_soft_placement
解析:If you would like TensorFlow to automatically choose an existing and supported device to run the operations in case the specified one doesn’t exist, you can set allow_soft_placement to True in the configuration option when creating the session.

# Creates a graph.
with tf.device('/device:GPU:2'):
  a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
  b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
  c = tf.matmul(a, b)
# Creates a session with allow_soft_placement and log_device_placement set
# to True.
sess = tf.Session(config=tf.ConfigProto(
      allow_soft_placement=True, log_device_placement=True))
# Runs the op.
print(sess.run(c))

结果输出,如下所示:

Device mapping:
MatMul: (MatMul)/job:localhost/replica:0/task:0/cpu:0
b: (Const)/job:localhost/replica:0/task:0/cpu:0
a: (Const)/job:localhost/replica:0/task:0/cpu:0
[[ 22.  28.]
 [ 49.  64.]]

5.L2 Regularization(Weight Decay)
解析:带 L2 正则化项的损失函数,如下所示:

C=C0+λ2nww2

w b 求偏导,如下所示:
Cw=C0w+λnwCb=C0b

w b 的更新方程,如下所示:
wwηC0wηλnw=(1ηλn)wηC0wbbηC0b

基于mini-batch的SGD, w b 的更新方程,如下所示:
w(1ηλn)wηmxCxwbbηmxCxb

在不使用 L2 正则化时,求导结果中 w 的系数为1。现在 w 前面的系数为 1ηλn ,因为 ηλn 都是正数,所以 1ηλn 小于1,它的效果是减小 w ,即权重衰减(weight decay)。但考虑到后面的导数项, w 最终的值可能增大也可能减小。

6.L1 Regularization
L1 正则化项的损失函数,如下所示:
C=C0+λnw|w|

w 求偏导,如下所示:
Cw=C0w+λnsgn(w)

w 的更新方程,如下所示:
ww=wηλnsgn(w)ηC0w

其中, sgn(w) 表示 w 的符号函数(sgn(0)=0,sgn(w>0)=1,sgn(w<0)=-1)。如上所示,当 w 为正数时,更新后的 w 变小。当w为负数时,更新后的 w 变大。因此它的效果是让 w 往0靠近,使网络中的权重尽可能为0,也就相当于减小了网络复杂度,防止过拟合。

7.Dropout防止过拟合
解析:运用Dropout的训练过程,相当于训练了很多个只有半数隐层单元的神经网络(后面简称为“半数网络”),每一个这样的半数网络,都可以给出一个分类结果,这些结果有的是正确的,有的是错误的。随着训练的进行,大部分半数网络都可以给出正确的分类结果,那么少数的错误分类结果就不会对最终结果造成大的影响。

8.正则化方法
解析:
(1)L1和L2 regularization
(2)数据增广
(3)Dropout

9.tf.nn.dropout
解析:tf.nn.dropout(x, keep_prob, noise_shape=None, seed=None,name=None)。其中,keep_prob表示神经元被选中的概率,在初始化时keep_prob是一个占位符。

10.tf.contrib.rnn.DropoutWrapper
解析:构造函数,如下所示:

__init__(self, 
         cell, 
         input_keep_prob=1.0, 
         output_keep_prob=1.0,
         state_keep_prob=1.0, 
         variational_recurrent=False,
         input_size=None, 
         dtype=None, 
         seed=None)

其中,input_keep_prob表示输入的dropout概率;output_keep_prob表示输出的dropout概率。

11.initialize_all_variables和global_variables_initializer
解析:initialize_all_variables已被弃用,使用tf.global_variables_initializer代替。

12.tf.train.import_meta_graph
解析:直接加载持久化的图。

13.tf.Graph.get_operation_by_name(name)
解析:returns the Operation with the given name.

14.tf.ones_like(tensor, dtype=None, name=None)
解析:该方法用于创建一个所有参数均为1的tensor对象。

15.tf.rint(x,name=None)
解析:计算离x最近的整数,若为中间值,取偶数值。

16.keras.layers.core.Activation(activation)
解析:激活层对一个层的输出施加激活函数。activation表示将要使用的激活函数,为预定义激活函数名或一个Tensorflow/Theano的函数。

17.keras.layers.core.Reshape(target_shape)
解析:Reshape层用来将输入shape转换为特定的shape。target_shape表示目标shape,为整数的tuple,不包含样本数目的维度(batch大小)。

18.keras.layers.core.Permute(dims)
解析:Permute层将输入的维度按照给定模式进行重排,例如,当需要将RNN和CNN网络连接时,可能会用到该层。dims为整数tuple,指定重排的模式,不包含样本数的维度。重拍模式的下标从1开始。例如(2,1)代表将输入的第二个维度重拍到输出的第一个维度,而将输入的第一个维度重排到第二个维度。

19.keras.layers.core.RepeatVector(n)
解析:RepeatVector层将输入重复n次。

20.keras.layers.core.Lambda(function, output_shape=None, mask=None, arguments=None)
解析:该函数用以对上一层的输出施以任何Theano/TensorFlow表达式。

21.keras.layers.core.ActivityRegularization(l1=0.0, l2=0.0)
解析:经过本层的数据不会有任何变化,但会基于其激活值更新损失函数值。

22.keras.layers.core.Masking(mask_value=0.0)
解析:使用给定的值对输入的序列信号进行“屏蔽”,用以定位需要跳过的时间步。

参考文献:
[1] fit_generator:https://keras-cn.readthedocs.io/en/latest/models/sequential/

猜你喜欢

转载自blog.csdn.net/shengshengwang/article/details/79060479
今日推荐