Google Colab上的Tensorflow-Keras可再现性问题 [英] Tensorflow-Keras reproducibility problem on Google Colab
问题描述
我有一个简单的代码可以在Google Colab上运行(我使用CPU模式):
I have a simple code to run on Google Colab (I use CPU mode):
import numpy as np
import pandas as pd
## LOAD DATASET
datatrain = pd.read_csv("gdrive/My Drive/iris_train.csv").values
xtrain = datatrain[:,:-1]
ytrain = datatrain[:,-1]
datatest = pd.read_csv("gdrive/My Drive/iris_test.csv").values
xtest = datatest[:,:-1]
ytest = datatest[:,-1]
import tensorflow as tf
from tensorflow.keras.layers import Dense, Activation
from tensorflow.keras.utils import to_categorical
## SET ALL SEED
import os
os.environ['PYTHONHASHSEED']=str(66)
import random
random.seed(66)
np.random.seed(66)
tf.set_random_seed(66)
from tensorflow.keras import backend as K
session_conf = tf.ConfigProto(intra_op_parallelism_threads=1, inter_op_parallelism_threads=1)
sess = tf.Session(graph=tf.get_default_graph(), config=session_conf)
K.set_session(sess)
## MAIN PROGRAM
ycat = to_categorical(ytrain)
# build model
model = tf.keras.Sequential()
model.add(Dense(10, input_shape=(4,)))
model.add(Activation("sigmoid"))
model.add(Dense(3))
model.add(Activation("softmax"))
#choose optimizer and loss function
model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])
# train
model.fit(xtrain, ycat, epochs=15, batch_size=32)
#get prediction
classes = model.predict_classes(xtest)
#get accuration
accuration = np.sum(classes == ytest)/len(ytest) * 100
我已在此处阅读了用于创建可重复性代码的设置使用带有TensorFlow后端的Keras的可重复结果,我将所有代码都放入了在同一单元格中.但是每次运行该单元格(使用shift + enter
运行该单元格)时,结果(例如损失)总是不同的.
I have read the setup to create a reproducibility code here Reproducible results using Keras with TensorFlow backend and I put all code in the same cell. But the result (e.g. the loss) is always different every time I run that cell (run the cell using shift + enter
).
就我而言,如果只有以下情况,则可以复制上面代码的结果:
In my case, the result from the code above can be reproduced, if only:
- 我使用运行时">重新启动并全部运行"运行,或者
- 我将该代码放在一个文件中,然后使用命令行(
python3 file.py
)运行它
- I run using "runtime" > "restart and run all" or,
- I put that code in a single file and run it using the command line (
python3 file.py
)
我想念些什么来使结果可重现而不重新启动运行时吗?
is there something I miss to make the result reproducible without restart the runtime?
推荐答案
您还应该在Dense
层中修复kernel_initializer
的种子.因此,您的模型将如下所示:
You should also fix the seed for kernel_initializer
in your Dense
layers. So, your model will be like:
model = tf.keras.Sequential()
model.add(Dense(10, kernel_initializer=keras.initializers.glorot_uniform(seed=66), input_shape=(4,)))
model.add(Activation("sigmoid"))
model.add(Dense(3, kernel_initializer=keras.initializers.glorot_uniform(seed=66)))
model.add(Activation("softmax"))
这篇关于Google Colab上的Tensorflow-Keras可再现性问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!