epochs = 5
device = torch.device(“cuda:0” if torch.cuda.is_available() else “cpu”)
Assuming that we are on a CUDA machine, this should print a CUDA device:
rpn = RegionProposalNetwork()
rpn = rpn.to(device)
params  = []
for name, param in rpn.named_parameters():
if param.requires_grad == True:
params.append(param)

print("\t", name)

lr = .0001
#params = list(rpn.parameters())
#print(list(rpn.parameters()))
optimizer = optim.Adam(rpn.parameters(), lr=lr)# momentum=0.9)
#torch.autograd.set_detect_anomaly(True)
print(list(rpn.parameters())[0].grad)
for i in idSALAMI[:epochs]:
print(“epoch:”, i, “loss:”, loss)
a = list(rpn.parameters())[0].clone()
i = 355
optimizer.zero_grad()
sample = salamiScaled[i]
rpn_loc, rpn_score, gt_rpn_loc, gt_rpn_score = rpn(image=sample[‘image’].to(device), bbox=sample[‘bboxes’].to(device), img_size=[800,800])
loss = rpn_loss(rpn_loc, rpn_score, gt_rpn_loc, gt_rpn_score)
loss.backward()
optimizer.step()
b = list(rpn.parameters())[0].clone()
print(torch.equal(a.data, b.data))