-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbertTest.py
More file actions
29 lines (24 loc) · 814 Bytes
/
bertTest.py
File metadata and controls
29 lines (24 loc) · 814 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from transformers import AutoModel
from transformers import AutoTokenizer
import torch
import numpy as np
model = AutoModel.from_pretrained("neuralmind/bert-large-portuguese-cased")
tokenizer = AutoTokenizer.from_pretrained(
"neuralmind/bert-large-portuguese-cased", do_lower_case=False
)
sentences = [
"Tinha uma pedra no meio do caminho.",
"Eu gosto de cachorro",
"Eu gosto de gato",
"Eu gosto de passarinho",
]
input_ids = [
tokenizer.encode(sentence, return_tensors="pt") for sentence in sentences
]
with torch.no_grad():
outs = [model(input_id) for input_id in input_ids]
encoded = [out[0][0, 1:-1] for out in outs]
final_representation = np.array(
[np.mean(sent.cpu().detach().numpy(), axis=0) for sent in encoded]
)
print(final_representation.shape)