I have seen 'seed' parameter in many codes of Keras. I have a code example here:

validation_generator = data_generator.flow_from_directory( 
    target_size = (img_width, img_height), 
    batch_size = batch_size,
    shuffle = True,
    class_mode = 'categorical',
    seed = 42,
    subset = 'validation')

Can you explain what is it? And when to use it?


1 Answer 1


As most of statistical models (regression, neural networks etc.) use probabilistic frameworks in their optimazation process (E.g. softmaxfrom a random distribution). You need a seedto reproduce your results. A seed is a fixed "value set" drawn from a random distribution.

If you would run your model several times you will get slightly different estimates as the starting values from your optimazation will slightly differ every time.


Some additional information on the optimazation process when dealing with Neural Networks.

What you are optimizing in neural networks are the weights (connections) between the Neurons. Connections that have strong predictable character get somewhat stronger (more weight) relative to other connections with less predictive power. This process is done when you "train" your data. However to train your network you need some "inital weights" which are normally assigned by random (there are different approaches, but this is the most common one).

Like previously said - The inital weights will have impact on your optimazation and the results. That other users can check your model / reproduce your results you need to save the inital weights. - Therefore you use a 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.