Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reference preserving hooks #85

Merged
merged 51 commits into from
Aug 17, 2020
Merged

Reference preserving hooks #85

merged 51 commits into from
Aug 17, 2020

Conversation

cgarciae
Copy link
Collaborator

@cgarciae cgarciae commented Aug 2, 2020

This PR implements Haiku's Module system in such a way that hooks preserve reference information. This fixes the following problems that are currently present:

  1. Haiku's resctriction of not being able to instantiate modules outside of a transform, which lead to the awkward use of defer.
  2. Since modules didn't carry their parameters and states with them (in fact you never got hold of a reference to any module) it made more difficult tasks like:
  • Extracting a submodule an doing inference with it, for example, training a VAE and using the Decoder to generate new samples.
  • Getting a pretrained model and adding more layers on top for transfer learning.
  • Training different parts of a Model seperately (GANs).

While point 2 was solvable by modifying the parameters and states dictionaries according to each new architecture, there was no clear strategy on how to make this ergnonomic.

Reference preserving hooks use python objects to store references to submodules, parameters and states as is commonly done in Keras and Pytorch, therefore any change in code architecture leads to a change in parameter structure.

Submodule hooks are implemented by leveraging Metaclasses in such a way that all submodules created inside call are automatically registered inside the parent module.

@cgarciae cgarciae changed the title refactor/object-oriented Refactor: object-oriented Aug 2, 2020
@cgarciae cgarciae changed the title Refactor: object-oriented [WIP] Refactor: object-oriented Aug 2, 2020
@codecov-commenter
Copy link

codecov-commenter commented Aug 15, 2020

Codecov Report

Merging #85 into master will decrease coverage by 3.04%.
The diff coverage is 67.06%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #85      +/-   ##
==========================================
- Coverage   73.42%   70.38%   -3.05%     
==========================================
  Files          77       81       +4     
  Lines        2751     3761    +1010     
==========================================
+ Hits         2020     2647     +627     
- Misses        731     1114     +383     
Impacted Files Coverage Δ
elegy/callbacks/early_stopping.py 77.04% <ø> (ø)
elegy/losses/categorical_crossentropy_test.py 100.00% <ø> (ø)
...egy/losses/sparse_categorical_crossentropy_test.py 100.00% <ø> (ø)
elegy/metrics/accuracy_test.py 100.00% <ø> (ø)
elegy/metrics/categorical_accuracy_test.py 100.00% <ø> (ø)
elegy/metrics/mean_absolute_error_test.py 100.00% <ø> (ø)
elegy/metrics/mean_squared_error_test.py 100.00% <ø> (ø)
elegy/metrics/sparse_categorical_accuracy_test.py 100.00% <ø> (ø)
elegy/nn/multi_head_attention.py 0.00% <0.00%> (ø)
elegy/nn/sequential_test.py 100.00% <ø> (ø)
... and 54 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ae58afe...f90b4da. Read the comment docs.

@cgarciae cgarciae changed the title [WIP] Refactor: object-oriented [WIP] Refactor: reference preserving hooks. Aug 16, 2020
@cgarciae cgarciae merged commit 9a0b5b5 into master Aug 17, 2020
@cgarciae cgarciae deleted the refactor/object-oriented branch August 17, 2020 20:10
@cgarciae cgarciae changed the title [WIP] Refactor: reference preserving hooks. Reference preserving hooks Aug 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants