I got an issue when I try to develop infer to give result. But unfortunately, it could be don. The following code will define infer and load the model saved before (*.h5 file):
def infer(text, tokenizer, max_len=120):
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
class_names = ['thế giới', 'thể thao', 'văn hóa', 'vi tính']
model = keras.models.load_model('./models/cnn_nlp_text_classification_4_classer.h5')
encoded_review = tokenizer.encode_plus(
text,
max_length=max_len,
truncation=True,
add_special_tokens=True,
padding='max_length',
return_attention_mask=True,
return_token_type_ids=False,
return_tensors='pt',
)
input_ids = encoded_review['input_ids']
# print(input_ids)
attention_mask = encoded_review['attention_mask']
# print(attention_mask)
output = model(input_ids, attention_mask)
output = torch.randn(1, 3)
_, y_pred = torch.max(output)
print(f'Text: {text}')
print(f'Sentiment: {class_names[y_pred]}')
After define infer, I do call to process but it returns the failure as below:
from tensorflow import keras
tokenizer = AutoTokenizer.from_pretrained("vinai/phobert-base")
infer('Chào mừng mọi người đến với bản tin thể thao 360 tối nay !', tokenizer)
Output and error:
Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.
Token indices sequence length is longer than the specified maximum sequence length for this model (3002 > 256). Running this sequence through the model will result in indexing errors
tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
tensor([[ 0, 29048, 36475, ..., 36475, 381, 2]])
tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-35-56984d69c6cb> in <module>
2 from tensorflow import keras
3 tokenizer = AutoTokenizer.from_pretrained("vinai/phobert-base")
----> 4 infer('Chào mừng mọi người đến với bản tin thể thao 360 tối nay !', tokenizer)
2 frames
/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py in display_shape(shape)
269
270 def display_shape(shape):
--> 271 return str(tuple(shape.as_list()))
272
273
AttributeError: 'torch.Size' object has no attribute 'as_list'