Tensorflow TypeError:应为任何非张量类型,但应为张量类型

Tensorflow TypeError:应为任何非张量类型,但应为张量类型,tensorflow,keras,deep-learning,chatbot,transformer,Tensorflow,Keras,Deep Learning,Chatbot,Transformer,我正在关注一篇关于“”的帖子。我在本地计算机上遇到了一个错误,尽管代码在colab中似乎运行良好。下面是代码片段 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(

我正在关注一篇关于“”的帖子。我在本地计算机上遇到了一个错误,尽管代码在colab中似乎运行良好。下面是代码片段

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)
我用下面的函数调用调用了上面的函数

sample_encoder_layer = encoder_layer(
    units=512,
    d_model=128,
    num_heads=4,
    dropout=0.3,
    name="sample_encoder_layer")
以下是错误的回溯:

---------------------------------------------------------------------------
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.
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
~/anaconda3/envs/tf chatbot/lib/python3.6/site-packages/tensorflow/python/framework/tensor\u util.py in\u AssertCompatible(值,数据类型)
323请尝试:
-->324 fn(数值)
325除ValueError为e外:
~/anaconda3/envs/tf chatbot/lib/python3.6/site-packages/tensorflow/python/framework/tensor\u util.py in\u check\u not\u tensor(值)
275定义检查非张量(值):
-->276[u检查失败(v),原因是嵌套中的v。展平(值)
277如果存在(v,运算张量)]
~/anaconda3/envs/tf chatbot/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py in(.0)
276[u检查失败(v),原因是嵌套中的v。展平(值)
-->277如果存在(v,运算张量)]
278#pylint:enable=无效名称
~/anaconda3/envs/tf chatbot/lib/python3.6/site-packages/tensorflow/python/framework/tensor\u util.py in\u check\u失败(v)
247#在这里使用是安全的。
-->248上升值错误(v)
249
ValueError:Tensor(“注意1/Identity:0”,shape=(无,无,128),dtype=float32)
在处理上述异常期间,发生了另一个异常:
TypeError回溯(最近一次调用上次)
在里面
---->1个示例\u编码器\u层=编码器\u层(单位=512,d\u模型=128,数量\u头=4,辍学=0.3,名称='示例\u编码器\u层')
2.
3 tf.keras.utils.plot_模型(
4示例\u编码器\u层,到\u file='encoder\u layer.png',显示\u shapes=True)
编码器_层中(单位、d_模型、数量_头、辍学、名称)
10“遮罩”:填充遮罩
11       })
--->12注意=tf.keras.layers.Dropout(比率=Dropout)(注意)
13注意=tf.keras.layers.layer正常化(
14ε=1e-6)(输入+注意)
~/anaconda3/envs/tf chatbot/lib/python3.6/site-packages/tensorflow/python/keras/engine/base\u layer.py在调用中(self,*args,**kwargs)
920非基本层实用程序。是否在函数()中:
921将auto_control_deps.AutomaticControlDependencies()作为acd:
-->922输出=调用fn(转换输入,*args,**kwargs)
923#在'tf.identity'中的'outputs'中包裹张量以避免
924#循环依赖。
调用中的~/anaconda3/envs/tf chatbot/lib/python3.6/site-packages/tensorflow/python/keras/layers/core.py(自我、输入、训练)
209输出=tf_utils.smart_cond(培训、,
210个单元输入,
-->211 lambda:array_ops.identity(输入))
212返回输出
213
智能状态下的~/anaconda3/envs/tf chatbot/lib/python3.6/site-packages/tensorflow/python/keras/utils/tf\u utils.py(pred,true\u fn,false\u fn,name)
63 pred,true\u fn=true\u fn,false\u fn=false\u fn,name=name)
64返回智能模块。智能状态(
--->65 pred,true\u fn=true\u fn,false\u fn=false\u fn,name=name)
66
67
~/anaconda3/envs/tf chatbot/lib/python3.6/site-packages/tensorflow/python/framework/smart\u cond.py in smart\u cond(pred,true\u fn,false\u fn,name)
57.其他:
58返回控制流量操作条件(预测值,真值=真值,假值=假值,
--->59姓名=姓名)
60
61
新函数中的~/anaconda3/envs/tf chatbot/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py(*args,**kwargs)
505“在未来版本中”如果日期不是其他日期(“在%s“%date”之后),
506(说明)
-->507返回函数(*args,**kwargs)
508
509 doc=\u添加\u不推荐的\u参数\u通知\u到\u docstring(
~/anaconda3/envs/tf chatbot/lib/python3.6/site-packages/tensorflow/python/ops/control\u flow\u ops.py在cond中(pred,true\u fn,false\u fn,strict,name,fn1,fn2)
1175 if(util.EnableControlFlowV2(ops.get_default_graph())和
1176不是上下文。急切地执行_()):
->1177返回cond_v2.cond_v2(pred,true,false,name)
1178
1179#我们需要为
~/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),35; pylint:disable=protected access
83添加控制依赖项=添加控制依赖项,
--->84操作返回值(返回值=pred)
85 false_graph=func_graph_module.func_graph_from_py_func(
86假名,
~/anaconda3/envs/tf chatbot/lib/python3.6/site-packages/tensorflow/python/framework/func\u graph.py in func\u graph\u from\u py func(名称、python\u func、args、kwargs、签名、func\u图、自动签名、自动签名选项、添加控制依赖项、arg\u名称、op\u返回值、集合、按值捕获、覆盖平面arg\u形状)
979,original\u func=tf\u decorator.unwrap(python\u func)
980
-->981 func_outputs=python_func(*func_args,**func_kwargs)
982
983#不变量:`func_outputs`只包含张量、复合传感器、,
在drop_inputs()中~/anaconda3/envs/tf chatbot/lib/python3.6/site-packages/tensorflow/python/keras/layers/core.py
205噪声形状=自身。获取噪声形状(输入),
206种子=自我种子,
-->207费率=自费率)
208
209输出=tf_utils.smart_cond(培训、,
新函数中的~/anaconda3/envs/tf chatbot/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py(*args,**kwargs)
505“在未来版本中”,如果日期不是其他日期
pip install tensorflow==2.3.0

pip install --upgrade tensorflow