模块“keras.api._v2.keras.layers”没有属性“LSTMStateTuple”

0s0u357o  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(143)

在运行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'

nhhxz33t

nhhxz33t1#

您无法使用LSTMStateTuple,因为:
LSTMStateTupletf.keras.layers.LSTM不是,而是在compat.v1

tf.compat.v1.nn.rnn_cell.LSTMStateTuple

字符串
文档链接:https://www.tensorflow.org/api_docs/python/tf/compat/v1/nn/rnn_cell/LSTMStateTuple

tf.keras.layers.LSTM是:

tf.keras.layers.LSTM(
    units,
    activation='tanh',
    recurrent_activation='sigmoid',
    use_bias=True,
    kernel_initializer='glorot_uniform',
    recurrent_initializer='orthogonal',
    bias_initializer='zeros',
    unit_forget_bias=True,
    kernel_regularizer=None,
    recurrent_regularizer=None,
    bias_regularizer=None,
    activity_regularizer=None,
    kernel_constraint=None,
    recurrent_constraint=None,
    bias_constraint=None,
    dropout=0.0,
    recurrent_dropout=0.0,
    return_sequences=False,
    return_state=False,
    go_backwards=False,
    stateful=False,
    time_major=False,
    unroll=False,
    **kwargs
)


文档链接:https://www.tensorflow.org/api_docs/python/tf/keras/layers/LSTM

相关问题