Views
No views yet
1from transformers import pipeline
2model = pipeline(task="fill-mask", model="hupd/hupd-distilroberta-base")
3model("Improved <mask> for playing a game of thumb wrestling.")1[{'score': 0.4274042248725891,
2 'sequence': 'Improved method for playing a game of thumb wrestling.',
3 'token': 5448,
4 'token_str': ' method'},
5 {'score': 0.06967400759458542,
6 'sequence': 'Improved system for playing a game of thumb wrestling.',
7 'token': 467,
8 'token_str': ' system'},
9 {'score': 0.06849079579114914,
10 'sequence': 'Improved device for playing a game of thumb wrestling.',
11 'token': 2187,
12 'token_str': ' device'},
13 {'score': 0.04544765502214432,
14 'sequence': 'Improved apparatus for playing a game of thumb wrestling.',
15 'token': 26529,
16 'token_str': ' apparatus'},
17 {'score': 0.025765646249055862,
18 'sequence': 'Improved means for playing a game of thumb wrestling.',
19 'token': 839,
20 'token_str': ' means'}]1import torch
2from transformers import AutoTokenizer, AutoModelForMaskedLM
3
4# cuda/cpu
5device = 'cuda' if torch.cuda.is_available() else 'cpu'
6
7tokenizer = AutoTokenizer.from_pretrained("hupd/hupd-distilroberta-base")
8model = AutoModelForMaskedLM.from_pretrained("hupd/hupd-distilroberta-base").to(device)
9
10TEXT = "Improved <mask> for playing a game of thumb wrestling."
11
12inputs = tokenizer(TEXT, return_tensors="pt").to(device)
13
14with torch.no_grad():
15 logits = model(**inputs).logits
16
17# retrieve indices of <mask>
18mask_token_indxs = (inputs.input_ids == tokenizer.mask_token_id)[0].nonzero(as_tuple=True)[0]
19
20for mask_idx in mask_token_indxs:
21 predicted_token_id = logits[0, mask_idx].argmax(axis=-1)
22 output = tokenizer.decode(predicted_token_id)
23 print(f'Prediction for the <mask> token at index {mask_idx}: "{output}"')Prediction for the <mask> token at index 2: " method"@article{suzgun2022hupd,
title={The Harvard USPTO Patent Dataset: A Large-Scale, Well-Structured, and Multi-Purpose Corpus of Patent Applications},
author={Suzgun, Mirac and Melas-Kyriazi, Luke and Sarkar, Suproteem K and Kominers, Scott and Shieber, Stuart},
year={2022}
}