Some Simple PyTorch Tricks
Contents
54. Some Simple PyTorch Tricks#
import torch
54.1. sum#
rdn_tensor = torch.randn(2,2)
rdn_tensor
tensor([[-0.5317, -1.7902],
[-2.4824, 0.4368]])
rdn_tensor.sum(0)
tensor([-3.0141, -1.3534])
rdn_tensor.sum(-1)
tensor([-2.3219, -2.0456])
rdn_tensor.sum(0, keepdim=True)
tensor([[-3.0141, -1.3534]])
54.2. parameters#
pararmeters_linear = torch.nn.Linear(2,1)
pararmeters_linear
Linear(in_features=2, out_features=1, bias=True)
pararmeters_linear_weights, pararmeters_linear_biases = pararmeters_linear.parameters()
pararmeters_linear_weights
Parameter containing:
tensor([[0.2917, 0.6330]], requires_grad=True)
pararmeters_linear_biases
Parameter containing:
tensor([0.6160], requires_grad=True)
54.3. nonzero#
rdn_tensor
tensor([[-0.5317, -1.7902],
[-2.4824, 0.4368]])
rdn_tensor.nonzero()
tensor([[0, 0],
[0, 1],
[1, 0],
[1, 1]])
54.4. view#
rdn_tensor.view(4,-1)
tensor([[-0.5317],
[-1.7902],
[-2.4824],
[ 0.4368]])
54.5. …#
rdn_tensor[...]
tensor([[-0.5317, -1.7902],
[-2.4824, 0.4368]])
rdn_tensor[..., None]
tensor([[[-0.5317],
[-1.7902]],
[[-2.4824],
[ 0.4368]]])
rdn_tensor.size()
torch.Size([2, 2])
rdn_tensor[...].size()
torch.Size([2, 2])
rdn_tensor[..., None].size()
torch.Size([2, 2, 1])