1

我正在阅读存储在我的数据库中的文本文件,它们都有不同的大小。当我运行我的代码时,它突然停止并给出这个错误。在任何地方都找不到任何相关的答案。我尝试更改 max_seq_embeddings 但仍然无法正常工作。一旦我遇到长度为 3619 的文件,它就会引发错误。

YES <class 'str'> 1814
YES <class 'str'> 1334
YES <class 'str'> 3619
Traceback (most recent call last):
  File "C:/Users/DeLL/PycharmProjects/Phase1/venv/src/Main.py", line 24, in <module>
    output = model.predict(data.text)
  File "C:\Users\DeLL\PycharmProjects\Phase1\venv\src\bert.py", line 77, in predict
    logits = self.model(input_ids, segment_ids, input_mask,valid_ids)
  File "C:\Users\DeLL\PycharmProjects\Phase1\venv\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py", line 891, in __call__
    outputs = self.call(cast_inputs, *args, **kwargs)
  File "C:\Users\DeLL\PycharmProjects\Phase1\venv\src\model.py", line 55, in call
    sequence_output = self.bert([input_word_ids, input_mask, input_type_ids],**kwargs)
  File "C:\Users\DeLL\PycharmProjects\Phase1\venv\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py", line 891, in __call__
    outputs = self.call(cast_inputs, *args, **kwargs)
  File "C:\Users\DeLL\PycharmProjects\Phase1\venv\lib\site-packages\tensorflow_core\python\keras\engine\network.py", line 708, in call
    convert_kwargs_to_constants=base_layer_utils.call_context().saving)
  File "C:\Users\DeLL\PycharmProjects\Phase1\venv\lib\site-packages\tensorflow_core\python\keras\engine\network.py", line 860, in _run_internal_graph
    output_tensors = layer(computed_tensors, **kwargs)
  File "C:\Users\DeLL\PycharmProjects\Phase1\venv\src\bert_modeling.py", line 197, in __call__
    return super(BertModel, self).__call__(inputs, **kwargs)
  File "C:\Users\DeLL\PycharmProjects\Phase1\venv\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py", line 891, in __call__
    outputs = self.call(cast_inputs, *args, **kwargs)
  File "C:\Users\DeLL\PycharmProjects\Phase1\venv\src\bert_modeling.py", line 217, in call
    word_embeddings=word_embeddings, token_type_ids=input_type_ids)
  File "C:\Users\DeLL\PycharmProjects\Phase1\venv\src\bert_modeling.py", line 329, in __call__
    return super(EmbeddingPostprocessor, self).__call__(inputs, **kwargs)
  File "C:\Users\DeLL\PycharmProjects\Phase1\venv\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py", line 891, in __call__
    outputs = self.call(cast_inputs, *args, **kwargs)
  File "C:\Users\DeLL\PycharmProjects\Phase1\venv\src\bert_modeling.py", line 355, in call
    tf.slice(self.position_embeddings, [0, 0], [seq_length, width]),
  File "C:\Users\DeLL\PycharmProjects\Phase1\venv\lib\site-packages\tensorflow_core\python\ops\array_ops.py", line 866, in slice
    return gen_array_ops._slice(input_, begin, size, name=name)
  File "C:\Users\DeLL\PycharmProjects\Phase1\venv\lib\site-packages\tensorflow_core\python\ops\gen_array_ops.py", line 9212, in _slice
    input, begin, size, name=name, ctx=_ctx)
  File "C:\Users\DeLL\PycharmProjects\Phase1\venv\lib\site-packages\tensorflow_core\python\ops\gen_array_ops.py", line 9251, in _slice_eager_fallback
    ctx=_ctx, name=name)
  File "C:\Users\DeLL\PycharmProjects\Phase1\venv\lib\site-packages\tensorflow_core\python\eager\execute.py", line 67, in quick_execute
    six.raise_from(core._status_to_exception(e.code, message), None)
  File "<string>", line 3, in raise_from
tensorflow.python.framework.errors_impl.InvalidArgumentError: Expected size[0] in [0, 512], but got 891 [Op:Slice]

Process finished with exit code 1

这是所有使用的文件的链接。 https://github.com/kamalkraj/BERT-NER-TF

4

1 回答 1

2

您在程序中使用tf.slicetf.slice接受以下参数 -

tf.slice(
    input_, begin, size, name=None
)

正如错误明确指出的那样,size参数期望值来自[0, 512], 但得到891

的范围size取决于begin参数,如果begin是,0那么您有完整的输入长度,sizesize可以从 的范围内取值[0,len(input)],否则如果begin参数设置为大于0,则size可以从 的范围内取值[Begin, len(input)-Begin]

让我用一个例子来解释 -

示例 1:在这里我设置了begin=[5]and size=[5],意思是从第 5 个位置开始,大小为 5。

x = tf.constant(("a","b","c","d","e","f","g","h","i","j"))
print(x)
y = tf.slice(x, [5], [5])
print(y)

输出 -

tf.Tensor([b'a' b'b' b'c' b'd' b'e' b'f' b'g' b'h' b'i' b'j'], shape=(10,), dtype=string)
tf.Tensor([b'f' b'g' b'h' b'i' b'j'], shape=(5,), dtype=string)

示例 2:在这里我设置了begin=[5]and size=[10],意思是从第 5 个位置开始,大小为 10。但是因为输入形状begin=[5]之后我没有任何输入。这将重现您面临的错误。size=[5]shape=(10,)

x = tf.constant(("a","b","c","d","e","f","g","h","i","j"))
print(x)
y = tf.slice(x, [5], [10])
print(y)

输出 -

tf.Tensor([b'a' b'b' b'c' b'd' b'e' b'f' b'g' b'h' b'i' b'j'], shape=(10,), dtype=string)
---------------------------------------------------------------------------
_FallbackException                        Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/gen_array_ops.py in _slice(input, begin, size, name)
   9082         _ctx._context_handle, tld.device_name, "Slice", name,
-> 9083         tld.op_callbacks, input, begin, size)
   9084       return _result

_FallbackException: This function does not handle the case of the path where all inputs are not already EagerTensors.

During handling of the above exception, another exception occurred:

InvalidArgumentError                      Traceback (most recent call last)
4 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     58     ctx.ensure_initialized()
     59     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 60                                         inputs, attrs, num_outputs)
     61   except core._NotOkStatusException as e:
     62     if name is not None:

InvalidArgumentError: Expected size[0] in [0, 5], but got 10 [Op:Slice]

希望这能回答你的问题。快乐学习。

于 2020-06-05T06:04:34.530 回答