songyouwei / ABSA-PyTorch

Aspect Based Sentiment Analysis, PyTorch Implementations. 基于方面的情感分析,使用PyTorch实现。
MIT License
2.03k stars 528 forks source link

Error while running infer_example.py for bert_spc #55

Closed SaratM34 closed 5 years ago

SaratM34 commented 5 years ago

Hi,

I am getting below error when running infer_example.py for bert_spc. Can someone help me solve it?

image

Below is the code for infer_example.py that I am using.

import torch
import torch.nn.functional as F
import torch.nn as nn
import argparse

from data_utils import build_tokenizer, build_embedding_matrix
from models import IAN, MemNet, ATAE_LSTM, AOA
from models.bert_spc import BERT_SPC

class Inferer:
    """A simple inference example"""

    def __init__(self, opt):
        self.opt = opt
        self.tokenizer = build_tokenizer(
            fnames=[opt.dataset_file['train'], opt.dataset_file['test']],
            max_seq_len=opt.max_seq_len,
            dat_fname='{0}_tokenizer.dat'.format(opt.dataset))
        embedding_matrix = build_embedding_matrix(
            word2idx=self.tokenizer.word2idx,
            embed_dim=opt.embed_dim,
            dat_fname='{0}_{1}_embedding_matrix.dat'.format(str(opt.embed_dim), opt.dataset))
        self.model = opt.model_class(embedding_matrix, opt)
        print('loading model {0} ...'.format(opt.model_name))
        self.model.load_state_dict(torch.load(opt.state_dict_path))
        self.model = self.model.to(opt.device)
        # switch model to evaluation mode
        self.model.eval()
        torch.autograd.set_grad_enabled(False)

    def evaluate(self, raw_texts):
        context_seqs = [self.tokenizer.text_to_sequence(raw_text.lower().strip()) for raw_text in raw_texts]
        aspect_seqs = [self.tokenizer.text_to_sequence('null')] * len(raw_texts)
        context_indices = torch.tensor(context_seqs, dtype=torch.int64).to(self.opt.device)
        aspect_indices = torch.tensor(aspect_seqs, dtype=torch.int64).to(self.opt.device)

        t_inputs = [context_indices, aspect_indices]
        t_outputs = self.model(t_inputs)

        t_probs = F.softmax(t_outputs, dim=-1).cpu().numpy()
        return t_probs

if __name__ == '__main__':
    model_classes = {
        'atae_lstm': ATAE_LSTM,
        'ian': IAN,
        'memnet': MemNet,
        'aoa': AOA,
        'bert_spc': BERT_SPC,
    }
    # set your trained models here
    model_state_dict_paths = {
        'atae_lstm': 'state_dict/atae_lstm_restaurant_acc0.7786',
        'ian': 'state_dict/ian_restaurant_acc0.7911',
        'memnet': 'state_dict/memnet_restaurant_acc0.7911',
        'aoa': 'state_dict/aoa_restaurant_acc0.8063',
        'bert_spc': 'state_dict/bert_spc_restaurant_val_acc0.8196',
    }

    class Option(object):
        pass
    opt = Option()
    opt.model_name = 'bert_spc'
    opt.model_class = model_classes[opt.model_name]
    opt.dataset = 'restaurant'
    opt.dataset_file = {
        'train': './datasets/semeval14/Restaurants_Train.xml.seg',
        'test': './datasets/semeval14/Restaurants_Test_Gold.xml.seg'
    }
    opt.state_dict_path = model_state_dict_paths[opt.model_name]
    opt.embed_dim = 300
    opt.hidden_dim = 300
    opt.max_seq_len = 80
    opt.polarities_dim = 3
    opt.dropout = 0.1
    opt.bert_dim = 768
    opt.hops = 3
    opt.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

    inf = Inferer(opt)
    t_probs = inf.evaluate(['happy memory', 'the service is terrible', 'just normal food'])
    print(t_probs.argmax(axis=-1) - 1)

Thanks

songyouwei commented 5 years ago

It looks like you're using a non-BERT tokenizer. Currently, infer_example.py only works for non-BERT-based models. You should use Tokenizer4Bert in your __init__. Refer to the following code: https://github.com/songyouwei/ABSA-PyTorch/blob/a736bf1cc07e4509fe60e986dbe9b8658d153817/train.py#L35-L48

If you have this work done, feel free to make a pull request!

MiniXC commented 5 years ago

I changed above code to use Tokenizer4Bert but run into the following issue which was a dead end for me.

loading model bert_spc ...
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-2-30eeab93ee96> in <module>
     87 
     88     inf = Inferer(opt)
---> 89     t_probs = inf.evaluate(['happy memory', 'the service is terrible', 'just normal food'])
     90     print(t_probs.argmax(axis=-1) - 1)

