日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

發布時間:2025/3/12 python 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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等等

但是每次運行時,沒有內核重置的結果總是不同的。在

任何建議我都會很有幫助的!在

總結

以上是生活随笔為你收集整理的python3.7 keras和tensorflow兼容_结果无法在Python中用Keras和TensorFlow重现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。