Repository navigation
Fix casting in Keras estimator - #18104
Conversation
| k[1], | ||
| K.zeros_like( | ||
| k[1])))([mask, | ||
| a_2]) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
Should this go in the outer loop?
|
I made the code cleaner. |
|
|
||
| def _convert_tensor(x): | ||
| """Create or cast tensor if needed.""" | ||
| if not isinstance(x, (ops.Tensor, sparse_tensor_lib.SparseTensor)): |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Why can it not be imported test-wide?
| return {name: array} | ||
|
|
||
|
|
||
| def lambda_string_to(k): |
There was a problem hiding this comment.
Why not just gen_parsing_ops.string_to_number directly?
|
We need a closure so that eager.context and all the other modules are loaded. 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. |
|
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)) |
There was a problem hiding this comment.
Thanks @Dref360! nit, we can remove sparse_tensor_lib.convert_to_tensor_or_sparse_tensor here?
yifeif
left a comment
There was a problem hiding this comment.
LGTM as well as long as the builds pass.
Fixes #18094
This allows users to have Inputs which are not numeric like booleans or strings.