<ipython-input-2-30eeab93ee96> in evaluate(self, raw_texts)
     43 
     44         t_inputs = [context_indices, aspect_indices]
---> 45         t_outputs = self.model(t_inputs)
     46 
     47         t_probs = F.softmax(t_outputs, dim=-1).cpu().numpy()

~/.local/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    491             result = self._slow_forward(*input, **kwargs)
    492         else:
--> 493             result = self.forward(*input, **kwargs)
    494         for hook in self._forward_hooks.values():
    495             hook_result = hook(self, input, result)

~/Research/ABSA-PyTorch/models/bert_spc.py in forward(self, inputs)
     21         # text_bert_indices = self.squeeze_embedding(text_bert_indices, text_bert_len)
     22         # bert_segments_ids = self.squeeze_embedding(bert_segments_ids, text_bert_len)
---> 23         _, pooled_output = self.bert(text_bert_indices, bert_segments_ids, output_all_encoded_layers=False)
     24         pooled_output = self.dropout(pooled_output)
     25         logits = self.dense(pooled_output)

~/.local/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    491             result = self._slow_forward(*input, **kwargs)
    492         else:
--> 493             result = self.forward(*input, **kwargs)
    494         for hook in self._forward_hooks.values():
    495             hook_result = hook(self, input, result)

~/.local/lib/python3.6/site-packages/pytorch_pretrained_bert/modeling.py in forward(self, input_ids, token_type_ids, attention_mask, output_all_encoded_layers)
    731         encoded_layers = self.encoder(embedding_output,
    732                                       extended_attention_mask,
--> 733                                       output_all_encoded_layers=output_all_encoded_layers)
    734         sequence_output = encoded_layers[-1]
    735         pooled_output = self.pooler(sequence_output)

~/.local/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    491             result = self._slow_forward(*input, **kwargs)
    492         else:
--> 493             result = self.forward(*input, **kwargs)
    494         for hook in self._forward_hooks.values():
    495             hook_result = hook(self, input, result)

~/.local/lib/python3.6/site-packages/pytorch_pretrained_bert/modeling.py in forward(self, hidden_states, attention_mask, output_all_encoded_layers)
    404         all_encoder_layers = []
    405         for layer_module in self.layer:
--> 406             hidden_states = layer_module(hidden_states, attention_mask)
    407             if output_all_encoded_layers:
    408                 all_encoder_layers.append(hidden_states)

~/.local/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    491             result = self._slow_forward(*input, **kwargs)
    492         else:
--> 493             result = self.forward(*input, **kwargs)
    494         for hook in self._forward_hooks.values():
    495             hook_result = hook(self, input, result)

~/.local/lib/python3.6/site-packages/pytorch_pretrained_bert/modeling.py in forward(self, hidden_states, attention_mask)
    389 
    390     def forward(self, hidden_states, attention_mask):
--> 391         attention_output = self.attention(hidden_states, attention_mask)
    392         intermediate_output = self.intermediate(attention_output)
    393         layer_output = self.output(intermediate_output, attention_output)

~/.local/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    491             result = self._slow_forward(*input, **kwargs)
    492         else:
--> 493             result = self.forward(*input, **kwargs)
    494         for hook in self._forward_hooks.values():
    495             hook_result = hook(self, input, result)

~/.local/lib/python3.6/site-packages/pytorch_pretrained_bert/modeling.py in forward(self, input_tensor, attention_mask)
    347 
    348     def forward(self, input_tensor, attention_mask):
--> 349         self_output = self.self(input_tensor, attention_mask)
    350         attention_output = self.output(self_output, input_tensor)
    351         return attention_output

~/.local/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    491             result = self._slow_forward(*input, **kwargs)
    492         else:
--> 493             result = self.forward(*input, **kwargs)
    494         for hook in self._forward_hooks.values():
    495             hook_result = hook(self, input, result)

~/.local/lib/python3.6/site-packages/pytorch_pretrained_bert/modeling.py in forward(self, hidden_states, attention_mask)
    298 
    299     def forward(self, hidden_states, attention_mask):
--> 300         mixed_query_layer = self.query(hidden_states)
    301         mixed_key_layer = self.key(hidden_states)
    302         mixed_value_layer = self.value(hidden_states)

~/.local/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    491             result = self._slow_forward(*input, **kwargs)
    492         else:
--> 493             result = self.forward(*input, **kwargs)
    494         for hook in self._forward_hooks.values():
    495             hook_result = hook(self, input, result)

~/.local/lib/python3.6/site-packages/torch/nn/modules/linear.py in forward(self, input)
     90     @weak_script_method
     91     def forward(self, input):
