Views
No views yet
1
2import torch
3from transformers import AutoTokenizer, BertForSequenceClassification, BertTokenizer, AutoModelForCausalLM, pipeline
4
5
6model_bert = torch.load('raicrits/BERT_ChangeOfTopic')
7model_bert = model_bert.to(device_bert)
8
9tokenizer_bert = AutoTokenizer.from_pretrained('bert-base-multilingual-cased')
10
11encoded_dict = tokenizer_bert.encode_plus(
12 '<text>',
13 add_special_tokens = True,
14 max_length = 256,
15 # max_length = min(max_len, 512),
16 truncation = True,
17 padding='max_length',
18 return_attention_mask = True,
19 return_tensors = 'pt',
20 )
21input_ids = encoded_dict['input_ids'].to(device_bert)
22input_mask = encoded_dict['attention_mask'].to(device_bert)
23with torch.no_grad():
24 output= model_bert(input_ids,
25 token_type_ids=None,
26 attention_mask=input_mask)
27 logits = output.logits
28 logits = logits.detach().cpu().numpy()
29 pred_flat = np.argmax(logits, axis=1).flatten()
30print(pred_flat[0])