Skip to content
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

Masking a layer that has an integer dtype raises an error in TensorFlow but not Theano. #7550

Closed
airalcorn2 opened this issue Aug 7, 2017 · 1 comment

Comments

@airalcorn2
Copy link
Contributor

airalcorn2 commented Aug 7, 2017

The following:

from keras.layers import Input, Masking

document = Input(shape = (10, ), dtype = "int32")
mask = Masking(mask_value = 21)
document_mask = mask(document)

produces this error:

----> 5 document_mask = mask(document)

/home/airalcorn2/.pyenv/versions/3.5.2/lib/python3.5/site-packages/keras/engine/topology.py in __call__(self, inputs, **kwargs)
    594 
    595             # Actually call the layer, collecting output(s), mask(s), and shape(s).
--> 596             output = self.call(inputs, **kwargs)
    597             output_mask = self.compute_mask(inputs, previous_mask)
    598 

/home/airalcorn2/.pyenv/versions/3.5.2/lib/python3.5/site-packages/keras/layers/core.py in call(self, inputs)
     62         boolean_mask = K.any(K.not_equal(inputs, self.mask_value),
     63                              axis=-1, keepdims=True)
---> 64         return inputs * K.cast(boolean_mask, K.floatx())
     65 
     66     def get_config(self):

/home/airalcorn2/.pyenv/versions/3.5.2/lib/python3.5/site-packages/tensorflow/python/ops/math_ops.py in binary_op_wrapper(x, y)
    827       if not isinstance(y, sparse_tensor.SparseTensor):
    828         try:
--> 829           y = ops.convert_to_tensor(y, dtype=x.dtype.base_dtype, name="y")
    830         except TypeError:
    831           # If the RHS is not a tensor, it might be a tensor aware object

/home/airalcorn2/.pyenv/versions/3.5.2/lib/python3.5/site-packages/tensorflow/python/framework/ops.py in convert_to_tensor(value, dtype, name, preferred_dtype)
    674       name=name,
    675       preferred_dtype=preferred_dtype,
--> 676       as_ref=False)
    677 
    678 

/home/airalcorn2/.pyenv/versions/3.5.2/lib/python3.5/site-packages/tensorflow/python/framework/ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype)
    739 
    740         if ret is None:
--> 741           ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
    742 
    743         if ret is NotImplemented:

/home/airalcorn2/.pyenv/versions/3.5.2/lib/python3.5/site-packages/tensorflow/python/framework/ops.py in _TensorTensorConversionFunction(t, dtype, name, as_ref)
    612     raise ValueError(
    613         "Tensor conversion requested dtype %s for Tensor with dtype %s: %r"
--> 614         % (dtype.name, t.dtype.name, str(t)))
    615   return t
    616 

ValueError: Tensor conversion requested dtype int32 for Tensor with dtype float32: 'Tensor("masking_1/Cast_1:0", shape=(?, 1), dtype=float32)'

when using TensorFlow as the backend, but works fine with Theano. The issue seems to be that Keras casts the mask to a float, even when the inputs are not floats themselves. Changing the return value to:

inputs * K.cast(boolean_mask, inputs.dtype)

fixes the issue.

ahundt added a commit to ahundt/keras that referenced this issue Aug 11, 2017
* master:
  model.fit(steps_per_epoch), 	mnist_tfrecord.py, progbar np.mean (keras-team#7113)
  Change default size to allow different MobileNet sizes (keras-team#7586)
  cntk backend: fix the reversed rnn bug (keras-team#7593)
  Fix mask for multi output --> multi inputs (keras-team#7591)
  [RELNOTES] Subtract merge layer (keras-team#7573)
  update docker with cntk 2.1 (keras-team#7565)
  [RELNOTES] Move constraint management to be based on variable attributes (like TF). (keras-team#7564)
  Add handling for `dtype` arg in initializer config.
  Fix keras-team#7550. (keras-team#7552)
  remove unnecessary function definition (keras-team#7542)
  refactor the function - _convert_string_dtype (keras-team#7540)
  Add batchnorm tests for CNTK (keras-team#7534)
  Clean up RNN tests (keras-team#7529)
  Add merge tests (keras-team#7533)

# Conflicts:
#	examples/mnist_tfrecord.py
#	keras/engine/training.py
#	keras/utils/generic_utils.py
#	tests/keras/engine/test_training.py
@ricardo-0x07
Copy link

I'm currently using 1.2.1, is there a work around for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants