Git Product home page Git Product logo

aspect-based-sentiment-analysis's Introduction

Aspect Based Sentiment Analysis

The task is to classify the sentiment of potentially long texts for several aspects. The key idea is to build a modern NLP package which supports explanations of model predictions. The approximated decision explanations help you to infer how reliable predictions are. The package is standalone, scalable, and can be freely extended to your needs. We sum up thoughts in the article:

Do You Trust in Aspect-Based Sentiment Analysis? Testing and Explaining Model Behaviors


There are over 100 repositories on GitHub around sentiment analysis 1 2 3 4 5 6 7 8 9 . All of them are hard to commercialize and reuse open-source research projects. We clean up this excellent research. Please give a star if you like the project. This is important to keep this project alive.


Quick Start

The aim is to classify the sentiments of a text concerning given aspects. We have made several assumptions to make the service more helpful. Namely, the text being processed might be a full-length document, the aspects could contain several words (so may be defined more precisely), and most importantly, the service should provide an approximate explanation of any decision made, therefore, a user will be able to immediately infer the reliability of a prediction.

import aspect_based_sentiment_analysis as absa

nlp = absa.load()
text = ("We are great fans of Slack, but we wish the subscriptions "
        "were more accessible to small startups.")

slack, price = nlp(text, aspects=['slack', 'price'])
assert price.sentiment == absa.Sentiment.negative
assert slack.sentiment == absa.Sentiment.positive

Above is an example of how quickly you can start to benefit from our open-source package. All you need to do is to call the load function which sets up the ready-to-use pipeline nlp. You can explicitly pass the model name you wish to use (a list of available models is below), or a path to your model. In spite of the simplicity of using fine-tune models, we encourage you to build a custom model which reflects your data. The predictions will be more accurate and stable.


Pipeline: Keeping the Process in Shape

The pipeline provides an easy-to-use interface for making predictions. Even a highly accurate model will be useless if it is unclear how to correctly prepare the inputs and how to interpret the outputs. To make things clear, we have introduced a pipeline that is closely linked to a model. It is worth to know how to deal with the whole process, especially if you plan to build a custom model.

The diagram above illustrates an overview of the pipeline stages. As usual, at the very beginning, we pre-process the inputs. We convert the text and the aspects into a task which keeps examples (pairs of a text and an aspect) that we can then further tokenize, encode and pass to the model. The model makes a prediction, and here is a change. Instead of directly post-processing the model outputs, we have added a review process wherein the independent component called the professor supervises and explains a model prediction. The professor might dismiss a model prediction if the model internal states or outputs seem suspicious. In the article [here], we discuss in detail how the model and the professor work.

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)
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)

Above is an example how to initialize the pipeline directly, and we revise in code the process being discussed by exposing what calling the pipeline does under the hood. We have omitted a lot of insignificant details but there's one thing we would like to highlight. The sentiment of long texts tends to be fuzzy and neutral. Therefore, you might want to split a text into smaller independent chunks, sometimes called spans. These could include just a single sentence or several sentences. It depends on how the text_splitter works. In this case, we are using the SpaCy CNN model, which splits a document into single sentences, and, as a result each sentence can then be processed independently. Note that longer spans have richer context information, so a model will have more information to consider. Please take a look at the pipeline details here.


Supervising Model Predictions

It's time to explain model reasoning, something which is extremely hard. The key concept is to frame the problem of explaining a model decision as an independent task wherein an aux. model, the pattern recognizer, predicts patterns (weighted compositions of tokens, presented below) given model inputs, outputs, and internal states. Due to time constraints, at first we did not want to research and build a trainable pattern recognizer. Instead, we decided to start with a pattern recognizer that originates from our observations, prior knowledge. The model, the aspect-based sentiment classifier, is based on the transformer architecture wherein self-attention layers hold the most parameters. Therefore, one might conclude that understanding self-attention layers is a good proxy to understanding a model as a whole. Accordingly, there are many articles that show how to explain a model decision in simple terms, using attention values (internal states of self-attention layers) straightforwardly. Inspired by these articles, we have also analyzed attention values (processing training examples) to search for any meaningful insights. This exploratory study has led us to create the BasicPatternRecognizer (details are here).

import aspect_based_sentiment_analysis as absa

recognizer = absa.aux_models.BasicPatternRecognizer()
nlp = absa.load(pattern_recognizer=recognizer)
completed_task = nlp(text=..., aspects=['slack', 'price'])
slack, price = completed_task.examples

absa.summary(slack)
absa.display(slack.review)

absa.summary(price)
absa.display(price.review)

The explanations are only useful if they are correct. To form the basic pattern recognizer, we have made several assumptions (prior beliefs), therefore we should be careful about interpreting the explanations too literally. Even if the attention values have thought-provoking properties, for example, they encode rich linguistic relationships, there is no proven chain of causation. There are a lot of articles that illustrate various concerns why drawing conclusions about model reasoning directly from attentions might be misleading. In the article here, we validate and analyse explanations in detail.


Ready-to-Use Models

In the table below, we present the State of the Art results on the SemEval 2014 evaluation dataset (dataset details are here). There are two available models for the restaurant and the laptop domains. The model implementation details here. The hyper-parameters optimization (with the explanation how to train a model) is here. You can easily reproduce our evaluations, look at the performance tests here.

Model Name Acc Rest Acc Lapt Release
LCF-ATEPC [code][paper] 90.18 82.29 Jan 2020
BERT-ADA [code][paper] 87.89 80.23 Nov 2019
BAT [code][paper] 86.03 79.35 Feb 2020
classifier-rest-0.2 85.17
classifier-lapt-0.2 79.78

Installation

You can use the pip:

pip install aspect-based-sentiment-analysis

Otherwise, clone the code and create the new environment via conda:

git clone [email protected]:ScalaConsultants/Aspect-Based-Sentiment-Analysis.git
conda env create -f=environment.yml
conda activate Aspect-Based-Sentiment-Analysis

The package works with the Python in the version 3.7 (the same as in Colab 2021).


References

How to use language models in the Aspect-Based Sentiment Analysis:

  • Utilizing BERT for Aspect-Based Sentiment Analysis via Constructing Auxiliary Sentence (NAACL 2019) [code][paper]
  • BERT Post-Training for Review Reading Comprehension and Aspect-based Sentiment Analysis (NAACL 2019) [code][paper]
  • Exploiting BERT for End-to-End Aspect-based Sentiment Analysis [code][paper]

