这是我第一次使用双向层,但我没有找到任何使用Keras功能API的例子。mask_mastrix是一个具有形状(样本、时间步长(的张量,其二进制值为True/False。
with strategy.scope():
input_tensor = Input(shape=(timesteps, features))
enc = Bidirectional(LSTM(timesteps * 2, activation = 'tanh', return_sequences = True))(input_tensor, mask=mask_matrix)
enc = Bidirectional(LSTM(timesteps * 1.5, activation = 'tanh', return_sequences = False))(enc)
decode1 = RepeatVector(timesteps)(enc)
decode1 = Bidirectional(LSTM(200, activation = 'tanh', return_sequences = True))(decode1)
decode1 = Bidirectional(LSTM(timesteps, activation = 'tanh', return_sequences = True))(decode1)
decode1 = TimeDistributed(Dense(8, activation="softmax"), name="dec1")(decode1)
decode2 = RepeatVector(timesteps)(enc)
decode2 = Bidirectional(LSTM(timesteps, activation = 'tanh', return_sequences = True))(decode2)
decode2 = TimeDistributed(Dense(2, activation = "tanh"), name="dec2")(decode2)
new_model = Model(inputs=input_tensor, outputs = [decode1, decode2])
new_model.compile(loss={"dec1":"categorical_crossentropy", "dec2":'mse'}, optimizer='adam')
plot_model(new_model, to_file='model.png')
我得到以下错误:
Dimension value must be integer or None or have an __index__ method, got value '1932.0' with type '<class 'float'>'
我对此有两个问题:
- 是什么导致了这个问题以及如何解决它
- 使用双向层时,遮罩值是如何工作的?因为根据我的理解,这个层创建了两个LSTM:第一个是前向序列,第二个是后向序列(以获得整个上下文(。掩码的论点也会被推翻吗
提前感谢!
更改此行:
enc = Bidirectional(LSTM(timesteps * 2 // 3, activation = 'tanh', return_sequences = False))(enc)