Hi,
Thanks for your response!
Your code works and does not affect the computation graph but i don’t think that it is taking the bias into consideration after modifying the weights.
I also tried this procedure during the back propagation and don’t think i am getting the right results-
def modify_weight(weight):
    with torch.no_grad():
        if weight is not None:
            for i in range(weight.shape[0]):
                for j in range(weight.shape[1]):
                    weight[i][j]=1
    #print(weight)
    return weight
def modify_weight_grad(weight):
    if weight is not None:
        for i in range(weight.shape[0]):
            for j in range(weight.shape[1]):
                weight[i][j]=1
    #print(weight)
    return weight
class MyModel(nn.Module):
    def __init__(self):
        super(MyModel, self).__init__()
        self.cl1 = nn.Linear(5, 4)
        self.cl2 = nn.Linear(4, 1)
        self.cl1.weight_orig=self.cl1.weight
        del self.cl1.weight
    def forward(self, x):
        #print(model.cl1.weight,model.cl1.bias)
        #print(torch.mm(x,model.cl1.weight.T))
        self.cl1.weight=modify_weight(self.cl1.weight_orig)
        print(self.cl1.bias)
        x = self.cl1(x)
        print(x)
        x = F.relu(self.cl2(x))
        return x



model = MyModel()
optimizer=torch.optim.SGD(model.parameters(),lr=0.01)
model.cl1.register_forward_hook(get_activation('fc2'))
x = torch.ones(1, 5)
output = model(x)
output.backward()
model.cl1.weight_orig.grad=model.cl1.weight.grad
print(model.cl1.weight.grad)
model.cl1.weight.grad=modify_weight_grad(model.cl1.weight_orig.grad)
print(model.cl1.weight.grad)
print(model.cl1.weight)
optimizer.step()
print(model.cl1.weight)

And the output i get is–
bias Parameter containing:
tensor([-0.2568,  0.2500,  0.1713, -0.3188], requires_grad=True)
x after forward propogation with new weight tensor([[5., 5., 5., 5.]], grad_fn=<MmBackward>)
Gradient of the cl1 layer tensor([[0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.]])
Gradient of cl1 layer after modification tensor([[1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1.]])
weight of cl1 layer Parameter containing:
tensor([[1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1.]], requires_grad=True)
weight of cl1 layer after taking an optimisation step Parameter containing:
tensor([[0.9900, 0.9900, 0.9900, 0.9900, 0.9900],
        [0.9900, 0.9900, 0.9900, 0.9900, 0.9900],
        [0.9900, 0.9900, 0.9900, 0.9900, 0.9900],
        [0.9900, 0.9900, 0.9900, 0.9900, 0.9900]], requires_grad=True)

As you can see from the above output the bias is not being considered and the value of the weight of cl1 layer after taking a step should be zero if i am not wrong.