File size: 1,877 Bytes
cc51a30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
---
language:
- 'no'
- nb
- nn
inference: false
tags:
- T5
- NorT5
- Norwegian
- encoder-decoder
license: cc-by-4.0
---

# NorT5 x-small


## Other sizes:
- [NorBERT 3 xs (15M)](https://huggingface.co/ltg/nort5-xs)
- [NorBERT 3 small (40M)](https://huggingface.co/ltg/nort5-small)
- [NorBERT 3 base (123M)](https://huggingface.co/ltg/nort5-base)
- [NorBERT 3 large (323M)](https://huggingface.co/ltg/nort5-large)


## Example usage

This model currently needs a custom wrapper from `modeling_nort5.py`. Then you can use it like this:

```python
import torch
from transformers import AutoTokenizer
from modeling_norbert import NorT5ForConditionalGeneration

tokenizer = AutoTokenizer.from_pretrained("path/to/folder")
t5 = NorT5ForConditionalGeneration.from_pretrained("path/to/folder")


# MASKED LANGUAGE MODELING

sentence = "Brukseksempel: Elektrisk oppvarming. Definisjonen på ordet oppvarming er[MASK_0]."
encoding = tokenizer(sentence)

input_tensor = torch.tensor([encoding.input_ids])
output_tensor = model.generate(input_tensor, decoder_start_token_id=7, eos_token_id=8)
tokenizer.decode(output_tensor.squeeze(), skip_special_tokens=True)

# should output:  å varme opp


# PREFIX LANGUAGE MODELING
# you need to finetune this model or use `nort5-{size}-lm` model, which is finetuned on prefix language modeling

sentence = "Brukseksempel: Elektrisk oppvarming. Definisjonen på ordet oppvarming er (Wikipedia) "
encoding = tokenizer(sentence)

input_tensor = torch.tensor([encoding.input_ids])
output_tensor = model.generate(input_tensor, max_new_tokens=50, num_beams=4, do_sample=False)
tokenizer.decode(output_tensor.squeeze())

# should output: [BOS]ˈoppvarming, det vil si at det skjer en endring i temperaturen i et medium, f.eks. en ovn eller en radiator, slik at den blir varmere eller kaldere, eller at den blir varmere eller kaldere, eller at den blir
```