[英]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.