[英]Keras functional API: fitting and testing model that takes multiple inputs
I build a Keras model that has 2 branches, each taking a different feature representation for the same data. 我建立了一个具有2个分支的Keras模型,每个分支对相同的数据采用不同的特征表示。 The task is classifying sentences into one of 6 classes.
任务是将句子分为6类之一。
I have tested my code up to model.fit
that takes in a list containing the two input feature matrices as X
. 我已经测试了直到
model.fit
代码,该代码接受一个包含两个输入要素矩阵的列表X
Everything works OK. 一切正常。 But on prediction, when I pass the two input feature matrices for test data, an error is generated.
但是在预测时,当我通过两个输入特征矩阵作为测试数据时,会生成错误。
The code is as follows: 代码如下:
X_train_feature1 = ... # shape: (2200, 100) each row a sentence and each column a feature
X_train_feature2 = ... # shape: (2200, 13) each row a sentence and each column a feature
y_train= ... # shape: (2200,6)
X_test_feature1 = ... # shape: (587, 100) each row a sentence and each column a feature
X_test_feature2 = ... # shape: (587, 13) each row a sentence and each column a feature
y_test= ... # shape: (587,6)
model= ... #creating a model with 2 branches, see the image below
model.fit([X_train_feature1, X_train_feature2],y_train,epochs=100, batch_size=10, verbose=2) #Model trains ok
model.predict([X_test_feature1, X_test_feature2],y_test,epochs=100, batch_size=10, verbose=2) #error here
The model looks like this: 该模型如下所示:
And the error is: 错误是:
predictions = model.predict([X_test_feature1,X_test_feature2], y_test, verbose=2)
File "/home/zz/Programs/anaconda3/lib/python3.6/site-packages/keras/engine/training.py", line 1748, in predict
verbose=verbose, steps=steps)
File "/home/zz/Programs/anaconda3/lib/python3.6/site-packages/keras/engine/training.py", line 1290, in _predict_loop
batches = _make_batches(num_samples, batch_size)
File "/home/zz/Programs/anaconda3/lib/python3.6/site-packages/keras/engine/training.py", line 384, in _make_batches
num_batches = int(np.ceil(size / float(batch_size)))
TypeError: only length-1 arrays can be converted to Python scalars
I would really appreciate some help to understand the error and how to fix it. 我非常感谢您提供一些帮助,以了解该错误以及如何修复该错误。
The predict
method only takes as input the data (ie x
) and the batch_size
(it is not necessary to set this). predict
方法仅将数据(即x
)和batch_size
(无需设置)作为输入。 It does not take labels or epochs as inputs. 它不使用标签或时期作为输入。
If you want to predict classes then you should use predict_classes
method which gives you the predicted class labels (rather than the probabilities which predict
method gives): 如果要预测类,则应使用
predict_classes
方法,该方法为您提供预测的类标签(而不是predict
方法提供的概率):
preds_prob = model.predict([X_test_feature1, X_test_feature2])
preds = model.predict_classes([X_test_feature1, X_test_feature2])
And if you want to evaluate your model on the test data to find the loss and metric values then you should use evaluate
method: 而且,如果您想根据测试数据评估模型以找到损失和度量值,则应使用
evaluate
方法:
loss_metrics = model.evaluate([X_test_feature1, X_test_feature2], y_test)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.