在运行LSTM模型时,我被这段代码卡住了-
# Create cell state and hidden state variables to maintain the state of the LSTM
c, h = [], []
initial_state = []
for li in range(n_layers):
c.append(tf.Variable(tf.zeros([batch_size, num_nodes[li]]), trainable=False))
h.append(tf.Variable(tf.zeros([batch_size, num_nodes[li]]), trainable=False))
initial_state.append(tf.keras.layers.LSTMStateTuple(c[li], h[li]))
# Do several tensor transformations, because the function dynamic_rnn requires the output to be of
# a specific format. Read more at: https://www.tensorflow.org/api_docs/python/tf/nn/dynamic_rnn
all_inputs = tf.concat([tf.expand_dims(t, 0) for t in train_inputs], axis=0)
# Create LSTM layer
lstm_layer = tf.keras.layers.LSTM(num_nodes[-1], return_sequences=True, return_state=True, dropout=dropout)
# Pass inputs and initial state to the LSTM layer
all_lstm_outputs, final_state, _ = lstm_layer(all_inputs, initial_state=initial_state)
all_outputs = tf.keras.layers.Dense(1)(all_lstm_outputs)
split_outputs = tf.split(all_outputs, num_unrollings, axis=0)```
字符串
我得到了这个错误:
AttributeError: module 'keras.api._v2.keras.layers' has no attribute 'LSTMStateTuple'
型
1条答案
按热度按时间nhhxz33t1#
您无法使用
LSTMStateTuple
,因为:LSTMStateTuple
在tf.keras.layers.LSTM
中不是,而是在compat.v1
中字符串
文档链接:https://www.tensorflow.org/api_docs/python/tf/compat/v1/nn/rnn_cell/LSTMStateTuple
tf.keras.layers.LSTM是:
型
文档链接:https://www.tensorflow.org/api_docs/python/tf/keras/layers/LSTM