Introduction to the BERT interpretability:

  • Are Sixteen Heads Really Better than One? [code][paper]
  • A Primer in BERTology: What we know about how BERT works [paper]
  • What Does BERT Look At? An Analysis of BERT's Attention [code][paper]
  • Visualizing and Measuring the Geometry of BERT [code][paper]
  • Is BERT Really Robust? A Strong Baseline for Natural Language Attack on Text Classification and Entailment [paper]
  • Adversarial Training for Aspect-Based Sentiment Analysis with BERT [paper]
  • Adv-BERT: BERT is not robust on misspellings! Generating nature adversarial samples on BERT [paper]
  • exBERT: A Visual Analysis Tool to Explore Learned Representations in Transformers Models [code][paper]
  • Does BERT Make Any Sense? Interpretable Word Sense Disambiguation with Contextualized Embeddings [code][paper]
  • Attention is not Explanation [code][paper]
  • Attention is not not Explanation [code][paper][blog post]
  • Hierarchical interpretations for neural network predictions [code][paper]
  • Analysis Methods in Neural NLP [code][paper]
  • Visualization for Sequential Neural Networks with Attention [code]
  • NeuroX: Toolkit for finding and analyzing important neurons in neural networks [code][paper]

The State of the Art results:

  • A Multi-task Learning Model for Chinese-oriented Aspect Polarity Classification and Aspect Term Extraction [code][paper]
  • Adapt or Get Left Behind: Domain Adaptation through BERT Language Model Finetuning for Aspect-Target Sentiment Classification [code][paper]
  • Adversarial Training for Aspect-Based Sentiment Analysis with BERT [code][paper]

Other interesting:

  • Multi-Dimensional Explanation of Ratings from Reviews [paper]
  • Extracting Syntactic Trees from Transformer Encoder Self-Attentions [paper]
  • Master Thesis: Transfer and Multitask Learning for Aspect-Based Sentiment Analysis Using the Google Transformer Architecture [code]
  • Create interactive textual heat maps for Jupiter notebooks [code]
  • A pyTorch implementation of the DeepMoji model: state-of-the-art deep learning model for analyzing sentiment, emotion, sarcasm etc [code]
  • More you can find here.

Developed by Scalac

aspect-based-sentiment-analysis's People

Contributors

dependabot[bot] avatar jczuchnowski avatar mend-for-github-com[bot] avatar mohamedbarhdadi avatar redroy44 avatar rolczynski avatar surajdonthi avatar worekleszczy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

aspect-based-sentiment-analysis's Issues

NOT WORKING - Local Jupyter and Google Colab!!

I am using latest Python 3.7....
While in Colab it gives the error like: ModuleNotFoundError: No module named 'aspect_based_sentiment_analysis'

In Jupyter it gives an error as below:

ImportError Traceback (most recent call last)
~\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow.py in
57
---> 58 from tensorflow.python.pywrap_tensorflow_internal import *
59

~\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py in
27 return _mod
---> 28 _pywrap_tensorflow_internal = swig_import_helper()
29 del swig_import_helper

~\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py in swig_import_helper()
23 try:
---> 24 _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
25 finally:

~\Anaconda3\lib\imp.py in load_module(name, file, filename, details)
241 else:
--> 242 return load_dynamic(name, filename, file)
243 elif type_ == PKG_DIRECTORY:

~\Anaconda3\lib\imp.py in load_dynamic(name, path, file)
341 name=name, loader=loader, origin=path)
--> 342 return _load(spec)
343

ImportError: DLL load failed: The specified module could not be found.

During handling of the above exception, another exception occurred:

ImportError Traceback (most recent call last)
in
----> 1 import aspect_based_sentiment_analysis as absa
2
3 nlp = absa.load()
4 text = ("We are great fans of Slack, but we wish the subscriptions "
5 "were more accessible to small startups.")

~\Anaconda3\lib\site-packages\aspect_based_sentiment_analysis_init_.py in
2 version = "1.1.2"
3
----> 4 from .alignment import tokenize
5 from .alignment import make_alignment
6 from .alignment import merge_input_attentions

~\Anaconda3\lib\site-packages\aspect_based_sentiment_analysis\alignment.py in
4 from typing import Tuple
5
----> 6 import tensorflow as tf
7 import transformers
8 import numpy as np

~\Anaconda3\lib\site-packages\tensorflow_init_.py in
39 import sys as _sys
40
---> 41 from tensorflow.python.tools import module_util as _module_util
42 from tensorflow.python.util.lazy_loader import LazyLoader as _LazyLoader
43

~\Anaconda3\lib\site-packages\tensorflow\python_init_.py in
48 import numpy as np
49
---> 50 from tensorflow.python import pywrap_tensorflow
51
52 # Protocol buffers

