En/review eg/losse val grad#39
Conversation
…ction instead of only a grad function. This is necessary for the augmented lagragian modifications in the parallel pull request, since in lbfgsb the val_and_grad option is more efficient and the only viable option.
rogeriojorge
left a comment
There was a problem hiding this comment.
Mostly 3 minor stuff:
- losses.py:111, custom_loss.call_pytree():
dofs_to_pytree now returns a named dict, but call_pytree() still does self.fun(*dofs_pytree, **self.kwargs). If dofs_pytree is a dict, this passes the keys as positional args. I think this should either be:
args = tuple(dofs_pytree[name] for name in self.args_names)
return self.fun(*args, **self.kwargs)
or, if the custom loss functions are expected to accept keyword args:
return self.fun(**dofs_pytree, **self.kwargs)
- losses.py:132-136, custom_loss.grad_pytree()
same things. It currently does *dofs_pytree, which will be wrong if dofs_pytree is the new named dict. I would build
args = tuple(dofs_pytree[name] for name in self.args_names)
and differentiate with those positional args, then put the gradients back into buffer.
- losses.py:80, _ensure_unravelers
I think "or" is actually the right condition here. Using "and" would only rebuild when all of them are None, which could leave _dofs_to_args or _dofs_to_pytree unset. But not sure.
A small test for custom_loss.value_and_grad() with two dependencies, and dofs_to_pytree() followed by call_pytree() / grad_pytree() would probably catch the dict-vs-tuple issue above.
|
I think I addressed all the comments for this PR. |
|
Can you just add one assertion for value_and_grad() itself in test_custom_loss_named_unraveler()? |
rogeriojorge
left a comment
There was a problem hiding this comment.
Can you just add one assertion for value_and_grad() itself in test_custom_loss_named_unraveler()?
Otherwise, it's good for me to merge
|
Added assertion for val_and_grad |
EstevaoMGomes
left a comment
There was a problem hiding this comment.
After fixing the test, this PR is ready to be merged.
Modifying losses.py to allow custom_losses to have a val_and_grad function instead of only a grad function. This is necessary for the augmented lagragian modifications in the parallel pull request, since in lbfgsb the val_and_grad option is more efficient and the only viable option. Changes on losses.py.