简体   繁体   English

为什么使用相同的 Keras 模型和输入进行预测会得到不同的结果?

[英]Why am I getting different results on a prediction using the same Keras model and input?

posting here is my last resort cause I can't find anything like it online.在这里发帖是我最后的手段,因为我在网上找不到类似的东西。 I trained a model to classify embeddings into categories (a simple three layer Dense neural network).我训练了一个模型来将嵌入分类(一个简单的三层密集神经网络)。

Now I want to use the trained model to make predictions in real time, but I discovered that if I input the whole test dataframe to the model, get the prediction for say the element number i , and compare it to the prediction that I get by inputting just the element number i of the test data frame into the model, I get different results.现在我想使用经过训练的模型进行实时预测,但我发现如果我将整个测试数据框输入模型,则获取元素编号i的预测,并将其与我得到的预测进行比较仅将测试数据帧的元素编号i输入模型,我得到不同的结果。 This is the code in case I didn't explain it good enough:这是代码,以防我解释得不够好:

i = 522
y_pred = model.predict(X_test)
y_pred_2 = model.predict(X_test.iloc[[i]])

print (f'{np.argmax(y_pred[i])} {np.argmax(y_pred_2)}')

output: 8 5

It's like my model is behaving differently if it processes the whole test set in a single run than if it processes a single row at a time.就像我的模型在一次运行中处理整个测试集而不是一次处理一行时的行为不同。 I'm using pandas for the input data.我正在使用熊猫作为输入数据。

EDIT : More info, the output shapes of y_pred and y_pred_2 are (603, 10) and (1, 10) respectively, where 10 is the number of classes I have.编辑:更多信息, y_predy_pred_2的输出形状y_pred_2(603, 10)(1, 10) ,其中 10 是我拥有的类数。

Some example values for both predictions, with an arbitrary i :两个预测的一些示例值,带有任意i

y_pred[i]: array([1.3353945e-02, 2.8374636e-09, 1.4435661e-08, 3.4135045e-18,
   7.7986561e-02, 3.7737598e-03, 2.0284578e-10, 2.7154891e-03,
   9.0203673e-01, 1.3346069e-04], dtype=float32)

y_pred_2 = array([[1.1702824e-16, 1.6781385e-37, 2.5281618e-33, 0.0000000e+00,
        2.3075200e-09, 1.0000000e+00, 9.9125501e-35, 6.2606384e-22,
        5.8689110e-14, 2.3486194e-24]], dtype=float32)

np.argmax defaults to the row axis. np.argmax默认为行轴。 You're getting the maximum prediction across rows, and you want it across columns.您正在获得跨行的最大预测,并且您希望跨列进行预测。 Try:尝试:

print(f'{np.argmax(y_pred[i], axis=1)} {np.argmax(y_pred_2, axis=1)}')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我使用 Scikit-Learn Keras 模型函数时,为什么相同网络配置的准确率结果不同? - Why am I having different accuracy results for the same network configuration when I am using Scikit-Learn Keras model function? 为什么我使用不同的算法得到不同的引导结果? - Why am I getting different bootstrap results using different algorithms? Keras LSTM - 为什么“相同”模型和相同权重的结果不同? - Keras LSTM - why different results with “same” model & same weights? 为什么我得到与所有人预测相同的价值? - why am i getting same value as prediction for all? 实现我的第一个 keras model:为什么我的输入 arrays 不匹配? - Implementing my first keras model: Why am I getting a mismatch in input arrays? 当我测试为多个类别数据集训练的图像分类模型时,为什么会得到相同的类别预测? - Why am I getting same class prediction when I test image classification model trained for multiple class datasets? 为什么我使用相同的脚本得到结果? - Why am I getting results with the same script? 相同的输入导致两次不同的预测 - Same input results in different prediction in two times 用自定义层加载Keras中保存的模型,预测结果不一样? - Loading a saved model in Keras with a custom layer and prediction results are different? 为什么我在 pytorch 中保存和加载模型权重后得到不同的结果? - Why am I getting different results after saving and loading model weights in pytorch?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM