0
$\begingroup$

I have class that I use to optimize parameters of a Keras LSTM model. It is known that to set seed for keras, one must input the follow on its code. But what I'm not understanding is where to put it in the case of a class that will build and modify the models.

from numpy.random import seed
seed(1)
from tensorflow.random import set_seed
set_seed(2)

Should it be in the __init__ like bellow?

from numpy.random import seed
from tensorflow.random import set_seed

class OptimizeLSTM:
    def __init__(self, X_train, y_train, X_test, y_test, verbose=False):
        self._X_train = X_train
        self._y_train = y_train
        self._X_test = X_test
        self._y_test = y_test
        self._verbose = verbose
        seed(1)
        set_seed(2)
$\endgroup$
0
$\begingroup$

No you should declare it ahead of the class, just after importing the packages. You have declare the seed for numpy and tensorflow separately. For further details read this blog - https://machinelearningmastery.com/reproducible-results-neural-networks-keras/

$\endgroup$
3
  • $\begingroup$ Thanks, I did read the content of the link, but it wasn't that clear about using the seed globally for a class. $\endgroup$
    – Hugo Abreu
    Mar 25 at 0:24
  • $\begingroup$ See if you invoke many classes, it is better that you have one universal seed. Seeds preserve reproducibility of your experiments, and if the experiments use two different classes it is better all are generated by the "randomness scheme". $\endgroup$
    – Jaswin
    Mar 25 at 6:54
  • $\begingroup$ Yes, that what I want! $\endgroup$
    – Hugo Abreu
    Mar 25 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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