1

I am reading text files stored in my database and they all have different sizes. When I run my code it suddenly stops and giving this error. Not finding any relevant answers anywhere. I have tried changing the max_seq_embeddings but still does not work. As soon as I encounter a file of length 3619 it raised an error.

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

Here is the link to all the file used. https://github.com/kamalkraj/BERT-NER-TF

1 Answer 1

3

You are using tf.slice in your program. tf.slice accepts below arguments -

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

As the error clearly states, the size argument is expecting the value from the range of [0, 512], but got 891.

The range of size depends on the begin parameter, if begin is 0 then you have complete length of input as size .i.e. size can have value from the range of [0,len(input)], else if begin parameter is set greater than 0 ,then size can have value form the range of [Begin, len(input)-Begin].

Let me explain with a example -

Example 1 : Here I have set begin=[5] and size=[5], meaning start after 5th position for size of 5.

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

Output -

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)

Example 2 : Here I have set begin=[5] and size=[10], meaning start after 5th position for size of 10. But as begin=[5] I don't have any input after size=[5] as the input shape is shape=(10,). This will reproduce the error your are facing.

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

Output -

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]

Hope this answers your question. Happy Learning.

Sign up to request clarification or add additional context in comments.

2 Comments

@Kushal Vijay - Hope we have answered your question. Can you please accept and upvote the answer if you are satisfied with the answer.
Thanks @Tensorflow Warrior . Your answer helped me to resolve the problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.