I have a python script for building a keras sequential model. Everytime i am getting different results without any changes in script. kindly have a look on script. where i am wrong please help.
我有一个用于构建keras顺序模型的python脚本。每次我得到不同的结果而不改变脚本。请仔细看看剧本。哪里我错了请帮忙。
thedata = pandas.read_csv("C:/User/Downloads/LSTM/data.csv", sep=', ', delimiter=',', header='infer', names=None)
np.random.seed(1337)
x = thedata['Review']
y = thedata['Polarity_Numeral']
x = x.iloc[:].values
y = y.iloc[:].values
tk = Tokenizer(num_words=40000, lower=True, split=" ")
tk.fit_on_texts(x)
x = tk.texts_to_sequences(x)
max_len = 120
x = pad_sequences(x, maxlen=max_len)
max_features = 40000
testx = x[51000:52588]
print (testx)
testy = y[51000:52588]
x = x[0:50999]
y = y[0:50999]
model = Sequential()
model.add(Embedding(max_features, 128, input_length=max_len))
model.add(SpatialDropout1D(0.3))
model.add(GaussianNoise(0.2))
model.add(LSTM(128 , dropout_W=0.3, dropout_U=0.3, return_sequences=False))
model.add(Dense(1, W_regularizer=l2(0.2)))
model.add(Activation('sigmoid'))
model.summary()
adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.00)
model.compile(loss='binary_crossentropy', optimizer=adam,metrics = ['accuracy'] )
model_history = model.fit(x, y=y, batch_size=64, epochs=1, verbose=1,validation_split = 0.2)
model.save('C:/User/Downloads/model.h5')
model.save_weights('C:/User/Downloads/weight_model.h5')
predictions = model.predict(testx)
print (predictions)
On first time run, i am getting i.e 57% On Second time run .. 53% On third .. 55% Everytime it is changing randomly. Thanks for the help!
在第一次运行时,我得到的是57%在第二次运行.. 53%在第三次... 55%每次它随机变化。谢谢您的帮助!
3 个解决方案
#1
1
If you are running "exactly" that code, know that you're entirely creating a new model.
如果您正在“完全”运行该代码,请知道您正在创建一个新模型。
You're not loading a model, you're not adding your own weights to the model. You're simply creating a new model, with an entirely new random set of weights.
您没有加载模型,也没有将自己的权重添加到模型中。您只是创建一个新模型,使用全新的随机权重集。
So, yes, it will produce different results. There is nothing wrong.
所以,是的,它会产生不同的结果。没有任何错误。
You probably should be using some kind of "load saved model" (perhaps model.load_weights()
) if you want the same model to be kept. (In case you have the model saved somewhere)
如果你想保留相同的模型,你可能应该使用某种“加载保存的模型”(也许是model.load_weights())。 (如果您将模型保存在某处)
Or you should "set_weights()" at some point after creating the model (if you know what weights you want, or if you have your weights saved)
或者你应该在创建模型后的某个时刻“set_weights()”(如果你知道你想要什么权重,或者你是否保存了你的权重)
Or you can use the initializers in each layer (as mentioned in another answer), if you want a new model with known weights.
或者,如果您需要具有已知权重的新模型,则可以在每个图层中使用初始值设定项(如另一个答案中所述)。
#2
0
with a quick look i don't see anything wrong.. you should remember that when you compile your model, keras randomly initializes all the weights in your model (you can also specify how you would like this to be done, or if you don't want it to be random, but the default is usually fine). So every time you compile you will get different weights and different results... given enough epochs they should all converge to the same result.
快速查看我没有看到任何错误..你应该记住,当你编译你的模型时,keras会随机初始化你模型中的所有权重(你也可以指定你希望如何完成这项工作,或者你是否愿意不希望它是随机的,但默认通常很好。因此,每次编译时,您将得到不同的权重和不同的结果......给定足够的时期,它们都应该收敛到相同的结果。
#3
0
This code is for tensorflow backend
此代码用于tensorflow后端
This is because the weights are initialised using random numbers and hence you will get different results every time. This is expected behaviour. To have reproducible result you need to set the random seed as:
这是因为权重是使用随机数初始化的,因此每次都会得到不同的结果。这是预期的行为。要获得可重现的结果,您需要将随机种子设置为:
import tensorflow as tf
import random as rn
os.environ['PYTHONHASHSEED'] = '0'
# Setting the seed for numpy-generated random numbers
np.random.seed(37)
# Setting the seed for python random numbers
rn.seed(1254)
# Setting the graph-level random seed.
tf.set_random_seed(89)
from keras import backend as K
session_conf = tf.ConfigProto(
intra_op_parallelism_threads=1,
inter_op_parallelism_threads=1)
#Force Tensorflow to use a single thread
sess = tf.Session(graph=tf.get_default_graph(), config=session_conf)
K.set_session(sess)
# Rest of the code follows from here on ...
#1
1
If you are running "exactly" that code, know that you're entirely creating a new model.
如果您正在“完全”运行该代码,请知道您正在创建一个新模型。
You're not loading a model, you're not adding your own weights to the model. You're simply creating a new model, with an entirely new random set of weights.
您没有加载模型,也没有将自己的权重添加到模型中。您只是创建一个新模型,使用全新的随机权重集。
So, yes, it will produce different results. There is nothing wrong.
所以,是的,它会产生不同的结果。没有任何错误。
You probably should be using some kind of "load saved model" (perhaps model.load_weights()
) if you want the same model to be kept. (In case you have the model saved somewhere)
如果你想保留相同的模型,你可能应该使用某种“加载保存的模型”(也许是model.load_weights())。 (如果您将模型保存在某处)
Or you should "set_weights()" at some point after creating the model (if you know what weights you want, or if you have your weights saved)
或者你应该在创建模型后的某个时刻“set_weights()”(如果你知道你想要什么权重,或者你是否保存了你的权重)
Or you can use the initializers in each layer (as mentioned in another answer), if you want a new model with known weights.
或者,如果您需要具有已知权重的新模型,则可以在每个图层中使用初始值设定项(如另一个答案中所述)。
#2
0
with a quick look i don't see anything wrong.. you should remember that when you compile your model, keras randomly initializes all the weights in your model (you can also specify how you would like this to be done, or if you don't want it to be random, but the default is usually fine). So every time you compile you will get different weights and different results... given enough epochs they should all converge to the same result.
快速查看我没有看到任何错误..你应该记住,当你编译你的模型时,keras会随机初始化你模型中的所有权重(你也可以指定你希望如何完成这项工作,或者你是否愿意不希望它是随机的,但默认通常很好。因此,每次编译时,您将得到不同的权重和不同的结果......给定足够的时期,它们都应该收敛到相同的结果。
#3
0
This code is for tensorflow backend
此代码用于tensorflow后端
This is because the weights are initialised using random numbers and hence you will get different results every time. This is expected behaviour. To have reproducible result you need to set the random seed as:
这是因为权重是使用随机数初始化的,因此每次都会得到不同的结果。这是预期的行为。要获得可重现的结果,您需要将随机种子设置为:
import tensorflow as tf
import random as rn
os.environ['PYTHONHASHSEED'] = '0'
# Setting the seed for numpy-generated random numbers
np.random.seed(37)
# Setting the seed for python random numbers
rn.seed(1254)
# Setting the graph-level random seed.
tf.set_random_seed(89)
from keras import backend as K
session_conf = tf.ConfigProto(
intra_op_parallelism_threads=1,
inter_op_parallelism_threads=1)
#Force Tensorflow to use a single thread
sess = tf.Session(graph=tf.get_default_graph(), config=session_conf)
K.set_session(sess)
# Rest of the code follows from here on ...