[英]Python: Keras model returns different results for the same data and same model
Last few hours, I have been trying to make my first model for image classification.过去几个小时,我一直在尝试制作我的第一个图像分类模型。 For this purpose, I have used Image classification from scratch tutorial.
为此,我从头开始使用图像分类教程。 As I followed the steps I managed to reach the end of the tutorial.
当我按照步骤操作时,我设法完成了教程。
The only differences I made compared to the code in tutorial are:与教程中的代码相比,我所做的唯一区别是:
make_model
function (one row of code).make_model
函数(一行代码)中删除了图像增强块。 Now, I am getting to what my problem is.现在,我正在解决我的问题。 At the end, when I try to get the prediction results for the same data and the same model again, the results are different.
最后,当我再次尝试获得相同数据和相同模型的预测结果时,结果却有所不同。 Look at this simple code:
看看这个简单的代码:
>>> for i in range(5):
... predictions = model.predict(val_ds)
... predictions_list = [round(pred[0], 3) for pred in predictions]
... print(predictions_list[:10])
and the result:结果:
[0.937, 0.905, 1.0, 0.094, 0.021, 0.095, 0.07, 0.006, 1.0, 1.0]
[0.905, 1.0, 1.0, 1.0, 1.0, 1.0, 0.122, 1.0, 1.0, 0.0]
[0.996, 0.003, 1.0, 0.887, 1.0, 1.0, 0.798, 1.0, 1.0, 1.0]
[1.0, 1.0, 0.819, 0.999, 1.0, 0.887, 0.087, 1.0, 0.914, 1.0]
[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.916, 0.102]
I assume, that results can be different only in case I retrain the model.我认为,只有在我重新训练模型的情况下,结果才会有所不同。 But that is not my case!
但这不是我的情况! I only rerun
.predict()
method.我只重新运行
.predict()
方法。 So, my question is - can you help me, what I am doing wrong, please?所以,我的问题是 - 你能帮我吗,我做错了什么? Am I missing something?
我错过了什么吗?
Can you try with the code below, please?你可以试试下面的代码吗?
>>> val_item = val_ds.take(1)
>>> for i in range(5):
... predictions = model.predict(val_item)
... predictions_list = [round(pred[0], 3) for pred in predictions]
... print(predictions_list[:10])
In your code, you are using different items not the same one.在您的代码中,您使用的是不同的项目,而不是相同的项目。 You can check it by manually printing the val_ds value.
您可以通过手动打印 val_ds 值来检查它。
The problem was with reading data function tf.keras.preprocessing.image_dataset_from_directory
, which has got its shuffle
argument set to True
.问题在于读取数据函数
tf.keras.preprocessing.image_dataset_from_directory
,它已将其shuffle
参数设置为True
。
When I reload the data again and set shuffle=False
like this:当我再次重新加载数据并像这样设置
shuffle=False
:
>>> val_ds = tf.keras.preprocessing.image_dataset_from_directory(
... 'PetImages',
... shuffle=False,
... validation_split=0.2,
... subset="validation",
... seed=1337,
... image_size=image_size,
... batch_size=batch_size,
... )
>>> for i in range(5):
... predictions = model.predict(val_ds)
... predictions_list = [round(pred[0], 3) for pred in predictions]
... print(predictions_list[:10])
then the result looks as I expected:然后结果看起来和我预期的一样:
[0.998, 0.994, 1.0, 1.0, 0.885, 1.0, 0.998, 1.0, 0.979, 1.0]
[0.998, 0.994, 1.0, 1.0, 0.885, 1.0, 0.998, 1.0, 0.979, 1.0]
[0.998, 0.994, 1.0, 1.0, 0.885, 1.0, 0.998, 1.0, 0.979, 1.0]
[0.998, 0.994, 1.0, 1.0, 0.885, 1.0, 0.998, 1.0, 0.979, 1.0]
[0.998, 0.994, 1.0, 1.0, 0.885, 1.0, 0.998, 1.0, 0.979, 1.0]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.