我正在Keras中试验双向包装器,示例代码如下。
T = 8
D = 2
M = 3
input_ = Input(shape=(T, D))
rnn = Bidirectional(LSTM(M, return_state=True, return_sequences=True))
#rnn = LSTM(M, return_state=True, return_sequences=True)
x = rnn(input_)
model = Model(inputs=input_, outputs=x)
X = np.random.randn(1,T,D)
o, h1, c1, h2, c2 = model.predict(X)
但是,它给出了一个错误
ValueError Traceback (most recent call last)
<ipython-input-82-53f1c7a28b54> in <module>()
19 print("c:", c1)
20
---> 21 lstm1()
<ipython-input-82-53f1c7a28b54> in lstm1()
8 rnn = Bidirectional(LSTM(M, return_state=True, return_sequences=True))
9 #rnn = LSTM(M, return_state=True, return_sequences=True)
---> 10 x = rnn(input_)
...
ValueError: Tried to convert 'tensor' to a tensor and failed. Error:
Shapes must be equal rank, but are 3 and 2
From merging shape 0 with other shapes. for 'bidirectional_26/ReverseV2_1
/packed' (op: 'Pack') with input shapes: [?,?,3], [?,3], [?,3].
如果我删除了双向包装器,即
rnn = LSTM(M, return_state=True, return_sequences=True)
然后没有问题。任何建议将不胜感激!