Questions tagged [pytorch]

PyTorch is an open-source deep learning framework and API that creates a Dynamic Computational Graph, which allows you to flexibly change the way your neural network behaves on the fly and is capable of performing automatic backward differentiation.

PyTorch

Tensors and Dynamic neural networks in Python with strong GPU acceleration.

PyTorch is a deep learning framework that puts Python first.


Important: and are different frameworks. Please, do not use PyTorch's tag when asking about Torch and vice-versa.

22623 questions
521
votes
18 answers

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.
vvvvv
  • 25,404
  • 19
  • 49
  • 81
376
votes
10 answers

How do I save a trained model in PyTorch?

How do I save a trained model in PyTorch? I have read that: torch.save()/torch.load() is for saving/loading a serializable object. model.state_dict()/model.load_state_dict() is for saving/loading model state.
Wasi Ahmad
  • 35,739
  • 32
  • 114
  • 161
333
votes
9 answers

What does .view() do in PyTorch?

What does .view() do to a tensor x? What do negative values mean? x = x.view(-1, 16 * 5 * 5)
Wasi Ahmad
  • 35,739
  • 32
  • 114
  • 161
331
votes
7 answers

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.
user1424739
  • 11,937
  • 17
  • 63
  • 152
318
votes
11 answers

How do I print the model summary in PyTorch?

How do I print the summary of a model in PyTorch like what model.summary() does in Keras: Model Summary: ____________________________________________________________________________________________________ Layer (type) Output…
Wasi Ahmad
  • 35,739
  • 32
  • 114
  • 161
254
votes
10 answers

How do I initialize weights in PyTorch?

How do I initialize weights and biases of a network (via e.g. He or Xavier initialization)?
Fábio Perez
  • 23,850
  • 22
  • 76
  • 100
246
votes
4 answers

What does model.eval() do in pytorch?

When should I use .eval()? I understand it is supposed to allow me to "evaluate my model". How do I turn it back off for training? Example training code using .eval().
Gulzar
  • 23,452
  • 27
  • 113
  • 201
224
votes
5 answers

What's the difference between reshape and view in pytorch?

In numpy, we use ndarray.reshape() for reshaping an array. I noticed that in pytorch, people use torch.view(...) for the same purpose, but at the same time, there is also a torch.reshape(...) existing. So I am wondering what the differences are…
Lifu Huang
  • 11,930
  • 14
  • 55
  • 77
209
votes
6 answers

What does model.train() do in PyTorch?

Does it call forward() in nn.Module? I thought when we call the model, forward method is being used. Why do we need to specify train()?
aerin
  • 20,607
  • 28
  • 102
  • 140
204
votes
8 answers

What does .contiguous() do in PyTorch?

What does x.contiguous() do for a tensor x?
MBT
  • 21,733
  • 19
  • 84
  • 102
172
votes
5 answers

Why do we "pack" the sequences in PyTorch?

I was trying to replicate How to use packing for variable-length sequence inputs for rnn but I guess I first need to understand why we need to "pack" the sequence. I understand why we "pad" them but why is "packing" (via pack_padded_sequence)…
aerin
  • 20,607
  • 28
  • 102
  • 140
167
votes
10 answers

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.
Fábio Perez
  • 23,850
  • 22
  • 76
  • 100
166
votes
6 answers

pytorch - connection between loss.backward() and optimizer.step()

Where is an explicit connection between the optimizer and the loss? How does the optimizer know where to get the gradients of the loss without a call liks this optimizer.step(loss)? -More context- When I minimize the loss, I didn't have to pass the…
aerin
  • 20,607
  • 28
  • 102
  • 140
163
votes
4 answers

PyTorch preferred way to copy a tensor

There seems to be several ways to create a copy of a tensor in PyTorch, including y = tensor.new_tensor(x) #a y = x.clone().detach() #b y = torch.empty_like(x).copy_(x) #c y = torch.tensor(x) #d b is explicitly preferred over a and d according…
dkv
  • 6,602
  • 10
  • 34
  • 54
163
votes
6 answers

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?
DukeLover
  • 2,184
  • 2
  • 19
  • 27
1
2 3
99 100