-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcls_models.py
executable file
·52 lines (42 loc) · 1.62 KB
/
cls_models.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
import torch
import torch.nn as nn
import numpy as np
from numpy import linalg as LA
class ClsModel(nn.Module):
def __init__(self, num_classes=4):
super(ClsModel, self).__init__()
self.fc1 = nn.Linear(in_features=1024, out_features=num_classes, bias=True)
self.lsm = nn.LogSoftmax(dim=1)
def forward(self, feats=None, classifier_only=False):
x = self.fc1(feats)
x = self.lsm(x)
return x
class ClsModelTrain(nn.Module):
def __init__(self, num_classes=4):
super(ClsModelTrain, self).__init__()
self.fc1 = nn.Linear(in_features=1024, out_features=num_classes, bias=True)
def forward(self, feats=None, classifier_only=False):
x = self.fc1(feats)
return x
class ClsUnseen(torch.nn.Module):
def __init__(self, att):
super(ClsUnseen, self).__init__()
self.W = att.type(torch.float).cuda()
self.fc1 = nn.Linear(in_features=1024, out_features=300, bias=True)
self.lsm = nn.LogSoftmax(dim=1)
print(f"__init__ {self.W.shape}")
def forward(self, feats=None, classifier_only=False):
f = self.fc1(feats)
x = f.mm(self.W.transpose(1,0))
x = self.lsm(x)
return x
class ClsUnseenTrain(torch.nn.Module):
def __init__(self, att):
super(ClsUnseenTrain, self).__init__()
self.W = att.type(torch.float).cuda()
self.fc1 = nn.Linear(in_features=1024, out_features=300, bias=True)
print(f"__init__ {self.W.shape}")
def forward(self, feats=None, classifier_only=False):
f = self.fc1(feats)
x = f.mm(self.W.transpose(1,0))
return x