简体   繁体   English

为什么 model.predict() 的精度比具有相同 x_train 的 model.fit() 更差?

[英]Why model.predict() is getting worse accuracy than model.fit() with the same x_train?

images receives an array of images loaded with cv2 images 接收用 cv2 加载的图像数组

images=np.array(images)
labels=np.array(labels)

idLabels=[]
for i in labels:
    idLabels.append(dicTipos[i])
labels=np.array(idLabels)

images = np.array(images, dtype = 'float32')
print("images done")
print("labels", labels)
labels = np.array(labels, dtype = 'int32')


x_train=images
y_train=labels

I defined the model and after that I use fit()我定义了 model 然后我使用 fit()

model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
              metrics=['accuracy'])


history = model.fit(x_train, y_train, epochs=10)

The final line of the output is: output的最后一行是:

Epoch 10/10
25/25 [==============================] - 101s 4s/step - loss: 0.0047 - accuracy: 0.9987

Immediately I use predict() function and the accurary is really bad:我立即使用 predict() function 并且准确度非常糟糕:

predicted=model.predict(x_train)
rounded_predictions=np.argmax(predicted,axis=1)
temp = sum(y_train == rounded_predictions)
temp=temp/len(y_train)
print("Accuracy:  ", temp)

Output: Output:

Accuracy:   0.12625

I don't know why this (worse accuracy than fit) could happen if I'm setting the same x_train for training and the same x_train for testing如果我为训练设置相同的 x_train 为测试设置相同的 x_train,我不知道为什么会发生这种情况(比拟合更差)

You should directly use the accuracy metrics that you compiled with the model.您应该直接使用使用 model 编译的准确度指标。 This will ensure you get accuracy evaluated the same way when training and testing这将确保您在训练和测试时以相同的方式评估准确性

Try:尝试:

perf = model.evaluate(x_train, y_train,return_dict = True)
print (perf)

暂无
暂无

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

相关问题 model.predict 的 Tensorflow 精度与 model.fit 的最终时期 val_accuracy 不匹配 - Tensorflow accuracy from model.predict does not match final epoch val_accuracy of model.fit Keras模型:用于model.fit的同一数组未在model.predict中处理 - Keras Model: Same array that is used for model.fit is not being processed in model.predict model.predict 会导致 oom 问题,但 model.fit 不会: - model.predict leads to oom issues, but model.fit does not: model.predict()和model.fit()做什么? - What do model.predict() and model.fit() do? model.fit vs model.predict-sklearn中的差异和用法 - model.fit vs model.predict - differences & usage in sklearn 不同的model.fit输出(加载模型无训练后)和keras中的model.predict - different output of model.fit (after loading model no training) and model.predict in keras model.fit(X_train, y_train, epochs=5, validation_data=(X_test, y_test) 不工作 - model.fit(X_train, y_train, epochs=5, validation_data=(X_test, y_test) isn't working 成功训练 Model.fit 但在 collab 上运行 model.predict 时提示资源错误 - Successfully trained Model.fit but prompted resource error when running model.predict on collab Keras:我可以使用model.predict而不使用model.predict_generator来预测是否使用model.fit_generator训练模型 - Keras: can I use model.predict but not model.predict_generator to predict if I train the model with model.fit_generator Keras:使用 model.train_on_batch() 和 model.fit() 获得不同的精度。 可能是什么原因以及如何解决? - Keras: Getting different accuracy using model.train_on_batch() and model.fit(). What could be the reason and how to fix that?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM