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

[TF 2.0] categorical_column_with_vocabulary_list no longer usable with tf.functiion #29872

Closed
MartinNowak opened this issue Jun 17, 2019 · 2 comments
Assignees
Labels
comp:ops OPs related issues stat:awaiting tensorflower Status - Awaiting response from tensorflower TF 2.0 Issues relating to TensorFlow 2.0 type:bug Bug

Comments

@MartinNowak
Copy link

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow): yes, slightly
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Linux Fedora 30
  • Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device:
  • TensorFlow installed from (source or binary): binary
  • TensorFlow version (use command below): v2.0.0-beta0-16-g1d91213fe7 2.0.0-beta1
  • Python version: 3.5
  • Bazel version (if compiling from source):
  • GCC/Compiler version (if compiling from source):
  • CUDA/cuDNN version:
  • GPU model and memory:

Describe the current behavior

The initialization of the lookup table fails with:

    TypeError: An op outside of the function building code is being passed
    a "Graph" tensor. It is possible to have Graph tensors
    leak out of the function building context by including a
    tf.init_scope in your function building code.
    For example, the following function will fail:
      @tf.function
      def has_init_scope():
        my_constant = tf.constant(1.)
        with tf.init_scope():
          added = my_constant * 2
    The graph tensor has name: dense_features/kind_embedding/kind_lookup/Const:0

Describe the expected behavior

As with v2.0.0-alpha.0, feature columns with constant vocabulary lists should be usable in tf.function graphs.

Code to reproduce the issue

import tensorflow as tf
import tensorflow.feature_column as fc

COLUMNS = [
  fc.embedding_column(
    fc.categorical_column_with_vocabulary_list('kind', ['a', 'b', 'c']),
    2
  )
]

feature_layer = tf.keras.layers.DenseFeatures(COLUMNS, trainable=False)

features = {
  'kind': tf.constant(['a', 'a', 'b', 'b', 'c', 'c']),
}

@tf.function
def func(features):
  return feature_layer(features)

func(features)

Provide a reproducible test case that is the bare minimum necessary to generate the problem.

Other info / logs

Traceback
Traceback (most recent call last):
  File "bug.py", line 21, in 
    print(func(features))
  File ".../venv/lib/python3.5/site-packages/tensorflow/python/eager/def_function.py", line 416, in __call__
    self._initialize(args, kwds, add_initializers_to=initializer_map)
  File ".../venv/lib/python3.5/site-packages/tensorflow/python/eager/def_function.py", line 359, in _initialize
    *args, **kwds))
  File ".../venv/lib/python3.5/site-packages/tensorflow/python/eager/function.py", line 1360, in _get_concrete_function_internal_garbage_collected
    graph_function, _, _ = self._maybe_define_function(args, kwargs)
  File ".../venv/lib/python3.5/site-packages/tensorflow/python/eager/function.py", line 1648, in _maybe_define_function
    graph_function = self._create_graph_function(args, kwargs)
  File ".../venv/lib/python3.5/site-packages/tensorflow/python/eager/function.py", line 1541, in _create_graph_function
    capture_by_value=self._capture_by_value),
  File ".../venv/lib/python3.5/site-packages/tensorflow/python/framework/func_graph.py", line 716, in func_graph_from_py_func
    func_outputs = python_func(*func_args, **func_kwargs)
  File ".../venv/lib/python3.5/site-packages/tensorflow/python/eager/def_function.py", line 309, in wrapped_fn
    return weak_wrapped_fn().__wrapped__(*args, **kwds)
  File ".../venv/lib/python3.5/site-packages/tensorflow/python/framework/func_graph.py", line 706, in wrapper
    raise e.ag_error_metadata.to_exception(type(e))
TypeError: in converted code:
bug.py:19 func  *
    return feature_layer(features)
.../venv/lib/python3.5/site-packages/tensorflow/python/keras/engine/base_layer.py:667 __call__
    outputs = call_fn(inputs, *args, **kwargs)
