tf.keras.layers.LSTM 示例

import tensorflow as tf

输入的维度只能是3维的

inputs = tf.random.normal([
    32,  # 样本数
    10,  # 时间步长
    8  # 输入维度
]) 
print(inputs.shape)
(32, 10, 8)
lstm = tf.keras.layers.LSTM(4)
print(lstm)
<tensorflow.python.keras.layers.recurrent_v2.LSTM object at 0x000002E3D8C25AF0>
output = lstm(inputs)
print(output.shape)
(32, 4)
lstm = tf.keras.layers.LSTM(
    4,  # 输出空间的正整数、维度。
    return_sequences=True  # 是返回输出序列中的最后一个输出,还是全部序列。
)
output = lstm(inputs)
print(output.shape)
(32, 10, 4)
lstm = tf.keras.layers.LSTM(
    4,  # 输出空间的正整数、维度。
    return_sequences=True,  # 是返回输出序列中的最后一个输出,还是全部序列。
    return_state=True # 除输出外是否返回最后状态
)
whole_seq_output, final_memory_state, final_carry_state = lstm(inputs)
print(whole_seq_output.shape)
(32, 10, 4)
print(final_memory_state.shape)
(32, 4)
print(final_carry_state.shape)
(32, 4)

猜你喜欢

转载自blog.csdn.net/weixin_44493841/article/details/121419233