How does Pytorch’s “Fold” and “Unfold” work?

unfold imagines a tensor as a longer tensor with repeated columns/rows of values ‘folded’ on top of each other, which is then “unfolded”: size determines how large the folds are step determines how often it is folded E.g. for a 2×5 tensor, unfolding it with step=1, and patch size=2 across dim=1: x = torch.tensor([[1,2,3,4,5], [6,7,8,9,10]]) … Read more

What does model.train() do in PyTorch?

model.train() tells your model that you are training the model. This helps inform layers such as Dropout and BatchNorm, which are designed to behave differently during training and evaluation. For instance, in training mode, BatchNorm updates a moving average on each new batch; whereas, for evaluation mode, these updates are frozen. More details: model.train() sets … Read more

Why do we need to call zero_grad() in PyTorch?

In PyTorch, for every mini-batch during the training phase, we typically want to explicitly set the gradients to zero before starting to do backpropragation (i.e., updating the Weights and biases) because PyTorch accumulates the gradients on subsequent backward passes. This accumulating behaviour is convenient while training RNNs or when we want to compute the gradient … Read more

How do I initialize weights in PyTorch?

Single layer To initialize the weights of a single layer, use a function from torch.nn.init. For instance: conv1 = torch.nn.Conv2d(…) torch.nn.init.xavier_uniform(conv1.weight) Alternatively, you can modify the parameters by writing to conv1.weight.data (which is a torch.Tensor). Example: conv1.weight.data.fill_(0.01) The same applies for biases: conv1.bias.data.fill_(0.01) nn.Sequential or custom nn.Module Pass an initialization function to torch.nn.Module.apply. It will … Read more

How to initialize weights in PyTorch?

Single layer To initialize the weights of a single layer, use a function from torch.nn.init. For instance: conv1 = torch.nn.Conv2d(…) torch.nn.init.xavier_uniform(conv1.weight) Alternatively, you can modify the parameters by writing to conv1.weight.data (which is a torch.Tensor). Example: conv1.weight.data.fill_(0.01) The same applies for biases: conv1.bias.data.fill_(0.01) nn.Sequential or custom nn.Module Pass an initialization function to torch.nn.Module.apply. It will … Read more

How do I save a trained model in PyTorch?

Found this page on their github repo: Recommended approach for saving a model There are two main approaches for serializing and restoring a model. The first (recommended) saves and loads only the model parameters: torch.save(the_model.state_dict(), PATH) Then later: the_model = TheModelClass(*args, **kwargs) the_model.load_state_dict(torch.load(PATH)) The second saves and loads the entire model: torch.save(the_model, PATH) Then later: … Read more

Best way to save a trained model in PyTorch? [closed]

Found this page on their github repo, I’ll just copy-paste the content here. Recommended approach for saving a model There are two main approaches for serializing and restoring a model. The first (recommended) saves and loads only the model parameters: torch.save(the_model.state_dict(), PATH) Then later: the_model = TheModelClass(*args, **kwargs) the_model.load_state_dict(torch.load(PATH)) The second saves and loads the … Read more