-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlearn_spacetime_reps.py
307 lines (261 loc) · 11 KB
/
learn_spacetime_reps.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import torch
import numpy as np
import itertools
from tools import cg_lib
from enum import Enum, auto
from algebra.poincare import irrep_lie_algebra_gens_so31
import tools.tensor
tools.tensor.dtype = torch.float64
from tools.tensor import dtype, device, cplx_contractor, \
cplx_to_np, np_to_cplx, i_times, bracket, \
levi_nonzero, random_walk
dtype = torch.float64
torch.manual_seed(528491)
class Group(Enum):
lorentz = auto()
galilean = auto()
rotations = auto()
def derive_explicit_matrix_reps_retry(**kwargs):
result = None
while result is None:
result = derive_explicit_matrix_reps(**kwargs)
return result
def derive_explicit_matrix_reps(n=4, group=Group.lorentz,
num_space_dims=3, init_gens=None, verbose=False,
convergence_tol=1e-9, lr_cutoff=1e-13, use_exact_sol=False
):
# if n != 4:
# exit(0)
print(f'Deriving explicit {n}-dimensional matrix reps for the {group} group in {num_space_dims} spatial dimensions')
logs = {
'loss': [],
'min_norm': [],
'lr': []
}
if group is Group.lorentz:
if num_space_dims == 3:
J_1 = torch.randn(n, n, device=device, dtype=dtype, requires_grad=True)
K_1 = torch.randn(n, n, device=device, dtype=dtype, requires_grad=True)
K_2 = torch.randn(n, n, device=device, dtype=dtype, requires_grad=True)
elif num_space_dims == 2:
K_1 = torch.randn(n, n, device=device, dtype=dtype, requires_grad=True)
J_3 = torch.randn(n, n, device=device, dtype=dtype, requires_grad=True)
else:
raise ValueError(f'bad num_space_dims {num_space_dims}')
elif group is Group.rotations:
if num_space_dims == 3:
# j_1_diag = torch.randn(n, 2, device=device, dtype=dtype, requires_grad=True)
J_1 = torch.randn(n, n, device=device, dtype=dtype, requires_grad=True)
J_2 = torch.randn(n, n, device=device, dtype=dtype, requires_grad=True)
else:
raise ValueError(f'so{num_space_dims} not supported for so')
if group is Group.lorentz:
if num_space_dims == 3:
optimizer = torch.optim.Adam([J_1, K_1, K_2], lr=1e-1)
elif num_space_dims == 2:
optimizer = torch.optim.Adam([K_1, J_3], lr=1e-1)
elif group is Group.rotations:
if num_space_dims == 3:
optimizer = torch.optim.Adam([J_1, J_2], lr=1e-1)
else:
raise ValueError(f'so{num_space_dims} not supported for so')
else:
raise ValueError(f'unknown group: {group}')
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(
optimizer, 'min', verbose=True,
cooldown=100,
patience=100,
min_lr=1e-16, eps=1e-16
)
t = 0
while True:
optimizer.zero_grad()
loss = 0
if group is Group.lorentz:
if num_space_dims == 3:
# J_1 = torch.zeros(n, n, 2, device=device, dtype=dtype)
# J_1[range(n), range(n)] = j_1_diag
# K_1 = torch.zeros(n, n, 2, device=device, dtype=dtype)
# K_1[range(n), range(n)] = k_1_diag
K_3 = bracket(J_1, K_2)
J_3 = -bracket(K_1, K_2)
J_2 = -bracket(J_1, J_3)
elif num_space_dims == 2:
K_2 = bracket(J_3, K_1)
J_1 = J_2 = K_3 = 1000*torch.ones(K_2.size(), device=device, dtype=dtype)
elif group is Group.rotations:
if num_space_dims == 3:
# J_1 = torch.zeros(n, n, 2, device=device, dtype=dtype)
# J_1[range(n), range(n),0] = j_1_diag[...,0]
# J_1_re = torch.zeros(*J_1.shape, device=device, dtype=dtype)
# J_1_re[...,0] = J_1[...,0]
# J_2_re = torch.zeros(*J_2.shape, device=device, dtype=dtype)
# J_2_re[...,0] = J_2[...,0]
# J_3_re = -i_times(bracket(J_1_re, J_2_re))
# J_3_re = bracket(J_1_re, J_2_re)
J_3 = bracket(J_1, J_2)
else:
raise ValueError(f'so{num_space_dims} not supported for so')
K = 1000*torch.ones(3, *J_1.size(), device=device, dtype=dtype)
if group is Group.lorentz:
J = torch.stack([J_1, J_2, J_3], dim=0)
K = torch.stack([K_1, K_2, K_3], dim=0)
if num_space_dims == 3:
# since for 2D we don't have overlap between Js and Ks
for i in range(3):
loss = loss + (bracket(J[i], K[i]) - 0).abs().mean()
else:
J = torch.stack([J_1, J_2, J_3], dim=0)
for (i, j) in itertools.product(range(3), range(3)):
if i==j:
continue
eps, k = levi_nonzero([i, j])
# Lorentz, Galilean, and so algebra
# [J_i, J_j] = i epsilon_ijk J_k
if num_space_dims == 3:
if group is not Group.rotations:
error = (bracket(J[i], J[j]) - eps * J[k]).abs().mean()
# print(f'i, j, k = {i, j, k}, error = {error.item()}')
loss = loss + error
else:
# print(f'i, j, k = {i, j, k}, error = {(bracket(J[i], J[j]) - eps * (J[k])).abs().mean().item()}')
loss = loss + (bracket(J[i], J[j]) - eps * J[k]).abs().mean()
if group is Group.lorentz and (num_space_dims == 3 or (i == 2 and j != 2)):
# we only have J_3 in the 2+1D case
# [J_i, K_j] = i epsilon_ijk K_k
error = (bracket(J[i], K[j]) - eps * K[k]).abs().mean()
loss = loss + error
if group is Group.lorentz:
if num_space_dims == 3 or (i != 2 and j != 2):
# [K_i, K_j] = -epsilon_ijk J_k
error = (bracket(K[i], K[j]) - -eps * J[k]).abs().mean()
loss = loss + error
if group is Group.lorentz:
min_norm = torch.min(torch.min(J.pow(2).sum((-1,-2))), torch.min(K.pow(2).sum((-1,-2))))
# print(min_norm.item())
elif group is Group.rotations:
min_norm = torch.min(J.pow(2).sum((-1,-2)))
else:
raise ValueError(f'unknown group: {group}')
# print(f'error_loss = {loss}')
min_norm = min_norm.clamp(max=1)
loss = loss / min_norm
# loss = loss + 1 / min_norm
# if group is Group.rotations:
# if n > 3:
# sv_penalty = simplecg.projector_sv(
# 3,
# n**2, simplecg.tprep(J),
# 1, simplecg.rep(torch.zeros(3,1,1,device=device,dtype=dtype)),
# # 3, simplecg.rep(simplecg.T),
# rank=-2
# ).clamp(max=1000)**2
# print(f'sv penalty = {sv_penalty.item()}')
# loss = loss / sv_penalty
logs['loss'].append(loss.item())
logs['min_norm'].append(min_norm.item())
logs['lr'].append(optimizer.param_groups[0]['lr'])
if t % 100 == 0:
print(f'lr = {optimizer.param_groups[0]["lr"]} min_norm = {min_norm.item()} loss = {loss.item()}', flush=True)
loss.backward()
optimizer.step()
scheduler.step(loss)
if loss.item() <= convergence_tol:
# if group == 'so':
# if num_space_dims == 3 and n == 3:
# w, v = np.linalg.eig(cplx_to_np(J.detach()))
# print(w.round(4))
# ranks = np.linalg.matrix_rank(cplx_to_np(J.detach()))
# if list(ranks) != [2,2,2]:
# print(f'Wrong Ranks: {ranks} Retry')
# return None
# print('appears convergent: saving to save_convergent.npy')
# np.save('save_convergent.npy', J[...,0].detach().cpu().numpy())
# print(J, K)
if group is Group.lorentz:
if num_space_dims == 3:
gens0 = np.concatenate(
[J.detach().cpu().numpy(), K.detach().cpu().numpy()],
axis=0
)
gens1 = gens0
gens2 = np.concatenate(
irrep_lie_algebra_gens_so31(0, 0),
axis=0
)
elif num_space_dims == 2:
gens0 = np.concatenate(
[[J.detach().cpu().numpy()[2]], K.detach().cpu().numpy()[:-1]],
axis=0
)
gens1 = gens0
gens2 = np.concatenate(
irrep_lie_algebra_gens_so31(0, 0),
axis=0
)[:3]
elif group is Group.rotations:
gens0 = J.detach().cpu().numpy()
gens1 = gens0
gens2 = irrep_lie_algebra_gens_so31(0, 0)[0]
print(gens0.shape, gens1.shape, gens2.shape)
cg_coeffs = cg_lib.clebsch_gordan(
gens0,
gens1,
gens2,
zero=1e-2
)
if type(cg_coeffs) is int and cg_coeffs == 0:
print('0 norms found, TRY AGAIN')
# exit(1)
return None
if cg_coeffs.shape[0] > 1:
print('too many norms, TRY AGAIN')
# exit(1)
return None
print('~convergence')
break
if loss.item() < 100 and optimizer.param_groups[0]['lr'] / loss.item() < 1e-4:
print('lr / loss < 1e-4 TRY AGAIN')
return None
if optimizer.param_groups[0]['lr'] < lr_cutoff:
print('TRY AGAIN')
return None
t += 1
gens = np.concatenate((J.detach().cpu().numpy(), K.detach().cpu().numpy()), axis=0)
if group is Group.lorentz and num_space_dims == 2:
gens = np.array([gens[2], gens[3], gens[4]])
elif group is Group.rotations and num_space_dims == 3:
gens = np.array(gens[:3])
return {'gens': gens, 'logs': logs}
if __name__ == '__main__':
convergence_tol = 1e-10
irrep_dims_so_3_1 = [4]
irrep_dims_so_2_1 = [3]
irrep_dims_so_3 = [3]
irreps = {}
irreps['so_3_1'] = {
n: derive_explicit_matrix_reps_retry(
n=n, group=Group.lorentz,
convergence_tol=convergence_tol
)
for n in irrep_dims_so_3_1
}
irreps['so_2_1'] = {
n: derive_explicit_matrix_reps_retry(
n=n, group=Group.lorentz,
convergence_tol=convergence_tol,
num_space_dims=2
)
for n in irrep_dims_so_2_1
}
irreps['so_3'] = {
n: derive_explicit_matrix_reps_retry(
n=n, group=Group.rotations, num_space_dims=3,
convergence_tol=convergence_tol
)
for n in irrep_dims_so_3
}
irreps['so_3_1'][1] = {'gens': np.zeros((6,1,1)), 'logs': None}
irreps['so_2_1'][1] = {'gens': np.zeros((3,1,1)), 'logs': None}
irreps['so_3'][1] = {'gens': np.zeros((3,1,1)), 'logs': None}
np.save('irreps.npy', irreps)