简体   繁体   English

Python:Keras 模型对于相同的数据和相同的模型返回不同的结果

[英]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:与教程中的代码相比,我所做的唯一区别是:

  • in order to make training process faster, I changed number of epochs from 50 to 10,为了使训练过程更快,我将时代数从 50 更改为 10,
  • I deleted Image augmentation block from 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.

相关问题 tensorflowjs 和 keras 在相同模型和张量上的不同结果 - Different results for tensorflowjs and keras on same model and tensor Keras LSTM - 为什么“相同”模型和相同权重的结果不同? - Keras LSTM - why different results with “same” model & same weights? 当以不同格式输入相同数据时,`tf.keras.model.evaluate()`会提供不同的结果 - `tf.keras.model.evaluate()` provides different results when fed the same data in different formats 训练模型相同测试数据的不同结果 - Different results for same test data with trained model 训练具有相同初始权重和相同数据的模型时的结果不同 - Different results when training a model with same initial weights and same data Keras LSTM 模型没有产生相同的结果 - Keras LSTM model not producing same results 对于相同的keras模型,我得到不同的结果 - I get different result for the same keras model 使用 Keras 训练具有两个不同输出的相同 model - Training the same model with two different outputs with Keras Keras模型的输出相同 - Same output of the Keras model 相同的Tensorflow模型在Android和Python上提供不同的结果 - Same Tensorflow model giving different results on Android and Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM