Pytorch Notes
Pytorch Notes
- Be aware of the tensor shape. The shape of
torch.tensor([1])andtorch.tensor(1)is different. One istorch.Size([1]), the other istorch.Size([]).
Note that the output shape of
nn.Linear(input_dim, 1)is nottorch.Size([]); It istorch.Size([1]). If we want that output to do some caculation with scalar tensor of shapetorch.Size([]), we should dox = x.squeeze(0).
-
When caculating loss, always place
predas the first argument andlabelas the second arg. -
When calulating CrossEntropy, we can use the class number(scalar) as label and tensor with shape(batch_size, num_of_all_classes) as prediction.
-
Remember to flatten the tensor to shape(batch_size, num) before applying
nn.Linear(num, num2). -
torch.argmax(t)will decrement the dimension number of t by one. -
If dataset returns a tuple
(tensor with shape(x1, y1), tensor with shape(x2, y2)), corresponding dataloader returns[tensor with shape(batch_size, x1, y1), tensor with shape(batch_size, x2, y2)].
浙公网安备 33010602011771号