import tensorflow as tf
inputs = tf.random.normal([
1,
2,
3
])
print("输入:", inputs.shape)
输入: (1, 2, 3)
lstm = tf.keras.layers.LSTM(4)
output = lstm(inputs)
print("最后一个输出值:", output)
最后一个输出值: tf.Tensor([[-0.02074971 -0.06181227 -0.00734914 0.00274102]], shape=(1, 4), dtype=float32)
lstm = tf.keras.layers.LSTM(
4,
return_sequences=True
)
output = lstm(inputs)
print("所有输出值:", output)
所有输出值: tf.Tensor(
[[[ 0.15341435 0.09737927 -0.13387154 0.05538211]
[ 0.0466701 0.04704784 -0.14747564 0.01897318]]], shape=(1, 2, 4), dtype=float32)
lstm = tf.keras.layers.LSTM(
4,
return_state=True
)
lstm1, state_h, state_c = lstm(inputs)
print("最后一个输出值:", lstm1)
print("最后一个输出值:", state_h)
print("隐藏状态值:", state_c)
最后一个输出值: tf.Tensor([[ 0.02579227 0.05082012 -0.00955747 0.04579654]], shape=(1, 4), dtype=float32)
最后一个输出值: tf.Tensor([[ 0.02579227 0.05082012 -0.00955747 0.04579654]], shape=(1, 4), dtype=float32)
隐藏状态值: tf.Tensor([[ 0.06498837 0.11387149 -0.02051686 0.10319157]], shape=(1, 4), dtype=float32)
lstm = tf.keras.layers.LSTM(
4,
return_sequences=True,
return_state=True
)
lstm1, state_hidden, state_cell = lstm(inputs)
print("所有输出值:", lstm1)
print("最后一个输出值:", state_hidden)
print("隐藏状态值:", state_cell)
所有输出值: tf.Tensor(
[[[-0.2615968 -0.11358638 -0.02559761 0.03946158]
[ 0.001031 -0.0546873 0.03478507 0.00940466]]], shape=(1, 2, 4), dtype=float32)
最后一个输出值: tf.Tensor([[ 0.001031 -0.0546873 0.03478507 0.00940466]], shape=(1, 4), dtype=float32)
隐藏状态值: tf.Tensor([[ 0.00235827 -0.10970385 0.08445251 0.01719776]], shape=(1, 4), dtype=float32)