Sequential Model Give A Different Result At Every Run
Solution 1:
This code is for tensorflow backend
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 ...
Solution 2:
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)
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)
Or you can use the initializers in each layer (as mentioned in another answer), if you want a new model with known weights.
Solution 3:
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.
Post a Comment for "Sequential Model Give A Different Result At Every Run"