-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaki.py
More file actions
39 lines (31 loc) · 1.11 KB
/
aki.py
File metadata and controls
39 lines (31 loc) · 1.11 KB
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
from torch import nn, Tensor
from huggingface_hub import PyTorchModelHubMixin
from typing import Optional
class AKIClassifier(nn.Module, PyTorchModelHubMixin):
def __init__(self, input_size: int, n_classes: int):
super(AKIClassifier, self).__init__()
is_binary = n_classes <= 2
layers = [
self.nn_block(input_size, 256),
self.nn_block(256),
self.nn_block(256),
self.nn_block(256),
self.nn_block(256),
self.nn_block(256),
self.nn_block(256),
]
if is_binary:
layers.extend((nn.Linear(256, 1), nn.Sigmoid(), nn.Flatten(0, -1)))
else:
layers.extend((nn.Linear(256, n_classes), nn.Softmax(dim=-1)))
self.layers = nn.Sequential(*layers)
def nn_block(
self, in_c: int, out_c: Optional[int] = None, dropout: float = 0.2
) -> nn.Module:
return nn.Sequential(
nn.Linear(in_c, out_c or in_c),
nn.ReLU(),
nn.Dropout(dropout),
)
def forward(self, x: Tensor) -> Tensor:
return self.layers(x)