-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkoikoinet2L.py
88 lines (69 loc) · 2.53 KB
/
koikoinet2L.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
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 11 10:34:35 2021
@author: shguan3
"""
import torch # 1.8.1
import torch.nn as nn
import torch.nn.functional as F
# length: 48(discard/pick), 50(koikoi)
# origin input size: (BATCH_SIZE, nFeature, length)
# conv1d input size: (BATCH_SIZE, nFeature, length)
# multiheadattention input size: (length, BATCH_SIZE, nFeature)
#%%
NetParameter = {
'nInput':300,
'nEmb':256,
'nFw':512,
'nAttnHead':4,
'nLayer':2}
class KoiKoiEncoderBlock(nn.Module):
def __init__(self, nInput, nEmb, nFw, nAttnHead, nLayer):
super(KoiKoiEncoderBlock,self).__init__()
self.f1 = nn.Conv1d(nInput, nFw, 1)
self.f2 = nn.Conv1d(nFw, nEmb, 1)
attn_layer = nn.TransformerEncoderLayer(nEmb, nAttnHead, nFw)
self.attn_encoder = nn.TransformerEncoder(attn_layer, nLayer)
def forward(self,x):
x = self.f2(F.relu(self.f1(x)))
x = F.layer_norm(x,[x.size(-1)])
x = x.permute(2,0,1)
x = self.attn_encoder(x)
x = x.permute(1,2,0)
return x
class DiscardModel(nn.Module):
def __init__(self):
super(DiscardModel,self).__init__()
self.encoder_block = KoiKoiEncoderBlock(**NetParameter)
self.out = nn.Conv1d(NetParameter['nEmb'], 1, 1)
def forward(self,x):
x = self.encoder_block(x)
x = self.out(x).squeeze(1)
return x
class PickModel(nn.Module):
def __init__(self):
super(PickModel,self).__init__()
self.encoder_block = KoiKoiEncoderBlock(**NetParameter)
self.out = nn.Conv1d(NetParameter['nEmb'], 1, 1)
def forward(self,x):
x = self.encoder_block(x)
x = self.out(x).squeeze(1)
return x
class KoiKoiModel(nn.Module):
def __init__(self):
super(KoiKoiModel,self).__init__()
self.encoder_block = KoiKoiEncoderBlock(**NetParameter)
self.out = nn.Conv1d(NetParameter['nEmb'], 1, 1)
def forward(self,x):
x = self.encoder_block(x)
x = self.out(x[:,:,[0,1]]).squeeze(1)
return x
class TargetQNet(nn.Module):
def __init__(self):
super(TargetQNet,self).__init__()
self.encoder_block = KoiKoiEncoderBlock(**NetParameter)
self.out = nn.Conv1d(NetParameter['nEmb'], 1, 1)
def forward(self,x):
x = self.encoder_block(x)
x = self.out(x[:,:,0].unsqueeze(2)).squeeze(1)
return x