Skip to content
This repository has been archived by the owner on Nov 3, 2022. It is now read-only.

CRF Layer fails tensor dtype cast on Keras 2.2.5 #515

Open
dataframing opened this issue Aug 26, 2019 · 6 comments
Open

CRF Layer fails tensor dtype cast on Keras 2.2.5 #515

dataframing opened this issue Aug 26, 2019 · 6 comments

Comments

@dataframing
Copy link

Hi all,

I noticed this morning that the recent release of Keras 2.2.5 breaks this library's implementation of the CRF layer.

I ran the official CRF example here using keras==2.2.4 and was able to successfully train both the CRF and bi-LSTM CRF. However, upgrading to keras==2.2.5 led to the following issue:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~/.local/share/virtualenvs/information-collection-model-IthLzmFg/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
    470                 preferred_dtype=default_dtype,
--> 471                 as_ref=input_arg.is_ref)
    472             if input_arg.number_attr and len(

~/.local/share/virtualenvs/information-collection-model-IthLzmFg/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in internal_convert_n_to_tensor(values, dtype, name, as_ref, preferred_dtype, ctx)
   1292             preferred_dtype=preferred_dtype,
-> 1293             ctx=ctx))
   1294   return ret

~/.local/share/virtualenvs/information-collection-model-IthLzmFg/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, ctx, accept_symbolic_tensors, accept_composite_tensors)
   1223     if ret is None:
-> 1224       ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
   1225 

~/.local/share/virtualenvs/information-collection-model-IthLzmFg/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in _TensorTensorConversionFunction(t, dtype, name, as_ref)
   1017         "Tensor conversion requested dtype %s for Tensor with dtype %s: %r" %
-> 1018         (dtype.name, t.dtype.name, str(t)))
   1019   return t

ValueError: Tensor conversion requested dtype bool for Tensor with dtype float32: 'Tensor("crf_1/zeros_like_4:0", shape=(?, ?), dtype=float32)'

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-1-6055c06dd262> in <module>
     80 model.add(Embedding(len(vocab), EMBED_DIM, mask_zero=True))  # Random embedding
     81 crf = CRF(len(class_labels), sparse_target=True)
---> 82 model.add(crf)
     83 model.summary()
     84 

~/.local/share/virtualenvs/information-collection-model-IthLzmFg/lib/python3.7/site-packages/keras/engine/sequential.py in add(self, layer)
    180                 self.inputs = network.get_source_inputs(self.outputs[0])
    181         elif self.outputs:
--> 182             output_tensor = layer(self.outputs[0])
    183             if isinstance(output_tensor, list):
    184                 raise TypeError('All layers in a Sequential model '

~/.local/share/virtualenvs/information-collection-model-IthLzmFg/lib/python3.7/site-packages/keras/engine/base_layer.py in __call__(self, inputs, **kwargs)
    449             # Actually call the layer,
    450             # collecting output(s), mask(s), and shape(s).
--> 451             output = self.call(inputs, **kwargs)
    452             output_mask = self.compute_mask(inputs, previous_mask)
    453 

~/.local/share/virtualenvs/information-collection-model-IthLzmFg/lib/python3.7/site-packages/keras_contrib/layers/crf.py in call(self, X, mask)
    290 
    291         if self.test_mode == 'viterbi':
--> 292             test_output = self.viterbi_decoding(X, mask)
    293         else:
    294             test_output = self.get_marginal_prob(X, mask)

~/.local/share/virtualenvs/information-collection-model-IthLzmFg/lib/python3.7/site-packages/keras_contrib/layers/crf.py in viterbi_decoding(self, X, mask)
    562                 input_energy, mask, self.left_boundary, self.right_boundary)
    563 
--> 564         argmin_tables = self.recursion(input_energy, mask, return_logZ=False)
    565         argmin_tables = K.cast(argmin_tables, 'int32')
    566 

~/.local/share/virtualenvs/information-collection-model-IthLzmFg/lib/python3.7/site-packages/keras_contrib/layers/crf.py in recursion(self, input_energy, mask, go_backwards, return_sequences, return_logZ, input_length)
    514 
    515         if mask is not None:
--> 516             mask2 = K.cast(K.concatenate([mask, K.zeros_like(mask[:, :1])], axis=1),
    517                            K.floatx())
    518             constants.append(mask2)

~/.local/share/virtualenvs/information-collection-model-IthLzmFg/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py in concatenate(tensors, axis)
   2174         return tf.sparse_concat(axis, tensors)
   2175     else:
-> 2176         return tf.concat([to_dense(x) for x in tensors], axis)
   2177 
   2178 

~/.local/share/virtualenvs/information-collection-model-IthLzmFg/lib/python3.7/site-packages/tensorflow/python/util/dispatch.py in wrapper(*args, **kwargs)
    178     """Call target, and fall back on dispatchers if there is a TypeError."""
    179     try:
--> 180       return target(*args, **kwargs)
    181     except (TypeError, ValueError):
    182       # Note: convert_to_eager_tensor currently raises a ValueError, not a

~/.local/share/virtualenvs/information-collection-model-IthLzmFg/lib/python3.7/site-packages/tensorflow/python/ops/array_ops.py in concat(values, axis, name)
   1297               tensor_shape.scalar())
   1298       return identity(values[0], name=scope)
-> 1299   return gen_array_ops.concat_v2(values=values, axis=axis, name=name)
   1300 
   1301 

~/.local/share/virtualenvs/information-collection-model-IthLzmFg/lib/python3.7/site-packages/tensorflow/python/ops/gen_array_ops.py in concat_v2(values, axis, name)
   1254   _attr_N = len(values)
   1255   _, _, _op = _op_def_lib._apply_op_helper(
-> 1256         "ConcatV2", values=values, axis=axis, name=name)
   1257   _result = _op.outputs[:]
   1258   _inputs_flat = _op.inputs

~/.local/share/virtualenvs/information-collection-model-IthLzmFg/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
    497                                 (prefix, dtype.name))
    498               else:
--> 499                 raise TypeError("%s that don't all match." % prefix)
    500             else:
    501               raise TypeError(

TypeError: Tensors in list passed to 'values' of 'ConcatV2' Op have types [bool, float32] that don't all match.

From the 2.2.5 release notes, I can only suspect that the point regarding the new dtype argument on the base keras.Layer could be introducing some sort of breaking change. This issue may also be a duplicate of #498 .

cc: @lzfelix

@WEN-MIN
Copy link

WEN-MIN commented Sep 30, 2019

Hi, I got the same question when keras update to 2.25. if there any solution for this problem except back keras version to 2.24?

@lan2720
Copy link

lan2720 commented Nov 29, 2019

the same question...

@RayX-X
Copy link

RayX-X commented Jan 7, 2020

so is this can be solved? or just waiting for tensorflow addons?

@FanYaning
Copy link

for Keras 2.2.5
--> 516 mask2 = K.cast(K.concatenate([mask, K.zeros_like(mask[:, :1])], axis=1), 517 K.floatx())

try change to mask2 = K.concatenate([K.cast(mask, K.floatx()), K.zeros_like(mask[:, :1])], axis=1)

@kdja90
Copy link

kdja90 commented Sep 24, 2021

@FanYaning
I use keras 2.6.0
i not fined where this line
--> 516 mask2 = K.cast(K.concatenate([mask, K.zeros_like(mask[:, :1])], axis=1), 517 K.floatx())
which file is it in?

@FanYaning
Copy link

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

No branches or pull requests

6 participants