~\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow.py in
67 for some common reasons and solutions. Include the entire stack trace
68 above this error message when asking for help.""" % traceback.format_exc()
---> 69 raise ImportError(msg)
70
71 # pylint: enable=wildcard-import,g-import-not-at-top,unused-import,line-too-long

ImportError: Traceback (most recent call last):
File "C:\Users\ddebnath2\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in
from tensorflow.python.pywrap_tensorflow_internal import *
File "C:\Users\ddebnath2\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in
_pywrap_tensorflow_internal = swig_import_helper()
File "C:\Users\ddebnath2\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 24, in swig_import_helper
_mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
File "C:\Users\ddebnath2\Anaconda3\lib\imp.py", line 242, in load_module
return load_dynamic(name, filename, file)
File "C:\Users\ddebnath2\Anaconda3\lib\imp.py", line 342, in load_dynamic
return _load(spec)
ImportError: DLL load failed: The specified module could not be found.

Failed to load the native TensorFlow runtime.

See https://www.tensorflow.org/install/errors

for some common reasons and solutions. Include the entire stack trace
above this error message when asking for help.

Can't use the library on M1 mac

It seems like the library hasn't been updated for the latest version of Tensorflow 2.4.0-rc0. Tensorflow 2.4.0-rc0 is the only version available on Apple silicon and it's working perfectly. But the library doesn't seem to support Tensorflow versions >2.2. The accepted tensorflow version list needs to be updated

AttributeError: 'CompletedSubTask' object has no attribute 'aspect_representation'

Hi, I have a following problem: when I start the example you provided, I manage to obtain slack and price sentiments but the problem occurs when I try to create html (html = absa.probing.explain(slack)). This command is followed by the error:

AttributeError Traceback (most recent call last)
in
----> 1 html = absa.probing.explain(slack)

~/anaconda3/envs/torch_tf2/lib/python3.7/site-packages/aspect_based_sentiment_analysis/probing/plots.py in explain(example)
47
48 def explain(example: PredictedExample):
---> 49 aspect = example.aspect_representation
50 texts = [f'Words connected with the "{example.aspect}" aspect:
']
51 texts.extend(highlight_sequence(aspect.tokens, aspect.look_at))

AttributeError: 'CompletedSubTask' object has no attribute 'aspect_representation'

Multiple tokens

Hi, thanks for this great library! Everything works great but I have 2 questions:

  1. Is there any way to accommodate for aspects which have more than one word, e.g. task = nlp(sentence, aspects=['social media']) raises an error:
    149             raise ValueError
    150         if len(example.aspect_tokens) > 1:
--> 151             raise ValueError
    152 
    153     @staticmethod

ValueError:
  1. Are there any other properties of task.batch, such as the overall sentiment score or the words that constitute that sentiment score?

Thanks so much!

not able run this model in docker

Traceback (most recent call last):
  File "kfserving-absa.py", line 30, in <module>
    model.load()
  File "kfserving-absa.py", line 14, in load
    self.nlp = absa.load()
  File "/usr/local/lib/python3.7/site-packages/aspect_based_sentiment_analysis/loads.py", line 34, in load
    model = BertABSClassifier.from_pretrained(name, config=config)
  File "/usr/local/lib/python3.7/site-packages/transformers/modeling_tf_utils.py", line 730, in from_pretrained
    model(model.dummy_inputs, training=False)  # build the network with dummy inputs
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py", line 968, in __call__
    outputs = self.call(cast_inputs, *args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/aspect_based_sentiment_analysis/models.py", line 150, in call
    logits = self.classifier(pooled_output)
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py", line 964, in __call__
    self._maybe_build(inputs)
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py", line 2398, in _maybe_build
    self.input_spec, inputs, self.name)
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/keras/engine/input_spec.py", line 196, in assert_input_compatibility
    str(x.shape.as_list()))
ValueError: Input 0 of layer classifier is incompatible with the layer: : expected min_ndim=2, found ndim=0. Full shape received: []

aspect-based-sentiment-analysis==2.0.1
tensorflow==2.2.0

tried different tf version but issue same

value error

@marioosh @lkuczera @molowny @marekklis @jczuchnowski

image

image

ValueError Traceback (most recent call last)
in
1 import aspect_based_sentiment_analysis as absa
2
----> 3 nlp = absa.load()
4 text = ("We are great fans of Slack, but we wish the subscriptions "
5 "were more accessible to small startups.")

D:\rj\ana3\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)

D:\rj\ana3\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)

D:\rj\ana3\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:

D:\rj\ana3\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

D:\rj\ana3\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):

D:\rj\ana3\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in _maybe_build(self, inputs)
2616 if not self.built:
2617 input_spec.assert_input_compatibility(
-> 2618 self.input_spec, inputs, self.name)
2619 input_list = nest.flatten(inputs)
2620 if input_list and self._dtype_policy.compute_dtype is None:

D:\rj\ana3\lib\site-packages\tensorflow\python\keras\engine\input_spec.py in assert_input_compatibility(input_spec, inputs, layer_name)
194 ', found ndim=' + str(ndim) +
195 '. Full shape received: ' +
--> 196 str(x.shape.as_list()))
197 # Check dtype.
198 if spec.dtype is not None:

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

what should I do about this error ? thx~

End to End ABSA

I think this repository is a wonderful resource, however what seems like a natural extension to the pipeline, is prepending an Aspect Term Extraction module, so as to provide an option to perform End to End ABSA.

Would love to hear thoughts of the authors of this repository on the same.

Strength

Does this model have any ability to support more than a binary classification? Our use case benefits a lot from having a magnitude, something like returning a range from -1 to 1 in float, where 1 is as positive as possible, 0 neutral, -1 negative.

Read me

In the supervising model predictions in readme in the final sentence you talk about
There are a lot of articles that illustrate various concerns why drawing conclusions about model reasoning directly from attentions might be misleading. In the article [here], we validate and analyse explanations in detail.

The article regarding the attention analysis is not hyperlinked, is it possible to share the link that would be really insightful?
Thanks for your help!

Update dependencies for usability

Hi all,

This seems like a great library, but it's currently unusable because of the outdated dependences (tensorflow==2.2, transformers==2.5) which conflict with the versions required by other modern NLP libraries (e.g. Sentence-Transformers) that are often used in the same data pipelines.

I see you've updated the tensorflow dependency in the master github version, but have not pushed it to the pip installable version.

Please update the pip-installable library to work with current versions of tensorflow and transformers. This would make your library much more usable and increase adoption by others.

Cannot install via pip

When I run pip install aspect_based_sentiment_analysis it gets stuck indefinitely downloading boto3, after saying

INFO: pip is looking at multiple versions of boto3 to determine which version is compatible with other requirements. This could take a while.

s

Assertion Error Fixed

tensorflow.python.framework.errors_impl.InvalidArgumentError: indices[0,529] = 529 is not in [0, 512) [Op:ResourceGather]

I search on internet and find that this issue is due the length of the 'text' I am passing to the below nlp function.

import aspect_based_sentiment_analysis as absa
name = 'absa/classifier-lapt-0.2'
recognizer = absa.aux_models.BasicPatternRecognizer()
nlp = absa.load(name,pattern_recognizer=recognizer)
text= " " #some multiline long review text
completed_task = nlp(text, aspects=['camera','design'])
camera,design = completed_task.examples

print(camera.sentiment)

when I reduced the size of the text. It is working fine. Any other solution to this

Traceback (most recent call last):
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-28-07dbd638851c> in <module>
     17 aspect='camera'
     18 camera=0
---> 19 completed_task = nlp(text, aspects=['camera','design'])
     20 camera,design = completed_task.examples

~\AppData\Local\Programs\Python\Python37\lib\site-packages\aspect_based_sentiment_analysis\pipelines.py in __call__(self, text, aspects)
    206     def __call__(self, text: str, aspects: List[str]) -> CompletedTask:
    207         task = self.preprocess(text, aspects)
--> 208         predictions = self.transform(task.examples)
    209         completed_task = self.postprocess(task, predictions)
    210         return completed_task

~\AppData\Local\Programs\Python\Python37\lib\site-packages\aspect_based_sentiment_analysis\pipelines.py in transform(self, examples)
    222         tokenized_examples = self.tokenize(examples)
    223         input_batch = self.encode(tokenized_examples)
--> 224         output_batch = self.predict(input_batch)
    225         predictions = self.review(tokenized_examples, output_batch)
    226         return predictions

~\AppData\Local\Programs\Python\Python37\lib\site-packages\aspect_based_sentiment_analysis\pipelines.py in predict(self, input_batch)
    252                 token_ids=input_batch.token_ids,
    253                 attention_mask=input_batch.attention_mask,
--> 254                 token_type_ids=input_batch.token_type_ids
    255             )
    256             # We assume that our predictions are correct. This is

~\AppData\Local\Programs\Python\Python37\lib\site-packages\aspect_based_sentiment_analysis\models.py in call(self, token_ids, attention_mask, token_type_ids, training, **bert_kwargs)
    144             token_type_ids=token_type_ids,
    145             training=training,
--> 146             **bert_kwargs
    147         )
    148         sequence_output, pooled_output, hidden_states, attentions = outputs

~\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in __call__(self, *args, **kwargs)
    966           with base_layer_utils.autocast_context_manager(
    967               self._compute_dtype):
--> 968             outputs = self.call(cast_inputs, *args, **kwargs)
    969           self._handle_activity_regularization(inputs, outputs)
    970           self._set_mask_metadata(inputs, outputs, input_masks)

~\AppData\Local\Programs\Python\Python37\lib\site-packages\transformers\modeling_tf_bert.py in call(self, inputs, attention_mask, token_type_ids, position_ids, head_mask, inputs_embeds, training)
    564             # head_mask = tf.constant([0] * self.num_hidden_layers)
    565 
--> 566         embedding_output = self.embeddings([input_ids, position_ids, token_type_ids, inputs_embeds], training=training)
    567         encoder_outputs = self.encoder([embedding_output, extended_attention_mask, head_mask], training=training)
    568 

~\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in __call__(self, *args, **kwargs)
    966           with base_layer_utils.autocast_context_manager(
    967               self._compute_dtype):
--> 968             outputs = self.call(cast_inputs, *args, **kwargs)
    969           self._handle_activity_regularization(inputs, outputs)
    970           self._set_mask_metadata(inputs, outputs, input_masks)

~\AppData\Local\Programs\Python\Python37\lib\site-packages\transformers\modeling_tf_bert.py in call(self, inputs, mode, training)
    146         """
    147         if mode == "embedding":
