Skip to content

Fix casting in Keras estimator - #18104

Merged
yifeif merged 10 commits into
tensorflow:masterfrom
Dref360:estimator
Apr 20, 2018
Merged

yifeif merged 10 commits into
tensorflow:masterfrom
Dref360:estimator

Conversation

@Dref360

@Dref360 Dref360 commented Mar 29, 2018

Copy link
Copy Markdown

Fixes #18094

This allows users to have Inputs which are not numeric like booleans or strings.

@fchollet fchollet left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

k[1],
K.zeros_like(
k[1])))([mask,
a_2])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please format the above lines in a more readable way

"""Cast tensor to keras's floatx dtype if it is not already the same dtype."""
if x.dtype == K.floatx():
if not check_ops.is_numeric_tensor(x):
return sparse_tensor_lib.convert_to_tensor_or_sparse_tensor(x)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment to explain what this is for? In which cases would we have x.dtype == K.floatx() and not check_ops.is_numeric_tensor(x)? If x is a Numpy array?

def _cast_tensor_to_floatx(x):
"""Cast tensor to keras's floatx dtype if it is not already the same dtype."""
if x.dtype == K.floatx():
if not check_ops.is_numeric_tensor(x):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this go in the outer loop?

@Dref360

Dref360 commented Apr 13, 2018

Copy link
Copy Markdown
Author

I made the code cleaner.
is_numeric_tensor will return False when provided with a numpy array so that's why the code was weird.

@yifeif yifeif added the kokoro:force-run Tests on submitted change label Apr 13, 2018
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Apr 13, 2018

def _convert_tensor(x):
"""Create or cast tensor if needed."""
if not isinstance(x, (ops.Tensor, sparse_tensor_lib.SparseTensor)):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer using tensor_util.is_tensor() which I believe will cover more valid cases (variables? resource variables?).

# Dummy function which reads a number.
# This will fail if k.dtype != string.
# Locally importing gen_parsing_ops for it to be available in the scope.
from tensorflow.python.ops.parsing_ops import gen_parsing_ops

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can it not be imported test-wide?

return {name: array}


def lambda_string_to(k):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just gen_parsing_ops.string_to_number directly?

@rmlarsen rmlarsen added stat:awaiting response Status - Awaiting response from author and removed awaiting review Pull request awaiting review labels Apr 13, 2018
@Dref360

Dref360 commented Apr 13, 2018 •

Copy link
Copy Markdown
Author

We need a closure so that eager.context and all the other modules are loaded.
Otherwise, you would get errors like context is None.

I'll try to dig into this issue before Monday or should I just try a simpler test to fix this issue and investigate the eager context issue after?

EDIT: We still need a local import for the closure, but it works with a lambda. When we clone the model to create an estimator, the environment of the function is not saved. So probably a bug in the get_config of Lambda.

@tensorflowbutler tensorflowbutler removed the stat:awaiting response Status - Awaiting response from author label Apr 14, 2018
@rmlarsen rmlarsen added the kokoro:force-run Tests on submitted change label Apr 16, 2018
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Apr 16, 2018
@Dref360

Dref360 commented Apr 19, 2018

Copy link
Copy Markdown
Author

The Lambda bug is harder than expected. So I simplified the test to fix the actual bug (forcing casting to float). Ready for review.

target_tensors = [
_cast_tensor_to_floatx(
_convert_tensor(
sparse_tensor_lib.convert_to_tensor_or_sparse_tensor(labels))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Dref360! nit, we can remove sparse_tensor_lib.convert_to_tensor_or_sparse_tensor here?

@yifeif yifeif added the kokoro:force-run Tests on submitted change label Apr 20, 2018
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Apr 20, 2018
@yifeif yifeif added the kokoro:force-run Tests on submitted change label Apr 20, 2018
@kokoro-team kokoro-team removed kokoro:force-run Tests on submitted change labels Apr 20, 2018

@fchollet fchollet left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but I'd like to get a second opinion (@yifeif?)

@yifeif yifeif left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM as well as long as the builds pass.

@yifeif
yifeif merged commit 3fa8795 into tensorflow:master Apr 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants