I tried to implement like this:
self.weight = torch.randn((2, 4, 6), requires_grad = True)
with torch.no_grad():
self.weight[:,0,0] = 0
self.weight[:,1,1] = 0
self.weight[:,2,0] = 0
self.weight[:,3,1] = 0
self.convnet1 = nn.Sequential(
nn.Conv1d(2, 4, kernel_size=6, stride=1, padding=1),
nn.ReLU(inplace=True),)
self.convnet1.weight = nn.Parameter(self.weight)
But I get error in the following line:
for p in model.parameters():
p.grad.data.div_(batch_size)  # divide by the actual batch size
total_norm += p.grad.data.norm() ** 2
Error: p.grad has no variable named data.