-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrn_model.py
159 lines (126 loc) · 4.92 KB
/
rn_model.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
147
148
149
150
151
152
153
154
155
156
157
158
159
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
def conv_out_n(inputs, kernel_size, padding, stride, dilation=1):
'''
Compute the output size of a convolution or pool torch function.
For convolution, stride should default to 1, for pooling, to kernel_size
'''
outs = inputs + 2*padding - dilation*(kernel_size-1) - 1
outs = outs/stride + 1
outs = math.floor(outs)
return int(outs)
def k_conv_out_n(k, inputs, kernel_size, pool_kernel_size, padding):
'''
Compute the output size of a series of convolution (and optionally pooling) layers).
'''
n = 0
out = inputs
for i in range(k):
out = conv_out_n(out, kernel_size, padding, 1)
if pool_kernel_size > 0:
out = conv_out_n(out, pool_kernel_size, 0, pool_kernel_size)
return out
def params_count(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
class BrainConv(nn.Module):
def __init__(self, ch_in, ch_out, kernel_size, div=0):
super().__init__()
self.kernel_size = kernel_size
self.div = div
if self.div <= 0:
self.div = self.kernel_size-1
self.pad = (self.div)//2
self.ch_in = ch_in
self.ch_out = ch_out
self.conv = nn.Conv1d(self.ch_in, self.ch_out, kernel_size=self.kernel_size, padding=self.pad)
self.batch_norm = nn.BatchNorm1d(num_features=self.ch_out)
def forward(self, x):
y = F.max_pool1d(torch.relu(self.batch_norm(self.conv(x))), self.div)
return y
def output_n(self, input_n):
n = k_conv_out_n(1, input_n, self.kernel_size, self.div, self.pad)
return (n, self.ch_out)
class BrainConvSkip(BrainConv):
def forward(self, x):
y = F.max_pool1d(torch.relu(self.batch_norm(self.conv(x))) + x, self.div)
return y
class BrainLine(nn.Module):
def __init__(self, inputs, outputs):
super().__init__()
self.line = nn.Linear(inputs, outputs)
def forward(self, x):
y = torch.tanh(self.line(x))
return y
class ResidualUnit(nn.Module):
def __init__(self, chs, kernel_size):
super().__init__()
self.kernel_size = kernel_size
self.pad = (self.kernel_size-1)//2
self.chs = chs
self.conv1 = nn.Conv1d(self.chs, self.chs, kernel_size=self.kernel_size, padding=self.pad)
self.conv2 = nn.Conv1d(self.chs, self.chs, kernel_size=self.kernel_size, padding=self.pad)
nn.init.kaiming_normal_(self.conv1.weight, mode='fan_out', nonlinearity='relu')
nn.init.kaiming_normal_(self.conv2.weight, mode='fan_out', nonlinearity='relu')
def forward(self, x):
z = x
x = torch.relu(self.conv1(x))
x = torch.relu(self.conv2(x) + z)
return x
def output_n(self, input_n):
n = conv_out_n(input_n, self.kernel_size, self.pad, 1)
n = conv_out_n(n, self.kernel_size, self.pad, 1)
return (n, self.chs)
class ResidualStack(nn.Module):
def __init__(self, ch_in, ch_out, kernel_size, div=0):
super().__init__()
self.kernel_size = kernel_size
self.div = div
if self.div <= 0:
self.div = self.kernel_size-1
self.pad = (self.div)//2
self.ch_in = ch_in
self.ch_out = ch_out
self.conv = nn.Conv1d(self.ch_in, self.ch_out, kernel_size=1)
self.res1 = ResidualUnit(self.ch_out, self.kernel_size)
self.res2 = ResidualUnit(self.ch_out, self.kernel_size)
def forward(self, x):
x = self.conv(x)
x = self.res1(x)
x = self.res2(x)
x = F.max_pool1d(torch.relu(x), self.div)
return x
def output_n(self, n):
n = self.res1.output_n(n)[0]
n = self.res2.output_n(n)[0]
n = conv_out_n(n, self.div, 0, self.div)
return (n, self.ch_out)
class CharmBrain(nn.Module):
def __init__(self, chunk_size=20000):
super().__init__()
chs = 4 # convolution output channels
self.conv_layers = nn.ModuleList()
self.line_layers = nn.ModuleList()
self.conv_layers.append(ResidualStack(2, chs, 3, 2))
for _ in range(3):
self.conv_layers.append(ResidualStack(chs, chs, 5, 5))
self.conv_layers.append(ResidualStack(chs, chs, 3, 2))
self.ll1_n = chunk_size
for c in self.conv_layers:
self.ll1_n = c.output_n(self.ll1_n)[0]
self.ll1_n *= chs
self.ll2_n = 16
self.ll3_n = 16
self.line_layers.append(BrainLine(self.ll1_n, self.ll2_n))
self.line_layers.append(BrainLine(self.ll2_n, self.ll3_n))
self.line_layers.append(BrainLine(self.ll3_n, 3))
#print(f"Inner nodes: {self.ll1_n}")
#print(f"Parameters: {params_count(self)}")
def forward(self, x):
for layer in self.conv_layers:
x = layer(x)
x = x.view(-1, self.ll1_n)
for layer in self.line_layers:
x = layer(x)
return x