KerasとThensorFlowで結果を再現できないという問題があります。結果がPythonのKerasとTensorFlowで再現できない
最近この問題のためにに公開された回避策がありましたが、どういうわけか私のためには機能しません。
私は間違っていますか?
MBP Retina(Nvidia GPUなし)でJupyter Notebookを使用しています。
# ** Workaround from Keras Documentation **
import numpy as np
import tensorflow as tf
import random as rn
# The below is necessary in Python 3.2.3 onwards to
# have reproducible behavior for certain hash-based operations.
# See these references for further details:
# https://docs.python.org/3.4/using/cmdline.html#envvar-PYTHONHASHSEED
# https://github.com/fchollet/keras/issues/2280#issuecomment-306959926
import os
os.environ['PYTHONHASHSEED'] = '0'
# The below is necessary for starting Numpy generated random numbers
# in a well-defined initial state.
np.random.seed(42)
# The below is necessary for starting core Python generated random numbers
# in a well-defined state.
rn.seed(12345)
# Force TensorFlow to use single thread.
# Multiple threads are a potential source of
# non-reproducible results.
# For further details, see: https://stackoverflow.com/questions/42022950/which-seeds-have-to-be-set-where-to-realize-100-reproducibility-of-training-res
session_conf = tf.ConfigProto(intra_op_parallelism_threads=1, inter_op_parallelism_threads=1)
from keras import backend as K
# The below tf.set_random_seed() will make random number generation
# in the TensorFlow backend have a well-defined initial state.
# For further details, see: https://www.tensorflow.org/api_docs/python/tf/set_random_seed
tf.set_random_seed(1234)
sess = tf.Session(graph=tf.get_default_graph(), config=session_conf)
K.set_session(sess)
# ** Workaround end **
# ** Start of my code **
# LSTM and CNN for sequence classification in the IMDB dataset
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers.embeddings import Embedding
from keras.preprocessing import sequence
from sklearn import metrics
# fix random seed for reproducibility
#np.random.seed(7)
# ... importing data and so on ...
# create the model
embedding_vecor_length = 32
neurons = 91
epochs = 1
model = Sequential()
model.add(Embedding(top_words, embedding_vecor_length, input_length=max_review_length))
model.add(LSTM(neurons))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='mean_squared_logarithmic_error', optimizer='adam', metrics=['accuracy'])
print(model.summary())
model.fit(X_train, y_train, epochs=epochs, batch_size=64)
# Final evaluation of the model
scores = model.evaluate(X_test, y_test, verbose=0)
print("Accuracy: %.2f%%" % (scores[1]*100))
Python 3.6.3 | Anaconda custom(x86_64)| (デフォルト、Oct 6 2017、12:04:38) [GCC 4.2.1互換Clang 4.0.1(tags/RELEASE_401/final)]
回避策は既にコードに含まれています(効果なし)。
毎回私は練習用の部分をしていますが、私は別の結果を得ています。
Jupyter Notebookのカーネルをリセットすると、1回目は初回に、2回目は2回目に対応します。
だから私は常にセカンドランなど
しかし上、最初の実行でたとえば0.7782のために0.7732を取得しますリセットした後、カーネル・リセットなしの結果は、私はそれを実行するたびに、常に異なっています。
私はどんな提案にも役立つでしょう!
'np.random.get_state()'と 'rn.getstate()'を出力に追加できますか? GPUやCPUを使用していますか?あなたは 'python'でスクリプトを試すことができますか? – Maxim