--> 148             return self._embedding(inputs, training=training)
    149         elif mode == "linear":
    150             return self._linear(inputs)

~\AppData\Local\Programs\Python\Python37\lib\site-packages\transformers\modeling_tf_bert.py in _embedding(self, inputs, training)
    169         if inputs_embeds is None:
    170             inputs_embeds = tf.gather(self.word_embeddings, input_ids)
--> 171         position_embeddings = self.position_embeddings(position_ids)
    172         token_type_embeddings = self.token_type_embeddings(token_type_ids)
    173 

~\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in __call__(self, *args, **kwargs)
    966           with base_layer_utils.autocast_context_manager(
    967               self._compute_dtype):
--> 968             outputs = self.call(cast_inputs, *args, **kwargs)
    969           self._handle_activity_regularization(inputs, outputs)
    970           self._set_mask_metadata(inputs, outputs, input_masks)

~\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\layers\embeddings.py in call(self, inputs)
    182     if dtype != 'int32' and dtype != 'int64':
    183       inputs = math_ops.cast(inputs, 'int32')
--> 184     out = embedding_ops.embedding_lookup(self.embeddings, inputs)
    185     return out
    186 

~\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\ops\embedding_ops.py in embedding_lookup(params, ids, partition_strategy, name, validate_indices, max_norm)
    324       name=name,
    325       max_norm=max_norm,
--> 326       transform_fn=None)
    327 
    328 

~\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\ops\embedding_ops.py in _embedding_lookup_and_transform(params, ids, partition_strategy, name, max_norm, transform_fn)
    135       with ops.colocate_with(params[0]):
    136         result = _clip(
--> 137             array_ops.gather(params[0], ids, name=name), ids, max_norm)
    138         if transform_fn:
    139           result = transform_fn(result)

~\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\util\dispatch.py in wrapper(*args, **kwargs)
    178     """Call target, and fall back on dispatchers if there is a TypeError."""
    179     try:
--> 180       return target(*args, **kwargs)
    181     except (TypeError, ValueError):
    182       # Note: convert_to_eager_tensor currently raises a ValueError, not a

~\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\ops\array_ops.py in gather(***failed resolving arguments***)
   4520     # TODO(apassos) find a less bad way of detecting resource variables
   4521     # without introducing a circular dependency.
-> 4522     return params.sparse_read(indices, name=name)
   4523   except AttributeError:
   4524     return gen_array_ops.gather_v2(params, indices, axis, name=name)

~\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\ops\resource_variable_ops.py in sparse_read(self, indices, name)
    674       variable_accessed(self)
    675       value = gen_resource_variable_ops.resource_gather(
--> 676           self._handle, indices, dtype=self._dtype, name=name)
    677 
    678       if self._dtype == dtypes.variant:

~\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\ops\gen_resource_variable_ops.py in resource_gather(resource, indices, dtype, batch_dims, validate_indices, name)
    554         pass  # Add nodes to the TensorFlow graph.
    555     except _core._NotOkStatusException as e:
--> 556       _ops.raise_from_not_ok_status(e, name)
    557   # Add nodes to the TensorFlow graph.
    558   dtype = _execute.make_type(dtype, "dtype")

~\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\framework\ops.py in raise_from_not_ok_status(e, name)
   6651   message = e.message + (" name: " + name if name is not None else "")
   6652   # pylint: disable=protected-access
-> 6653   six.raise_from(core._status_to_exception(e.code, message), None)
   6654   # pylint: enable=protected-access
   6655 

~\AppData\Local\Programs\Python\Python37\lib\site-packages\six.py in raise_from(value, from_value)

InvalidArgumentError: indices[0,529] = 529 is not in [0, 512) [Op:ResourceGather]

Unable to reach good accuracy on Training using the train_classifier.py

examples = absa.load_examples(domain=domain)
when I load data using the above code for laptop. I am getting an accuracy of only 42%.
Can you please guide me on any errors I am using.
What specifications of gpu or memory on gpu are we expected to train on

While training on custom data as well reaching only 35%

ValueError: The first argument to `Layer.call` must always be passed.

I install your module and run quick start

import aspect_based_sentiment_analysis as absa
recognizer = absa.aux_models.BasicPatternRecognizer()
nlp = absa.load(pattern_recognizer=recognizer) 

and the following error occurred.


ValueError                                Traceback (most recent call last)
<ipython-input-356-900f8907a6c9> in <module>
      2 
      3 recognizer = absa.aux_models.BasicPatternRecognizer()
----> 4 nlp = absa.load(pattern_recognizer=recognizer)

~/anaconda3/envs/myenv1/lib/python3.8/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/myenv1/lib/python3.8/site-packages/transformers/modeling_tf_utils.py in from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs)

~/anaconda3/envs/myenv1/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, *args, **kwargs)
   1010   def trainable(self, value):
   1011     self._trainable = value
-> 1012     for layer in getattr(self, '_layers', []):
   1013       layer.trainable = value
   1014 

~/anaconda3/envs/myenv1/lib/python3.8/site-packages/aspect_based_sentiment_analysis/models.py in call(self, token_ids, attention_mask, token_type_ids, training, **bert_kwargs)
    139             **bert_kwargs
    140     ) -> Tuple[tf.Tensor, Tuple[tf.Tensor, ...], Tuple[tf.Tensor, ...]]:
--> 141         outputs = self.bert(
    142             inputs=token_ids,
    143             attention_mask=attention_mask,

~/anaconda3/envs/myenv1/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, *args, **kwargs)
    940             # TODO(fchollet): consider py_func as an alternative, which
    941             # would enable us to run the underlying graph if needed.
--> 942             outputs = self._symbolic_call(inputs)
    943 
    944           if outputs is None:

~/anaconda3/envs/myenv1/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py in _split_out_first_arg(self, args, kwargs)

