预计将看到1个数组,但获得了以下2个数组的列表:

时间:2019-08-13 09:44:56

标签: tensorflow keras lstm

我正在尝试使用来自keras的功能性API制作多层模型。想法是获取描述图像的矢量(来自预先训练的模型),并将其与来自与该图像关联的文本的另一个矢量连接起来。第一个分支处理来自图像的输入,第二个分支将文本作为输入。两者都是连接在一起的,然后再与其他几层一起处理。

当我使用以下方法训练模型时:

model = Model(inputs=[xa.input, xb.input], outputs=z)

这个合适的电话:

model.fit([oraciones_img, X], y, epochs=2, batch_size=5, verbose=1)

一切正常。

当我想进行预测时,问题就来了。我向model.predict函数提供了两个具有与oraciones_img和X相同尺寸的数组,但是会引发此错误:


ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 array(s), but instead got the following list of 2 arrays: [array([[1.1081828 , 0.4828459 , 1.6334529 , ..., 0.03270344, 0.17258629,
    0.03792314],
   [0.8138524 , 0.        , 0.65887845, ..., 0.6183274 , 0.00807555,
    0.6506448 ],
   [0.8...

我再次检查了训练和测试期间输入的尺寸是否相同,但没有解决。我尝试了对预测函数的训练数据,假设至少应该可以解决这个问题,但是很奇怪,这会引发相同的错误。从理论上讲,我应该提供以下信息:

model.predict([ (n_samples, 405504) , (n_samples, 50) ])

其中: (n_samples,405504)是来自n个图像的向量 (n_samples,50)是来自与图像相关联的n个文本的向量

但是该模型以某种方式要求我提供单个数组。我提供了一个跟进对象,预测函数要求使用dim4的单个数组,这有点令人困惑。

我已经看到其他有关数据形状类似问题的文章,但是所有这些文章在运行fit函数时都遇到问题。我已经通过了,这与预测功能有关。

有什么想法吗?

定义模型

seq_length = 50
seq_length_img = (200704 + 2048)*2

InputA = Input(shape=(seq_length_img,))
InputB = Input(shape=(seq_length,))

xa = Dense(512, activation='relu')(InputA)
xa = Model(inputs = InputA, outputs= xa)

xb = Embedding(input_dim=vocab_size, output_dim=512, input_length=seq_length)(InputB)
xb = Flatten()(xb)
xb = Dense(512, activation='relu')(xb)
xb = Model(inputs = InputB, outputs= xb)

combined = concatenate([xa.output, xb.output], axis=-1)
combined = Reshape((1024, 1))(combined)

z = LSTM(1024, input_shape=(1024, 1), return_sequences=True)(combined)
z = LSTM(1024)(z)
z = Dense(100, activation='relu')(z)
z = Dense(vocab_size, activation='softmax')(z)

model = Model(inputs=[xa.input, xb.input], outputs=z)
model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy'])
model.fit([oraciones_img, X], y, epochs=2, batch_size=5, verbose=1)

yhat = model.predict([feat, pred_sentence])

0 个答案:

没有答案