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

Error When Using CRF with tf.keras #540

Open
tonychenxyz opened this issue May 2, 2020 · 1 comment
Open

Error When Using CRF with tf.keras #540

tonychenxyz opened this issue May 2, 2020 · 1 comment

Comments

@tonychenxyz
Copy link

Converted call: <bound method CRF.call of <keras_contrib.layers.crf.CRF object at 0x7fc4937fb990>>
args: (<tf.Tensor 'add_24/Identity:0' shape=(None, None, 128) dtype=float32>,)
kwargs: {}

Not whitelisted: <method-wrapper 'call' of method object at 0x7fc5d70bf690>: default rule
Not whitelisted: <class 'keras_contrib.layers.crf.CRF'>: default rule
Not whitelisted: <bound method CRF.call of <keras_contrib.layers.crf.CRF object at 0x7fc4937fb990>>: default rule
Cache hit for entity <bound method CRF.call of <keras_contrib.layers.crf.CRF object at 0x7fc4937fb990>> key <code object call at 0x7fc4c161d8a0, file "/home/tonychenxyz/anaconda3/envs/tf210/lib/python3.7/site-packages/tf_keras_contrib-2.0.8-py3.7.egg/keras_contrib/layers/crf.py", line 287> subkey (<tensorflow.python.autograph.core.converter.ConversionOptions object at 0x7fc5bc09eb10>, frozenset()): _ConvertedEntityFactoryInfo(tf__call in tmpp0p91xwy)
Defaults of <function create_converted_entity_factory..create_converted_entity..tf__call at 0x7fc5bc082320> : (None,)
KW defaults of <function create_converted_entity_factory..create_converted_entity..tf__call at 0x7fc5bc082320> : None
Calling <function create_converted_entity_factory..create_converted_entity..tf__call at 0x7fc5bc082320> with
self: <keras_contrib.layers.crf.CRF object at 0x7fc4937fb990>
X: Tensor("add_24/Identity:0", shape=(None, None, 128), dtype=float32)
mask: None

Converted call: <bound method CRF.viterbi_decoding of <keras_contrib.layers.crf.CRF object at 0x7fc4937fb990>>
args: (<tf.Tensor 'add_24/Identity:0' shape=(None, None, 128) dtype=float32>, None)
kwargs: None

Not whitelisted: <method-wrapper 'call' of method object at 0x7fc5d76dc9b0>: default rule
Not whitelisted: <class 'keras_contrib.layers.crf.CRF'>: default rule
Not whitelisted: <bound method CRF.viterbi_decoding of <keras_contrib.layers.crf.CRF object at 0x7fc4937fb990>>: default rule
Entity <bound method CRF.viterbi_decoding of <keras_contrib.layers.crf.CRF object at 0x7fc4937fb990>> is not cached for key <code object viterbi_decoding at 0x7fc4c16174b0, file "/home/tonychenxyz/anaconda3/envs/tf210/lib/python3.7/site-packages/tf_keras_contrib-2.0.8-py3.7.egg/keras_contrib/layers/crf.py", line 558> subkey (<tensorflow.python.autograph.core.converter.ConversionOptions object at 0x7fc5bc09ea10>, frozenset())
Converting <bound method CRF.viterbi_decoding of <keras_contrib.layers.crf.CRF object at 0x7fc4937fb990>>
Source code of <bound method CRF.viterbi_decoding of <keras_contrib.layers.crf.CRF object at 0x7fc4937fb990>>:

from future import absolute_import
from future import division
def viterbi_decoding(self, X, mask=None):
input_energy = self.activation(K.dot(X, self.kernel) + self.bias)
if self.use_boundary:
input_energy = self.add_boundary_energy(
input_energy, mask, self.left_boundary, self.right_boundary)

argmin_tables = self.recursion(input_energy, mask, return_logZ=False)
argmin_tables = K.cast(argmin_tables, 'int32')

# backward to find best path, `initial_best_idx` can be any,
# as all elements in the last argmin_table are the same
argmin_tables = K.reverse(argmin_tables, 1)
# matrix instead of vector is required by tf `K.rnn`
initial_best_idx = [K.expand_dims(argmin_tables[:, 0, 0])]
if K.backend() == 'theano':
    from theano import tensor as T
    initial_best_idx = [T.unbroadcast(initial_best_idx[0], 1)]

def gather_each_row(params, indices):
    n = K.shape(indices)[0]
    if K.backend() == 'theano':
        from theano import tensor as T
        return params[T.arange(n), indices]
    elif K.backend() == 'tensorflow':
        import tensorflow as tf
        indices = K.transpose(K.stack([tf.range(n), indices]))
        return tf.gather_nd(params, indices)
    else:
        raise NotImplementedError

def find_path(argmin_table, best_idx):
    next_best_idx = gather_each_row(argmin_table, best_idx[0][:, 0])
    next_best_idx = K.expand_dims(next_best_idx)
    if K.backend() == 'theano':
        from theano import tensor as T
        next_best_idx = T.unbroadcast(next_best_idx, 1)
    return next_best_idx, [next_best_idx]

_, best_paths, _ = K.rnn(find_path, argmin_tables, initial_best_idx,
input_length=K.int_shape(X)[1], unroll=self.unroll)
best_paths = K.reverse(best_paths, 1)
best_paths = K.squeeze(best_paths, 2)

return K.one_hot(best_paths, self.units)

Error transforming entity <bound method CRF.viterbi_decoding of <keras_contrib.layers.crf.CRF object at 0x7fc4937fb990>>
WARNING: AutoGraph could not transform <bound method CRF.viterbi_decoding of <keras_contrib.layers.crf.CRF object at 0x7fc4937fb990>> and will run it as-is.
Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, export AUTOGRAPH_VERBOSITY=10) and attach the full output.
Cause: LIVE_VARS_IN
Converted call: <function dot at 0x7fc62c801cb0>
args: (<tf.Tensor 'add_24/Identity:0' shape=(None, None, 128) dtype=float32>, <tf.Variable 'crf/kernel:0' shape=(128, 11) dtype=float32>)
kwargs: None

Converted call: <function zeros_like at 0x7fc62c801680>
args: (<tf.Tensor 'crf/Reshape_5:0' shape=(None, None, 11) dtype=float32>,)
kwargs: None

Converted call: <function in_train_phase at 0x7fc62c80a950>
args: (<tf.Tensor 'crf/zeros_like_2:0' shape=(None, None, 11) dtype=float32>, <tf.Tensor 'crf/one_hot:0' shape=(None, None, 11) dtype=float32>)
kwargs: None

@himanshudce
Copy link

himanshudce commented Jun 24, 2020

try with tensorflow==1.13.2 with python==3.7 or less
or
use
import tensorflow.python.keras as K

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

2 participants