简体   繁体   中英

Trained and Loaded Keras Sequential Model is giving different result

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:

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 ie 76%. and sometimes I'm getting random accuracy like an untrained model. commands are given below which i have used for loading:

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.

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

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 .

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM