ScalaConsultants/Aspect-Based-Sentiment-Analysis

InvalidArgumentError when making predictions

ivdorelian opened this issue · 3 comments

I get the following error when making predictions on some news articles:

  File "/home/vlad/anaconda3/envs/data-science/lib/python3.8/site-packages/aspect_based_sentiment_analysis/pipelines.py", line 208, in __call__
    predictions = self.transform(task.examples)
  File "/home/vlad/anaconda3/envs/data-science/lib/python3.8/site-packages/aspect_based_sentiment_analysis/pipelines.py", line 224, in transform
    output_batch = self.predict(input_batch)
  File "/home/vlad/anaconda3/envs/data-science/lib/python3.8/site-packages/aspect_based_sentiment_analysis/pipelines.py", line 251, in predict
    logits, hidden_states, attentions = self.model.call(
  File "/home/vlad/anaconda3/envs/data-science/lib/python3.8/site-packages/aspect_based_sentiment_analysis/models.py", line 141, in call
    outputs = self.bert(
  File "/home/vlad/anaconda3/envs/data-science/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py", line 985, in __call__
    outputs = call_fn(inputs, *args, **kwargs)
  File "/home/vlad/anaconda3/envs/data-science/lib/python3.8/site-packages/transformers/modeling_tf_bert.py", line 601, in call
    embedding_output = self.embeddings(input_ids, position_ids, token_type_ids, inputs_embeds, training=training)
  File "/home/vlad/anaconda3/envs/data-science/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py", line 985, in __call__
    outputs = call_fn(inputs, *args, **kwargs)
  File "/home/vlad/anaconda3/envs/data-science/lib/python3.8/site-packages/transformers/modeling_tf_bert.py", line 159, in call
    return self._embedding(input_ids, position_ids, token_type_ids, inputs_embeds, training=training)
  File "/home/vlad/anaconda3/envs/data-science/lib/python3.8/site-packages/transformers/modeling_tf_bert.py", line 185, in _embedding
    position_embeddings = tf.cast(self.position_embeddings(position_ids), inputs_embeds.dtype)
  File "/home/vlad/anaconda3/envs/data-science/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py", line 985, in __call__
    outputs = call_fn(inputs, *args, **kwargs)
  File "/home/vlad/anaconda3/envs/data-science/lib/python3.8/site-packages/tensorflow/python/keras/layers/embeddings.py", line 189, in call
    out = embedding_ops.embedding_lookup_v2(self.embeddings, inputs)
  File "/home/vlad/anaconda3/envs/data-science/lib/python3.8/site-packages/tensorflow/python/util/dispatch.py", line 201, in wrapper
    return target(*args, **kwargs)
  File "/home/vlad/anaconda3/envs/data-science/lib/python3.8/site-packages/tensorflow/python/ops/embedding_ops.py", line 394, in embedding_lookup_v2
    return embedding_lookup(params, ids, "div", name, max_norm=max_norm)
  File "/home/vlad/anaconda3/envs/data-science/lib/python3.8/site-packages/tensorflow/python/util/dispatch.py", line 201, in wrapper
    return target(*args, **kwargs)
  File "/home/vlad/anaconda3/envs/data-science/lib/python3.8/site-packages/tensorflow/python/ops/embedding_ops.py", line 322, in embedding_lookup
    return _embedding_lookup_and_transform(
  File "/home/vlad/anaconda3/envs/data-science/lib/python3.8/site-packages/tensorflow/python/ops/embedding_ops.py", line 138, in _embedding_lookup_and_transform
    array_ops.gather(params[0], ids, name=name), ids, max_norm)
  File "/home/vlad/anaconda3/envs/data-science/lib/python3.8/site-packages/tensorflow/python/util/dispatch.py", line 201, in wrapper
    return target(*args, **kwargs)
  File "/home/vlad/anaconda3/envs/data-science/lib/python3.8/site-packages/tensorflow/python/ops/array_ops.py", line 4676, in gather
    return params.sparse_read(indices, name=name)
  File "/home/vlad/anaconda3/envs/data-science/lib/python3.8/site-packages/tensorflow/python/ops/resource_variable_ops.py", line 687, in sparse_read
    value = gen_resource_variable_ops.resource_gather(
  File "/home/vlad/anaconda3/envs/data-science/lib/python3.8/site-packages/tensorflow/python/ops/gen_resource_variable_ops.py", line 556, in resource_gather
    _ops.raise_from_not_ok_status(e, name)
  File "/home/vlad/anaconda3/envs/data-science/lib/python3.8/site-packages/tensorflow/python/framework/ops.py", line 6843, in raise_from_not_ok_status
    six.raise_from(core._status_to_exception(e.code, message), None)
  File "<string>", line 3, in raise_from
tensorflow.python.framework.errors_impl.InvalidArgumentError: indices[0,560] = 560 is not in [0, 512) [Op:ResourceGather]

My code:

nlp_spacy = spacy.load('en_core_web_sm')
recognizer = absa.aux_models.BasicPatternRecognizer()
nlp_absa = absa.load('absa/classifier-rest-0.2.1', pattern_recognizer=recognizer)
completed_task = nlp_absa(text=text, aspects=[ent.text for ent in nlp_spacy(text).ents])
found = completed_task.examples
print(found)

Any ideas how I could get around it?

I remember having a similar error and I believe the source of this problem is the size of the text. I ended up going for the second example in the README in order to use a text_splitter. See below.

import aspect_based_sentiment_analysis as absa

name = 'absa/classifier-rest-0.2'
model = absa.BertABSClassifier.from_pretrained(name)

# tokenizer = absa.BertTokenizer.from_pretrained(name) <-- this also gave me an error
from transformers import BertTokenizer
tokenizer = BertTokenizer.from_pretrained(name)

professor = absa.Professor(...)     # Explained in detail later on.
text_splitter = absa.sentencizer()  # The English CNN model from SpaCy.
nlp = absa.Pipeline(model, tokenizer, professor, text_splitter)

# Break down the pipeline `call` method.
task = nlp.preprocess(text=..., aspects=...)
tokenized_examples = nlp.tokenize(task.examples)
input_batch = nlp.encode(tokenized_examples)
output_batch = nlp.predict(input_batch)
predictions = nlp.review(tokenized_examples, output_batch)
completed_task = nlp.postprocess(task, predictions)

Hi,I get the error when i try to run following code any clues ?where am wrong
import aspect_based_sentiment_analysis as absa
recognizer = absa.aux_models.BasicPatternRecognizer()
nlp = absa.load('absa/classifier-rest-0.2',pattern_recognizer=recognizer)
text=('We are great fans of Slack, but we wish the subscriptions')
completed_task = nlp(text, aspects=['slack', 'price'])
slack, price = completed_task.examples

I am getting the follwoing errror

ValueError Traceback (most recent call last)
in
1 recognizer = absa.aux_models.BasicPatternRecognizer()
----> 2 nlp = absa.load('absa/classifier-rest-0.2',pattern_recognizer=recognizer)
3 text=('We are great fans of Slack, but we wish the subscriptions')
4 completed_task = nlp(text, aspects=['slack', 'price'])
5 slack, price = completed_task.examples

~\Anaconda3\envs\ABSA\lib\site-packages\aspect_based_sentiment_analysis\loads.py in load(name, text_splitter, reference_recognizer, pattern_recognizer, **model_kwargs)
32 try:
33 config = BertABSCConfig.from_pretrained(name, **model_kwargs)
---> 34 model = BertABSClassifier.from_pretrained(name, config=config)
35 tokenizer = transformers.BertTokenizer.from_pretrained(name)
36 professor = Professor(reference_recognizer, pattern_recognizer)

~\Anaconda3\envs\ABSA\lib\site-packages\transformers\modeling_tf_utils.py in from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs)
728 return load_pytorch_checkpoint_in_tf2_model(model, resolved_archive_file, allow_missing_keys=True)
729
--> 730 model(model.dummy_inputs, training=False) # build the network with dummy inputs
731
732 assert os.path.isfile(resolved_archive_file), "Error retrieving file {}".format(resolved_archive_file)

~\Anaconda3\envs\ABSA\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in call(self, *args, **kwargs)
983
984 with ops.enable_auto_cast_variables(self._compute_dtype_object):
--> 985 outputs = call_fn(inputs, *args, **kwargs)
986
987 if self._activity_regularizer:

~\Anaconda3\envs\ABSA\lib\site-packages\aspect_based_sentiment_analysis\models.py in call(self, token_ids, attention_mask, token_type_ids, training, **bert_kwargs)
148 sequence_output, pooled_output, hidden_states, attentions = outputs
149 pooled_output = self.dropout(pooled_output, training=training)
--> 150 logits = self.classifier(pooled_output)
151 return logits, hidden_states, attentions

~\Anaconda3\envs\ABSA\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in call(self, *args, **kwargs)
980 with ops.name_scope_v2(name_scope):
981 if not self.built:
--> 982 self._maybe_build(inputs)
983
984 with ops.enable_auto_cast_variables(self._compute_dtype_object):

~\Anaconda3\envs\ABSA\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in _maybe_build(self, inputs)
2615 # Check input assumptions set before layer building, e.g. input rank.
2616 if not self.built:
-> 2617 input_spec.assert_input_compatibility(
2618 self.input_spec, inputs, self.name)
2619 input_list = nest.flatten(inputs)

~\Anaconda3\envs\ABSA\lib\site-packages\tensorflow\python\keras\engine\input_spec.py in assert_input_compatibility(input_spec, inputs, layer_name)
189 ndim = x.shape.ndims
190 if ndim is not None and ndim < spec.min_ndim:
--> 191 raise ValueError('Input ' + str(input_index) + ' of layer ' +
192 layer_name + ' is incompatible with the layer: '
193 ': expected min_ndim=' + str(spec.min_ndim) +

ValueError: Input 0 of layer classifier is incompatible with the layer: : expected min_ndim=2, found ndim=0. Full shape received: []