-
Notifications
You must be signed in to change notification settings - Fork 1
/
module.py
executable file
·136 lines (118 loc) · 4.62 KB
/
module.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
131
132
133
134
135
136
# Copyright 2021 Hirokazu Kameoka
import torch
import torch.nn as nn
def calc_padding(kernel_size, dilation, causal, stride=1):
if causal:
padding = (kernel_size-1)*dilation+1-stride
else:
padding = ((kernel_size-1)*dilation+1-stride)//2
return padding
class LinearWN(torch.nn.Module):
def __init__(self, in_dim, out_dim, bias=True):
super(LinearWN, self).__init__()
self.linear_layer = torch.nn.Linear(in_dim, out_dim, bias=bias)
#nn.init.xavier_normal_(self.linear_layer.weight,gain=0.1)
self.linear_layer = nn.utils.weight_norm(self.linear_layer)
def forward(self, x):
return self.linear_layer(x)
class ConvGLU1D(nn.Module):
def __init__(self, in_ch, out_ch, ks, sd, normtype='IN'):
super(ConvGLU1D, self).__init__()
self.conv1 = nn.Conv1d(
in_ch, out_ch*2, ks, stride=sd, padding=(ks-sd)//2)
nn.init.xavier_normal_(self.conv1.weight,gain=0.1)
if normtype=='BN':
self.norm1 = nn.BatchNorm1d(out_ch*2)
elif normtype=='IN':
self.norm1 = nn.InstanceNorm1d(out_ch*2)
elif normtype=='LN':
self.norm1 = nn.LayerNorm(out_ch*2)
self.conv1 = nn.utils.weight_norm(self.conv1)
self.normtype = normtype
def __call__(self, x):
h = self.conv1(x)
if self.normtype=='BN' or self.normtype=='IN':
h = self.norm1(h)
elif self.normtype=='LN':
B, D, N = h.shape
h = h.permute(0,2,1).reshape(-1,D)
h = self.norm1(h)
h = h.reshape(B,N,D).permute(0,2,1)
h_l, h_g = torch.split(h, h.shape[1]//2, dim=1)
h = h_l * torch.sigmoid(h_g)
return h
class DeconvGLU1D(nn.Module):
def __init__(self, in_ch, out_ch, ks, sd, normtype='IN'):
super(DeconvGLU1D, self).__init__()
self.conv1 = nn.ConvTranspose1d(
in_ch, out_ch*2, ks, stride=sd, padding=(ks-sd)//2)
nn.init.xavier_normal_(self.conv1.weight,gain=0.1)
if normtype=='BN':
self.norm1 = nn.BatchNorm1d(out_ch*2)
elif normtype=='IN':
self.norm1 = nn.InstanceNorm1d(out_ch*2)
elif normtype=='LN':
self.norm1 = nn.LayerNorm(out_ch*2)
self.conv1 = nn.utils.weight_norm(self.conv1)
self.normtype = normtype
def __call__(self, x):
h = self.conv1(x)
if self.normtype=='BN' or self.normtype=='IN':
h = self.norm1(h)
elif self.normtype=='LN':
B, D, N = h.shape
h = h.permute(0,2,1).reshape(-1,D)
h = self.norm1(h)
h = h.reshape(B,N,D).permute(0,2,1)
h_l, h_g = torch.split(h, h.shape[1]//2, dim=1)
h = h_l * torch.sigmoid(h_g)
return h
class PixelShuffleGLU1D(nn.Module):
def __init__(self, in_ch, out_ch, ks, sd, normtype='IN'):
super(PixelShuffleGLU1D, self).__init__()
self.conv1 = nn.Conv1d(
in_ch, out_ch*2*sd, ks, stride=1, padding=(ks-1)//2)
self.r = sd
if normtype=='BN':
self.norm1 = nn.BatchNorm1d(out_ch*2)
elif normtype=='IN':
self.norm1 = nn.InstanceNorm1d(out_ch*2)
self.conv1 = nn.utils.weight_norm(self.conv1)
self.normtype = normtype
def __call__(self, x):
h = self.conv1(x)
N, pre_ch, pre_len = h.shape
r = self.r
post_ch = pre_ch//r
post_len = pre_len * r
h = torch.reshape(h, (N, r, post_ch, pre_len))
h = h.permute(0,2,3,1)
h = torch.reshape(h, (N, post_ch, post_len))
if self.normtype=='BN' or self.normtype=='IN':
h = self.norm1(h)
h_l, h_g = torch.split(h, h.shape[1]//2, dim=1)
h = h_l * torch.sigmoid(h_g)
return h
def concat_dim1(x,y):
assert x.shape[0] == y.shape[0]
if torch.Tensor.dim(x) == 3:
y0 = torch.unsqueeze(y,2)
N, n_ch, n_t = x.shape
yy = y0.repeat(1,1,n_t)
h = torch.cat((x,yy), dim=1)
elif torch.Tensor.dim(x) == 4:
y0 = torch.unsqueeze(torch.unsqueeze(y,2),3)
N, n_ch, n_q, n_t = x.shape
yy = y0.repeat(1,1,n_q,n_t)
h = torch.cat((x,yy), dim=1)
return h
def concat_dim2(x,y):
assert x.shape[0] == y.shape[0]
if torch.Tensor.dim(x) == 3:
y0 = torch.unsqueeze(y,1)
N, n_t, n_ch = x.shape
yy = y0.repeat(1,n_t,1)
h = torch.cat((x,yy), dim=2)
elif torch.Tensor.dim(x) == 2:
h = torch.cat((x,y), dim=1)
return h