1

这是我第一次使用双向层,但我没有找到任何使用 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'>'

我对此有两个问题:

  1. 是什么导致了问题以及如何解决?
  2. 使用双向图层时,掩码值如何工作?因为据我了解,这一层创建了两个 LSTM:第一个是前向序列,第二个是后向序列(以获取整个上下文)。掩码参数是否也反转了?

提前致谢!

4

1 回答 1

0

更改此行:

enc = Bidirectional(LSTM(timesteps * 2 // 3, activation = 'tanh', return_sequences = False))(enc)
于 2020-11-16T11:54:51.037 回答