How to use Hook or other way to modify a layer's output

Hello,
I am trying to modify a layer’s output and pass it to the next, I try to use the forward hook function but I can only read the data, not modify them, could you give me some advice? I really appreciate it!

activation = {}
def get_activation(name):
    def hook(model, input, output):
        activation[name] = output.detach()
    return hook

model2.fc3.register_forward_hook(get_activation('fc3'))

outputs = model2(images)
print(outputs)
kc = activation['fc3']
zc=kc[:, :16]
out_feature=find_close(feature,zc,kc)
outputs = model2(images)

You can modify the activation by returning it in the hook:

activation = {}
def get_activation(name):
    def hook(model, input, output):
        activation[name] = output.detach()
        return torch.ones_like(output)
    return hook

model = nn.Linear(1, 1)
model.register_forward_hook(get_activation('fc'))

outputs = model(torch.randn(1, 1))
print(outputs)
# > tensor([[1.]])
1 Like