-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
25 lines (23 loc) · 883 Bytes
/
model.py
File metadata and controls
25 lines (23 loc) · 883 Bytes
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
import torch
from torch import nn
class CNNTransformer(nn.Module):
def __init__(self, input_size=257, num_classes=1):
super(CNNTransformer, self).__init__()
self.cnn = nn.Sequential(
nn.Conv1d(input_size, 128, kernel_size=3, padding=1),
nn.ReLU(),
nn.MaxPool1d(kernel_size=2),
nn.Conv1d(128, 32, kernel_size=3, padding=1),
nn.ReLU(),
nn.MaxPool1d(kernel_size=2)
)
self.transformer_layer = nn.TransformerEncoderLayer(d_model=32, nhead=2)
self.transformer = nn.TransformerEncoder(self.transformer_layer, num_layers=1)
self.fc = nn.Linear(32, num_classes)
def forward(self, x):
x = self.cnn(x)
x = x.permute(2, 0, 1)
x = self.transformer(x)
x = x.mean(dim=0)
x = self.fc(x)
return x