-
Notifications
You must be signed in to change notification settings - Fork 0
/
representation.py
248 lines (214 loc) · 7.27 KB
/
representation.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import torch
from typing import List
# class Chain(torch.nn.Module):
# def __init__(self, layers:List[torch.nn.Module]):
# super().__init__()
# self.ms = torch.nn.ModuleList(layers)
# for m in self.ms:
# self.n_feature = m.n_feature
# def feed(self, x, offset:int=0):
# for m in self.ms:
# x = m.feed(x, offset)
# return x
# def reset(self):
# for m in self.ms:
# m.reset()
# def replace(self, other:"Chain"):
# # for m,mo in zip(self.ms, other.ms):
# # m.replace(mo)
# for i,m in enumerate(self.ms):
# m.replace(other.ms[i])
class Slice(torch.nn.Module):
def __init__(self, start:int, end:int):
super().__init__()
self.start = start
self.end = end
self.n_feature = end - start
def reset(self):
pass
def feed(self, x, offset:int=0):
return x[self.start:self.end]
def replace(self, other:"Slice"):
pass
class Cat2(torch.nn.Module):
def __init__(self, a:torch.nn.Module, b:torch.nn.Module):
super().__init__()
self.a = a
self.b = b
# self.ms = torch.nn.ModuleDict({str(k):m for k,m in enumerate(ms)})
self.n_feature = a.n_feature + b.n_feature
def reset(self):
self.a.reset()
self.b.reset()
def feed(self, x, offset:int=0):
return torch.cat((self.a.feed(x, offset), self.b.feed(x, offset)), -1)
def replace(self, other:"Cat2"):
self.a.replace(other.a)
self.b.replace(other.b)
class Chain2(torch.nn.Module):
def __init__(self, a:torch.nn.Module, b:torch.nn.Module):
super().__init__()
self.a = a
self.b = b
# self.ms = torch.nn.ModuleDict({str(k):m for k,m in enumerate(ms)})
self.n_feature = b.n_feature
def reset(self):
self.a.reset()
self.b.reset()
def feed(self, x, offset:int=0):
return self.b.feed(self.a.feed(x, offset), offset)
def replace(self, other:"Chain2"):
self.a.replace(other.a)
self.b.replace(other.b)
def Cat(ms:List[torch.nn.Module]):
if len(ms)==1:
return ms[0]
else:
return Cat2(ms[0], Cat(ms[1:]))
def Chain(ms:List[torch.nn.Module]):
if len(ms)==1:
return ms[0]
else:
return Chain2(ms[0], Chain(ms[1:]))
# class Cat(torch.nn.Module):
# def __init__(self, ms:List[torch.nn.Module]):
# super().__init__()
# self.n = len(ms)
# self.ms = torch.nn.ModuleList(ms)
# # self.ms = torch.nn.ModuleDict({str(k):m for k,m in enumerate(ms)})
# self.n_feature = sum(m.n_feature for m in ms)
# def reset(self):
# for m in self.ms:
# m.reset()
# def feed(self, x, offset:int=0):
# xs = []
# for m in self.ms:
# xs.append(m.feed(x, offset))
# return torch.cat(xs, -1)
# def replace(self, other:"Cat"):
# oms:torch.nn.ModuleList = other.ms
# for i,m in enumerate(self.ms):
# for j,mo in enumerate(other.ms):
# if i==j:
# m.replace(mo)
class Quadratic(torch.nn.Module):
def __init__(self, m:torch.nn.Module):
super().__init__()
self.m = m
self.n_feature = m.n_feature**2
def reset(self):
self.m.reset()
def feed(self, x, offset:int=0):
x = self.m.feed(x, offset)
return (x * x[...,None]).view(-1)
def replace(self, other:"Quadratic"):
self.m.replace(other.m)
class DividedQuadratic(torch.nn.Module):
def __init__(self, m:torch.nn.Module):
super().__init__()
self.m = m
self.n_feature = m.n_feature**2 * 2
def reset(self):
self.m.reset()
def feed(self, x, offset:int=0):
x = self.m.feed(x, offset)
return (torch.cat((
torch.nn.functional.softplus(x), torch.nn.functional.softplus(-x)
)) * x[...,None]).view(-1)
def replace(self, other:"DividedQuadratic"):
self.m.replace(other.m)
class Window(torch.nn.Module):
"""
"""
record_index:int
def __init__(self,
n_target:int,
n_ctx:int,
):
super().__init__()
self.record_index = 0
self.n_ctx = n_ctx
self.n_memory = n_ctx
self.register_buffer('memory', torch.empty(self.n_memory, n_target))
# read by Loop
self.n_feature = n_ctx*n_target
self.reset()
def reset(self):
self.memory.zero_()
def replace(self, other:"Window"):
self.memory[:] = other.memory
self.record_index = other.record_index
def wrap(self, idx:int):
return idx % self.n_memory
def feed(self, x, offset:int=0):
"""feed a vector into loop memory and update pointer
Args:
x: input vector
offset: number of frames in the past to rewrite
"""
idx = self.wrap(self.record_index - offset)
self.memory[idx] = x
self.record_index = self.wrap(self.record_index+1)
return self.get(offset)
def get(self, offset:int=0):
"""read out of loop memory"""
# end = (self.record_index) % self.n_ctx + 1
end = self.wrap(self.record_index - 1 - offset) + 1
begin = self.wrap(end - self.n_ctx)
# print(n, begin, end)
if begin<end:
r = self.memory[begin:end]
else:
r = torch.cat((self.memory[begin:], self.memory[:end]))
return r.reshape(-1)
class RNN(torch.nn.Module):
"""
"""
record_index:int
def __init__(self,
n_target:int,
n_hidden:int,
n_out:int,
):
super().__init__()
self.register_buffer('hidden', torch.empty(3, n_hidden))
self.register_buffer('ih', torch.empty(n_target, n_hidden))
self.register_buffer('hh', torch.empty(n_hidden, n_hidden))
self.register_buffer('ho', torch.empty(n_hidden, n_out))
# read by Loop
self.n_feature = n_out
self.reset()
def init(self, t):
t[:] = torch.randn_like(t)
# t.mul_((torch.rand_like(t) > 0.5).int())
t.mul_(2**-0.5 / t.pow(2).sum(0).sqrt())
def reset(self):
self.hidden.zero_()
# self.ih[:] = torch.randn_like(self.ih)
# self.ih = self.ih / self.ih.pow(2).sum(0).sqrt()
# self.ho[:] = torch.randn_like(self.ho)
# self.ho = self.ho / self.ho.pow(2).sum(0).sqrt()
# self.hh[:] = torch.randn_like(self.hh)
# self.hh = self.hh / self.hh.pow(2).sum(0).sqrt()
self.init(self.ih)
self.init(self.hh)
self.init(self.ho)
def replace(self, other:"Window"):
self.hidden[:] = other.hidden
def wrap(self, idx:int):
return idx % self.n_memory
def feed(self, x, offset:int=0):
"""feed a vector into loop memory and update pointer
Args:
x: input vector
offset: number of frames in the past to rewrite
"""
h = self.hidden[offset]
h_new = x @ self.ih + h @ self.hh
h_new = h_new.sin()
# mix = torch.linspace(0, -5, h.shape[0]).exp()
# h = mix * h_new + (1-mix) * h
for i in range(2,offset,-1):
self.hidden[i] = self.hidden[i-1]
self.hidden[offset] = h
return h @ self.ho