tensorflow.python.framework.errors_impl.InvalidArgumentError:预期的大小[0]在[0,512]中,但得到891 [Op:Slice]

时间:2020-01-30 15:06:07

标签: python tensorflow bert-language-model

我正在读取存储在数据库中的文本文件,它们的大小都不同。当我运行代码时,它突然停止并给出此错误。在任何地方都找不到任何相关答案。 我曾尝试更改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

1 个答案:

答案 0 :(得分:2)

您在程序中使用tf.slicevoid f(const int&) { std::cout << "L"; } void f(int&&) { std::cout << "R"; } int main() { int i = 0; f(i); // prints "L" f(0); // prints "R" } 接受以下参数-

tf.slice

正如错误明确指出的那样,tf.slice( input_, begin, size, name=None ) 参数期望值在size范围内,但得到[0, 512]

891的范围取决于size参数,如果beginbegin,则输入的完整长度为0,即size可以具有size范围内的值,否则,如果[0,len(input)]参数设置为大于begin,则0可以具有{范围内的值{1}}。

让我用一个例子来解释-

示例1:在这里,我设置了size[Begin, len(input)-Begin],这意味着从5号位置开始,尺寸为5。

begin=[5]

输出-

size=[5]

示例2:在这里,我设置了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) ,这意味着从第5个位置开始,尺寸为10。但是作为begin=[5],我没有size=[10]之后有任何输入,因为输入形状为begin=[5]这将重现您面临的错误。

size=[5]

输出-

shape=(10,)

希望这能回答您的问题。学习愉快。