-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroberta2roberta.py
More file actions
39 lines (34 loc) · 1.11 KB
/
roberta2roberta.py
File metadata and controls
39 lines (34 loc) · 1.11 KB
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
import torch.nn as nn
from transformers import EncoderDecoderModel
import torch
model = EncoderDecoderModel.from_encoder_decoder_pretrained("klue/roberta-base", "klue/roberta-base")
class Roberta2Roberta(nn.Module):
def __init__(self):
super(Roberta2Roberta, self).__init__()
self.roberta = model
def generative(self,
input_ids,
do_sample=True,
max_length=50,
top_p=0.95,
top_k=80,
temperature=0.6,
no_repeat_ngram_size=2,
num_return_sequence=3,
early_stopping = False
):
return self.roberta.generate(
input_ids,
do_sample=do_sample,
max_length=max_length,
#num_beams=num_beams,
temperature=temperature,
top_k = top_k,
top_p = top_p,
no_repeat_ngram_size=no_repeat_ngram_size,
#num_return_sequence=num_return_sequence,
early_stopping=early_stopping
)
def forward(self, input, labels):
outputs = self.roberta(input_ids=input, decoder_input_ids=labels, labels=labels)
return outputs