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
Prefer tf.ones and tf.zeros over tf.fill #47826
Conversation
array_ops.fill( | ||
array_ops.shape(y), | ||
constant_op.constant(1, dtype=y.dtype, name="grad_ys_%d" % i))) | ||
array_ops.ones( | ||
array_ops.shape(y), dtype=y.dtype, name="grad_ys_%d" % i)) |
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 could be further simplified, but would require changes to
tensorflow/tensorflow/python/ops/default_gradient.py
Lines 52 to 57 in 21f11eb
def zeros_like(t): | |
"""Like array_ops.zeros_like, but respects resource handles.""" | |
if t.dtype == dtypes.resource: | |
return array_ops.zeros(*shape_and_dtype(t)) | |
else: | |
return array_ops.zeros_like(t) |
to accept keyword arguments like
name
. Let me know if you think that's worth doing.
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.
Does the array_ops.py ones_like work?
tensorflow/tensorflow/python/ops/array_ops.py
Line 3002 in 85c8b2a
def ones_like(tensor, dtype=None, name=None, optimize=True): |
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.
I don't think that's possible since judging from this check, y
can be a tf.resource
which is not supported by array_ops.ones_like
.
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.
None of these (fill, array_ops.zeros, array_ops.zeros_like) support resource-dtype tensors. It looks like it's calling the same "ones" kernel right now, but having it call the standard API would be useful if we ever decide to have a "OnesLike" kernel like we do for zeros.
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.
Not sure if I fully understand your comment. The current code works correctly for resource-dtype tensors since tf.shape
can handle resource-dtype tensors, so array_ops.zeros
never receives a resource-dtype tensors itself.
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.
Shape works fine, but neither constant_op.constant(1, dtype=dtypes.resource) nor array_ops.ones(shape=..., dtype=dtypes.resource) work. array_ops.ones_like just moves some of the boilerplate to a helper, the functionality is equivalent.
array_ops.fill( | ||
array_ops.shape(y), | ||
constant_op.constant(1, dtype=y.dtype, name="grad_ys_%d" % i))) | ||
array_ops.ones( | ||
array_ops.shape(y), dtype=y.dtype, name="grad_ys_%d" % i)) |
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.
Does the array_ops.py ones_like work?
tensorflow/tensorflow/python/ops/array_ops.py
Line 3002 in 85c8b2a
def ones_like(tensor, dtype=None, name=None, optimize=True): |
@allenlavoie I had to update the PR to fix a pylint error on CI, do you mind approving it again? |
This PR removes usage of
tf.fill
in favour oftf.ones
ortf.zeros
to simplify the code.