-
Notifications
You must be signed in to change notification settings - Fork 950
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
added check for input.key() to be layer instance #863
Conversation
The failing cases are when the input key type is |
The affected tests will need to be changed to play by the rules of |
lasagne/layers/helper.py
Outdated
for input_key in treat_as_input: | ||
if not isinstance(input_key, Layer): | ||
raise KeyError("The inputs dictionary keys must be" | ||
" lasagne layers not %s." % type(input_key)) |
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.
That's a good place from an execution standpoint, but not so much for reading the code, since it doesn't match the comment. I'd suggest to move it to the beginning of the function, with a comment saying that it checks whether the input directory is valid, even if this means you'll need an additional if isinstance(inputs, dict)
in front. Also KeyError
is wrong, that is reserved for missing dictionary keys. It should be a TypeError
instead. (See https://docs.python.org/2/library/exceptions.html.)
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.
Done. Thanks.
Thanks @f0k for the explanation. I managed to find a way such that mock would pass the isinstance. But I still need to work on two tests that were using |
My understanding of these two tests is that since we were using |
You're right. Reading through the tests, it seems this was intentional -- it is currently possible to set the input for a free-floating layer by passing a Free-floating layers (layers constructed with a shape tuple as the first argument, rather than a Layer instance) were meant as a simple way to use a Lasagne layer as part of a Theano expression, without having to use all of Lasagne. Free-floating stacks of layers were meant as a way to provide the computation to perform in the input-to-hidden or hidden-to-hidden step of a CustomRecurrentLayer, but ultimately we required such stacks to be built on an We have two options now:
The second option is less intrusive. You'd just need to change the beginning of Sorry I forgot about the free-floating layers -- it's a rather obscure use case... |
I see. No problem. After changing these tests I did also think that |
645b2a8
to
4bce026
Compare
I squashed all my commits into one commit. |
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.
Thank you, looks good now, including the test! Just a minor problem with the test for None
-- I've put a recommendation below. Let me know if I should just update your PR myself.
lasagne/layers/helper.py
Outdated
if isinstance(inputs, dict): | ||
for input_key in inputs.keys(): | ||
if not isinstance(input_key, Layer): | ||
if input_key: |
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.
This is too weak, this would also allow False
or ''
or 0
as a key. Tests for None
should better be explicit. I'd suggest the following (it does some things slightly differently):
# check if the keys of the dictionary are valid
if isinstance(inputs, dict):
for key in inputs:
if (key is not None) and (not isinstance(key, Layer)):
raise TypeError(... % type(key))
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.
Alright. Thanks.
4bce026
to
eb8b3f1
Compare
I guess this is good to merge. I am going to work on another ticket. |
Yes, thank you, merged this! Was actually trying to merge it yesterday, but github was down. |
Fixes #773. I think I will still need to add tests for coverage.