-
Notifications
You must be signed in to change notification settings - Fork 1
/
layers.py
181 lines (129 loc) · 4.19 KB
/
layers.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import math
from functools import reduce
import operator
from typing import Union
import numpy as np
import torch
from torch import nn as nn
import torch.jit
from torch.nn import functional as F
from utils import broadcast3d
class CumSum(torch.jit.ScriptModule):
def __init__(self, **kwargs):
self.kwargs = kwargs
super().__init__()
def forward(self, inputs):
return torch.cumsum(inputs, **self.kwargs)
class Squash(nn.Module):
def forward(self, x):
y = x ** 3
return torch.clamp(y, min=0) / (1 + y.abs())
class Flatten(nn.Module):
def __init__(self, out_size=None):
super().__init__()
self.out_size = out_size
def forward(self, x):
return x.view(x.size(0), -1)
class Print(nn.Module):
def __init__(self, f=None):
self.f = f
super().__init__()
def forward(self, x):
if self.f is None:
print(x)
else:
print(self.f(x))
return x
class Log(nn.Module):
def forward(self, x):
return torch.log(x)
class Exp(nn.Module):
def forward(self, x):
return torch.exp(x)
class Sum(nn.Module):
def __init__(self, **kwargs):
self.kwargs = kwargs
super().__init__()
def forward(self, inputs):
if isinstance(inputs, (tuple, list)):
return sum(inputs)
else:
return torch.sum(inputs, **self.kwargs)
class ShallowCopy(nn.Module):
def __init__(self, n: int):
self.n = n
super().__init__()
def forward(self, x):
return (x,) * self.n
class Concat(torch.jit.ScriptModule):
def __init__(self, **kwargs):
self.kwargs = kwargs
super().__init__()
def forward(self, inputs):
return torch.cat(inputs, **self.kwargs)
class Reshape(torch.jit.ScriptModule):
def __init__(self, *shape):
super().__init__()
self.shape = shape
def forward(self, inputs):
return inputs.view(inputs.size(0), *self.shape)
class Broadcast3d(torch.jit.ScriptModule):
def __init__(self, *shape):
super().__init__()
self.shape = shape
def forward(self, inputs):
return broadcast3d(inputs, self.shape)
class Product(torch.jit.ScriptModule):
@staticmethod
def forward(inputs):
return reduce(operator.mul, inputs, 1)
class Parallel(torch.jit.ScriptModule):
def __init__(self, *modules):
super().__init__()
self.module_list = nn.ModuleList(modules)
def forward(self, inputs):
return tuple([m(x) for m, x in zip(self.module_list, inputs)])
def wrap_parameter(x, requires_grad):
if isinstance(x, torch.Tensor):
return nn.Parameter(x, requires_grad=requires_grad)
else:
return x
class Plus(torch.jit.ScriptModule):
def __init__(self, x, requires_grad=False):
super().__init__()
self.x = wrap_parameter(x, requires_grad)
def forward(self, inputs):
return self.x + inputs
class Times(torch.jit.ScriptModule):
def __init__(self, x, requires_grad=False):
super().__init__()
self.x = wrap_parameter(x, requires_grad)
def forward(self, inputs):
return self.x * inputs
class MultiEmbeddingBag(nn.Module):
def __init__(self, nvec: Union[np.ndarray, torch.Tensor], **kwargs):
super().__init__()
self.embedding = nn.Embedding(num_embeddings=nvec.sum(), **kwargs)
self.register_buffer(
"offset",
F.pad(torch.tensor(nvec[:-1]).cumsum(0), [1, 0]),
)
def forward(self, inputs):
return self.embedding(self.offset + inputs).sum(-2)
class IntEncoding(nn.Module):
def __init__(self, d_model: int):
self.d_model = d_model
nn.Module.__init__(self)
self.register_buffer(
"div_term",
torch.exp(
torch.arange(0, d_model, 2).float() * (-math.log(10000.0) / d_model)
),
)
def forward(self, x):
shape = x.shape
div_term = self.div_term.view(*(1 for _ in shape), -1)
x = x.unsqueeze(-1)
sins = torch.sin(x * div_term)
coss = torch.cos(x * div_term)
return torch.stack([sins, coss], dim=-1).view(*shape, self.d_model)