Hello im newbie in neural network , i have written a model CNN architecture Resnet50 using google colab after train my model then saving models afterwards load model without restart run time get same result but why when restart runtime google colab and run xtrain,ytest,x_val,y_val then load model again getting different result
here my code from setting parameter
#hyperparameter and callback batch_size = 128 num_epochs = 120 input_shape = (48, 48, 1) num_classes = 7 #Compile the model. from keras.optimizers import Adam, SGD model = ResNet50(input_shape = (48, 48, 1), classes = 7) optimizer = SGD(learning_rate=0.0005) model.compile(optimizer= optimizer, loss='sparse_categorical_crossentropy', metrics=['accuracy']) model.summary() history = model.fit( data_generator.flow(xtrain, ytrain,), steps_per_epoch=len(xtrain) / batch_size, epochs=num_epochs, verbose=1, validation_data= (x_val,y_val)) import matplotlib.pyplot as plt model.save('Fix_Model_resnet50editSGD5st.h5') #plot graph accuracy = history.history['accuracy'] val_accuracy = history.history['val_accuracy'] loss = history.history['loss'] val_loss = history.history['val_loss'] num_epochs = range(len(accuracy)) plt.plot(num_epochs, accuracy, 'r', label='Training acc') plt.plot(num_epochs, val_accuracy, 'b', label='Validation acc') plt.title('Training and validation accuracy') plt.ylabel('accuracy') plt.xlabel('epoch') plt.legend() plt.figure() plt.plot(num_epochs, loss, 'r', label='Training loss') plt.plot(num_epochs, val_loss, 'b', label='Validation loss') plt.title('Training and validation loss') plt.ylabel('loss') plt.xlabel('epoch') plt.legend() plt.show() #load model from keras.models import load_model model_load = load_model('Fix_Model_resnet50editSGD5st.h5') model_load.summary() testdatamodel = model_load.evaluate(xtest, ytest) print("Test Loss " + str(testdatamodel[0])) print("Test Acc: " + str(testdatamodel[1])) traindata = model_load.evaluate(xtrain, ytrain) print("Test Loss " + str(traindata[0])) print("Test Acc: " + str(traindata[1])) valdata = model_load.evaluate(x_val, y_val) print("Test Loss " + str(valdata[0])) print("Test Acc: " + str(valdata[1]))
-after training and saving model then run load model without restart runtime google colab : as you can see the Test get loss: 0.9411 - accuracy: 0.6514
Train get loss: 0.7796 - accuracy: 0.7091
ModelEvaluateTest & Train
just run load model again after restart runtime colab:
Test get loss: 0.7928 - accuracy: 0.6999
Train get loss: 0.8189 - accuracy: 0.6965
after Restart Runtime Evaluate test and train
ou need to set random seed to get same results on every iteration either in same session or post restarting.
tf.random.set_seed( seed )
check https://www.tensorflow.org/api_docs/python/tf/random/set_seed