使用多个输入训练 Keras 模型

2024-03-15

我想使用 keras 训练具有 3 个不同输入的模型。训练数据 -x_train, 左列车, 右列车形状为 (10000,83,12)。这是代码的一部分。

from keras.layers import Dense, Input, LSTM
...
x = Input(shape = (83,12), dtype = "float32")
left = Input(shape = (83,12), dtype = "float32")
right = Input(shape = (83,12), dtype = "float32")
...
model = Model(inputs = [x, left, right], outputs = output)
model.compile(optimizer = "adadelta", loss = "categorical_crossentropy", metrics = ["accuracy"])
model.fit([x_train, left_train, right_train], y_train, validation_data=(x_test, y_test), epochs=20, batch_size=128)
...

我在训练时遇到以下错误:

ValueError                       Traceback (most recent call last)
<ipython-input-17-261d36872e91> in <module>()
     51 
     52 
---> 53 model.fit([x_train, left, right], y_train, validation_data= 
(x_test, y_test), epochs=20, batch_size=128)
     54 
     55 scores = model.evaluate(x_test, y_test)
     ...
     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 3 array(s), but instead got the 
following list of 1 arrays: [array(...

我在调用时确实传递了 3 个输入的列表fit方法。有什么问题?


The validation_data and model.evaluate还需要多输入。在你的情况下,你只提供一个数组(x_test, y_test)并且只是x_test它会类似于([x_test, left_test, right_test], y_test)。本质上,验证数据需要具有与训练数据相同数量的输入/输出。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用多个输入训练 Keras 模型 的相关文章

随机推荐