I want to have the same weights for layer initializations in all my networks, so that when I'm comparing their first epoch loss they all start from the same value. Is there a way in keras to do this?

I have set the random seed for the numpy and tensorflow, but still I get different results in initializations.

  • $\begingroup$ Have a look at this keras.io/initializers $\endgroup$
    – Ankit Seth
    Commented Aug 19, 2019 at 6:04
  • $\begingroup$ @Ankit Seth yes i have set the seed for my initializer as well $\endgroup$
    – Moeinh77
    Commented Aug 19, 2019 at 6:47
  • 1
    $\begingroup$ hi - answered a similar question a little while back, it's a little trickier than you might think! datascience.stackexchange.com/a/37418/32697 $\endgroup$
    – redhqs
    Commented Aug 19, 2019 at 10:43
  • $\begingroup$ @redhqs hi,i followed your instructions but i couldn't get it to work $\endgroup$
    – Moeinh77
    Commented Aug 20, 2019 at 13:08
  • 1
    $\begingroup$ @redhqs no errors,the only problem is the cost function value of first epoch is different in each run $\endgroup$
    – Moeinh77
    Commented Aug 24, 2019 at 5:27

1 Answer 1


You need to specify the seed in the initializer, e.g:

from keras.initializers import RandomUniform

seed = 0

model.add(Dense(64, kernel_initializer = RandomUniform(minval = -0.05, 
                                                       maxval =  0.05, 
                                                       seed = seed)))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.