Keras模型图层未对齐

时间:2019-12-19 19:11:20

标签: python tensorflow keras

使用功能性API,我试图构建一个具有两个输入(A和B)和一个输出的模型。 A具有输入的时间序列,B具有输入的一列是分类变量。

通过拆分两个输入,我可以为B创建一个嵌入,然后再合并A和B。我目前拥有的模型设置如下:

time_series = Input(batch_shape=(10, 60,5,))
ticker_input = Input(shape=(1,) )

lstm_layer = LSTM(100, 
                   dropout=0.0000, 
                   recurrent_dropout=0.000,  return_sequences = False,
                   stateful=True, kernel_initializer='random_uniform')(time_series)

reshape_layer = Reshape((10, 10))(lstm_layer)

embedding = Embedding(3, 10, input_length=10)(ticker_input)

reshaped_embedding = Reshape((10,))(embedding)

repeated = RepeatVector(10)(reshaped_embedding)

merge_one = concatenate([reshape_layer, repeated], axis=0)

dense_inner = Dense(20)(merge_one)
dense_output = Dense(20, activation="relu")(dense_inner)

reshape = Reshape((200,))(dense_output)

dense_final = Dense(1, activation="sigmoid", use_bias=False)(reshape)


model = Model(inputs=[time_series, ticker_input], outputs=dense_final)


model.compile(loss='mean_squared_error',
              optimizer=optimizers.RMSprop(lr=0.001),
              metrics=['accuracy'])

model.summary()

model.fit(x_train, y, epochs=2, verbose=1, batch_size=BATCH_SIZE, shuffle=False)

重塑的目的是确保两个形状相似。但是,在训练模型时,出现以下错误:Dimensions must be equal, but are 20 and 10 for 'loss/dense_136_loss/SquaredDifference' (op: 'SquaredDifference') with input shapes: [20,1], [10,1].编译成功,但是只有在训练期间才出现此错误。 Keras报告发生此不匹配的层是最后一层dense_final

我尝试了一组重塑以使形状匹配,但无济于事。 model.summary的输出似乎表明层的形状应该没有问题,但是无论我尝试什么,我总是会遇到此错误。有人知道吗?

0 个答案:

没有答案