Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set seed when using pytorch lightning?

I have a training code using pytorch lightning. To get the same results in each run, I set the seeds like this:

if __name__ == '__main__':
    pl.seed_everything(1234)
    random.seed(1234)
    np.random.seed(1234)
    torch.manual_seed(1234)

but I still get different prediction results. What should I do to make sure the output of the model are always the same for all runs?

like image 771
A User Avatar asked Oct 24 '25 14:10

A User


1 Answers

Try using the function seed_everything from lightning.pytorch and also specify deterministic=True when initializing pl.Trainer.

from lightning.pytorch import seed_everything
import lightning.pytorch as pl

seed_everything(42, workers=True)

trainer = pl.Trainer(limit_train_batches=100, max_epochs=1, deterministic=True)
like image 134
Anna Andreeva Rogotulka Avatar answered Oct 28 '25 04:10

Anna Andreeva Rogotulka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!