【发布时间】:2020-10-31 13:28:52
【问题描述】:
在 tensorflow 1 中,有一个层 tf.compat.v1.keras.layers.CuDNNLSTM 是为使用 cuDNN 而构建的,而在 tensorflow 2 中,该层已被弃用,有利于将 tf.keras.layers.LSTM 与
1. `activation` == `tanh`
2. `recurrent_activation` == `sigmoid`
3. `recurrent_dropout` == 0
4. `unroll` is `False`
5. `use_bias` is `True`
6. Inputs are not masked or strictly right padded.
用于 cuDNN 实现。我不知道是否存在未实现的错误或某些差异,但似乎与 CuDNNLSTM 使用输入偏差 和 经常偏差存在差异,其中 LSTM 在上面tf2 cuDNN 规则仅使用循环偏差。
相关代码
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM
from tensorflow.compat.v1.keras.layers import CuDNNLSTM
print(tf.__version__)
model1 = Sequential()
model1.add(LSTM(1, activation='tanh', recurrent_dropout=0, unroll=False, use_bias=True, return_sequences=0, input_shape=(1, 1)))
print(model1.summary())
model2 = Sequential()
model2.add(CuDNNLSTM(1, return_sequences=0, input_shape=(1, 1)))
print(model2.summary())
2.2.0
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
lstm (LSTM) (None, 1) 12
=================================================================
Total params: 12
Trainable params: 12
Non-trainable params: 0
_________________________________________________________________
None
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
cu_dnnlstm (CuDNNLSTM) (None, 1) 16
=================================================================
Total params: 16
Trainable params: 16
Non-trainable params: 0
_________________________________________________________________
请注意,总参数相差 N_units * 4,这意味着它缺少每个单元格的额外偏置向量。
请注意,LSTM 的 pytorch 实现与 tf1 CuDNNLSTM 匹配,这是我偶然发现的。
是否有一些我遗漏的修复程序或者是否应该将其提升为 github 问题?
【问题讨论】:
标签: python tensorflow keras lstm