---> 92         return F.linear(input, self.weight, self.bias)
     93 
     94     def extra_repr(self):

~/.local/lib/python3.6/site-packages/torch/nn/functional.py in linear(input, weight, bias)
   1406         ret = torch.addmm(bias, input, weight.t())
   1407     else:
-> 1408         output = input.matmul(weight.t())
   1409         if bias is not None:
   1410             output += bias

RuntimeError: cublas runtime error : resource allocation failed at /pytorch/aten/src/THC/THCGeneral.cpp:228

My Code


import torch
import torch.nn.functional as F
import torch.nn as nn
import argparse
from pytorch_pretrained_bert import BertModel

from data_utils import build_tokenizer, build_embedding_matrix, Tokenizer4Bert
from models import IAN, MemNet, ATAE_LSTM, AOA
from models.bert_spc import BERT_SPC

class Inferer:
    """A simple inference example"""

    def __init__(self, opt):
        self.opt = opt
        if 'bert' in opt.model_name:
            self.tokenizer = Tokenizer4Bert(opt.max_seq_len, 'bert-base-uncased') 
            bert = BertModel.from_pretrained('bert-base-uncased') 
            self.model = opt.model_class(bert, self.opt)
        else:
            self.tokenizer = build_tokenizer(
                fnames=[opt.dataset_file['train'], opt.dataset_file['test']],
                max_seq_len=opt.max_seq_len,
                dat_fname='{0}_tokenizer.dat'.format(opt.dataset))
            embedding_matrix = build_embedding_matrix(
                word2idx=self.tokenizer.word2idx,
                embed_dim=opt.embed_dim,
                dat_fname='{0}_{1}_embedding_matrix.dat'.format(str(opt.embed_dim), opt.dataset))
            self.model = opt.model_class(embedding_matrix, opt)
        print('loading model {0} ...'.format(opt.model_name))
        self.model.load_state_dict(torch.load(opt.state_dict_path))
        self.model = self.model.to(opt.device)
        # switch model to evaluation mode
        self.model.eval()
        torch.autograd.set_grad_enabled(False)

    def evaluate(self, raw_texts):
        context_seqs = [self.tokenizer.text_to_sequence(raw_text.lower().strip()) for raw_text in raw_texts]
        aspect_seqs = [self.tokenizer.text_to_sequence('null')] * len(raw_texts)
        context_indices = torch.tensor(context_seqs, dtype=torch.int64).to(self.opt.device)
        aspect_indices = torch.tensor(aspect_seqs, dtype=torch.int64).to(self.opt.device)

        t_inputs = [context_indices, aspect_indices]
        t_outputs = self.model(t_inputs)

        t_probs = F.softmax(t_outputs, dim=-1).cpu().numpy()
        return t_probs

if __name__ == '__main__':
    model_classes = {
        'atae_lstm': ATAE_LSTM,
        'ian': IAN,
        'memnet': MemNet,
        'aoa': AOA,
        'bert_spc': BERT_SPC,
    }
    # set your trained models here
    model_state_dict_paths = {
        'atae_lstm': 'state_dict/atae_lstm_restaurant_acc0.7786',
        'ian': 'state_dict/ian_restaurant_acc0.7911',
        'memnet': 'state_dict/memnet_restaurant_acc0.7911',
        'aoa': 'state_dict/aoa_restaurant_acc0.8063',
        'bert_spc': 'state_dict/bert_spc_restaurant_val_acc0.8473',
    }

    class Option(object):
        pass
    opt = Option()
    opt.model_name = 'bert_spc'
    opt.model_class = model_classes[opt.model_name]
    opt.dataset = 'restaurant'
    opt.dataset_file = {
        'train': './datasets/semeval14/Restaurants_Train.xml.seg',
        'test': './datasets/semeval14/Restaurants_Test_Gold.xml.seg'
    }
    opt.state_dict_path = model_state_dict_paths[opt.model_name]
    opt.embed_dim = 300
    opt.hidden_dim = 300
    opt.max_seq_len = 80
    opt.polarities_dim = 3
    opt.dropout = 0.1
    opt.bert_dim = 768
    opt.hops = 3
    opt.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

    inf = Inferer(opt)
    t_probs = inf.evaluate(['happy memory', 'the service is terrible', 'just normal food'])
    print(t_probs.argmax(axis=-1) - 1)
abhinandansrivastava commented 5 years ago

Is there any update on this issue ?

rmarcacini commented 5 years ago

Is there any update on this issue ?

The 'infer_example.py' does not work for bert models because different preparation of input texts (tokenization) is required. Here, I wrote a prediction example for bert models and tested for bert_spc.

https://github.com/rmarcacini/ABSA-PyTorch/blob/master/infer_example_bert_models.py