ValueError: The first argument to `Layer.call` must always be passed.

Can I know the solution?

module 'aspect_based_sentiment_analysis' has no attribute 'BertTokenizer'

name = 'absa/classifier-rest-0.2'
model = absa.BertABSClassifier.from_pretrained(name)
tokenizer = absa.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)

I got this error module 'aspect_based_sentiment_analysis' has no attribute 'BertTokenizer' while runnnig the above code , any ideas ?

Pre-trained laptop classifier accuracy does not match what is stated in README

I replaced the domain and classifier in test_performance.py from restaurants to laptops and I get an accuracy of 0.38 (the accuracy stated in the README is 0.8)

This was the code:

import numpy as np
import aspect_based_sentiment_analysis as absa
from aspect_based_sentiment_analysis.training import ConfusionMatrix

def test_semeval_classification_laptops():
    examples = absa.load_examples(dataset='semeval',
                                  domain='laptop',
                                  test=True)
    nlp = absa.load('absa/bert-lapt-0.1')

    metric = ConfusionMatrix(num_classes=3)
    confusion_matrix = nlp.evaluate(examples, metric, batch_size=32)
    confusion_matrix = confusion_matrix.numpy()
    accuracy = np.diagonal(confusion_matrix).sum() / confusion_matrix.sum()

    print(confusion_matrix)
    print(accuracy)

FutureWarning: The `pad_to_max_length` argument is deprecated

Using the aspect-based sentiment analysis model classifier-rest-0.1 , results in a warning from \transformers\tokenization_utils_base.py:1773 FutureWarning:

The `pad_to_max_length` argument is deprecated and will be removed in a future version, use `padding=True` or `padding='longest'` to pad to the longest sequence in the batch, or use `padding='max_length'` to pad to a max length. In this case, you can give a specific length with `max_length` (e.g. `max_length=45`) or leave max_length to None to pad to the maximal input size of the model (e.g. 512 for Bert).
  FutureWarning,

a problem on training with optuna

Hello! I'm on WIN10 and can't install optuna, can you provide some sample training sessions that are not optimized with optuna? Thank you!
image

Error in Usage

Hi,

I cloned the repo and was trying to import the module locally. I've installed transformers as well. I'm getting this error.

Python: 3.6
Screenshot from 2020-04-23 11-42-53

InvalidArgumentError when making predictions

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?

Package import error

I already install the package, but when I import it, I got the following error:
ImportError: cannot import name 'collections_abc' from 'six.moves' (unknown location)

Is there a way to skip this part?

Thank you

Can't install the package on Ubuntu 18.04 with Python 3.7.5

I am doing the following:

sudo pip3 install aspect-based-sentiment-analysis

Failing with tensorflow:

Collecting tensorflow>=2.1 (from aspect-based-sentiment-analysis)
Could not find a version that satisfies the requirement tensorflow>=2.1

Please advise how can I install it on Ubuntu 18.04 with Python 3.7.5.

I can do it smoothly on Windows 10, but I need it on my work laptop.

Attribute error with spacy text splitter

Hi there!

When I try to follow the pipeline steps laid out in the README exactly, I receive the following error at the preprocessing stage:

AttributeError: 'spacy.tokens.span.Span' object has no attribute 'string'

Upon removing the text_splitter from the pipeline setup I no longer get this error, but it would be useful to be able to initialize the pipeline with the text splitter (e.g. for passing in texts whose tokenization is longer than 512 tokens).

Thank you very much for the help!

About neural sentiment

nlp = absa.load()
text = ("We are great fans of Slack, but we wish the subscriptions "
"were more accessible to small startups.")
slack, price = nlp(text, aspects=['slack', 'price'])

It seems that the model only classify the sentiment by positive and negative. Can the model detect neural sentiment?

Can't execute the example

Hi!

I tried to excute

import aspect_based_sentiment_analysis as absa

nlp = absa.load()
text = ("We are great fans of Slack, but we wish the subscriptions "
        "were more accessible to small startups.")

slack, price = nlp(text, aspects=['slack', 'price'])
assert price.sentiment == absa.Sentiment.negative
assert slack.sentiment == absa.Sentiment.positive

but I only get

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-1-0cd7d305f4c1> in <module>
----> 1 import aspect_based_sentiment_analysis as absa
      2 
      3 nlp = absa.load()
      4 text = ("We are great fans of Slack, but we wish the subscriptions "
      5         "were more accessible to small startups.")

~\Miniconda3\lib\site-packages\aspect_based_sentiment_analysis\__init__.py in <module>
      2 __version__ = "2.0.1"
      3 
----> 4 from .alignment import tokenize
      5 from .alignment import make_alignment
      6 from .alignment import merge_tensor

~\Miniconda3\lib\site-packages\aspect_based_sentiment_analysis\alignment.py in <module>
      7 import numpy as np
      8 
----> 9 from .data_types import TokenizedExample
     10 
     11 

~\Miniconda3\lib\site-packages\aspect_based_sentiment_analysis\data_types.py in <module>
    159 
    160 
--> 161 @dataclass(frozen=True)
    162 class InputBatch:
    163     """ The model uses these tensors to perform a prediction.

~\Miniconda3\lib\site-packages\aspect_based_sentiment_analysis\data_types.py in InputBatch()
    170     indicate first and second portions of the inputs, zeros
    171     and ones. """
--> 172     token_ids: tf.Tensor
    173     attention_mask: tf.Tensor
    174     token_type_ids: tf.Tensor

AttributeError: module 'tensorflow' has no attribute 'Tensor'

Do I need an older Tensoflow version?

Thank you!

Best regards
Robert

Indexing error in pipeline module for long sentences

Getting the following error for long sentences.

The python code used is as below:

import aspect_based_sentiment_analysis as absa
nlp = absa.load()
sentence = "...."
aspects = [....]
outputs = nlp(sentence, aspects=aspects)

Error:

IndexError: indices[0,512] = 512 is not in [0, 512) [Op:ResourceGather]

Training custom model

I want to use this package for the mobile phone's ABSA, Can anyone tell me how to train the model and use the trained model in the nlp.load() method.
Actually, I am new to the NLP. Can anyone show the code to do that?
Also, I don't know what I have to do after running the script "train_classifier.py".
This script also showing an error that to "upgrade storage by giving database path " when running this on google collab
!python /content/Aspect-Based-Sentiment-Analysis/examples/train_classifier.py --domain Smartphones.

 Traceback (most recent call last):
  File "/content/Aspect-Based-Sentiment-Analysis/examples/train_classifier.py", line 183, in <module>
    load_if_exists=True)
  File "/usr/local/lib/python3.7/dist-packages/optuna/study.py", line 1055, in create_study
    storage = storages.get_storage(storage)
  File "/usr/local/lib/python3.7/dist-packages/optuna/storages/__init__.py", line 27, in get_storage
    return _CachedStorage(RDBStorage(storage))
  File "/usr/local/lib/python3.7/dist-packages/optuna/storages/_rdb/storage.py", line 173, in __init__
    self._version_manager.check_table_schema_compatibility()
  File "/usr/local/lib/python3.7/dist-packages/optuna/storages/_rdb/storage.py", line 1288, in check_table_schema_compatibility
    raise RuntimeError(message)
