forked from apple/axlearn
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support GAN loss to CompositeLearner.
Currently, `CompositeLearner` computes gradients for all sublayers using the default `loss` from `forward`. However, in cases like GANs, the discriminative network requires gradients computed using the discriminative loss for parameter updates. To generalize this functionality, a `sublearner_to_loss` flag is added to `CompositeLearner`. This flag maps a sublearner name to its corresponding loss name for gradient computation. `model.forward` returns `tuple(loss, aux)`, and the loss name picks which loss to use. - If set to `None`, all sublearners use the default loss (the first value of the tuple). - If the value is `"loss"`, the default loss is used. - For a specific loss name, it retrieves the loss from the `aux` dict (e.g., aux["loss_name"]). The specified loss must be a scalar float Tensor. - Sublearner names not explicitly mapped are automatically assigned to the default loss. Example: For a GAN, users can configure `CompositeLearner.Config` as follows: ``` cfg = CompositeLearner.default_config().set( rules=[(".*encoder.*", "encoder"), (".*decoder.*", "decoder")], learners={ "encoder": Learner.default_config(), "decoder": Learner.default_config(), }, sublearner_to_loss=dict(encoder="loss", decoder="discriminative_loss"), ) ``` The implementation calculates gradients in `forward_and_backward()` by invoking `value_and_grad` for each loss name and its corresponding parameters. Updates are then merged.
- Loading branch information
Showing
3 changed files
with
208 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters