如何在tensorflow2中重写这个?尝试使用
tf_upgrade_v2
但失败了。也尝试过使用 tf.keras.layers.Dropout
,但将 MultiRNNCell
转换为张量 时遇到一些问题
rnn_cells = tf.compat.v1.nn.rnn_cell.MultiRNNCell(
[lstm_cell(size_layer) for _ in range(num_layers)],
state_is_tuple = False,
)
drop = tf.contrib.rnn.DropoutWrapper(rnn_cells, output_keep_prob = forget_bias)
这是 TensorFlow 2 中重写的代码,其中包含关键更改和说明:
1。导入必要的图层:
from tensorflow.keras.layers import LSTMCell, RNN, Dropout
2。创建 LSTM 单元
lstm_cells = [LSTMCell(size_layer) for _ in range(num_layers)]
3.构建带 dropout 的 RNN 层:
rnn = RNN(
lstm_cells,
return_sequences=True, # Set to True if you need to return all outputs
return_state=False, # Set to True if you need to return the final state
)
drop = Dropout(forget_bias)(rnn) # Apply dropout after the RNN layer