Suppose we have @Tudor_Berariu code in Manually feeding trainable parameters to the optimizer :
import torch
import torch.optim as optim
from torch.autograd import Variable

w = Variable(torch.randn(3, 5), requires_grad=True)
b = Variable(torch.randn(3), requires_grad=True)
x = Variable(torch.randn(5))

optimizer = optim.SGD([w,b], lr=0.01)

optimizer.zero_grad()
y = torch.mv(w, x) + b
y.backward(torch.randn(3))
optimizer.step()

Is there an automated way to find the total number of trainable parameters used to construct y? In the above the total number of trainable parameters used to construct y will be the sum of the total number of trainable of parameters in w and the total number of trainable parameters in b i.e. 3*5 + 3 = 18.