junekk
junekk

Reputation: 21

why the output of model is different in pytorch

I have a simple model, just only one linear layer.

model = torch.nn.Linear(1,1).to(device)
x_train1 = torch.FloatTensor([[1], [2], [3]])
out = model(x_train1)
print(out)

But whenever I tried to run this code, the printed output is diffrent.

Also I set these random seeds.

import random
import torch
import numpy as np
random_seed=76
torch.manual_seed(random_seed)
torch.cuda.manual_seed(random_seed)
torch.cuda.manual_seed_all(random_seed) # if use multi-GPU
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(random_seed)
random.seed(random_seed)

I want to know why the output keep changing when the code is run.

Upvotes: 2

Views: 550

Answers (1)

Dimitri Sifoua
Dimitri Sifoua

Reputation: 499

You must set the seed every time you run the code if want to get the same result.

import torch

def my_func(device: str, seed: int):
    torch.manual_seed(seed)
    model = torch.nn.Linear(1,1).to(device)
    x_train1 = torch.FloatTensor([[1], [2], [3]])
    out = model(x_train1)
    print(out)

# Whenever you run the function you'll get the same result!

my_func(device="cpu", seed=76)
# tensor([[0.3573],
#         [0.5021],
#         [0.6470]], grad_fn=<AddmmBackward>)

my_func(device="cpu", seed=76)
# tensor([[0.3573],
#         [0.5021],
#         [0.6470]], grad_fn=<AddmmBackward>)

Upvotes: 1

Related Questions