-
Notifications
You must be signed in to change notification settings - Fork 0
/
gated_conv.py
146 lines (117 loc) · 5.18 KB
/
gated_conv.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import sys
sys.path.append('./trainer')
import six
import chainer
import functools
import numpy as np
import nutszebra_chainer
import chainer.links as L
import chainer.functions as F
class DoNothing(object):
def __call__(self, x):
return x
class Conv_For_GLU(nutszebra_chainer.Model):
def __init__(self, in_channel, out_channel, timestep=3):
super(Conv_For_GLU, self).__init__(
conv=L.Convolution2D(1, out_channel, (in_channel, timestep), 1, 0),
)
self.pad = timestep - 1
def count_parameters(self):
return functools.reduce(lambda a, b: a * b, self.conv.W.data.shape)
def weight_initialization(self):
self.conv.W.data = self.weight_relu_initialization(self.conv)
self.conv.b.data = self.bias_initialization(self.conv, constant=0)
@staticmethod
def add_zero_pad(x, pad, axis, front=True, dtype=np.float32):
if pad < 1:
return x
sizes = list(x.data.shape)
sizes[axis] = pad
pad_mat = chainer.Variable(np.zeros(sizes, dtype=dtype), volatile=x.volatile)
if not type(x.data) == np.ndarray:
pad_mat.to_gpu()
if front:
return F.concat((pad_mat, x), axis=axis)
else:
return F.concat((x, pad_mat), axis=axis)
def __call__(self, x, train=False):
# x: batch, 1, in_channel, input_length
return self.conv(self.add_zero_pad(x, self.pad, 3))
class Gated_Unit(nutszebra_chainer.Model):
def __init__(self, in_channel, out_channel, timestep=2, activation=DoNothing()):
super(Gated_Unit, self).__init__()
modules = []
modules += [('conv', Conv_For_GLU(in_channel, out_channel, timestep))]
modules += [('conv_f', Conv_For_GLU(in_channel, out_channel, timestep))]
# register layers
[self.add_link(*link) for link in modules]
self.modules = modules
self.pad = timestep - 1
self.activation = activation
def weight_initialization(self):
[link.weight_initialization() for _, link in self.modules]
def count_parameters(self):
return int(np.sum([link.count_parameters() for _, link in self.modules]))
def __call__(self, x, train=False):
# x: batch, 1, in_channel, input_length
A = self.activation(self.conv(x, train))
B = F.sigmoid(self.conv_f(x, train))
h = A * B
batch, out_channel, _, input_length = h.shape
return F.reshape(h, (batch, 1, out_channel, input_length))
class ResBlock(nutszebra_chainer.Model):
def __init__(self, in_channel, out_channel, timestep=2, block_num=2):
super(ResBlock, self).__init__()
modules = []
for i in six.moves.range(block_num):
modules += [('conv{}'.format(i), Gated_Unit(in_channel, out_channel, timestep))]
in_channel = out_channel
# register layers
[self.add_link(*link) for link in modules]
self.modules = modules
self.block_num = block_num
def weight_initialization(self):
[link.weight_initialization() for _, link in self.modules]
def count_parameters(self):
return int(np.sum([link.count_parameters() for _, link in self.modules]))
def __call__(self, x, train=False):
h = x
for i in six.moves.range(self.block_num):
h = self['conv{}'.format(i)](h, train)
diff_channel = h.data.shape[2] - x.data.shape[2]
return h + Conv_For_GLU.add_zero_pad(x, diff_channel, 2)
class Gated_Convolutional_Network(nutszebra_chainer.Model):
def __init__(self, embed_dimension, category_num):
super(Gated_Convolutional_Network, self).__init__()
modules = []
# register layers
[self.add_link(*link) for link in modules]
modules += [('resblock_1', ResBlock(embed_dimension, 16, 4))]
modules += [('resblock_2', ResBlock(16, 16, 4))]
modules += [('resblock_3', ResBlock(16, 16, 4))]
modules += [('resblock_4', ResBlock(16, 16, 4))]
modules += [('resblock_5', ResBlock(16, 16, 4))]
modules += [('resblock_6', ResBlock(16, 16, 4))]
modules += [('resblock_7', ResBlock(16, 16, 4))]
modules += [('resblock_8', ResBlock(16, 16, 4))]
modules += [('resblock_9', ResBlock(16, 16, 4))]
modules += [('resblock_10', ResBlock(16, 16, 4))]
modules += [('conv', Conv_For_GLU(16, category_num, 4))]
# register layers
[self.add_link(*link) for link in modules]
self.modules = modules
self.embed_dimension = embed_dimension
self.category_num = category_num
self.name = 'Gated_Convolutional_Network_{}_{}'.format(embed_dimension, category_num)
def weight_initialization(self):
[link.weight_initialization() for _, link in self.modules]
def count_parameters(self):
return int(np.sum([link.count_parameters() for _, link in self.modules]))
def __call__(self, x, train=True):
for i in six.moves.range(1, 10 + 1):
x = self['resblock_{}'.format(i)](x, train)
batch = x.data.shape[0]
return F.reshape(self.conv(x), (batch, self.category_num, -1))
def calc_loss(self, y, t):
loss = F.softmax_cross_entropy(y, t)
return loss