Tensorflow:了解带和不带辍学包装器的LSTM输出
我使用的是Tensorflow 1.8.0 我希望Tensorflow:了解带和不带辍学包装器的LSTM输出,tensorflow,lstm,dropout,Tensorflow,Lstm,Dropout,我使用的是Tensorflow 1.8.0 我希望y2的输出与y1类似,因为y2使用与y1相同的LSTM单元,只是它也通过一个退出层。由于只在LSTM单元的输出上应用了漏值,因此我认为y2的值将与y1的值相同,只是有些零零。但这就是我为y1所得到的: import tensorflow as tf import tensorflow.contrib.eager as tfe tfe.enable_eager_execution() x = tf.range(1, 11, dtype=tf.f
y2
的输出与y1
类似,因为y2
使用与y1
相同的LSTM单元,只是它也通过一个退出层。由于只在LSTM单元的输出上应用了漏值,因此我认为y2
的值将与y1
的值相同,只是有些零零。但这就是我为y1所得到的:
import tensorflow as tf
import tensorflow.contrib.eager as tfe
tfe.enable_eager_execution()
x = tf.range(1, 11, dtype=tf.float32)
x = tf.reshape(x, (5, 1, 2))
cell = tf.contrib.rnn.LSTMCell(10)
initial_state = cell.zero_state(5, dtype=tf.float32)
y1, _ = tf.nn.dynamic_rnn(cell, x, dtype=tf.float32, initial_state=initial_state)
y2, _ = tf.nn.dynamic_rnn(
tf.contrib.rnn.DropoutWrapper(cell, input_keep_prob=1.0, output_keep_prob=0.5, state_keep_prob=1.0),
x,
dtype=tf.float32,
initial_state=initial_state)
y2
中的非零值与y1
中相应位置的值完全不同
这是一个错误还是我对在LSTM单元格的输出上应用辍学意味着什么有错误的想法?
y2相当于y1\u辍学/0.5
当退出
应用于y1
,保留概率为p
,然后将输出除以p
如果同时检查两个矩阵,y2
只不过是随机删除一半输入,然后按0.5的比例缩放
引用辍学论文第10节
“我们将辍学描述为一种方法,即我们用
训练时的概率p
,并通过
在测试时将它们乘以系数p
实现相同的效果是通过
在训练时乘以1/p
,在训练时不修改权重
测试时间。这些方法与适当的
每层的学习速率和权重初始化。”
参考资料:
<tf.Tensor: id=5540, shape=(5, 1, 10), dtype=float32, numpy=
array([[[-4.2897560e-02, 1.9367093e-01, -1.1827464e-01, -1.2339889e-01,
1.3408028e-01, 1.3082971e-02, -2.4622230e-02, -1.5669680e-01,
1.1127964e-01, -5.3087018e-02]],
[[-7.1379542e-02, 4.5163053e-01, -1.6180833e-01, -1.3278724e-01,
2.2819680e-01, -4.8406985e-02, -8.2188733e-03, -2.5466946e-01,
2.8928292e-01, -7.3916554e-02]],
[[-5.9056517e-02, 6.1984581e-01, -1.9882108e-01, -9.6297756e-02,
2.5009862e-01, -8.0139056e-02, -2.2850712e-03, -2.7935350e-01,
4.4566888e-01, -7.8914449e-02]],
[[-3.8571563e-02, 6.9930458e-01, -2.2960691e-01, -6.1545946e-02,
2.5194761e-01, -7.9383254e-02, -5.4560765e-04, -2.7542716e-01,
5.5587584e-01, -7.3568568e-02]],
[[-2.2481792e-02, 7.3400390e-01, -2.5636050e-01, -3.7012421e-02,
2.4684550e-01, -6.3926049e-02, -1.1120128e-04, -2.5999820e-01,
6.2801009e-01, -6.3132115e-02]]], dtype=float32)>
<tf.Tensor: id=5609, shape=(5, 1, 10), dtype=float32, numpy=
array([[[-0.08579512, 0.38734186, -0.23654927, -0.24679779,
0. , 0.02616594, -0. , -0.3133936 ,
0. , -0. ]],
[[-0.14275908, 0. , -0.32361665, -0.26557449,
0. , -0. , -0. , -0.5093389 ,
0. , -0. ]],
[[-0.11811303, 0. , -0.39764217, -0. ,
0.50019723, -0.16027811, -0.00457014, -0. ,
0.89133775, -0. ]],
[[-0. , 0. , -0.45921382, -0.12309189,
0. , -0. , -0. , -0. ,
1.1117517 , -0.14713714]],
[[-0. , 0. , -0. , -0.07402484,
0. , -0. , -0. , -0.5199964 ,
1.2560202 , -0. ]]], dtype=float32)>