# Một số tip hay ### Deterministic Behavior ```python= import torch import numpy as np import random seed = 42 torch.mannual_seed(seed) np.random.seed(seed) random.seed(seed) # if using cuda torch.cuda.mannual_seed_all(seed) torch.backends.cudnn.deterministic = True torch.backends.cudnn.benchmark = False ``` --- ### Mixed Precision ```python= import torch # Creates once at the beginning of training scaler = torch.cuda.amp.GradScaler() for data, label in data_iter: optimizer.zero_grad() # Casts operations to mixed precision with torch.cuda.amp.autocast(): loss = model(data) # Scales the loss, and calls backward() # to create scaled gradients scaler.scale(loss).backward() # Unscales gradients and calls # or skips optimizer.step() scaler.step(optimizer) # Updates the scale for next iteration scaler.update() ``` --- ### Oversampling data ```python= path = '/content' data = datasets.FashionMNIST(path, transform=transforms.ToTensor(), download=True) len = data.__len__() weights = [] for i in range(len): weights.append(np.random.randint(1, 100)) sampler = WeightedRandomSampler(weights=weights, num_samples=len, replacement=True) dataLoader = DataLoader(dataset=data, batch_size=10, shuffle=False, sampler=sampler) ```