Views
No views yet
1from transformers import pipeline
2unmasker = pipeline('fill-mask', model='mstaron/CyLBERT')
3unmasker("Hello I'm a <mask> model.")1# import the model via the huggingface library
2from transformers import AutoTokenizer, AutoModelForMaskedLM
3
4# load the tokenizer and the model for the pretrained CyLBERT
5tokenizer = AutoTokenizer.from_pretrained('mstaron/CyLBERT')
6
7# load the model
8model = AutoModelForMaskedLM.from_pretrained("mstaron/CyLBERT")
9
10# import the feature extraction pipeline
11from transformers import pipeline
12
13# create the pipeline, which will extract the embedding vectors
14# the models are already pre-defined, so we do not need to train anything here
15features = pipeline(
16 "feature-extraction",
17 model=model,
18 tokenizer=tokenizer,
19 return_tensor = False
20)
21
22# extract the features == embeddings
23lstFeatures = features('Class HTTP::X1')
24
25# print the first token's embedding [CLS]
26# which is also a good approximation of the whole sentence embedding
27# the same as using np.mean(lstFeatures[0], axis=0)
28lstFeatures[0][0]