简体   繁体   English

无法使用 Keras CNN Model 获得可重现的结果

[英]Cannot get Reproducible Results with Keras CNN Model

I have read several other posts about how to get reproducible results using tensorflow/keras.我已经阅读了其他几篇关于如何使用 tensorflow/keras 获得可重现结果的文章。 However, I am still getting varying results.但是,我仍然得到不同的结果。

The additional random components of my script include a sklearn.train_test_split() which I set a random_state = 123456 and also a ImageDataGenerator from Keras for data augmentation.我的脚本的其他随机组件包括一个 sklearn.train_test_split() ,我设置了一个 random_state = 123456 以及一个来自 Keras 的ImageDataGenerator用于数据扩充。 I am setting the seed there as well.我也在那里播种。

Below is the code and where I set the seed.下面是代码和我设置种子的位置。 I am running my model for 1 epoch to compare the accuracies and they are different every time.我正在运行我的 model 1 个时期来比较准确度,每次都不同。 I am setting the random seeds at the top of my code and then within the functions I use the random state. If I don't utilize the random_state in sklearn.model_selection.train_test_split or seed in keras.preprocessing.image.ImageDataGenerator I also do not get the same results.我在代码顶部设置随机种子,然后在函数中使用随机 state。如果我不使用random_state中的sklearn.model_selection.train_test_splitkeras.preprocessing.image.ImageDataGenerator中的seed ,我也会这样做得不到相同的结果。

# seeding necessary for neural network reproducibility at top of the code
SEED = 123456
import os
import random as rn
import numpy as np

os.environ['PYTHONHASHSEED']=str(SEED)
np.random.seed(SEED)
tf.random.set_seed(SEED)
rn.seed(SEED)

# within another function
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size = validation_size, random_state = 123456)

# in the model building function
data_generator = ImageDataGenerator(
    rotation_range = 15,
    width_shift_range = 0.1,
    height_shift_range = 0.1,
    horizontal_flip = True
    #zoom_range = [0.5, 1.0] # half zoom to double zoom possibilities
)
data_generator.fit(xtrain, seed = 123456)

After some trial and error.经过反复试验。 I found there is a library for NVIDIA GPU (which I am using) that will give me deterministic results.我发现有一个 NVIDIA GPU 库(我正在使用)可以给我确定性的结果。

First I install the library with pip install tensorflow-determinism Then set os environment variable:首先我安装库pip install tensorflow-determinism然后设置 os 环境变量:

os.environ['TF_DETERMINISTIC_OPS'] = '1' . os.environ['TF_DETERMINISTIC_OPS'] = '1' I am now getting reproducible results:我现在得到可重现的结果:

Test error: 1.6772334575653076, test accuracy: 0.5085999965667725
Test error: 1.6772334575653076, test accuracy: 0.5085999965667725

I think your problem are how the keras model parameters are initialized.我认为你的问题是 keras model 参数是如何初始化的。 You can specify your own initializers to get reproducible behaviour as documented here您可以指定自己的初始化程序以获得此处记录的可重现行为

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

相关问题 如何在CNN模型中使用tensorflow-gpu获得可重现的结果? - How to get reproducible results using tensorflow-gpu in CNN model? 如何在 keras 中获得可重现的结果 - How to get reproducible results in keras 如何使用 Keras 获得可重现的结果? - How do I get reproducible results with Keras? 在 Python 中使用 Keras 和 TensorFlow 无法重现结果 - Results not reproducible with Keras and TensorFlow in Python 如何获得 T5 变压器模型的可重现结果 - How to get reproducible results of T5 transformer model Keras 顺序 model 结果不可重现,在使用 Optuna 优化的相同数据集和参数上的结果非常不一致 - Keras sequential model results not reproducible with wildly inconsistent results on same dataset and parameters optimized using Optuna 在这个用于图像分类的简单 CNN Tensorflow Keras model 中,我的测试数据准确率无法超过 50% - I cannot get over 50% accuracy on my test data in this simple CNN Tensorflow Keras model for image classification CNN模型Keras分类预测结果差异巨大 - Huge disparity in classification prediction results in CNN model Keras Keras:CNN模型不是在学习 - Keras: CNN model is not learning 构建Keras项目以在GPU中实现可重现的结果 - Structuring a Keras project to achieve reproducible results in GPU
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM