Views
No views yet
1from transformers import BertTokenizer, BertModel
2tokenizer = BertTokenizer.from_pretrained('Intel/bert-base-uncased-mrpc')
3model = BertModel.from_pretrained("Intel/bert-base-uncased-mrpc")
4text = "The inspector analyzed the soundness in the building."
5encoded_input = tokenizer(text, return_tensors='pt')
6output = model(**encoded_input)
7# print BaseModelOutputWithPoolingAndCrossAttentions and pooler_output
8
9# Print tokens * ids in of inmput string below
10print('Tokenized Text: ', tokenizer.tokenize(text), '\n')
11print('Token IDs: ', tokenizer.convert_tokens_to_ids(tokenizer.tokenize(text)))
12
13#Print tokens in text
14encoded_input['input_ids'][0]
15tokenizer.convert_ids_to_tokens(encoded_input['input_ids'][0])1BaseModelOutputWithPoolingAndCrossAttentions(last_hidden_state=tensor([[[ 0.0219, 0.1258, -0.8529, ..., 0.6416, 0.6275, 0.5583],
2 [ 0.3125, -0.1921, -0.9895, ..., 0.6069, 1.8431, -0.5939],
3 [ 0.6147, -0.6098, -0.3517, ..., -0.1145, 1.1748, -0.7104],
4 ...,
5 [ 0.8959, -0.2324, -0.6311, ..., 0.2424, 0.1025, 0.2101],
6 [ 0.2484, -0.3004, -0.9474, ..., 1.0401, 0.5493, -0.4170],
7 [ 0.8206, 0.2023, -0.7929, ..., 0.7073, 0.0779, -0.2781]]],
8 grad_fn=<NativeLayerNormBackward0>), pooler_output=tensor([[-0.7867, 0.1878, -0.8186, 0.8494, 0.4263, 0.5157, 0.9564, 0.1514,
9 -0.9176, -0.9994, 0.2962, 0.2891, -0.3301, 0.8786, 0.9234, -0.7643,
10 0.2487, -0.5245, -0.0649, -0.6722, 0.8550, 1.0000, -0.7785, 0.5322,
11 0.6056, 0.4622, 0.2838, 0.5501, 0.6981, 0.2597, -0.7896, -0.1189,1@article{DBLP:journals/corr/abs-1810-04805,
2 author = {Jacob Devlin and
3 Ming{-}Wei Chang and
4 Kenton Lee and
5 Kristina Toutanova},
6 title = {{BERT:} Pre-training of Deep Bidirectional Transformers for Language
7 Understanding},
8 journal = {CoRR},
9 volume = {abs/1810.04805},
10 year = {2018},
11 url = {http://arxiv.org/abs/1810.04805},
12 archivePrefix = {arXiv},
13 eprint = {1810.04805},
14 timestamp = {Tue, 30 Oct 2018 20:39:56 +0100},
15 biburl = {https://dblp.org/rec/journals/corr/abs-1810-04805.bib},
16 bibsource = {dblp computer science bibliography, https://dblp.org}
17}