Keras 및 ThensorFlow로 결과를 재현 할 수 없다는 문제가 있습니다.결과가 Python에서 Keras 및 TensorFlow로 재현되지 않습니다.
최근에이 문제에 대한 에 해결 방법이 게시 된 것처럼 보이지만 어쨌든 그것은 나를 위해 작동하지 않습니다.
내가 뭘 잘못하고 있니?
나는 MBP 망막 (엔비디아 GPU 제외)에서 주피터 노트를 사용하고 있습니다.
# ** 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))
파이썬 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의 커널을 재설정 할 때 첫 번째 시간은 첫 번째 시간과 두 번째 시간이 두 번째 시간과 일치합니다.
그래서 난 항상 두 번째 실행 등
그러나에, 첫 번째 실행에서 예를 0.7782에 대해 0.7732을 얻을 것이다 재설정 한 후 커널 재설정없이 결과는 내가 그것을 실행할 때마다 항상 다르다.
나는 어떤 제안에도 도움이 될 것입니다!
출력에'np.random.get_state()'와'rn.getstate()'를 추가 할 수 있습니까? GPU 또는 CPU를 사용합니까? 'python'으로 스크립트를 사용해 볼 수 있습니까? – Maxim