forked from hsd1503/resnet1d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
acnn1d.py
116 lines (94 loc) · 3.5 KB
/
acnn1d.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
"""
cnn + self-attention for 1-d signal data, pytorch version
Shenda Hong, Jan 2020
"""
import numpy as np
from collections import Counter
from tqdm import tqdm
from matplotlib import pyplot as plt
from sklearn.metrics import classification_report
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.utils.data import Dataset, DataLoader
class MyDataset(Dataset):
def __init__(self, data, label):
self.data = data
self.label = label
def __getitem__(self, index):
return (torch.tensor(self.data[index], dtype=torch.float), torch.tensor(self.label[index], dtype=torch.long))
def __len__(self):
return len(self.data)
class ACNN(nn.Module):
"""
Input:
X: (n_samples, n_channel, n_length)
Y: (n_samples)
Output:
out: (n_samples)
Pararmetes:
n_classes: number of classes
"""
def __init__(self, in_channels, out_channels, att_channels, n_len_seg, n_classes, device, verbose=False):
super(ACNN, self).__init__()
self.n_len_seg = n_len_seg
self.n_classes = n_classes
self.in_channels = in_channels
self.out_channels = out_channels
self.att_channels = att_channels
self.device = device
self.verbose = verbose
# (batch, channels, length)
self.cnn = nn.Conv1d(in_channels=self.in_channels,
out_channels=self.out_channels,
kernel_size=16,
stride=4)
self.W_att_channel = nn.Parameter(torch.randn(self.out_channels, self.att_channels))
self.v_att_channel = nn.Parameter(torch.randn(self.att_channels, 1))
self.dense = nn.Linear(out_channels, n_classes)
def forward(self, x):
self.n_channel, self.n_length = x.shape[-2], x.shape[-1]
assert (self.n_length % self.n_len_seg == 0), "Input n_length should divided by n_len_seg"
self.n_seg = self.n_length // self.n_len_seg
out = x
if self.verbose:
print(out.shape)
# (n_samples, n_channel, n_length) -> (n_samples, n_length, n_channel)
out = out.permute(0,2,1)
if self.verbose:
print(out.shape)
# (n_samples, n_length, n_channel) -> (n_samples*n_seg, n_len_seg, n_channel)
out = out.view(-1, self.n_len_seg, self.n_channel)
if self.verbose:
print(out.shape)
# (n_samples*n_seg, n_len_seg, n_channel) -> (n_samples*n_seg, n_channel, n_len_seg)
out = out.permute(0,2,1)
if self.verbose:
print(out.shape)
# cnn
out = self.cnn(out)
if self.verbose:
print(out.shape)
# global avg, (n_samples*n_seg, out_channels)
out = out.mean(-1)
if self.verbose:
print(out.shape)
# global avg, (n_samples, n_seg, out_channels)
out = out.view(-1, self.n_seg, self.out_channels)
if self.verbose:
print(out.shape)
# self attention
e = torch.matmul(out, self.W_att_channel)
e = torch.matmul(torch.tanh(e), self.v_att_channel)
n1 = torch.exp(e)
n2 = torch.sum(torch.exp(e), 1, keepdim=True)
gama = torch.div(n1, n2)
out = torch.sum(torch.mul(gama, out), 1)
if self.verbose:
print(out.shape)
# dense
out = self.dense(out)
if self.verbose:
print(out.shape)
return out