-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
73 lines (62 loc) · 2.9 KB
/
Copy pathmodel.py
File metadata and controls
73 lines (62 loc) · 2.9 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from module import Encoder, Head
class LSFL(nn.Module):
def __init__(self, emb_init, args):
super(LSFL, self).__init__()
self.emb = Embedding(emb_size=args.emb_size, emb_init=emb_init, emb_trainable=args.emb_trainable)
self.extractor = Extractor(args)
self.clf = Classifier(args)
def forward(self, input_id):
emb_out, lengths, masks = self.emb(input_id)
representation = self.extractor(emb_out, lengths, masks)
logit = self.clf(representation)
return logit
class Embedding(nn.Module):
def __init__(self, vocab_size=None, emb_size=None, emb_init=None, emb_trainable=True, padding_idx=0, dropout=0.2):
super(Embedding, self).__init__()
if emb_init is not None:
if vocab_size is not None:
assert vocab_size == emb_init.shape[0]
if emb_size is not None:
assert emb_size == emb_init.shape[1]
vocab_size, emb_size = emb_init.shape
self.emb = nn.Embedding(vocab_size, emb_size, padding_idx=padding_idx, sparse=True,
_weight=torch.from_numpy(emb_init).float() if emb_init is not None else None)
self.emb.weight.requires_grad = emb_trainable
self.dropout = nn.Dropout(dropout)
self.padding_idx = padding_idx
def forward(self, inputs):
emb_out = self.dropout(self.emb(inputs))
lengths, masks = (inputs != self.padding_idx).sum(dim=-1), inputs != self.padding_idx
return emb_out[:, :lengths.max()], lengths, masks[:, :lengths.max()]
class Extractor(nn.Module):
def __init__(self, args):
super(Extractor,self).__init__()
self.encoder = Encoder(args)
self.head = Head(args)
def forward(self, inputs, lengths, masks):
representation = self.encoder(inputs, lengths, masks)
representation = self.head(representation)
return representation
class Classifier(nn.Module):
def __init__(self, args):
super(Classifier, self).__init__()
self.classifier_mode = args.classifier_mode
if self.classifier_mode=="type1":
self.output_layer = torch.nn.Linear(args.feat_size, 1, bias=False)
nn.init.xavier_uniform_(self.output_layer.weight)
else:
self.output_layer = torch.nn.Parameter(torch.randn(args.label_size, args.feat_size))
nn.init.xavier_uniform_(self.output_layer)
def forward(self, representation): #N*L*D
if self.classifier_mode=="type1":
return torch.squeeze(self.output_layer(representation),-1)
else:
return torch.sum(representation * self.output_layer, -1)
def bce_loss(y_pred, y_true):
criteria = nn.BCEWithLogitsLoss()
loss = criteria(y_pred, y_true)
return loss