Layer requires_grad=True but layer.grad is None

Hi. I’m implementing Faster R-CNN from scratch and am having gradient issues. My cls_loc layer in my pooling layer has no grad, despite its requires_grad = True. After looking over my code I cannot find where the graph is broken or anything, but my model can’t update. Any help on this would be greatly appreciated. Here’s the module as it is now:

class PoolingCNN(nn.Module):

def init(self,

           sample_layer_params = {

                  "n_sample": 128,

                  "pos_ratio": 0.25,

                  "pos_iou_thresh": 0.25,

                  "neg_iou_thresh": 0.5,

                  "neg_iou_thresh_hi": 0.5,

                  "neg_iou_thresh_lo": 0.0,

                  "mode": "train"



super(PoolingCNN, self).__init__()

self.roi_head_classifier = nn.Sequential(*[nn.Linear(25088, 4096),

                                  nn.Linear(4096, 4096)])

self.cls_loc = nn.Linear(4096, 2 * 4) # (VOC 20 classes + 1 background. Each will have 4 co-ordinates), 0.01)

self.score = nn.Linear(4096, 2) # (VOC 20 classes + 1 background)

def forward(self, pred_bounds, gt_bounds, labels, out_map):

gt_roi_labels, gt_roi_locs, sample_roi = sample_rois(gt_bounds, pred_bounds, labels)

k = pooling_layer(sample_roi, out_map)



k = self.roi_head_classifier(k)


roi_cls_loc = self.cls_loc(k)

roi_cls_score = self.score(k)

return gt_roi_labels, gt_roi_locs, roi_cls_loc, roi_cls_score

def predict(self, out_map, roi):


k = pooling_layer(roi, out_map)

k = self.roi_head_classifier(k)

roi_cls_loc = self.cls_loc(k)

roi_cls_score = self.score(k)

index = np.array(roi_cls_score.detach().argmax(axis=1))

#print("index", index)


corr_bound = np.asarray([roi_cls_loc[i, (index[i]*4):(index[i]*4+4)].detach().numpy() for i in range(len(roi_cls_loc))])

#for i in range(len(roi_cls_loc)):

#  print(roi_cls_loc[i, (index[i]*4):(index[i]*4+4)])



bboxes = roi_to_bbox(np.asarray(roi), corr_bound)


return roi_cls_loc, roi_cls_score, bboxes

Hi Chris!

I can’t figure out what you are doing here. But here are two comments
that may or may not be relevant:

First, a “layer” (in your case a Linear) doesn’t have a requires_grad
property; its Parameters do (such as Linear.weight).

Second, a tensor (or Parameter) that starts out with
requires_grad = True won’t have a non-None grad property
until it’s been backpropagated through.


>>> import torch
>>> torch.__version__
>>> _ = torch.manual_seed (2021)
>>> lin = torch.nn.Linear (4, 2)
>>> lin.requires_grad
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user/miniconda3/lib/python3.8/site-packages/torch/nn/modules/", line 778, in __getattr__
    raise ModuleAttributeError("'{}' object has no attribute '{}'".format(
torch.nn.modules.module.ModuleAttributeError: 'Linear' object has no attribute 'requires_grad'
>>> lin.weight.requires_grad
>>> lin.weight.grad
>>> lin (torch.randn (4)).sum().backward()
>>> lin.weight.grad
tensor([[-1.2991,  0.1929,  0.4697, -0.0257],
        [-1.2991,  0.1929,  0.4697, -0.0257]])


K. Frank