python3.7 keras和tensorflow兼容_结果无法在Python中用Keras和TensorFlow重现

我有个问题,我不能用Keras和sorflow重现我的结果。在

似乎最近在Keras documentation site上发布了一个解决这个问题的方法,但不知怎么的它对我不起作用。在

我做错什么了?在

我正在用一个Jupyter笔记本电脑在MBP视网膜上(没有Nvidia 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))

使用的Python版本:

^{pr2}$

解决方法已经包含在代码中(没有效果)。在

每次我做训练的时候,我都会得到不同的结果。在

重置Jupyter笔记本的内核时,第一次与第一次相对应,第二次与第二次相对应。在

所以在重置之后,我总是在第一次运行时得到0.7782,在第二次运行时得到0.7732等等

但是每次运行时,没有内核重置的结果总是不同的。在

任何建议我都会很有帮助的!在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值