Practical usage of nn.Variable() and nn.Parameter()

Variables are deprecated since PyTorch 0.4.0 (so for 4 years now :wink: ).
nn.Parameters wrap tensors and are trainable. They are initialized in nn.Modules and trained afterwards.
If you are writing a custom module, this would be an example how nn.Parameter is used:

class MyModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.param = nn.Parameter(torch.randn(1, 1))
        
    def forward(self, x):
        x = x * self.param
        return x

model = MyModel()
print(dict(model.named_parameters()))
# {'param': Parameter containing:
# tensor([[0.6077]], requires_grad=True)}

out = model(torch.randn(1, 1))
loss = out.mean()
loss.backward()

print(model.param.grad)
# tensor([[-1.3033]])