-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder.py
130 lines (110 loc) · 3.96 KB
/
encoder.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
import torch.optim as optim
import matplotlib.pyplot as plt
import numpy as np
import math, copy
import random
import torch
import torch, gc
import torch.nn as nn
import torch.nn.functional as F
class Encoder(nn.Module):
def __init__(self, in_channel=4, embed_dim=256):
super(Encoder, self).__init__()
#self.conv1 = nn.Conv1d(in_channel, embed_dim//2, kernel_size=9, stride=4, padding=2)
#self.relu = nn.GELU()
#self.conv2 = nn.Conv1d(embed_dim//2, embed_dim, kernel_size=4, stride=2, padding=1)
self.conv01 = nn.Conv1d(in_channel, in_channel, kernel_size=5, stride=1, padding=2)
self.relu = nn.GELU()
self.conv02 = nn.Conv1d(in_channel, in_channel, kernel_size=3, stride=1, padding=1)
self.conv03 = nn.Conv1d(in_channel, in_channel, kernel_size=1, stride=1, padding=0)
self.conv = nn.Sequential(
nn.Conv1d(in_channel, embed_dim//4, kernel_size=4, stride=2, padding=1),
nn.GELU(),
nn.Conv1d(embed_dim//4, embed_dim//2, kernel_size=4, stride=2, padding=1),
nn.GELU(),
nn.Conv1d(embed_dim//2, embed_dim, kernel_size=4, stride=2, padding=1),
)
def forward(self, x):
#x = x + self.relu(self.conv01(x))
#x = x + self.relu(self.conv02(x))
#x = x + self.relu(self.conv03(x))
x = self.conv(x)
#x = self.conv1(x)
#x = self.relu(x)
#x = self.conv2(x)
return x.transpose(-1,-2).contiguous()
class Decoder(nn.Module):
def __init__(self, embed_dim=256, out_channel=4):
super(Decoder, self).__init__()
self.conv1 = nn.ConvTranspose1d(embed_dim, embed_dim//2, kernel_size=4, stride=2, padding=1)
self.relu = nn.GELU()
self.conv2 = nn.ConvTranspose1d(embed_dim//2, out_channel, kernel_size=9, stride=4, padding=2)
def forward(self, x):
x = x.transpose(-1, -2).contiguous()
x = self.conv1(x)
x = self.relu(x)
x = self.conv2(x)
return x
class Classifier(nn.Module):
def __init__(self, emb_dim=256, classes=3, Len=31):
super(Classifier, self).__init__() #input:[bs, len, emb]
self.emb_dim = emb_dim
self.len = Len
self.mlp1 = nn.Sequential(
nn.Linear(emb_dim, emb_dim//2),
nn.ReLU(),
nn.Linear(emb_dim//2, classes),
)
def forward(self, x):
x = self.mlp1(x)
return F.softmax(x, dim=-1)
class Classifier1(nn.Module):
def __init__(self, emb_dim=256, classes=3, Len=31):
super(Classifier1, self).__init__() #input:[bs, len, emb]
self.emb_dim = emb_dim
self.len = Len
self.mlp1 = nn.Sequential(
nn.Linear(emb_dim, emb_dim//2),
nn.ReLU(),
nn.Linear(emb_dim//2, 1),
)
self.mlp2 = nn.Sequential(
nn.Linear(Len, Len//2),
nn.ReLU(),
nn.Linear(Len//2, classes),
)
def forward(self, x):
x = x.reshape(-1, self.emb_dim)
x = self.mlp1(x)
x = x.reshape(-1, self.len)
x = self.mlp2(x)
return F.softmax(x, dim=-1)
class Classifier2(nn.Module):
def __init__(self, emb_dim=256, classes=3, Len=31):
super(Classifier2, self).__init__() #input:[bs, len, emb]
self.emb_dim = emb_dim
self.len = Len
self.mlp1 = nn.Sequential(
nn.Linear(emb_dim, emb_dim//2),
nn.ReLU(),
nn.Linear(emb_dim//2, classes),
)
self.mlp2 = nn.Sequential(
nn.Linear(Len, Len//2),
nn.ReLU(),
nn.Linear(Len//2, 1),
)
def forward(self, x):
x = x.reshape(-1, self.len)
x = self.mlp2(x)
x = x.reshape(-1, self.emb_dim)
x = self.mlp1(x)
return F.softmax(x, dim=-1)
if __name__ == "__main__":
x = torch.randn(16, 4, 250)
model = Encoder(4, 256)
x = model(x)
print(x.shape)
#model = Decoder(256, 4)
#x = model(x)
#print(x.shape)