generated from XpressAI/xai-component-library-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtorch_nn_layers.py
360 lines (279 loc) · 11.3 KB
/
torch_nn_layers.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
from xai_components.base import InArg, InCompArg, OutArg, Component, xai_component
import os
import sys
import torch
from torch import nn
@xai_component
class TorchModel(Component):
"""Creates a custom Torch Model config.
##### outPorts:
- model_in: List of layers to make into Sequential Model
- model_config: resulting model.
- loss_fn: nn.CrossEntropyLoss()
- optimizer: torch.optim.SGD(model.parameters(), lr=1e-3)
"""
model_in: InArg[list]
loss_in: InArg[str]
learning_rate: InArg[float]
optimizer_in: InArg[str]
should_flatten: InArg[bool]
model_config: OutArg[nn.Module]
loss_fn: OutArg[any]
optimizer: OutArg[any]
def __init__(self):
super().__init__()
self.learning_rate.value = 1e-3
self.should_flatten.value = False
def execute(self,ctx) -> None:
should_flatten = self.should_flatten.value
stack = self.model_in.value
# Define model
class NeuralNetwork(nn.Module):
def __init__(self):
super(NeuralNetwork, self).__init__()
if should_flatten:
self.flatten = nn.Flatten()
self.stack = nn.Sequential(*stack)
def forward(self, x):
if should_flatten:
x = self.flatten(x)
logits = self.stack(x)
return logits
# Get cpu or gpu device for training.
device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Using {device} device")
model = NeuralNetwork().to(device)
print(model)
loss_fn = nn.MSELoss()
if self.loss_in.value == 'CrossEntropyLoss':
loss_fn = nn.CrossEntropyLoss()
elif self.loss_in.value == 'L1Loss':
loss_fn = nn.L1Loss()
elif self.loss_in.value == 'CTCLoss':
loss_fn = nn.CTCLoss()
else:
loss_fn = eval(self.loss_in.value)
optimizer = torch.optim.SGD(model.parameters(), lr=self.learning_rate.value)
if self.optimizer_in.value == 'Adam':
optimizer = torch.optim.Adam(model.parameters(), lr=self.learning_rate.value)
elif self.optimizer_in.value == 'RMSprop':
optimizer = torch.optim.RMSprop(model.parameters(), lr=self.learning_rate.value)
else:
optimizer = eval(self.optimizer_in.value)
self.model_config.value = model
self.loss_fn.value = loss_fn
self.optimizer.value = optimizer
@xai_component
class TorchAddLinearLayer(Component):
"""Adds a LinearLayer to a sequential model."""
model_in: InArg[list]
in_features: InArg[int]
out_features: InArg[int]
bias: InArg[bool]
model_out: OutArg[list]
def execute(self,ctx) -> None:
bias = True if self.bias.value is None else False
in_size = self.in_features.value
out_size = self.out_features.value
if self.model_in.value is None:
self.model_out.value = [nn.Linear(in_size, out_size, bias)]
else:
self.model_out.value = self.model_in.value + [nn.Linear(in_size, out_size, bias)]
@xai_component
class TorchAddConv1DLayer(Component):
"""Adds a Conv1DLayer to a sequential model."""
model_in: InArg[list]
in_channels: InCompArg[int]
out_channels: InCompArg[int]
kernel_size: InCompArg[any] #int or tuple
stride: InArg[any] #int or tuple
padding: InArg[any] #int, tuple or str
dilation: InArg[any] #int or tuple
groups: InArg[int]
bias: InArg[bool]
padding_mode: InArg[str]
model_out: OutArg[list]
def __init__(self):
super().__init__()
self.stride.value = 1
self.padding.value = 0
self.dilation.value = 1
self.groups.value = 1
self.bias.value = True
self.padding_mode.value = 'zeros'
def execute(self,ctx) -> None:
in_channels = self.in_channels.value
out_channels = self.out_channels.value
kernel_size = self.kernel_size.value
stride = self.stride.value
padding = self.padding.value
dilation = self.dilation.value
groups = self.groups.value
bias = self.bias.value
padding_mode = self.padding_mode.value
if self.model_in.value is None:
self.model_out.value = [nn.Conv1d(in_channels, out_channels, kernel_size, stride, padding, dilation, groups, bias, padding_mode)]
else:
self.model_out.value = self.model_in.value + [nn.Conv1d(in_channels, out_channels, kernel_size, stride, padding, dilation, groups, bias, padding_mode)]
@xai_component
class TorchAddConv2DLayer(Component):
"""Adds a Conv2DLayer to a sequential model."""
model_in: InArg[list]
in_channels: InCompArg[int]
out_channels: InCompArg[int]
kernel_size: InCompArg[any] #int or tuple
stride: InArg[any] #int or tuple
padding: InArg[any] #int, tuple or str
dilation: InArg[any] #int or tuple
groups: InArg[int]
bias: InArg[bool]
padding_mode: InArg[str]
model_out: OutArg[list]
def __init__(self):
super().__init__()
self.stride.value = 1
self.padding.value = 0
self.dilation.value = 1
self.groups.value = 1
self.bias.value = True
self.padding_mode.value = 'zeros'
def execute(self,ctx) -> None:
in_channels = self.in_channels.value
out_channels = self.out_channels.value
kernel_size = self.kernel_size.value
stride = self.stride.value
padding = self.padding.value
dilation = self.dilation.value
groups = self.groups.value
bias = self.bias.value
padding_mode = self.padding_mode.value
if self.model_in.value is None:
self.model_out.value = [nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding, dilation, groups, bias, padding_mode)]
else:
self.model_out.value = self.model_in.value + [nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding, dilation, groups, bias, padding_mode)]
@xai_component
class TorchAddTransformerEncoderLayer(Component):
"""Adds a TransformerEncoderLayer to a sequential model."""
model_in: InArg[list]
d_model : InCompArg[int]
nhead : InCompArg[int]
dim_feedforward : InArg[int]
dropout : InArg[float]
activation : InArg[any] #Union[str, Callable[[Tensor], Tensor]]
layer_norm_eps : InArg[float]
batch_first : InArg[bool]
norm_first : InArg[bool]
model_out: OutArg[list]
def __init__(self):
super().__init__()
self.dim_feedforward.value = 2048
self.dropout.value = 0.1
self.activation.value = 'relu'
self.layer_norm_eps.value = 1e-05
self.batch_first.value = False
self.norm_first.value = False
def execute(self,ctx) -> None:
d_model = self.d_model.value
nhead = self.nhead.value
dim_feedforward = self.dim_feedforward.value
dropout = self.dropout.value
activation = self.activation.value
layer_norm_eps = self.layer_norm_eps.value
batch_first = self.batch_first.value
norm_first = self.norm_first.value
if self.model_in.value is None:
self.model_out.value = [nn.TransformerEncoderLayer(d_model, nhead, dim_feedforward, dropout, activation, layer_norm_eps, batch_first, norm_first)]
else:
self.model_out.value = self.model_in.value + [nn.TransformerEncoderLayer(d_model, nhead, dim_feedforward, dropout, activation, layer_norm_eps, batch_first, norm_first)]
@xai_component
class TorchAddTransformerDecoderLayer(Component):
"""Adds a TransformerDecoderLayer to a sequential model."""
model_in: InArg[list]
d_model : InCompArg[int]
nhead : InCompArg[int]
dim_feedforward : InArg[int]
dropout : InArg[float]
activation : InArg[any] #Union[str, Callable[[Tensor], Tensor]]
layer_norm_eps : InArg[float]
batch_first : InArg[bool]
norm_first : InArg[bool]
model_out: OutArg[list]
def __init__(self):
super().__init__()
self.dim_feedforward.value = 2048
self.dropout.value = 0.1
self.activation.value = 'relu'
self.layer_norm_eps.value = 1e-05
self.batch_first.value = False
self.norm_first.value = False
def execute(self,ctx) -> None:
d_model = self.d_model.value
nhead = self.nhead.value
dim_feedforward = self.dim_feedforward.value
dropout = self.dropout.value
activation = self.activation.value
layer_norm_eps = self.layer_norm_eps.value
batch_first = self.batch_first.value
norm_first = self.norm_first.value
if self.model_in.value is None:
self.model_out.value = [nn.TransformerDecoderLayer(d_model, nhead, dim_feedforward, dropout, activation, layer_norm_eps, batch_first, norm_first)]
else:
self.model_out.value = self.model_in.value + [nn.TransformerDecoderLayer(d_model, nhead, dim_feedforward, dropout, activation, layer_norm_eps, batch_first, norm_first)]
@xai_component
class TorchLSTM(Component):
"""Adds a LSTM to a sequential model."""
model_in: InArg[list]
input_size: InCompArg[int]
hidden_size: InCompArg[int]
num_layers: InArg[int]
bias: InArg[bool]
batch_first: InArg[bool]
dropout: InArg[float]
bidirectional: InArg[bool]
proj_size: InArg[int]
model_out: OutArg[list]
def __init__(self):
super().__init__()
self.num_layers.value = 1
self.bias.value = True
self.batch_first.value = False
self.dropout.value = 0
self.bidirectional.value = False
self.proj_size.value = 0
def execute(self,ctx) -> None:
input_size = self.input_size.value
hidden_size = self.hidden_size.value
num_layers = self.num_layers.value
bias = self.bias.value
batch_first = self.batch_first.value
dropout = self.dropout.value
bidirectional = self.bidirectional.value
proj_size = self.proj_size.value
if self.model_in.value is None:
self.model_out.value = [nn.LSTM(input_size, hidden_size, num_layers, bias, batch_first, dropout, bidirectional, proj_size)]
else:
self.model_out.value = self.model_in.value + [nn.LSTM(input_size, hidden_size, num_layers, bias, batch_first, dropout, bidirectional, proj_size)]
@xai_component
class TorchAddReluLayer(Component):
"""Adds a Relu activation to a sequential model."""
model_in: InArg[list]
model_out: OutArg[list]
def execute(self, ctx) -> None:
if self.model_in.value is None:
self.model_out.value = [nn.ReLU()]
else:
self.model_out.value = self.model_in.value + [nn.ReLU()]
@xai_component
class TorchAddDropoutLayer(Component):
"""Adds a Dropout to a sequential model."""
model_in: InArg[list]
prob_zero: InArg[float]
model_out: OutArg[list]
def execute(self, ctx) -> None:
prob = 0.5
if self.prob_zero.value is not None:
prob = self.prob_zero.value
if self.model_in.value is None:
self.model_out.value = [nn.Dropout(prob)]
else:
self.model_out.value = self.model_in.value + [nn.Dropout(prob)]