RuntimeError: The runtime optuna version 2.5.0 is no longer compatible with the table schema (set up by optuna 2.2.0). Please execute "$ optuna storage upgrade --storage $STORAGE_URL" for upgrading the storage.

The stepwise guide would be awesome.
Thanks for such a good package.

Problem load `BERT-ADA` pretrained model

Hi, I want to say the work is really wonderful!

I installed the package and it works! Then I want to see if the other pretained model works, so I downloaded the BERT-ADA pretained model listed on README, specifically, I downloaded laptops_and_restaurants_2mio_ep15.

name = "/home/projects/pre_train_models/laptops_and_restaurants_2mio_ep15"
model = absa.BertABSClassifier.from_pretrained(name)

The error I got is:

OSError: Error no file named ['pytorch_model.bin', 'tf_model.h5'] found in directory /home/projects/pre_train_models/laptops_and_restaurants_2mio_ep15 or `from_pt` set to False

Then I put in from_pt=True

name = "/home/projects/pre_train_models/laptops_and_restaurants_2mio_ep15"
model = absa.BertABSClassifier.from_pretrained(name, from_pt=True)

This time I got AttributeError: 'BertConfig' object has no attribute 'num_polarities'.

I am quite new to this area and experimenting, could you help to solve the issue?

Another question is, where the model is stored? I couldn't find absa/classifier-lapt-0.2 in package folder, or anywhere. I looked at the load.py file, it is supposed to download to a download folder, but I also can't find it. If I find it, I may compare the config.json file with other model.

I have created two conda env, one with transformers==4.2 and another with transformers=2.5, both has the same error indicated above.

Thank you very much!

AM getting Error when I try to run the First code in the readme


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: []

and here is my code:
Am not sure enough where I 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

can't install the package on a clean conda env

Here is the error I've got when trying to pip install the package:
Not to sure what to do to make it work, any suggestions please ?

Thanks

Collecting aspect-based-sentiment-analysis
  ERROR: Could not find a version that satisfies the requirement aspect-based-sentiment-analysis (from versions: none)
ERROR: No matching distribution found for aspect-based-sentiment-analysis

Pre-trained models

I want to start off by saying that I really love your work! Is there any chance that you provide the pre-trained models somewhere?

Inference on large data

Hi,

I am trying to use the pretrained 'absa/classifier-rest-0.2' model for generating labels for approximately 300K reviews. In the README examples, I could not find a way to provide batches of data (batches or list of reviews) to the model. Feeding batches would allow me to potentially speed up the inference process.

I am merely looking to generate labels from your model as a means for a different analysis. There is no intention to train as my task is exactly what you model has been trained on.

Any other ideas on how to speed up the inference process would also be welcome!

Can't install the package in anaconda!

I wan't it to run in conda jupyter lab. I also followed steps to clone the code to create new conda env, but it didn't work. Somebody pls let me through steps to resolve the issue.

AttributeError with Tokenizer

I'm trying to reproduce the example in the README.

name = 'absa/classifier-rest-0.2'
model = absa.BertABSClassifier.from_pretrained(name)
tokenizer = absa.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)

But I get an AttributeError with the tokenizer.

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-9-c6e986c7be44> in <module>
      1 name = 'absa/classifier-rest-0.2'
      2 model = absa.BertABSClassifier.from_pretrained(name)
----> 3 tokenizer = absa.BertTokenizer.from_pretrained(name)
      4 professor = absa.Professor()     # Explained in detail later on.
      5 text_splitter = absa.sentencizer()  # The English CNN model from SpaCy.

AttributeError: module 'aspect_based_sentiment_analysis' has no attribute 'BertTokenizer'

Could you also clarify how the professor works. The article is missing the hyperlink in the README: "In the article [here], we discuss in detail how the model and the professor work"

Thanks in advance.

Extract Weights and scores from result

`import numpy as np
import aspect_based_sentiment_analysis as absa
from aspect_based_sentiment_analysis import alignment
from aspect_based_sentiment_analysis import Example

text = "I love mascara"
aspects = ['mascara']

recognizer = absa.aux_models.BasicPatternRecognizer()
nlp = absa.load(pattern_recognizer=recognizer)
task = nlp(text=text, aspects=aspects)
slack = task.examples

print(slack)

[PredictedExample(text='I love mascara', aspect='mascara', sentiment=<Sentiment.positive: 2>, text_tokens=['i', 'love', 'mascara'], text_subtokens=['i', 'love', 'mascara'], aspect_tokens=['mascara'], aspect_subtokens=['mascara'], tokens=['[CLS]', 'i', 'love', 'mascara', '[SEP]', 'mascara', '[SEP]'], subtokens=['[CLS]', 'i', 'love', 'mascara', '[SEP]', 'mascara', '[SEP]'], alignment=[[0], [1], [2], [3], [4], [5], [6]], scores=[0.0005469007, 0.0009526035, 0.99850047], review=Review(is_reference=None, patterns=[Pattern(importance=1.0, tokens=['i', 'love', 'mascara'], weights=[0.28, 1.0, 0.71]), Pattern(importance=0.58, tokens=['i', 'love', 'mascara'], weights=[0.13, 0.58, 0.58]), Pattern(importance=0.25, tokens=['i', 'love', 'mascara'], weights=[0.25, 0.25, 0.17])]))]
`

print("###########")
print("Aspect :",slack.aspect)
print("Sentiment :",slack.sentiment)
print("Scores (neutral/negative/positive): ",slack.scores)
#print("Tokens :",slack.text_subtokens)
#print("Words weights related to the aspect :",slack.review.patterns[0].weights)
word = []
list_numbers = slack.review.patterns[0].weights
g = [i for i, n in enumerate(list_numbers) if n > 0.5] # Generator expression
for i in range(0,len(g)):
word_indx = g[i]
word.append(slack.text_subtokens[word_indx])
print(word)

###########
Result

###########
Aspect : price
Sentiment : Sentiment.positive
Scores (neutral/negative/positive): [0.0005469007, 0.0009526035, 0.99850047]
['love', 'mascara']

#########
Problem
Now I receive each time when I run it again:


AttributeError Traceback (most recent call last)
in
1 print("###########")
----> 2 print("Aspect :",slack.aspect)
3 print("Sentiment :",slack.sentiment)
4 print("Scores (neutral/negative/positive): ",slack.scores)
5 print("Tokens :",slack.text_subtokens)

AttributeError: 'list' object has no attribute 'aspect'


AttributeError Traceback (most recent call last)
in
----> 1 absa.summary(skin)
2 absa.display(skin.scores)

