-
Notifications
You must be signed in to change notification settings - Fork 2
/
prompt_encoder.py
38 lines (34 loc) · 1.64 KB
/
prompt_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
import torch
import torch.nn as nn
class PromptEncoder(torch.nn.Module):
def __init__(self, template, hidden_size, tokenizer, args):
super().__init__()
self.spell_length = sum(template)
self.hidden_size = hidden_size
self.tokenizer = tokenizer
self.args = args
# ent embedding
self.cloze_length = template
self.cloze_mask = [
[1] * self.cloze_length[0] # first cloze
+ [1] * self.cloze_length[1] # second cloze
]
self.cloze_mask = torch.LongTensor(self.cloze_mask).bool().to(args.device)
self.seq_indices = torch.LongTensor(list(range(len(self.cloze_mask[0])))).to(args.device)
# embedding
self.embedding = torch.nn.Embedding(len(self.cloze_mask[0]), self.hidden_size).to(args.device)
# LSTM
self.lstm_head = torch.nn.LSTM(input_size=self.hidden_size,
hidden_size=self.hidden_size // 2,
num_layers=2,
dropout=self.args.lstm_dropout,
bidirectional=True,
batch_first=True)
self.mlp_head = nn.Sequential(nn.Linear(self.hidden_size, self.hidden_size),
nn.ReLU(),
nn.Linear(self.hidden_size, self.hidden_size))
print("init prompt encoder...")
def forward(self):
input_embeds = self.embedding(self.seq_indices).unsqueeze(0)
output_embeds = self.mlp_head(self.lstm_head(input_embeds)[0]).squeeze()
return output_embeds