【问题标题】:Tensorflow2 LSTM (cuDNN args) vs tensorflow1 CuDNNLSTM implementation differenceTensorflow2 LSTM (cuDNN args) vs tensorflow1 CuDNNLSTM 实现差异
【发布时间】: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


    【解决方案1】:

    不,这不是错误。

    CuDNNLSTM 中的 2x 偏差是 recurrent kernel 的单独偏差。

    CuDNNLSTMtf.keras.layers.LSTM 中可用时,您可以看到code 的编写方式是它不对recurrent kernel 使用单独的偏差,而是调用LSTMCell,这是一个基类,并且没有单独的偏差。

    您可以使用model.layers[0].trainable_weights 来查看两种实现之间偏差的形状差异。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-15
      • 2021-06-14
      • 2019-02-15
      • 1970-01-01
      • 2018-10-03
      • 1970-01-01
      • 2023-03-09
      • 2020-07-04
      相关资源
      最近更新 更多