Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python AssertionError:无法计算输出张量(“keras”layer/cond/Identity:0”,shape=(无,768),dtype=float32)_Python_Tensorflow_Deep Learning_Embedding_Bert Language Model - Fatal编程技术网

Python AssertionError:无法计算输出张量(“keras”layer/cond/Identity:0”,shape=(无,768),dtype=float32)

Python AssertionError:无法计算输出张量(“keras”layer/cond/Identity:0”,shape=(无,768),dtype=float32),python,tensorflow,deep-learning,embedding,bert-language-model,Python,Tensorflow,Deep Learning,Embedding,Bert Language Model,我遵循一个教程,使用BERT执行句子嵌入。 代码如下: import tensorflow as tf import tensorflow_hub as hub import bert from tensorflow.keras.models import Model import math from tensorflow.keras.layers import Input import numpy as np def get_masks(tokens, max_seq_length):

我遵循一个教程,使用BERT执行句子嵌入。 代码如下:

import tensorflow as tf
import tensorflow_hub as hub
import bert
from tensorflow.keras.models import Model 
import math
from tensorflow.keras.layers import Input
import numpy as np


def get_masks(tokens, max_seq_length):
    """Mask for padding"""
    if len(tokens)>max_seq_length:
        raise IndexError("Token length more than max seq length!")
    return [1]*len(tokens) + [0] * (max_seq_length - len(tokens))


def get_segments(tokens, max_seq_length):
    """Segments: 0 for the first sequence, 1 for the second"""
    if len(tokens)>max_seq_length:
        raise IndexError("Token length more than max seq length!")
    segments = []
    current_segment_id = 0
    for token in tokens:
        segments.append(current_segment_id)
        if token == "[SEP]":
            current_segment_id = 1
    return segments + [0] * (max_seq_length - len(tokens))


def get_ids(tokens, tokenizer, max_seq_length):
    """Token ids from Tokenizer vocab"""
    token_ids = tokenizer.convert_tokens_to_ids(tokens)
    input_ids = token_ids + [0] * (max_seq_length-len(token_ids))
    return input_ids

modelPath = "tfhub_modules/bert"
max_seq_length = 128 
input_word_ids = tf.keras.layers.Input(shape=(max_seq_length,), dtype=tf.int32,name="input_word_ids")
input_mask = tf.keras.layers.Input(shape=(max_seq_length,), dtype=tf.int32,name="input_mask")
segment_ids = tf.keras.layers.Input(shape=(max_seq_length,), dtype=tf.int32,name="segment_ids")
bert_layer = hub.KerasLayer(modelPath,trainable=True)
pooled_output, sequence_output = bert_layer([input_word_ids, input_mask, segment_ids])

model = Model(inputs=[input_word_ids, input_mask, segment_ids], outputs=[pooled_output, sequence_output])

fullTokenizer = bert.bert_tokenization.FullTokenizer
vocab_file = bert_layer.resolved_object.vocab_file.asset_path.numpy()
do_lower_case = bert_layer.resolved_object.do_lower_case.numpy()
tokenizer = fullTokenizer(vocab_file, do_lower_case)

s = "This is a beautiful sentence"
stokens = tokenizer.tokenize(s)
stokens = ["[CLS]"] + stokens + ["[SEP]"]

input_ids = get_ids(stokens, tokenizer, max_seq_length)
input_masks = get_masks(stokens, max_seq_length)
input_segments = get_segments(stokens, max_seq_length)

pool_embs, all_embs = model.predict([[input_ids],[input_masks],[input_segments]])
引发的错误如下所示:

WARNING:tensorflow:Model was constructed with shape (None, 128) for input Tensor("input_word_ids:0", shape=(None, 128), dtype=int32), but it was called on an input with incompatible shape (None, 1, 128).
WARNING:tensorflow:Model was constructed with shape (None, 128) for input Tensor("input_word_ids:0", shape=(None, 128), dtype=int32), but it was called on an input with incompatible shape (None, 1, 128).
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-52-82ba7e20d77c> in <module>
----> 1 pool_embs, all_embs = model.predict([[input_ids],[input_masks],[input_segments]])

~/.conda/envs/python3_7/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py in _method_wrapper(self, *args, **kwargs)
    128       raise ValueError('{} is not supported in multi-worker mode.'.format(
    129           method.__name__))
