-
Notifications
You must be signed in to change notification settings - Fork 19.5k
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
is_placeholder() implemented #7066
Conversation
small part of keras-team#6928 and keras-team#7046
Can you explain the use of |
keras/backend/cntk_backend.py
Outdated
@@ -2,6 +2,7 @@ | |||
import cntk as C | |||
import numpy as np | |||
from .common import _FLOATX, _EPSILON, image_dim_ordering, image_data_format | |||
from .common import is_placeholder |
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.
from .common import is_placeholder
is this necessary?
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.
Yes, I think this import is necessary, how else could it be provided in the backend? Is there a better option?
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.
If the goal is just to be able to use K.is_placeholder, then I think it's better to add the from .common import is_placeholder
to __init__.py
So you add it there once and it's available for every backend.
Imports from line 4 are used in the module, so it makes sense to have it there for each backend separately. I don't think that applies in this case.
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.
good point, removed.
keras/backend/tensorflow_backend.py
Outdated
@@ -14,6 +14,7 @@ | |||
from .common import floatx | |||
from .common import _EPSILON | |||
from .common import image_data_format | |||
from .common import is_placeholder |
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.
from .common import is_placeholder
Again, is this necessary?
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.
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.
resolved
keras/backend/theano_backend.py
Outdated
@@ -17,6 +17,7 @@ | |||
|
|||
import numpy as np | |||
from .common import _FLOATX, floatx, _EPSILON, image_data_format | |||
from .common import is_placeholder |
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.
necessary?
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.
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.
resolved
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.
@hgaiser is_placeholder
is used to differentiate inputs where a numpy array is expected from inputs where one is not expected because the array is provided directly from a tensor such as the tf RecordInput op. This is used internally within #6928.
As for the existing example that uses is_placeholder
, I don't actually know, I just updated it with the new API. :-)
When would you use it aside from the internal need in #6928? It would make sense in graph modifying APIs or any other code that needs meta-information to instantiate or modify tensors and numpy arrays correctly such as in a heterogeneous list.
keras/backend/cntk_backend.py
Outdated
@@ -2,6 +2,7 @@ | |||
import cntk as C | |||
import numpy as np | |||
from .common import _FLOATX, _EPSILON, image_dim_ordering, image_data_format | |||
from .common import is_placeholder |
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.
Yes, I think this import is necessary, how else could it be provided in the backend? Is there a better option?
keras/backend/tensorflow_backend.py
Outdated
@@ -14,6 +14,7 @@ | |||
from .common import floatx | |||
from .common import _EPSILON | |||
from .common import image_data_format | |||
from .common import is_placeholder |
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.
keras/backend/theano_backend.py
Outdated
@@ -17,6 +17,7 @@ | |||
|
|||
import numpy as np | |||
from .common import _FLOATX, floatx, _EPSILON, image_data_format | |||
from .common import is_placeholder |
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.
@PavlosMelissinos Could you confirm that I've addressed everything? Thanks for the review! |
|
How about this?
I think keeping
It should be
Good idea, I'll add this when I have a chance. |
Yeah, something like that or as a more complete keras docstring:
I'm not sure that's the case:
My main objection is redundancy because it would have to be implemented three times instead of once. I think I'm slightly in favor of turning it into a class member mostly due to encapsulation but redundancy is an issue so yeah, it's not a big deal, either solution sounds ok to me!
Last (tangential and slightly off-topic) question: Would it make sense to determine this on the spot? Flags are fast (they don't require any computation) but they're more prone to bugs in general. I know the flag was there before the PR and this is probably out of scope but I'm just wondering if maybe there's a better way. |
keras/engine/topology.py
Outdated
@@ -1621,7 +1635,7 @@ def __init__(self, inputs, outputs, name=None): | |||
i, | |||
layer.__class__.__name__)) | |||
self.input_names.append(layer.name) | |||
if layer.is_placeholder: | |||
if K.is_placeholder(layer): |
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.
if getattr(layer, 'is_placeholder', False):
Based on a conversation with @fchollet there is no longer an independent |
small part of #6928 and #7046
is_placeholder()
I use is_placeholder() to differentiate numpy inputs from tensorflow op inputs, which allows
y_true
andy_pred
to be correctly assigned to the appropriate tf op, without this change the program crashes by trying to incorrectly pass an op to K.placeholder.is_placeholder
can also be useful when enumerating the layers, inputs, outputs, and transforming an arbitrary model. For example anything that walks the graph may or may not want to modify placeholders, such as an automated labeling model to segmentation model converter.The implementation of
is_placeholder
was based on _uses_learning_phase and _keras_shape.