~\Anaconda3\envs\asba_aymen_setup\lib\site-packages\aspect_based_sentiment_analysis\plots.py in summary(example)
64
65 def summary(example: PredictedExample):
---> 66 print(f'{str(example.sentiment)} for "{example.aspect}"')
67 rounded_scores = np.round(example.scores, decimals=3)
68 print(f'Scores (neutral/negative/positive): {rounded_scores}')

AttributeError: 'list' object has no attribute 'sentiment'

Can someone help me out please :)

Cannot install via pip

I tried to install the package via pip, which returns the following error.

% pip install aspect-based-sentiment-analysis
ERROR: Could not find a version that satisfies the requirement aspect-based-sentiment-analysis (from versions: none)
ERROR: No matching distribution found for aspect-based-sentiment-analysis

Any ideas?

Questions about BasicPatternRecognizer

Hi,

Firstly, just want to say what a wonderful resource this is! I have several questions about the BasicPatternRecognizer:

  1. References - Of the references you have provided on how attention values can explain a model decision in simple terms [1, 2, 3, 4, 5 ], none seem to mention the using attention gradients. If possible could you either provided any references that informed your thinking on this or provide some intuition for why your method works. For example, in BasicPatternRecognizer you use x = tf.reduce_sum(x, axis=[0, 1], keepdims=True) to combine the attention_scores * gradients for all heads and layers. I think I understand why this works, but I've never seen it done before.
  2. Patterns - I am unclear on what some of the code used to construct the patterns is doing. In particular, I don't understand the line w = x[0, text_mask] , specifically, what 0 is doing; why do we care about the first row and why do we use it to calculate the importance of a given pattern?
  3. Combine patterns into one - I would like to be able to create a single visualization of what tokens the model treats as important of a given aspect. I have some ideas, like scaling the weights by importace and combining, but I really need to first understand the motivation behind the importance metric to do this. Do you have any thoughts of resources I could look at to achieve this? (I would also like to be able to extract the tokens / words which are most important of deciding the sentiment of a given aspect)

Thank you so much!

Josh

OOM issue during training of classifier on GPU instance

Hi there,

I am trying to train own models based on the provided template and it works well on a CPU machine with small training data.

However, when enlarging the training data (>10 sentences or even >100k sentences) I receive an OOM error message. This seems to always happen on GPU instances (tried on AWS with up to G3.16xlarge https://aws.amazon.com/de/ec2/instance-types/g3/ ).

Here is the part of the error message:

2020-08-22T12:59:27.126+02:00 | 2020-08-22 10:59:27.126292: W tensorflow/core/common_runtime/bfc_allocator.cc:434] Allocator (GPU_0_bfc) ran out of memory trying to allocate 48.00MiB (rounded to 50331648)
-- | --
  | 2020-08-22T12:59:27.126+02:00 | Current allocation summary follows.
  | 2020-08-22T12:59:27.126+02:00 | 2020-08-22 10:59:27.126394: I tensorflow/core/common_runtime/bfc_allocator.cc:934] BFCAllocator dump for GPU_0_bfc
  | 2020-08-22T12:59:27.126+02:00 | 2020-08-22 10:59:27.126408: I tensorflow/core/common_runtime/bfc_allocator.cc:941] Bin (256): Total Chunks: 56, Chunks in use: 56. 14.0KiB allocated for chunks. 14.0KiB in use in bin. 384B client-requested in use in bin.

...


