-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathRN.py
76 lines (56 loc) · 2.13 KB
/
RN.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import torch.nn as nn
import torch.nn.functional as F
import torch
class RelationNetwork(nn.Module):
def __init__(self, input_size, hidden_size):
super(RelationNetwork, self).__init__()
# self.cnn = cnn
# self.wordembedding = wordembedding
# for p in self.cnn.parameters():
# p.requires_grad = False
# for p in self.wordembedding.parameters():
# p.requires_grad = False
self.fc1 = nn.Linear(input_size, hidden_size)
self.fc2 = nn.Linear(hidden_size, 1)
def forward(self, visual_emb, semantic_emb):
# self.cnn.eval()
# self.wordembedding.eval()
# visual_emb, _ = self.cnn(image)
# batch_size = image.shape[0]
# semantic_emb = self.wordembedding(wd)
# class_num = semantic_emb.shape[0]
# visual_emb = visual_emb.unsqueeze(0).repeat(class_num, 1, 1)
# semantic_emb = semantic_emb.unsqueeze(0).repeat(batch_size, 1, 1)
# semantic_emb = torch.transpose(visual_emb, 0, 1)
dim = semantic_emb.shape[2]
x = torch.cat((visual_emb, semantic_emb), 2).view(-1,dim * 2)
x = F.relu(self.fc1(x))
x = F.sigmoid(self.fc2(x))
return x
class AttributeNetwork(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super().__init__()
self.word_emb_transformer = nn.Sequential(
nn.Linear(input_size, hidden_size),
nn.ReLU(),
nn.Linear(hidden_size, output_size),
nn.ReLU()
)
def forward(self, word_embeddings):
semantic_emb = self.word_emb_transformer(word_embeddings)
return semantic_emb
# class AttributeNetwork(nn.Module):
#
# def __init__(self, input_size, output_size):
# super().__init__()
# self.word_emb_transformer = nn.Sequential(
# nn.Linear(input_size, output_size),
# nn.ReLU(),
# # nn.Linear(hidden_size, output_size),
# # nn.ReLU()
# )
#
# def forward(self, word_embeddings):
# semantic_emb = self.word_emb_transformer(word_embeddings)
# return semantic_emb
#