-
Notifications
You must be signed in to change notification settings - Fork 600
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
[unitaryhack] Initializing respective weights by different method for TorchLayer #2678
Conversation
Merge nested if conditions. Convert for loop into list comprehension.
Modify comment to obey max line length limit of 100.
Hi @amitjansc, thank you for this PR! 🙂 🎉 Let us know if we should go for a review. 👍 |
Codecov Report
@@ Coverage Diff @@
## master #2678 +/- ##
=======================================
Coverage 99.61% 99.61%
=======================================
Files 251 251
Lines 20667 20675 +8
=======================================
+ Hits 20587 20595 +8
Misses 80 80
Continue to review full report at Codecov.
|
Hi @antalszava, I added a small commit checking the size of the given Tensor in the Please find below a working example where I initialize the weights using different methods and/or specific Tensor values:
Prints:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good @amitjansc! 🎉 Thank you for the contribution. 🙂
Just a couple of minor comments, the addition looks great otherwise. :)
Co-authored-by: antalszava <antalszava@gmail.com>
Co-authored-by: antalszava <antalszava@gmail.com>
All comments have been addressed @antalszava 🎉 :. Before merging, beware that I could not run |
Hi @amitjansc, as you've made your contribution during Unitary HACK you are eligible for swag. 🙂 To claim it you should be signed up for UnitaryHACK here and add [unitaryhack] in the title of the PR. Let me know if you have further questions (cc @CatalinaAlbornoz). |
Hi @antalszava. Great, thank you! I just signed up for UnitaryHACK. 👍 What is exactly a swag package? 😆 |
Before submitting
Please complete the following checklist when submitting a PR:
All new features must include a unit test.
If you've fixed a bug or added code that should be tested, add a test to the
test directory!
All new functions and code must be clearly commented and documented.
If you do make documentation changes, make sure that the docs build and
render correctly by running
make docs
.Note: Running
make docs
raises the following error:You must configure the bibtex_bibfiles setting
Ensure that the test suite passes, by running
make test
.Add a new entry to the
doc/releases/changelog-dev.md
file, summarizing thechange, and including a link back to the PR.
The PennyLane source code conforms to
PEP8 standards.
We check all of our code against Pylint.
To lint modified files, simply
pip install pylint
, and thenrun
pylint pennylane/path/to/file.py
.When all the above are checked, delete everything above the dashed
line and fill in the pull request template.
Context:
The
init_method
argument ofTorchLayer
's constructor has been re-defined. Now it can take two different types:torch.nn.init
method to initialize all weights.torch.nn.init
method or atorch.Tensor
for each different weight.Depending on the
init_method
used, the newly defined_init_weights
method initializes and registers all the weights.This PR also solves a small bug, where the user could define weights with negative shape and these would be assigned to a scalar Tensor.
Description of the Change:
pennylane/qnn/torch.py
:Moved weight initialization inside
_init_weights()
:This method defines another method (
init_weight
) which returns the initialized weights depending on the giveninit_method
. It then uses the aforementioned method to initialize all the weights defined inweight_shapes
.Changed
if
condition when building theweight_shapes
, such that only shapes equal to 0 or 1 are casted to an empty tuple.Style changes (a2ba68e): Merge nested if conditions. Convert for loop into list comprehension.
Added missing
QNode
type.tests/qnn/test_qnn_torch.py
: Added tests with all possible types of init_method arguments.Benefits:
In
TorchLayer
, we can now specify per weight initialization method. Consequently, it is possible to initialize, for example, multiple entangling layers in oneqnode
with different initialization methods.Possible Drawbacks:
If
init_method
is a dictionary, it must contain the same keys than theweight_shapes
dictionary. If not aKeyError
is raised. I can add a small check to raise a more self-explanatory error if needed.torch.Tensor
cannot be used when defining the input/output types of theTorchLayer
class methods, because it crashes the tests that don't usetorch
.Related GitHub Issues:
Issue #898.