--> 130     return method(self, *args, **kwargs)
    131 
    132   return tf_decorator.make_decorator(

~/.conda/envs/python3_7/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py in predict(self, x, batch_size, verbose, steps, callbacks, max_queue_size, workers, use_multiprocessing)
   1597           for step in data_handler.steps():
   1598             callbacks.on_predict_batch_begin(step)
-> 1599             tmp_batch_outputs = predict_function(iterator)
   1600             if data_handler.should_sync:
   1601               context.async_wait()

~/.conda/envs/python3_7/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py in __call__(self, *args, **kwds)
    778       else:
    779         compiler = "nonXla"
--> 780         result = self._call(*args, **kwds)
    781 
    782       new_tracing_count = self._get_tracing_count()

~/.conda/envs/python3_7/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py in _call(self, *args, **kwds)
    821       # This is the first call of __call__, so we have to initialize.
    822       initializers = []
--> 823       self._initialize(args, kwds, add_initializers_to=initializers)
    824     finally:
    825       # At this point we know that the initialization is complete (or less

~/.conda/envs/python3_7/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py in _initialize(self, args, kwds, add_initializers_to)
    695     self._concrete_stateful_fn = (
    696         self._stateful_fn._get_concrete_function_internal_garbage_collected(  # pylint: disable=protected-access
--> 697             *args, **kwds))
    698 
    699     def invalid_creator_scope(*unused_args, **unused_kwds):

~/.conda/envs/python3_7/lib/python3.7/site-packages/tensorflow/python/eager/function.py in _get_concrete_function_internal_garbage_collected(self, *args, **kwargs)
   2853       args, kwargs = None, None
   2854     with self._lock:
-> 2855       graph_function, _, _ = self._maybe_define_function(args, kwargs)
   2856     return graph_function
   2857 

~/.conda/envs/python3_7/lib/python3.7/site-packages/tensorflow/python/eager/function.py in _maybe_define_function(self, args, kwargs)
   3211 
   3212       self._function_cache.missed.add(call_context_key)
-> 3213       graph_function = self._create_graph_function(args, kwargs)
   3214       self._function_cache.primary[cache_key] = graph_function
   3215       return graph_function, args, kwargs

~/.conda/envs/python3_7/lib/python3.7/site-packages/tensorflow/python/eager/function.py in _create_graph_function(self, args, kwargs, override_flat_arg_shapes)
   3073             arg_names=arg_names,
   3074             override_flat_arg_shapes=override_flat_arg_shapes,
-> 3075             capture_by_value=self._capture_by_value),
   3076         self._function_attributes,
   3077         function_spec=self.function_spec,

~/.conda/envs/python3_7/lib/python3.7/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)
    984         _, original_func = tf_decorator.unwrap(python_func)
    985 
--> 986       func_outputs = python_func(*func_args, **func_kwargs)
    987 
    988       # invariant: `func_outputs` contains only Tensors, CompositeTensors,

~/.conda/envs/python3_7/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py in wrapped_fn(*args, **kwds)
    598         # __wrapped__ allows AutoGraph to swap in a converted function. We give
    599         # the function a weak reference to itself to avoid a reference cycle.
--> 600         return weak_wrapped_fn().__wrapped__(*args, **kwds)
    601     weak_wrapped_fn = weakref.ref(wrapped_fn)
    602 

~/.conda/envs/python3_7/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
    971           except Exception as e:  # pylint:disable=broad-except
    972             if hasattr(e, "ag_error_metadata"):
--> 973               raise e.ag_error_metadata.to_exception(e)
    974             else:
    975               raise

AssertionError: in user code:

    /home/zast/.conda/envs/python3_7/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py:1462 predict_function  *
        return step_function(self, iterator)
    /home/zast/.conda/envs/python3_7/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py:1452 step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    /home/zast/.conda/envs/python3_7/lib/python3.7/site-packages/tensorflow/python/distribute/distribute_lib.py:1211 run
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    /home/zast/.conda/envs/python3_7/lib/python3.7/site-packages/tensorflow/python/distribute/distribute_lib.py:2585 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    /home/zast/.conda/envs/python3_7/lib/python3.7/site-packages/tensorflow/python/distribute/distribute_lib.py:2945 _call_for_each_replica
        return fn(*args, **kwargs)
    /home/zast/.conda/envs/python3_7/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py:1445 run_step  **
        outputs = model.predict_step(data)
    /home/zast/.conda/envs/python3_7/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py:1418 predict_step
        return self(x, training=False)
    /home/zast/.conda/envs/python3_7/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py:985 __call__
        outputs = call_fn(inputs, *args, **kwargs)
    /home/zast/.conda/envs/python3_7/lib/python3.7/site-packages/tensorflow/python/keras/engine/functional.py:386 call
        inputs, training=training, mask=mask)
    /home/zast/.conda/envs/python3_7/lib/python3.7/site-packages/tensorflow/python/keras/engine/functional.py:517 _run_internal_graph
        assert x_id in tensor_dict, 'Could not compute output ' + str(x)

    AssertionError: Could not compute output Tensor("keras_layer/cond/Identity:0", shape=(None, 768), dtype=float32)
