23,953 questions
640
votes
21
answers
1.5m
views
How do I check if PyTorch is using the GPU?
How do I check if PyTorch is using the GPU? The nvidia-smi command can detect GPU activity, but I want to check it directly from inside a Python script.
385
votes
9
answers
320k
views
Why do we need to call zero_grad() in PyTorch?
Why does zero_grad() need to be called during training?
| zero_grad(self)
| Sets gradients of all model parameters to zero.
352
votes
9
answers
281k
views
What does `view()` do in PyTorch?
What does view() do to the tensor x? What do negative values mean?
x = x.view(-1, 16 * 5 * 5)
280
votes
11
answers
524k
views
How do I initialize weights in PyTorch?
How do I initialize weights and biases of a network (via e.g. He or Xavier initialization)?
252
votes
8
answers
175k
views
What does .contiguous() do in PyTorch?
What does x.contiguous() do for a tensor x?
211
votes
12
answers
192k
views
Check the total number of parameters in a PyTorch model
How do I count the total number of parameters in a PyTorch model? Something similar to model.count_params() in Keras.
199
votes
8
answers
376k
views
RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same
This:
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
for data in dataloader:
inputs, labels = data
outputs = model(inputs)
Gives ...
193
votes
24
answers
814k
views
How to avoid "CUDA out of memory" in PyTorch
I think it's a pretty common message for PyTorch users with low GPU memory:
RuntimeError: CUDA out of memory. Tried to allocate X MiB (GPU X; X GiB total capacity; X GiB already allocated; X MiB free; ...
188
votes
8
answers
611k
views
Pytorch tensor to numpy array
I have a pytorch Tensor of shape [4, 3, 966, 1296]. I want to convert it to numpy array using the following code:
imgs = imgs.numpy()[:, ::-1, :, :]
How does that code work?
143
votes
7
answers
156k
views
What does "unsqueeze" do in Pytorch?
The PyTorch documentation says:
Returns a new tensor with a dimension of size one inserted at the specified position. [...]
>>> x = torch.tensor([1, 2, 3, 4])
>>> torch.unsqueeze(x, ...
142
votes
8
answers
230k
views
How do I visualize a net in Pytorch?
Consider:
import torch
import torch.nn as nn
import torch.optim as optim
import torch.utils.data as data
import torchvision.models as models
import torchvision.datasets as dset
import torchvision....
137
votes
3
answers
191k
views
Understanding `torch.nn.Parameter()`
How does torch.nn.Parameter() work?
132
votes
9
answers
280k
views
L1/L2 regularization in PyTorch
How do I add L1/L2 regularization in PyTorch without manually computing it?
126
votes
9
answers
278k
views
How to fix RuntimeError "Expected object of scalar type Float but got scalar type Double for argument"?
I'm trying to train a classifier via PyTorch. However, I am experiencing problems with training when I feed the model with training data.
I get this error on y_pred = model(X_trainTensor):
...
121
votes
5
answers
201k
views
How to do gradient clipping in pytorch?
What is the correct way to perform gradient clipping in pytorch?
I have an exploding gradients problem.
118
votes
1
answer
147k
views
How does the "number of workers" parameter in PyTorch dataloader actually work?
If num_workers is 2, Does that mean that it will put 2 batches in the RAM and send 1 of them to the GPU or Does it put 3 batches in the RAM then sends 1 of them to the GPU?
What does actually happen ...
115
votes
3
answers
226k
views
Convert PyTorch tensor to python list
How do I convert a PyTorch Tensor into a python list?
I want to convert a tensor of size [1, 2048, 1, 1] into a list of 2048 elements. My tensor has floating point values. Is there a solution which ...
115
votes
11
answers
386k
views
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! when resuming training
I saved a checkpoint while training on gpu. After reloading the checkpoint and continue training I get the following error:
Traceback (most recent call last):
File "main.py", line 140, in &...
105
votes
3
answers
134k
views
How to load a list of numpy arrays to pytorch dataset loader?
I have a huge list of numpy arrays, where each array represents an image and I want to load it using torch.utils.data.Dataloader object. But the documentation of torch.utils.data.Dataloader mentions ...
102
votes
16
answers
481k
views
"AssertionError: Torch not compiled with CUDA enabled" in spite upgrading to CUDA version
I figured out this is a popular question, but still I couldn't find a solution for that.
I'm trying to run a simple repository here which uses PyTorch. Although I just upgraded my PyTorch to the ...
101
votes
26
answers
851k
views
No module named "Torch"
I successfully installed pytorch via conda:
conda install pytorch-cpu torchvision-cpu -c pytorch
I also successfully installed pytorch via pip:
pip3 install https://download.pytorch.org/whl/cpu/torch-...
100
votes
7
answers
193k
views
How to use multiple GPUs in pytorch?
I use this command to use a GPU.
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
But, I want to use two GPUs in jupyter, like this:
device = torch.device(&...
96
votes
2
answers
125k
views
How to change the learning rate of an optimizer at any given moment (no LR schedule)?
Is it possible in PyTorch to change the learning rate of the optimizer in the middle of training dynamically (I don't want to define a learning rate schedule beforehand)?
So let's say I have an ...
94
votes
18
answers
377k
views
Pytorch fails with CUDA error: device-side assert triggered on Colab
I am trying to initialize a tensor on Google Colab with GPU enabled.
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
t = torch.tensor([1,2], device=device)
But I am getting ...
89
votes
9
answers
203k
views
How do I display a single image in PyTorch?
How do I display a PyTorch Tensor of shape (3, 224, 224) representing a 224x224 RGB image?
Using plt.imshow(image) gives the error:
TypeError: Invalid dimensions for image data