训练和加载的Keras顺序模型给出了不同的结果

时间:2022-03-03 13:54:55

I have trained a model and saved in a particular directory, while training it is giving about 81% testing accuracy. I have used following commands:

我已经训练了一个模型并保存在一个特定的目录中,而在训练时它提供了大约81%的测试精度。我使用了以下命令:

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(LSTM(56, dropout_W = 0.4, dropout_U=0.4))  
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=128, epochs=2, verbose=1,validation_split = 0.2)   

model_json = model.to_json()  
with open("C:/Users/twelve_user/Downloads/model3.json", "w") as json_file:  
        json_file.write(model_json)   
model.save_weights("C:/Users/twelve_user/Downloads/weights_model3.h5")  
print("Saved model to disk")  
predictions = model.predict(testx)

But whenever I'm trying to load the same model in different python script, the accuracy falling down i.e 76%. and sometimes I'm getting random accuracy like an untrained model. commands are given below which i have used for loading:

但每当我试图在不同的python脚本中加载相同的模型时,准确率下降,即76%。有时我会像未经训练的模型那样获得随机准确性。我在下面给出了用于加载的命令:

json_file = open('C:/Users/twelve_user/Downloads/model3.json', 'r')  
loaded_model_json = json_file.read()  
json_file.close()  
model = model_from_json(loaded_model_json)  
model.load_weights("C:/Users/twelve_user/Downloads/weights_mode3.h5")  
print("Loaded model from disk")  

How is this possible? Both trained and loaded model's result should be the same. As i am quite new to Keras, not able to understand where i am wrong.
Thank you for the help! Any help would be appreciated.

这怎么可能?训练和加载模型的结果都应该相同。因为我对Keras很新,不能理解我错在哪里。感谢您的帮助!任何帮助,将不胜感激。

2 个解决方案

#1


0  

This is because the weights are initialised with random values, please find code snippet and details here

这是因为权重是使用随机值初始化的,请在此处查找代码段和详细信息

#2


-1  

It is most likely because you only save the model structure and the model weights. You do not save the state of your optimizer or training configuration. If you want exactly the same model use the keras function model.save.

这很可能是因为您只保存模型结构和模型权重。您不保存优化程序或培训配置的状态。如果你想要完全相同的模型使用keras函数model.save。

Also check this faq for more information.

另请查看此常见问题以获取更多信息。

Example code

示例代码

predictions_before = model.predict(testx)
model.save('model3.h5')
del model

model = load_model('model3.h5')
predictions_after = model.predict(testx)

#1


0  

This is because the weights are initialised with random values, please find code snippet and details here

这是因为权重是使用随机值初始化的,请在此处查找代码段和详细信息

#2


-1  

It is most likely because you only save the model structure and the model weights. You do not save the state of your optimizer or training configuration. If you want exactly the same model use the keras function model.save.

这很可能是因为您只保存模型结构和模型权重。您不保存优化程序或培训配置的状态。如果你想要完全相同的模型使用keras函数model.save。

Also check this faq for more information.

另请查看此常见问题以获取更多信息。

Example code

示例代码

predictions_before = model.predict(testx)
model.save('model3.h5')
del model

model = load_model('model3.h5')
predictions_after = model.predict(testx)