警告:tensorflow:Model是用shape(None,128)为输入张量(“input\u word\u ids:0”,shape=(None,128),dtype=int32)构造的,但它是在具有不兼容的shape(None,1,128)的输入上调用的。
警告:tensorflow:为输入张量(“input\u word\u ID:0”,shape=(None,128),dtype=int32)构造了形状(None,128)的模型,但在具有不兼容形状(None,1,128)的输入上调用了该模型。
---------------------------------------------------------------------------
AssertionError回溯(上次最近的调用)
在里面
---->1池内存,所有内存=模型。预测([[输入内存],[输入内存掩码],[输入内存段])
包装中的~/.conda/envs/python3_7/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py(self,*args,**kwargs)
128 raise VALUERROR(“{}在多工作模式下不受支持。”。格式(
129方法(名称)
-->130返回方法(self、*args、**kwargs)
131
132返回tf_decorator.make_decorator(
预测中的~/.conda/envs/python3\u 7/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py
1597对于数据处理程序中的步骤。步骤()
1598回调。开始预测批处理(步骤)
->1599 tmp_批处理_输出=预测_函数(迭代器)
1600如果数据处理器应同步:
1601 context.async_wait()
~/.conda/envs/python3_7/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py在调用中(self,*args,**kwds)
778其他:
779 compiler=“nonXla”
-->780结果=自身调用(*args,**kwds)
781
782 new_tracing_count=self._get_tracing_count()
调用中的~/.conda/envs/python3_7/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py(self,*args,**kwds)
821#这是"调用"的第一个调用,因此我们必须初始化。
822个初始值设定项=[]
-->823自身初始化(参数、KWD、添加初始化器到=初始化器)
824最后:
825#此时我们知道初始化已完成(或更少)
初始化中的~/.conda/envs/python3_7/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py(self、args、kwds、add_initializer_to)
695自具体状态fn=(
696 self._stateful_fn._get_concrete_function_internal_garbage_collected(#pylint:disable=protected access
-->697*args,**科威特第纳尔)
698
699 def无效的创建者范围(*未使用的参数,**未使用的参数):
~/.conda/envs/python3\u 7/lib/python3.7/site-packages/tensorflow/python/eager/function.py in\u get\u concrete\u function\u internal\u garbage\u collected(self,*args,**kwargs)
2853 args,kwargs=None,None
2854带自锁:
->2855图形函数,可能定义函数(args,kwargs)
2856返回图函数
2857
函数中的~/.conda/envs/python3\u 7/lib/python3.7/site-packages/tensorflow/python/eager/function.py(self、args、kwargs)
3211
3212自.\u函数\u缓存.missed.add(调用上下文\u键)
->3213图形函数=self.\u创建图形函数(args,kwargs)
3214自.\u函数\u缓存.primary[缓存\u键]=图形\u函数
3215返回图_函数,args,kwargs
创建图形函数中的~/.conda/envs/python3\u 7/lib/python3.7/site-packages/tensorflow/python/eager/function.py(self、args、kwargs、override\u flat\u arg\u形状)
3073参数名称=参数名称,
3074覆盖平面形状=覆盖平面形状,
->3075按值捕获=自身。_按值捕获),
3076自我功能属性,
3077功能规格=自身功能规格,
~/.conda/envs/python3\u 7/lib/python3.7/site-packages/tensorflow/python/framework/func\u graph.py来自func\u py\u func(名称、python\u func、args、kwargs、签名、func\u图、自动签名、自动签名、自动签名选项、添加控制依赖项、参数名、操作返回值、集合、按值捕获、覆盖平面参数形状)
984,original_func=tf_decorator.unwrap(python_func)
985
-->986 func_输出=python_func(*func_参数,**func_参数)
987
988#不变量:`func_outputs`只包含张量、复合传感器、,
包装中的~/.conda/envs/python3_7/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py(*args,**kwds)
598#uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
599#函数对自身进行弱引用以避免引用循环。
-->600返回弱_-wrapped_-fn()
601弱包层=弱包层参考(包层)
602
包装中的~/.conda/envs/python3_7/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py(*args,**kwargs)
971例外情况为e:#pylint:disable=broad except
972如果hasattr(e,“AGU错误元数据”):
-->973将e.ag\u错误\u元数据引发到\u异常(e)
974其他:
975提高
断言错误:在用户代码中:
/home/zast/.conda/envs/python3_7/lib/python3.7/site packages/tensorflow/python/keras/engine/training.py:1462 predict_函数*
返回步骤_函数(self、迭代器)
/home/zast/.conda/envs/python3_7/lib/python3.7/site packages/tensorflow/python/keras/engine/training.py:1452 step_函数**
输出=模型。分配策略。运行(运行步骤,参数=(数据,)
/home/zast/.conda/envs/python3_7/lib/python3.7/site packages/tensorflow/python/distribute/distribute_lib.py:1211运行
返回自我。扩展。为每个副本调用