dotnet / dotnet/TorchSharp

register_hook

Open
#1,483 0 comments 0 reactions 0 assignees View on GitHub
question
Dominant language
C#
Stars
1.9k
Forks
228
PR merge metrics
No merged PRs in 30d

Description

I cannot find a method to register a hook. Is there another way this can be accomplished?

```
for step_i in range(unroll_steps):
states = model.represent(states)
states.register_hook(lambda grad: grad * 0.5)
#Get the loss...

loss.register_hook(1.0/unroll_steps)
optimizer.zero_grad()
loss.backward()
```

The above is an example in python.

I believe I can do the loss gradient like this:
```
List grads = [torch.tensor(1.0f / unroll_steps)];
loss.backward(grads);
```

But I am still unsure how to scale the gradient of the 'states' tensor. Any help would be appreciated.

Just for background context. This is an implementation of MuZero. In their code, the scale the future latent states non-linearly. This gradient happens in the rollout loop prior to the loss gradient which is scaled linearly.

- OS: Windows
- Package Type: torchsharp-cuda-windows
- Version: 0.105.0

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.