.../venv/lib/python3.5/site-packages/tensorflow/python/feature_column/feature_column_v2.py:473 call  *
    tensor = column.get_dense_tensor(transformation_cache,
.../venv/lib/python3.5/site-packages/tensorflow/python/feature_column/feature_column_v2.py:3123 get_dense_tensor
    transformation_cache, state_manager)
.../venv/lib/python3.5/site-packages/tensorflow/python/feature_column/feature_column_v2.py:3714 get_sparse_tensors
    transformation_cache.get(self, state_manager), None)
.../venv/lib/python3.5/site-packages/tensorflow/python/feature_column/feature_column_v2.py:2562 get
    transformed = column.transform_feature(self, state_manager)
.../venv/lib/python3.5/site-packages/tensorflow/python/feature_column/feature_column_v2.py:3692 transform_feature
    return self._transform_input_tensor(input_tensor)
.../venv/lib/python3.5/site-packages/tensorflow/python/feature_column/feature_column_v2.py:3686 _transform_input_tensor
    name='{}_lookup'.format(self.key)).lookup(input_tensor)
.../venv/lib/python3.5/site-packages/tensorflow/python/ops/lookup_ops.py:1388 index_table_from_tensor
    table = StaticHashTableV1(init, default_value)
.../venv/lib/python3.5/site-packages/tensorflow/python/ops/lookup_ops.py:284 __init__
    super(StaticHashTable, self).__init__(default_value, initializer)
.../venv/lib/python3.5/site-packages/tensorflow/python/ops/lookup_ops.py:174 __init__
    self._init_op = self._initialize()
.../venv/lib/python3.5/site-packages/tensorflow/python/ops/lookup_ops.py:177 _initialize
    return self._initializer.initialize(self)
.../venv/lib/python3.5/site-packages/tensorflow/python/ops/lookup_ops.py:424 initialize
    self._values)
.../venv/lib/python3.5/site-packages/tensorflow/python/ops/gen_lookup_ops.py:785 lookup_table_import_v2
    table_handle, keys, values, name=name, ctx=_ctx)
.../venv/lib/python3.5/site-packages/tensorflow/python/ops/gen_lookup_ops.py:820 lookup_table_import_v2_eager_fallback
    attrs=_attrs, ctx=_ctx, name=name)
.../venv/lib/python3.5/site-packages/tensorflow/python/eager/execute.py:71 quick_execute
    raise e
.../venv/lib/python3.5/site-packages/tensorflow/python/eager/execute.py:61 quick_execute
    num_outputs)

TypeError: An op outside of the function building code is being passed
a "Graph" tensor. It is possible to have Graph tensors
leak out of the function building context by including a
tf.init_scope in your function building code.
For example, the following function will fail:
  @tf.function
  def has_init_scope():
    my_constant = tf.constant(1.)
    with tf.init_scope():
      added = my_constant * 2
The graph tensor has name: dense_features/kind_embedding/kind_lookup/Const:0

This bug was likely introduced by 2f0b7b1#diff-0972bc0b553e347b626ce302457971dfR383

Somewhat related issue #27086

@gadagashwini-zz
Copy link
Contributor

I am able to reproduce the issue with Tensorflow 2.0.0.beta1 on Google colab. Thanks!

@jvishnuvardhan jvishnuvardhan removed their assignment Jun 24, 2019
@jvishnuvardhan jvishnuvardhan added the stat:awaiting tensorflower Status - Awaiting response from tensorflower label Jun 24, 2019
@tensorflow-bot
Copy link

Are you satisfied with the resolution of your issue?
Yes
No

@lvenugopalan lvenugopalan added the TF 2.0 Issues relating to TensorFlow 2.0 label Apr 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:ops OPs related issues stat:awaiting tensorflower Status - Awaiting response from tensorflower TF 2.0 Issues relating to TensorFlow 2.0 type:bug Bug
Projects
None yet
Development

No branches or pull requests

6 participants