I was just curious about the NNs. So I did a small experiment. I took a simple Linear Layer nn.Linear(2 , 2)
. The training is simple
losses = []
for f , t in zip(fea , tar):
preds = layer(f)
loss = loss_f(preds , t)
losses.append(loss)
optim.zero_grad()
loss.backward()
optim.step()
Here the optimzers is torch.optim.Adam(layer.parameters())
and loss is nn.MSELoss()
. It works fine and updates the weights/parameters. But as soon as I do a slight change in optimizer as
x = nn.Parameter(torch.rand(2 , 2) , dtype = torch.float32)
optim = torch.optim.Adam([x])
The weights are not updated
Parameter containing:
tensor([[0.5747, 0.4457],
[0.5285, 0.7974]], requires_grad=True)
{'params': [Parameter containing:
tensor([[0.5747, 0.4457],
[0.5285, 0.7974]], requires_grad=True)],
'lr': 0.001,
'betas': (0.9, 0.999),
'eps': 1e-08,
'weight_decay': 0,
'amsgrad': False,
'maximize': False,
'foreach': None,
'capturable': False,
'differentiable': False,
'fused': None}
Why even after the same size, it is not changing and even if the shape is different. do otpimizer really check if it is the actual weights of a layer …?
Is there any way, where I can make this thing work…?