将BasicLSTMCell转换为双向LSTM



最近我尝试使用Tensorflow的BasicLSTMCell api来生成视频字幕。我正在使用一个代码,该代码以以下方式构建BasicLSTMCell:

self.lstm1 = tf.compat.v1.nn.rnn_cell.BasicLSTMCell(dim_hidden, state_is_tuple=False)
self.lstm2 = tf.compat.v1.nn.rnn_cell.BasicLSTMCell(dim_hidden, state_is_tuple=False)

然后按如下方式使用它:

with tf.compat.v1.variable_scope("Encoding") as scope:  
for i in range(0, self.n_video_lstm_step):
if i > 0:
scope.reuse_variables()
with tf.compat.v1.variable_scope("LSTM1"):
output1, state1 = self.lstm1(image_emb[:,i,:], state1)
with tf.compat.v1.variable_scope("LSTM2"):
output2, state2 = self.lstm2(tf.concat([padding, output1], 1), state2)

out_list.append(tf.concat([output1, output2], 1))

我希望这些LSTM单元是双向的,以满足我的需求。我试过使用

keras.layers.Bidirectional(keras.layers.LSTM(dim_hidden, unit_forget_bias=True, unroll=True))

但它没有起作用。有人能告诉我如何使用双向lstm吗。

根据您提出的问题,Convert BasicLSTMCell to bidirectional LSTM-您可以直接使用双向RNN包装器,如下代码所示。请澄清您是如何修改导致您面临错误的LSTM层类的。我会相应地更新我的答案。

import numpy as np
from tensorflow.keras import layers, Model, utils
X = np.random.random((100,10,3))
y = np.random.random((100,))
inp = layers.Input((10,3))
x = layers.Bidirectional(layers.LSTM(8, return_sequences=True))(inp)
x = layers.Bidirectional(layers.LSTM(8))(x)
out = layers.Dense(1, activation='softmax')(x)
model = Model(inp, out)
utils.plot_model(model, show_layer_names=False, show_shapes=True)
model.compile(optimizer='adam', loss='binary_crossentropy')
model.fit(X, y, epochs=3)
Epoch 1/3
4/4 [==============================] - 5s 10ms/step - loss: 0.6963
Epoch 2/3
4/4 [==============================] - 0s 22ms/step - loss: 0.6965
Epoch 3/3
4/4 [==============================] - 0s 11ms/step - loss: 0.6976
<tensorflow.python.keras.callbacks.History at 0x7f91066bf4c0>

相关内容

  • 没有找到相关文章

最新更新