Different results everytime calling forward()

import os
import torch
from torch import nn
from torch.utils.data import DataLoader, Dataset
import numpy as np
import pandas as pd
import random
import matplotlib.colors as mcolors
import matplotlib.pyplot as plt
from sklearn.neighbors import NearestNeighbors

Set random seeds for reproducibility

seed = 42
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)

Ensure deterministic behavior

torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False

class EMGDataset(Dataset):
def init(self, emg_data):
emg_data = emg_data.astype(np.float32)
self.x = np.array(emg_data)
self.n_samples = self.x.shape[0]

def __getitem__(self, index):
    return self.x[index]

def __len__(self):
    return self.n_samples

def create_dataloaders():
concat_all_mus = pd.read_csv(“EMG_dataset_prep.csv”, index_col=0)
dataset = EMGDataset(concat_all_mus.iloc[:, :1200])
dataloader = DataLoader(dataset, batch_size=len(dataset), shuffle=False, num_workers=0)
return dataloader

Ensure DataLoader consistency

whole_data = create_dataloaders()

Define device based on availability

device = ‘cuda’ if torch.cuda.is_available() else ‘cpu’

Load model and set to evaluation mode

model = model_builder.VAE(input_shape=1200, hidden_shape=512, output_shape=2, device=device)
model.load_state_dict(torch.load(‘models/all_dataVAE_0.001_5000_512_2_beta_0.1.pth’, map_location=device))
model.to(device)
model.eval()

Define colors

colors = list(mcolors.CSS4_COLORS.keys())
selected_colors = colors[:130]

def plot_latent_space(example_index, whole_data, model, not_all):
data_iter = iter(whole_data)
data_sample = next(data_iter)
data_sample = data_sample.to(device)

model.eval()
with torch.no_grad():
    z, latent_mean, latent_std, reconstructed, _ = model(data_sample)

result = z_index(z.cpu(), index_dataframe)
choose_index = result[result['subject'].isin(example_index)]

print(f"Latent space mean: {np.mean(z.cpu().detach().numpy(), axis=0)}")
print(f"Latent space std: {np.std(z.cpu().detach().numpy(), axis=0)}")

plt.figure(figsize=(10, 10))
for idx in choose_index['subject'].unique():
    subset = choose_index[choose_index['subject'] == idx]
    plt.scatter(subset['z1'], subset['z2'], label=f'Index {idx}', c=selected_colors[idx])

if not_all:
    plt.legend(title='Index')

plt.xlabel('VAE_axis_1')
plt.ylabel('VAE_axis_2')
plt.title('Latent Space Result')
plt.show()

def z_index(z, index_dataframe):
result = pd.concat([pd.DataFrame(z.numpy(), columns=[‘z1’, ‘z2’]), index_dataframe], axis=1)
return result

Load index data

concat_all_mus = pd.read_csv(“EMG_dataset_index.csv”, index_col=0)
index_dataframe = concat_all_mus.iloc[:, 1200:1201]
all_index = np.unique(index_dataframe).tolist()

Call plot_latent_space function twice to check for consistency

plot_latent_space(all_index, whole_data, model, 0)
plot_latent_space(all_index, whole_data, model, 0)

I have a question about this code. When I call function plot_latent_space(), It gives different results. I was expecting it would give a equal result but it does not. Why does that happen?

Latent space mean: [0.04162313 0.03197911]
Latent space std: [0.9312542 0.911205 ]

Latent space mean: [0.03519088 0.02695682]
Latent space std: [0.938787 0.91289806]

What if you set random seed inside the plot_latent_space function?

Thanks! It worked. Do you know the reason why the seed should be set inside the function?

If you are using random operations in the function it will advance the random state so that the next time you execute the function the random operations would produce different results.