You would have to register the forward hook on a layer as given in my code snippet or you could alternatively return the desired tensor in a custom forward method. Based on the error message it seems you are trying to register the hook on a tensor, not a layer.