2020-08-22T12:59:27.319+02:00 | File "/usr/local/lib/python3.7/dist-packages/optuna/study.py", line 331, in optimize
-- | --
  | 2020-08-22T12:59:27.319+02:00 | func, n_trials, timeout, catch, callbacks, gc_after_trial, None
  | 2020-08-22T12:59:27.319+02:00 | File "/usr/local/lib/python3.7/dist-packages/optuna/study.py", line 626, in _optimize_sequential
  | 2020-08-22T12:59:27.319+02:00 | self._run_trial_and_callbacks(func, catch, callbacks, gc_after_trial)
  | 2020-08-22T12:59:27.319+02:00 | File "/usr/local/lib/python3.7/dist-packages/optuna/study.py", line 656, in _run_trial_and_callbacks
  | 2020-08-22T12:59:27.320+02:00 | trial = self._run_trial(func, catch, gc_after_trial)
  | 2020-08-22T12:59:27.320+02:00 | File "/usr/local/lib/python3.7/dist-packages/optuna/study.py", line 677, in _run_trial
  | 2020-08-22T12:59:27.320+02:00 | result = func(trial)
  | 2020-08-22T12:59:27.320+02:00 | File "/usr/local/lib/python3.7/dist-packages/txtclassification/fine_tune_absa.py", line 236, in objective
  | 2020-08-22T12:59:27.320+02:00 | return experiment(local_folder_name=local_folder_name, **params)
  | 2020-08-22T12:59:27.320+02:00 | File "/usr/local/lib/python3.7/dist-packages/txtclassification/fine_tune_absa.py", line 160, in experiment
  | 2020-08-22T12:59:27.320+02:00 | test_dataset, callbacks, strategy)
  | 2020-08-22T12:59:27.320+02:00 | File "/usr/local/lib/python3.7/dist-packages/aspect_based_sentiment_analysis/training/classifier.py", line 60, in train_classifier
  | 2020-08-22T12:59:27.320+02:00 | callbacks=callbacks
  | 2020-08-22T12:59:27.320+02:00 | File "/usr/local/lib/python3.7/dist-packages/aspect_based_sentiment_analysis/training/routines.py", line 29, in train
  | 2020-08-22T12:59:27.320+02:00 | train_loop(train_step, train_dataset, callbacks, strategy)
  | 2020-08-22T12:59:27.320+02:00 | File "/usr/local/lib/python3.7/dist-packages/aspect_based_sentiment_analysis/training/routines.py", line 44, in train_loop
  | 2020-08-22T12:59:27.320+02:00 | train_step_outputs = step(tf_batch)
  | 2020-08-22T12:59:27.320+02:00 | File "/usr/local/lib/python3.7/dist-packages/aspect_based_sentiment_analysis/training/routines.py", line 62, in one_device
  | 2020-08-22T12:59:27.320+02:00 | return strategy.experimental_run_v2(step, args=batch)
  | 2020-08-22T12:59:27.320+02:00 | File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/deprecation.py", line 324, in new_func
  | 2020-08-22T12:59:27.321+02:00 | return func(*args, **kwargs)
  | 2020-08-22T12:59:27.321+02:00 | File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py", line 957, in experimental_run_v2
  | 2020-08-22T12:59:27.321+02:00 | return self.run(fn, args=args, kwargs=kwargs, options=options)
  | 2020-08-22T12:59:27.321+02:00 | File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/one_device_strategy.py", line 182, in run
  | 2020-08-22T12:59:27.321+02:00 | return super(OneDeviceStrategy, self).run(fn, args, kwargs, options)
  | 2020-08-22T12:59:27.321+02:00 | File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py", line 951, in run
  | 2020-08-22T12:59:27.321+02:00 | return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
  | 2020-08-22T12:59:27.321+02:00 | File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py", line 2290, in call_for_each_replica
  | 2020-08-22T12:59:27.322+02:00 | return self._call_for_each_replica(fn, args, kwargs)
  | 2020-08-22T12:59:27.322+02:00 | File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/one_device_strategy.py", line 362, in _call_for_each_replica
  | 2020-08-22T12:59:27.322+02:00 | return fn(*args, **kwargs)
  | 2020-08-22T12:59:27.322+02:00 | File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/autograph/impl/api.py", line 282, in wrapper
  | 2020-08-22T12:59:27.322+02:00 | return func(*args, **kwargs)
  | 2020-08-22T12:59:27.322+02:00 | File "/usr/local/lib/python3.7/dist-packages/aspect_based_sentiment_analysis/training/classifier.py", line 31, in train_step
  | 2020-08-22T12:59:27.322+02:00 | training=True
  | 2020-08-22T12:59:27.322+02:00 | File "/usr/local/lib/python3.7/dist-packages/aspect_based_sentiment_analysis/models.py", line 147, in call
  | 2020-08-22T12:59:27.322+02:00 | **bert_kwargs
  | 2020-08-22T12:59:27.322+02:00 | File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/base_layer.py", line 968, in __call__
  | 2020-08-22T12:59:27.323+02:00 | outputs = self.call(cast_inputs, *args, **kwargs)
  | 2020-08-22T12:59:27.323+02:00 | File "/usr/local/lib/python3.7/dist-packages/transformers/modeling_tf_bert.py", line 572, in call
  | 2020-08-22T12:59:27.323+02:00 | encoder_outputs = self.encoder([embedding_output, extended_attention_mask, head_mask], training=training)
  | 2020-08-22T12:59:27.323+02:00 | File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/base_layer.py", line 968, in __call__
  | 2020-08-22T12:59:27.323+02:00 | outputs = self.call(cast_inputs, *args, **kwargs)
  | 2020-08-22T12:59:27.323+02:00 | File "/usr/local/lib/python3.7/dist-packages/transformers/modeling_tf_bert.py", line 378, in call
  | 2020-08-22T12:59:27.323+02:00 | layer_outputs = layer_module([hidden_states, attention_mask, head_mask[i]], training=training)
  | 2020-08-22T12:59:27.323+02:00 | File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/base_layer.py", line 968, in __call__
  | 2020-08-22T12:59:27.324+02:00 | outputs = self.call(cast_inputs, *args, **kwargs)
  | 2020-08-22T12:59:27.324+02:00 | File "/usr/local/lib/python3.7/dist-packages/transformers/modeling_tf_bert.py", line 356, in call
  | 2020-08-22T12:59:27.324+02:00 | intermediate_output = self.intermediate(attention_output)
  | 2020-08-22T12:59:27.324+02:00 | File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/base_layer.py", line 968, in __call__
  | 2020-08-22T12:59:27.324+02:00 | outputs = self.call(cast_inputs, *args, **kwargs)
  | 2020-08-22T12:59:27.324+02:00 | File "/usr/local/lib/python3.7/dist-packages/transformers/modeling_tf_bert.py", line 322, in call
  | 2020-08-22T12:59:27.324+02:00 | hidden_states = self.intermediate_act_fn(hidden_states)
  | 2020-08-22T12:59:27.324+02:00 | File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/base_layer.py", line 968, in __call__
  | 2020-08-22T12:59:27.325+02:00 | outputs = self.call(cast_inputs, *args, **kwargs)
  | 2020-08-22T12:59:27.325+02:00 | File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/layers/core.py", line 420, in call
  | 2020-08-22T12:59:27.325+02:00 | return self.activation(inputs)
  | 2020-08-22T12:59:27.325+02:00 | File "/usr/local/lib/python3.7/dist-packages/transformers/modeling_tf_bert.py", line 65, in gelu
  | 2020-08-22T12:59:27.325+02:00 | cdf = 0.5 * (1.0 + tf.math.erf(x / tf.math.sqrt(2.0)))
  | 2020-08-22T12:59:27.325+02:00 | File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/math_ops.py", line 1010, in r_binary_op_wrapper
  | 2020-08-22T12:59:27.325+02:00 | return func(x, y, name=name)
  | 2020-08-22T12:59:27.325+02:00 | File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/math_ops.py", line 1276, in _add_dispatch
  | 2020-08-22T12:59:27.325+02:00 | return gen_math_ops.add_v2(x, y, name=name)
  | 2020-08-22T12:59:27.325+02:00 | File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/gen_math_ops.py", line 480, in add_v2
  | 2020-08-22T12:59:27.326+02:00 | _ops.raise_from_not_ok_status(e, name)
  | 2020-08-22T12:59:27.326+02:00 | File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/ops.py", line 6653, in raise_from_not_ok_status
  | 2020-08-22T12:59:27.327+02:00 | six.raise_from(core._status_to_exception(e.code, message), None)
  | 2020-08-22T12:59:27.327+02:00 | File "<string>", line 3, in raise_from
  | 2020-08-22T12:59:27.327+02:00 | tensorflow.python.framework.errors_impl.ResourceExhaustedError: OOM when allocating tensor with shape[32,128,3072] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc [Op:AddV2]

Would be great if you could provide any suggestions how to solve.

Thank you and best,
Tobias

OOM errors on long text

I'm still getting OOM errors despite splitting the text into sentences using the text_splitter = absa.sentencizer()

I have for instance a text of 4528 characters that gets split up into 43 sentences (the largest of which is 163 characters long) that throws an OOM error. Any tips/ideas how I could handle such cases?

About the AE feature

can I use this for aspect extraction task as an end2end sentiment analysis tool?

Probing is not working

Cloned repository, installed dependencies and ran patters.ipynb example.
It fails on the last step (absa.probing.explain) with error attached:

Error

Sentiment Analysis Example Code Output Improvement

I am running the Readme file example.

from transformers import BertTokenizer`
name = 'absa/classifier-lapt-0.2'`
model = absa.BertABSClassifier.from_pretrained(name)
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)
task = nlp(text="the laptop has excellent design.But battery life is not as my per my expectation.", aspects=['design','Battery','processor'])
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)

When I run this:
absa.summary(design)
I am getting right output i.e.
Sentiment.positive for "design" Scores (neutral/negative/positive): [0.028 0.057 0.915]

But when I run this
absa.summary(processor)
I am getting negative sentiment with a score above 0.9. Instead, I should get a neutral sentiment as the "processor" aspect is not included in the text. There is no sentence talking about the processor (performance).
If I want to do it such that the aspects which are not included in the text should be assigned a neutral sentiment instead of positive or negative how to do it?
Please If anyone has the idea or code for that add it here.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.