Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Expected any non-tensor type, got a tensor instead

Tags:

I Was following a post on 'Training a transformer model for a chatbot with TensorFlow 2.0'. I have encountered an error on my local machine although the code seems to work fine in colab. Below is the code snippet.

def encoder_layer(units, d_model, num_heads, dropout, name="encoder_layer"):
  inputs = tf.keras.Input(shape=(None, d_model), name="inputs")
  padding_mask = tf.keras.Input(shape=(1, 1, None), name="padding_mask")

  attention = MultiHeadAttention(
      d_model, num_heads, name="attention")({
          'query': inputs,
          'key': inputs,
          'value': inputs,
          'mask': padding_mask
      })
  attention = tf.keras.layers.Dropout(rate=dropout)(attention)
  attention = tf.keras.layers.LayerNormalization(
      epsilon=1e-6)(inputs + attention)

  outputs = tf.keras.layers.Dense(units=units, activation='relu')(attention)
  outputs = tf.keras.layers.Dense(units=d_model)(outputs)
  outputs = tf.keras.layers.Dropout(rate=dropout)(outputs)
  outputs = tf.keras.layers.LayerNormalization(
      epsilon=1e-6)(attention + outputs)

  return tf.keras.Model(
      inputs=[inputs, padding_mask], outputs=outputs, name=name)

I called above function with the following function call;

sample_encoder_layer = encoder_layer(
    units=512,
    d_model=128,
    num_heads=4,
    dropout=0.3,
    name="sample_encoder_layer")

Below is the traceback of the error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py in _AssertCompatible(values, dtype)
    323   try:
--> 324     fn(values)
    325   except ValueError as e:

~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py in _check_not_tensor(values)
    275 def _check_not_tensor(values):
--> 276   _ = [_check_failed(v) for v in nest.flatten(values)
    277        if isinstance(v, ops.Tensor)]

~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py in <listcomp>(.0)
    276   _ = [_check_failed(v) for v in nest.flatten(values)
--> 277        if isinstance(v, ops.Tensor)]
    278 # pylint: enable=invalid-name

~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py in _check_failed(v)
    247   # it is safe to use here.
--> 248   raise ValueError(v)
    249 

ValueError: Tensor("attention_1/Identity:0", shape=(None, None, 128), dtype=float32)

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-20-3fa05a9bbfda> in <module>
----> 1 sample_encoder_layer = encoder_layer(units=512, d_model=128, num_heads=4, dropout=0.3, name='sample_encoder_layer')
      2 
      3 tf.keras.utils.plot_model(
      4     sample_encoder_layer, to_file='encoder_layer.png', show_shapes=True)

<ipython-input-18-357ca53de1c0> in encoder_layer(units, d_model, num_heads, dropout, name)
     10           'mask': padding_mask
     11       })
---> 12   attention = tf.keras.layers.Dropout(rate=dropout)(attention)
     13   attention = tf.keras.layers.LayerNormalization(
     14       epsilon=1e-6)(inputs + attention)

~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, *args, **kwargs)
    920                     not base_layer_utils.is_in_eager_or_tf_function()):
    921                   with auto_control_deps.AutomaticControlDependencies() as acd:
--> 922                     outputs = call_fn(cast_inputs, *args, **kwargs)
    923                     # Wrap Tensors in `outputs` in `tf.identity` to avoid
    924                     # circular dependencies.

~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/keras/layers/core.py in call(self, inputs, training)
    209     output = tf_utils.smart_cond(training,
    210                                  dropped_inputs,
--> 211                                  lambda: array_ops.identity(inputs))
    212     return output
    213 

~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/keras/utils/tf_utils.py in smart_cond(pred, true_fn, false_fn, name)
     63         pred, true_fn=true_fn, false_fn=false_fn, name=name)
     64   return smart_module.smart_cond(
---> 65       pred, true_fn=true_fn, false_fn=false_fn, name=name)
     66 
     67 

~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/framework/smart_cond.py in smart_cond(pred, true_fn, false_fn, name)
     57   else:
     58     return control_flow_ops.cond(pred, true_fn=true_fn, false_fn=false_fn,
---> 59                                  name=name)
     60 
     61 

~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py in new_func(*args, **kwargs)
    505                 'in a future version' if date is None else ('after %s' % date),
    506                 instructions)
