demo
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import Sequential, Model
from tensorflow.keras.layers import Input, LSTM, Dense
import numpy as np
x = np.random.random(size=(10,4,2))
inputs = Input(shape=(4,2))
lstm1 = LSTM(units=3, return_sequences=False, return_state=False)(inputs)
lstm2 = LSTM(units=3, return_sequences=True, return_state=False)(inputs)
lstm3 = LSTM(units=3, return_sequences=True, return_state=True)(inputs)
lstm4 = LSTM(units=3, return_sequences=False, return_state=True)(inputs)
model1 = Model(inputs=inputs, outputs=lstm1)
model2 = Model(inputs=inputs, outputs=lstm2)
model3 = Model(inputs=inputs, outputs=lstm3)
model4 = Model(inputs=inputs, outputs=lstm4)
res1 = model1.predict(x)
res2 = model2.predict(x)
res3 = model3.predict(x)
res4 = model4.predict(x)
print("res1只有一个返回值")
print(res1.shape)
print("res2只有一个返回值")
print(res2.shape)
print("res3有三个返回值")
for i in res3:
print(i.shape)
上面的代码表示一个
x
i
[
10
,
2
]
∗
w
x
[
2
,
3
]
=
[
10
,
3
]
x_i ~ [10, 2] * w_x ~ [2, 3] = [10,3]
xi [10,2]∗wx [2,3]=[10,3]
h
i
[
10
,
3
]
∗
w
h
[
3
,
3
]
=
[
10
,
3
]
h_i ~ [10,3] * w_h [3, 3] = [10, 3]
hi [10,3]∗wh[3,3]=[10,3]
return参数
lstm中有两个参数
return_sequences
return_state
return_sequences
return_sequences: Boolean. Whether to return the last output in the output sequence, or the full sequence. Default: `False`.
这里是选择是否返回last_output,那么什么是last_output
output就是每个cell的output总和,每一个cell的输出为
[
10
,
3
]
[10,3]
[10,3],所有的output为
[
10
,
4
,
3
]
[10,4,3]
[10,4,3]
last_output就是所有cell总和中最后一个输出项
return_state
return_state: Boolean. Whether to return the last state in addition to the output. Default: `False`
state就是 h h h,它的维度是 [ 10 , 3 ] [10,3] [10,3]