Reputation: 11
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
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
Upvotes: 0
Views: 863
Reputation: 4960
It depends on the random numbers generated during computations and training.
For example, model learns from data, and you may shuffle the data before feeding to the model. So it shuffles the data randomly, and each time you restart the session, the input data to the model, vary in each batch and iteration.
The other place randomness affects the results, is in initialization weights in model. Model for the first time initialize the weights and biases. So, each time you start over, it gets different numbers as start, and the learning journey may have a different way.
So, one of the main reason is the randomness. You can set seed of randomness to be able to control the generated data. For example for tensorflow you can set it by tf.random.set_seed(0)
. The number you give to the function is arbitrary number. Or if you use numpy library, and may your code generate some data based on numpy library. Then if you want to control randomness you may set its' seed by numpy.random.seed(0)
.
You may set it on top of your code.
Upvotes: 0
Reputation: 373
You 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
Upvotes: 1