-
Notifications
You must be signed in to change notification settings - Fork 1
/
Encoder.py
54 lines (41 loc) · 1.83 KB
/
Encoder.py
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
import torch
import torch.nn as nn
from torch.nn.utils.rnn import pack_padded_sequence, pad_packed_sequence
from transformers import BertTokenizer, BertModel
from transformers import AlbertTokenizer, AlbertModel
from transformers import GPT2Tokenizer, GPT2LMHeadModel
from config import Config
import pytorch_lightning as pl
class Encoder(pl.LightningModule):
r""" The encoder """
def __init__(self):
super(Encoder, self).__init__()
config = Config()
data_language = config.data_language
if data_language == 'EN':
self.tok = GPT2Tokenizer.from_pretrained("gpt2",do_lower_case=False)
self.model = GPT2LMHeadModel.from_pretrained("gpt2")
self.tok.pad_token = self.tok.eos_token
else:
self.tok = BertTokenizer.from_pretrained("uer/gpt2-chinese-cluecorpussmall")
self.model = GPT2LMHeadModel.from_pretrained("uer/gpt2-chinese-cluecorpussmall")
self.config = Config()
self.max_length = self.config.max_len
def forward(self, inputs):
batch_input_encode = self.tok.batch_encode_plus(
inputs,
max_length=self.max_length,
truncation=True,
padding="longest",
return_tensors="pt",
)
# cuda
batch_input_encode = {k: v.to(self.device) for k, v in batch_input_encode.items()}
outputs = self.model(input_ids=batch_input_encode['input_ids'],
attention_mask=batch_input_encode['attention_mask'],
output_hidden_states=True,
return_dict=True,
)
# Return to the hidden states of the final model of the output of the last time step
pooler_output = outputs.hidden_states[-1][:, -1, :] # [batch, dim]
return pooler_output # [batch, dim]