--> 507       return func(*args, **kwargs)
    508 
    509     doc = _add_deprecated_arg_notice_to_docstring(

~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/ops/control_flow_ops.py in cond(pred, true_fn, false_fn, strict, name, fn1, fn2)
   1175   if (util.EnableControlFlowV2(ops.get_default_graph()) and
   1176       not context.executing_eagerly()):
-> 1177     return cond_v2.cond_v2(pred, true_fn, false_fn, name)
   1178 
   1179   # We needed to make true_fn/false_fn keyword arguments for

~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/ops/cond_v2.py in cond_v2(pred, true_fn, false_fn, name)
     82             true_name, collections=ops.get_default_graph()._collections),  # pylint: disable=protected-access
     83         add_control_dependencies=add_control_dependencies,
---> 84         op_return_value=pred)
     85     false_graph = func_graph_module.func_graph_from_py_func(
     86         false_name,

~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/framework/func_graph.py in func_graph_from_py_func(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, override_flat_arg_shapes)
    979         _, original_func = tf_decorator.unwrap(python_func)
    980 
--> 981       func_outputs = python_func(*func_args, **func_kwargs)
    982 
    983       # invariant: `func_outputs` contains only Tensors, CompositeTensors,

~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/keras/layers/core.py in dropped_inputs()
    205           noise_shape=self._get_noise_shape(inputs),
    206           seed=self.seed,
--> 207           rate=self.rate)
    208 
    209     output = tf_utils.smart_cond(training,

~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py in new_func(*args, **kwargs)
    505                 'in a future version' if date is None else ('after %s' % date),
    506                 instructions)
--> 507       return func(*args, **kwargs)
    508 
    509     doc = _add_deprecated_arg_notice_to_docstring(

~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py in dropout(x, keep_prob, noise_shape, seed, name, rate)
   4341     raise ValueError("You must provide a rate to dropout.")
   4342 
-> 4343   return dropout_v2(x, rate, noise_shape=noise_shape, seed=seed, name=name)
   4344 
   4345 

~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py in dropout_v2(x, rate, noise_shape, seed, name)
   4422       raise ValueError("rate must be a scalar tensor or a float in the "
   4423                        "range [0, 1), got %g" % rate)
-> 4424     x = ops.convert_to_tensor(x, name="x")
   4425     x_dtype = x.dtype
   4426     if not x_dtype.is_floating:

~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, dtype_hint, ctx, accepted_result_types)
   1339 
   1340     if ret is None:
-> 1341       ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
   1342 
   1343     if ret is NotImplemented:

~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py in _constant_tensor_conversion_function(v, dtype, name, as_ref)
    319                                          as_ref=False):
    320   _ = as_ref
--> 321   return constant(v, dtype=dtype, name=name)
    322 
    323 

~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py in constant(value, dtype, shape, name)
    260   """
    261   return _constant_impl(value, dtype, shape, name, verify_shape=False,
--> 262                         allow_broadcast=True)
    263 
    264 

~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py in _constant_impl(value, dtype, shape, name, verify_shape, allow_broadcast)
    298       tensor_util.make_tensor_proto(
    299           value, dtype=dtype, shape=shape, verify_shape=verify_shape,
--> 300           allow_broadcast=allow_broadcast))
    301   dtype_value = attr_value_pb2.AttrValue(type=tensor_value.tensor.dtype)
    302   const_tensor = g._create_op_internal(  # pylint: disable=protected-access

~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py in make_tensor_proto(values, dtype, shape, verify_shape, allow_broadcast)
    449       nparray = np.empty(shape, dtype=np_dt)
    450     else:
--> 451       _AssertCompatible(values, dtype)
    452       nparray = np.array(values, dtype=np_dt)
    453       # check to them.

~/anaconda3/envs/tf-chatbot/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py in _AssertCompatible(values, dtype)
    326     [mismatch] = e.args
    327     if dtype is None:
--> 328       raise TypeError("Expected any non-tensor type, got a tensor instead.")
    329     else:
    330       raise TypeError("Expected %s, got %s of type '%s' instead." %

TypeError: Expected any non-tensor type, got a tensor instead.
like image 794
botaskay Avatar asked Jun 27 '20 09:06

botaskay


1 Answers

I had this error when I converted a function argument of int datatype to tf.constant . I resolved the issue in my case by undoing it. I faced this issue when I was converting TF1 codes to TF2.3.0 . Looking at your error trace I can see it's pointed to handling some constants in tf-chatbot. Kindly check how that constant is handled.

like image 141
user760664 Avatar answered Sep 30 '22 15:09

user760664