sharonn18 commited on
Commit
bcf1c6b
·
verified ·
1 Parent(s): c769b17

Upload tgcn_model.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. tgcn_model.py +140 -0
tgcn_model.py ADDED
@@ -0,0 +1,140 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+ from __future__ import absolute_import
4
+ from __future__ import print_function
5
+
6
+ import math
7
+
8
+ import torch
9
+ import torch.nn as nn
10
+ from torch.nn.parameter import Parameter
11
+
12
+ import numpy as np
13
+
14
+
15
+ class GraphConvolution_att(nn.Module):
16
+ """
17
+ Simple GCN layer, similar to https://arxiv.org/abs/1609.02907
18
+ """
19
+
20
+ def __init__(self, in_features, out_features, bias=True, init_A=0):
21
+ super(GraphConvolution_att, self).__init__()
22
+ self.in_features = in_features
23
+ self.out_features = out_features
24
+ self.weight = Parameter(torch.FloatTensor(in_features, out_features))
25
+ self.att = Parameter(torch.FloatTensor(55, 55))
26
+ if bias:
27
+ self.bias = Parameter(torch.FloatTensor(out_features))
28
+ else:
29
+ self.register_parameter('bias', None)
30
+ self.reset_parameters()
31
+
32
+ def reset_parameters(self):
33
+ stdv = 1. / math.sqrt(self.weight.size(1))
34
+ self.weight.data.uniform_(-stdv, stdv)
35
+ self.att.data.uniform_(-stdv, stdv)
36
+ if self.bias is not None:
37
+ self.bias.data.uniform_(-stdv, stdv)
38
+
39
+ def forward(self, input):
40
+ # AHW
41
+ support = torch.matmul(input, self.weight) # HW
42
+ output = torch.matmul(self.att, support) # g
43
+ if self.bias is not None:
44
+ return output + self.bias
45
+ else:
46
+ return output
47
+
48
+ def __repr__(self):
49
+ return self.__class__.__name__ + ' (' \
50
+ + str(self.in_features) + ' -> ' \
51
+ + str(self.out_features) + ')'
52
+
53
+
54
+ class GC_Block(nn.Module):
55
+
56
+ def __init__(self, in_features, p_dropout, bias=True, is_resi=True):
57
+ super(GC_Block, self).__init__()
58
+ self.in_features = in_features
59
+ self.out_features = in_features
60
+ self.is_resi = is_resi
61
+
62
+ self.gc1 = GraphConvolution_att(in_features, in_features)
63
+ self.bn1 = nn.BatchNorm1d(55 * in_features)
64
+
65
+ self.gc2 = GraphConvolution_att(in_features, in_features)
66
+ self.bn2 = nn.BatchNorm1d(55 * in_features)
67
+
68
+ self.do = nn.Dropout(p_dropout)
69
+ self.act_f = nn.Tanh()
70
+
71
+ def forward(self, x):
72
+ y = self.gc1(x)
73
+ b, n, f = y.shape
74
+ y = self.bn1(y.view(b, -1)).view(b, n, f)
75
+ y = self.act_f(y)
76
+ y = self.do(y)
77
+
78
+ y = self.gc2(y)
79
+ b, n, f = y.shape
80
+ y = self.bn2(y.view(b, -1)).view(b, n, f)
81
+ y = self.act_f(y)
82
+ y = self.do(y)
83
+ if self.is_resi:
84
+ return y + x
85
+ else:
86
+ return y
87
+
88
+ def __repr__(self):
89
+ return self.__class__.__name__ + ' (' \
90
+ + str(self.in_features) + ' -> ' \
91
+ + str(self.out_features) + ')'
92
+
93
+
94
+ class GCN_muti_att(nn.Module):
95
+ def __init__(self, input_feature, hidden_feature, num_class, p_dropout, num_stage=1, is_resi=True):
96
+ super(GCN_muti_att, self).__init__()
97
+ self.num_stage = num_stage
98
+
99
+ self.gc1 = GraphConvolution_att(input_feature, hidden_feature)
100
+ self.bn1 = nn.BatchNorm1d(55 * hidden_feature)
101
+
102
+ self.gcbs = []
103
+ for i in range(num_stage):
104
+ self.gcbs.append(GC_Block(hidden_feature, p_dropout=p_dropout, is_resi=is_resi))
105
+
106
+ self.gcbs = nn.ModuleList(self.gcbs)
107
+
108
+ # self.gc7 = GraphConvolution_att(hidden_feature, output_feature)
109
+
110
+ self.do = nn.Dropout(p_dropout)
111
+ self.act_f = nn.Tanh()
112
+
113
+ # self.fc1 = nn.Linear(55 * output_feature, fc1_out)
114
+ self.fc_out = nn.Linear(hidden_feature, num_class)
115
+
116
+ def forward(self, x):
117
+ y = self.gc1(x)
118
+ b, n, f = y.shape
119
+ y = self.bn1(y.view(b, -1)).view(b, n, f)
120
+ y = self.act_f(y)
121
+ y = self.do(y)
122
+
123
+ for i in range(self.num_stage):
124
+ y = self.gcbs[i](y)
125
+
126
+ # y = self.gc7(y)
127
+ out = torch.mean(y, dim=1)
128
+ out = self.fc_out(out)
129
+
130
+ return out
131
+
132
+
133
+ if __name__ == '__main__':
134
+ num_samples = 32
135
+
136
+ model = GCN_muti_att(input_feature=num_samples*2, hidden_feature=256,
137
+ num_class=100, p_dropout=0.3, num_stage=2)
138
+ x = torch.ones([2, 55, num_samples*2])
139
+ print(model(x).size())
140
+