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