-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoment_kernels.py
More file actions
1734 lines (1448 loc) · 74.4 KB
/
moment_kernels.py
File metadata and controls
1734 lines (1448 loc) · 74.4 KB
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'''
Rotation and reflection equivariance using moment kernels in 2D.
We support scalar fields, vector fields, convolution maps between them, batch norm, and nonlinearity.
TODO
----
Try to move if statements out of the forward method and somehow into the init method.
Consider adding in rotation but not reflection for 2D.
Build 3D.
'''
import torch
import numpy as np
from itertools import permutations,product
class ScalarToScalar(torch.nn.Module):
def __init__(self,in_channels, out_channels, kernel_size, padding=0, bias=True, padding_mode='zeros'):
super().__init__()
self.in_channels = in_channels
self.out_channels = out_channels
if out_channels == 0:
self.forward = forward_empty
return
self.kernel_size = kernel_size
self.padding = padding
self.padding_mode = padding_mode
if padding_mode == 'zeros': self.padding_mode = 'constant'
# use kernel size to get x
r = (kernel_size - 1)//2
x = torch.arange(-r,r+1)
X = torch.stack(torch.meshgrid(x,x,indexing='ij'),-1)
R = torch.sqrt(torch.sum(X**2,-1))
Xhat = X/R[...,None]
Xhat[R==0] = 0
rs,inds = torch.unique(R,return_inverse=True)
# register buffers, this will allow them to move to devices
self.register_buffer('Xhat',Xhat)
self.register_buffer('inds',inds)
self.weights = torch.nn.parameter.Parameter(torch.randn(out_channels,in_channels,len(rs))/np.sqrt(3.0*in_channels)) # TODO: use the right normalizatoin
self.bias = torch.nn.parameter.Parameter(torch.randn(out_channels)/np.sqrt(3.0))
def forward_empty(self,x):
'''
Return an array that's the same size as the input but with 0 channels
This can be used to concatenate with other arguments
Note this requires a batch dimension
TODO: compute the correct size with respect to padding and kernel size
I'm not sure if this is a good approach.
'''
return torch.zeros((x.shape[0],0,x.shape[2],x.shape[3]),device=x.device,dtype=x.dtype)
def forward(self,x):
# note this works with size 1 images, as long as padding is 0
# convert the weights into a kernel
# we reshape from out x in x len(rs)
# to
# out x in x kernel_size x kernel_size
c = self.weights[...,self.inds]
self.c = c
tmp = torch.nn.functional.pad(x,(self.padding,self.padding,self.padding,self.padding),mode=self.padding_mode)
return torch.nn.functional.conv2d(tmp,c,self.bias)
class ScalarToVector(torch.nn.Module):
def __init__(self,in_channels, out_channels, kernel_size, padding=0, padding_mode='zeros'):
# with vectors, out channel will be the number of vectors, not the number of components
super().__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.kernel_size = kernel_size
self.padding = padding
self.padding_mode = padding_mode
if padding_mode == 'zeros': self.padding_mode = 'constant'
# use kernel size to get x
r = (kernel_size - 1)//2
x = torch.arange(-r,r+1)
X = torch.stack(torch.meshgrid(x,x,indexing='ij'),-1)
R = torch.sqrt(torch.sum(X**2,-1))
Xhat = X/R[...,None]
Xhat[R==0] = 0
# reshape it to the way I will want to use it
# it should match out channels on the left
Xhat = Xhat.permute(-1,0,1)[:,None]
Xhat = Xhat.repeat((out_channels,1,1,1))
rs,inds = torch.unique(R,return_inverse=True)
# register buffers, this will allow them to move to devices
self.register_buffer('Xhat',Xhat)
inds = inds - 1 # we will not use r=0. the filter will get assigned a different number, but then multiplied by 0
inds[inds==-1] = 0
self.register_buffer('inds',inds) # don't need a parameter for r=0, but this makes
self.weights = torch.nn.parameter.Parameter(torch.randn(out_channels,in_channels,len(rs)-1)/np.sqrt(3*in_channels)) # TODO: use the right normalizatoin
if x.shape[-1] == 1:
self.forward = self.forwarde1
else:
self.forward = self.forwardg1
def forwarde1(self,x):
# kernel size 1 needs to be a special case because self.inds is empty, the result is just 0
# no padding allowed
# note we assume square
return torch.zeros(x.shape[0],self.out_channels*2,1,1,dtype=x.dtype,device=x.device)
def forwardg1(self,x):
# convert the weights into a kernel
# we reshape from out x in x len(rs)
# to
# out x in x kernel_size x kernel_size
c = torch.repeat_interleave(self.weights,2,0)[...,self.inds]*self.Xhat
self.c = c
# for somme reason the output is not zero mean, has to do with padding
# here's a better way
tmp = torch.nn.functional.pad(x,(self.padding,self.padding,self.padding,self.padding),mode=self.padding_mode)
return torch.nn.functional.conv2d(tmp,c)
class ScalarToVector90(torch.nn.Module):
'''This module adds an additional basis function with a 90 degree rotation'''
def __init__(self,in_channels, out_channels, kernel_size, padding=0, padding_mode='zeros'):
# with vectors, out channel will be the number of vectors, not the number of components
super().__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.kernel_size = kernel_size
self.padding = padding
self.padding_mode = padding_mode
if padding_mode == 'zeros': self.padding_mode = 'constant'
# use kernel size to get x
r = (kernel_size - 1)//2
x = torch.arange(-r,r+1)
X = torch.stack(torch.meshgrid(x,x,indexing='ij'),-1)
R = torch.sqrt(torch.sum(X**2,-1))
Xhat = X/R[...,None]
Xhat[R==0] = 0
# reshape it to the way I will want to use it
# it should match out channels on the left
Xhat = Xhat.permute(-1,0,1)[:,None]
X90hat = Xhat.flip(0)*torch.tensor([-1.0,1.0])[:,None,None,None]
Xhat = Xhat.repeat((out_channels,1,1,1))
X90hat = X90hat.repeat((out_channels,1,1,1))
rs,inds = torch.unique(R,return_inverse=True)
# register buffers, this will allow them to move to devices
self.register_buffer('Xhat',Xhat)
self.register_buffer('X90hat',X90hat)
inds = inds - 1 # we will not use r=0. the filter will get assigned a different number, but then multiplied by 0
inds[inds==-1] = 0
self.register_buffer('inds',inds) # don't need a parameter for r=0, but this makes
self.weights = torch.nn.parameter.Parameter(torch.randn(out_channels,in_channels,len(rs)-1)/np.sqrt(3*in_channels)) #
self.weights90 = torch.nn.parameter.Parameter(torch.randn(out_channels,in_channels,len(rs)-1)/np.sqrt(3*in_channels)) # TODO: use the right normalizatoin
if x.shape[-1] == 1:
self.forward = self.forwarde1
else:
self.forward = self.forwardg1
def forwarde1(self,x):
# kernel size 1 needs to be a special case because self.inds is empty, the result is just 0
# no padding allowed
# note we assume square
return torch.zeros(x.shape[0],self.out_channels*2,1,1,dtype=x.dtype,device=x.device)
def forwardg1(self,x):
# convert the weights into a kernel
# we reshape from out x in x len(rs)
# to
# out x in x kernel_size x kernel_size
c = torch.repeat_interleave(self.weights,2,0)[...,self.inds]*self.Xhat
c90 = torch.repeat_interleave(self.weights90,2,0)[...,self.inds]*self.X90hat
self.c = c + c90
# for somme reason the output is not zero mean, has to do with padding
# here's a better way
tmp = torch.nn.functional.pad(x,(self.padding,self.padding,self.padding,self.padding),mode=self.padding_mode)
return torch.nn.functional.conv2d(tmp,c)
def rotate_vector_and_image(x):
with torch.no_grad():
tmp = x.rot90(1,(-1,-2))
tmp2 = tmp.clone()
for i in range(tmp.shape[1]//2):
tmp2[:,i*2] = tmp[:,i*2+1]
tmp2[:,i*2+1] = -tmp[:,i*2]
return tmp2
def rotate_vector(x):
with torch.no_grad():
tmp = x.clone()
tmp2 = x.clone()
for i in range(tmp.shape[1]//2):
tmp2[:,i*2] = tmp[:,i*2+1]
tmp2[:,i*2+1] = -tmp[:,i*2]
return tmp2
class VectorToScalar(torch.nn.Module):
def __init__(self,in_channels, out_channels, kernel_size, padding=0, bias=True, padding_mode='zeros'):
# with vectors, in channel will be the number of vectors, not the number of components
super().__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.kernel_size = kernel_size
self.padding = padding
self.padding_mode = padding_mode
if padding_mode == 'zeros': self.padding_mode = 'constant'
# use kernel size to get x
r = (kernel_size - 1)//2
x = torch.arange(-r,r+1)
X = torch.stack(torch.meshgrid(x,x,indexing='ij'),-1)
R = torch.sqrt(torch.sum(X**2,-1))
Xhat = X/R[...,None]
Xhat[R==0] = 0
# reshape it to the way I will want to use it
# it should match out channels on the left
Xhat = Xhat.permute(-1,0,1)[None]
Xhat = Xhat.repeat((1,in_channels,1,1))
rs,inds = torch.unique(R,return_inverse=True)
inds = inds - 1
inds[inds<0] = 0
# register buffers, this will allow them to move to devices
self.register_buffer('Xhat',Xhat)
self.register_buffer('inds',inds)
self.weights = torch.nn.parameter.Parameter(torch.randn(out_channels,in_channels,len(rs)-1)/np.sqrt(3*in_channels*2)) # TODO: use the right normalizatoin
if bias:
self.bias = torch.nn.parameter.Parameter(torch.randn(out_channels)/np.sqrt(3.0))
else:
self.bias = None
if x.shape[-1] == 1:
self.forward = self.forwarde1
else:
self.forward = self.forwardg1
def forwarde1(self,x):
# size 1 is a special case because there are no parameters, just return 0 + bias
# self.ind is empty
return torch.zeros(x.shape[0],self.out_channels,1,1,dtype=x.dtype,device=x.device) + self.bias[...,None,None]
def forwardg1(self,x):
# convert the weights into a kernel
# we reshape from out x in x len(rs)
# to
# out x in x kernel_size x kernel_size
c = torch.repeat_interleave(self.weights[...,self.inds],2,1)*self.Xhat
self.c = c
tmp = torch.nn.functional.pad(x,(self.padding,self.padding,self.padding,self.padding),mode=self.padding_mode)
return torch.nn.functional.conv2d(tmp,c,self.bias)
class VectorToScalar90(torch.nn.Module):
''' In this version we include the extra basis rotated by 90 degrees'''
def __init__(self,in_channels, out_channels, kernel_size, padding=0, bias=True, padding_mode='zeros'):
# with vectors, in channel will be the number of vectors, not the number of components
super().__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.kernel_size = kernel_size
self.padding = padding
self.padding_mode = padding_mode
if padding_mode == 'zeros': self.padding_mode = 'constant'
# use kernel size to get x
r = (kernel_size - 1)//2
x = torch.arange(-r,r+1)
X = torch.stack(torch.meshgrid(x,x,indexing='ij'),-1)
R = torch.sqrt(torch.sum(X**2,-1))
Xhat = X/R[...,None]
Xhat[R==0] = 0
# reshape it to the way I will want to use it
# it should match out channels on the left
Xhat = Xhat.permute(-1,0,1)[None]
X90hat = Xhat.flip(1)*torch.tensor([-1.0,1.0])[None,:,None,None]
Xhat = Xhat.repeat((1,in_channels,1,1))
X90hat = X90hat.repeat((1,in_channels,1,1))
rs,inds = torch.unique(R,return_inverse=True)
inds = inds - 1
inds[inds<0] = 0
# register buffers, this will allow them to move to devices
self.register_buffer('Xhat',Xhat)
self.register_buffer('X90hat',X90hat)
self.register_buffer('inds',inds)
self.weights = torch.nn.parameter.Parameter(torch.randn(out_channels,in_channels,len(rs)-1)/np.sqrt(3*in_channels*2)) # TODO: use the right normalizatoin
self.weights90 = torch.nn.parameter.Parameter(torch.randn(out_channels,in_channels,len(rs)-1)/np.sqrt(3*in_channels*2)) # TODO: use the right normalizatoin
if bias:
self.bias = torch.nn.parameter.Parameter(torch.randn(out_channels)/np.sqrt(3.0))
else:
self.bias = None
if x.shape[-1] == 1:
self.forward = self.forwarde1
else:
self.forward = self.forwardg1
def forwarde1(self,x):
# size 1 is a special case because there are no parameters, just return 0 + bias
# self.ind is empty
return torch.zeros(x.shape[0],self.out_channels,1,1,dtype=x.dtype,device=x.device) + self.bias[...,None,None]
def forwardg1(self,x):
# convert the weights into a kernel
# we reshape from out x in x len(rs)
# to
# out x in x kernel_size x kernel_size
c = torch.repeat_interleave(self.weights[...,self.inds],2,1)*self.Xhat + torch.repeat_interleave(self.weights90[...,self.inds],2,1)*self.X90hat
self.c = c
tmp = torch.nn.functional.pad(x,(self.padding,self.padding,self.padding,self.padding),mode=self.padding_mode)
return torch.nn.functional.conv2d(tmp,c,self.bias)
class VectorToVector(torch.nn.Module):
'''Question, should I separate these into two types and interleave them somehow rather than combining them'''
def __init__(self,in_channels, out_channels, kernel_size, padding=0, padding_mode='zeros'):
# with vectors, in channel will be the number of vectors, not the number of components
super().__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.kernel_size = kernel_size
self.padding = padding
self.padding_mode = padding_mode
if padding_mode == 'zeros': self.padding_mode = 'constant'
# use kernel size to get x
r = (kernel_size - 1)//2
x = torch.arange(-r,r+1)
X = torch.stack(torch.meshgrid(x,x,indexing='ij'),-1)
R = torch.sqrt(torch.sum(X**2,-1))
Xhat = X/R[...,None]
Xhat[R==0] = 0
# reshape it to the way I will want to use it
# it should match out channels on the left
# we need XhatXhat, and identity
Xhat = Xhat.permute(-1,0,1)
XhatXhat = Xhat[None,:]*Xhat[:,None]
XhatXhat = XhatXhat.repeat((out_channels,in_channels,1,1))
rs,inds = torch.unique(R,return_inverse=True)
indsxx = inds.clone()-1
indsxx[indsxx==-1] = 0# wlil get multiplied by zero
# register buffers, this will allow them to move to devices
indsidentity = inds
identity = torch.eye(2).repeat((out_channels,in_channels))[...,None,None]
self.register_buffer('XhatXhat',XhatXhat)
self.register_buffer('identity',identity)
self.register_buffer('indsxx',indsxx)
self.register_buffer('indsidentity',indsidentity)
self.weightsxx = torch.nn.parameter.Parameter(torch.randn(out_channels,in_channels,len(rs)-1)/np.sqrt(3*in_channels*2))
self.weightsidentity = torch.nn.parameter.Parameter(torch.randn(out_channels,in_channels,len(rs))/np.sqrt(3*in_channels*2))
# special case if kernel is size 1
# print(x.shape)
if x.shape[-1] == 1:
self.forward = self.forwarde1
else:
self.forward = self.forwardg1
def forwarde1(self,x):
cidentity = torch.repeat_interleave(torch.repeat_interleave(self.weightsidentity[...,self.indsidentity],2,0),2,1)*self.identity
self.cidentity = cidentity
return torch.nn.functional.conv2d(x,cidentity)
def forwardg1(self,x):
# convert the weights into a kernel
# we reshape from out x in x len(rs)
# to
# out x in x kernel_size x kernel_size
cxx = torch.repeat_interleave(torch.repeat_interleave(self.weightsxx,2,0),2,1)[...,self.indsxx]*self.XhatXhat
cidentity = torch.repeat_interleave(torch.repeat_interleave(self.weightsidentity,2,0),2,1)[...,self.indsidentity]*self.identity
c = cxx + cidentity
self.c = c
self.cxx = cxx
self.cidentity = cidentity
tmp = torch.nn.functional.pad(x,(self.padding,self.padding,self.padding,self.padding),mode=self.padding_mode)
return torch.nn.functional.conv2d(tmp,c) # no bias when output is vector
class VectorToVector90(torch.nn.Module):
'''Question, should I separate these into two types and interleave them somehow rather than combining them
This one uses the extra basis functions rotated by 90 degrees.
'''
def __init__(self,in_channels, out_channels, kernel_size, padding=0, padding_mode='zeros'):
# with vectors, in channel will be the number of vectors, not the number of components
super().__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.kernel_size = kernel_size
self.padding = padding
self.padding_mode = padding_mode
if padding_mode == 'zeros': self.padding_mode = 'constant'
# use kernel size to get x
r = (kernel_size - 1)//2
x = torch.arange(-r,r+1)
X = torch.stack(torch.meshgrid(x,x,indexing='ij'),-1)
R = torch.sqrt(torch.sum(X**2,-1))
Xhat = X/R[...,None]
Xhat[R==0] = 0
# reshape it to the way I will want to use it
# it should match out channels on the left
# we need XhatXhat, and identity
Xhat = Xhat.permute(-1,0,1)
X90hat = Xhat.flip(0)*torch.tensor([-1.0,1.0])[...,None,None]
# now there are 4
XhatXhat = Xhat[None,:]*Xhat[:,None]
XhatXhat = XhatXhat.repeat((out_channels,in_channels,1,1))
X90hatXhat = X90hat[None,:]*Xhat[:,None]
X90hatXhat = X90hatXhat.repeat((out_channels,in_channels,1,1))
XhatX90hat = Xhat[None,:]*X90hat[:,None]
XhatX90hat = XhatX90hat.repeat((out_channels,in_channels,1,1))
X90hatX90hat = X90hat[None,:]*X90hat[:,None]
X90hatX90hat = X90hatX90hat.repeat((out_channels,in_channels,1,1))
rs,inds = torch.unique(R,return_inverse=True)
indsxx = inds.clone()-1
indsxx[indsxx==-1] = 0# wlil get multiplied by zero
# register buffers, this will allow them to move to devices
indsidentity = inds
identity = torch.eye(2).repeat((out_channels,in_channels))[...,None,None]
self.register_buffer('XhatXhat',XhatXhat)
self.register_buffer('X90hatXhat',X90hatXhat)
self.register_buffer('XhatX90hat',XhatX90hat)
self.register_buffer('X90hatX90hat',X90hatX90hat)
self.register_buffer('identity',identity)
self.register_buffer('indsxx',indsxx)
self.register_buffer('indsidentity',indsidentity)
self.weightsxx = torch.nn.parameter.Parameter(torch.randn(out_channels,in_channels,len(rs)-1)/np.sqrt(3*in_channels*2))
self.weightsx90x = torch.nn.parameter.Parameter(torch.randn(out_channels,in_channels,len(rs)-1)/np.sqrt(3*in_channels*2))
self.weightsxx90 = torch.nn.parameter.Parameter(torch.randn(out_channels,in_channels,len(rs)-1)/np.sqrt(3*in_channels*2))
self.weightsx90x90 = torch.nn.parameter.Parameter(torch.randn(out_channels,in_channels,len(rs)-1)/np.sqrt(3*in_channels*2))
self.weightsidentity = torch.nn.parameter.Parameter(torch.randn(out_channels,in_channels,len(rs))/np.sqrt(3*in_channels*2))
# special case if kernel is size 1
# print(x.shape)
if x.shape[-1] == 1:
self.forward = self.forwarde1
else:
self.forward = self.forwardg1
def forwarde1(self,x):
cidentity = torch.repeat_interleave(torch.repeat_interleave(self.weightsidentity[...,self.indsidentity],2,0),2,1)*self.identity
self.cidentity = cidentity
return torch.nn.functional.conv2d(x,cidentity)
def forwardg1(self,x):
# convert the weights into a kernel
# we reshape from out x in x len(rs)
# to
# out x in x kernel_size x kernel_size
cxx = torch.repeat_interleave(torch.repeat_interleave(self.weightsxx,2,0),2,1)[...,self.indsxx]*self.XhatXhat
cx90x = torch.repeat_interleave(torch.repeat_interleave(self.weightsx90x,2,0),2,1)[...,self.indsxx]*self.X90hatXhat
cxx90 = torch.repeat_interleave(torch.repeat_interleave(self.weightsxx90,2,0),2,1)[...,self.indsxx]*self.XhatX90hat
cx90x90 = torch.repeat_interleave(torch.repeat_interleave(self.weightsx90x90,2,0),2,1)[...,self.indsxx]*self.X90hatX90hat
cidentity = torch.repeat_interleave(torch.repeat_interleave(self.weightsidentity,2,0),2,1)[...,self.indsidentity]*self.identity
c = cxx + cx90x + cxx90 + cx90x90 + cidentity
self.c = c
self.cxx = cxx # don't really need this, but may want to look at it later
self.cidentity = cidentity
tmp = torch.nn.functional.pad(x,(self.padding,self.padding,self.padding,self.padding),mode=self.padding_mode)
return torch.nn.functional.conv2d(tmp,c) # no bias when output is vector
class ScalarVectorToScalarVector(torch.nn.Module):
def __init__(self, in_scalars, in_vectors, out_scalars, out_vectors, kernel_size, padding=0, bias=True, padding_mode='zeros'):
super().__init__()
self.in_scalars = in_scalars
self.in_vectors = in_vectors
self.out_scalars = out_scalars
self.out_vectors = out_vectors
self.padding_mode = padding_mode
if padding_mode == 'zeros': self.padding_mode = 'constant'
if in_scalars > 0 and out_scalars > 0:
self.ss = ScalarToScalar(in_scalars, out_scalars, kernel_size, padding, bias, padding_mode)
if in_scalars > 0 and out_vectors > 0:
self.sv = ScalarToVector(in_scalars, out_vectors, kernel_size, padding, padding_mode)
if in_vectors > 0 and out_scalars > 0:
self.vs = VectorToScalar(in_scalars, out_scalars, kernel_size, padding, bias, padding_mode)
if in_vectors > 0 and out_vectors > 0:
self.vv = VectorToVector(in_scalars, out_vectors, kernel_size, padding, padding_mode)
# it seems there are 16 total possibilities for forward functions given missing data
# perhaps we could handle these cases above?
def forward(self,x):
# TODO implement this without if statements
outs = torch.zeros((x.shape[0],self.out_scalars,x.shape[2],x.shape[3]),device=x.device,dtype=x.dtype)
outv = torch.zeros((x.shape[0],self.out_vectors*2,x.shape[2],x.shape[3]),device=x.device,dtype=x.dtype)
#print(outs.shape,outv.shape)
if self.in_scalars > 0 and self.out_scalars > 0:
outs = outs + self.ss( x[:,:self.in_scalars])
if self.in_scalars > 0 and self.out_vectors > 0:
outv = outv + self.sv( x[:,:self.in_scalars])
if self.in_vectors > 0 and self.out_scalars > 0:
outs = outs + self.vs(x[:,self.in_scalars:])
if self.in_vectors > 0 and self.out_vectors > 0:
outv = outv + self.vv(x[:,self.in_scalars:])
#print(outs.shape,outv.shape)
return torch.concatenate( ( outs, outv ) , dim=-3)
class Downsample(torch.nn.Module):
def __init__(self):
super().__init__()
pass
def forward(self,x):
# downsample on the last two dimensions by a factor of 2
# if it is even, we average
# if it is odd we skip
if not x.shape[-1]%2: # if even
x = (x[...,0::2] + x[...,1::2])*0.5
else:
x = x[...,0::2]
if not x.shape[-2]%2: # if even
x = (x[...,0::2,:] + x[...,1::2,:])*0.5
else:
x = x[...,0::2,:]
return x
class Upsample(torch.nn.Module):
def __init__(self):
super().__init__()
def forward(self,x,roweven=True,coleven=True):
if coleven:
x = torch.repeat_interleave(x,2,dim=-1)
else:
# if odd we insert zeros
x = (torch.repeat_interleave(x,2,dim=-1) * (1-torch.arange(2*x.shape[-1])%2))[...,:-1]
if roweven:
x = torch.repeat_interleave(x,2,dim=-2)
else:
x = (torch.repeat_interleave(x,2,dim=-2) * (1-torch.arange(2*x.shape[-2])%2)[...,None])[...,:-1,:]
return x
# as before, the sigmoid is causing a problem that leads to nans
# perhaps because the sqrt has an infinite slope at x=0?
class VectorSigmoid(torch.nn.Module):
def __init__(self):
super().__init__()
def forward(self,x):
#return torch.relu(x)
#return torch.abs(x)
# the vector has some multiple of 2 chanels
x2 = x**2
l2 = x2[:,0::2] + x2[:,1::2] + 1e-6
#l2r = torch.repeat_interleave(l2,2,dim=1)
#return x * l2r / (l2r + 1.0)
#return x / torch.sqrt((l2r + 1.0))
#return x*torch.relu(l2r-1)/l2r
l = torch.sqrt(l2)
# now I have the length of each vector
lr = torch.repeat_interleave(l,2,dim=1)
# now it is repeated
return x*torch.relu((lr-1.0))/lr
#return torch.relu(x)
class VectorSigmoidLog(torch.nn.Module):
'''This one is just relu on the log magnitude'''
def __init__(self,ep=1e-6):
super().__init__()
self.ep = ep
def forward(self,x):
# first get magnitude
x2 = x**2
l2 = x2[:,0::2] + x2[:,1::2] + self.ep
logl2 = torch.log(l2)
newlogl2 = torch.relu(logl2)
factor = ( (newlogl2 - logl2)*0.5 ).exp()
return x*factor.repeat_interleave(2,dim=1)
class ScalarSigmoid(torch.nn.Module):
def __init__(self):
super().__init__()
def forward(self,x):
#l = torch.sqrt(x**2 + 1e-5)
#return x*torch.relu((l-1.0))/l
return torch.relu(x)
class ScalarVectorSigmoid(torch.nn.Module):
def __init__(self,n_scalars):
super().__init__()
self.n_scalars = n_scalars
self.s = ScalarSigmoid()
self.v = VectorSigmoid()
def forward(self,x):
return torch.concatenate((self.s(x[:,:self.n_scalars]), self.v(x[:,self.n_scalars:])),-3)
class ScalarBatchnorm(torch.nn.Module):
def __init__(self,n):
super().__init__()
self.b = torch.nn.BatchNorm2d(n)
def forward(self,x):
return self.b(x)
class VectorBatchnorm(torch.nn.Module):
def __init__(self,n):
super().__init__()
self.b = torch.nn.BatchNorm2d(n)
def forward(self,x):
magnitude2 = x[:,0::2]**2 + x[:,1::2]**2 + 1e-6
logmagnitude2 = torch.log(magnitude2)
#scaledlogmagnitude2 = self.b(logmagnitude2)
# let's think about this normalization
# do I really need the 0.5 below?
#return x * torch.repeat_interleave(( (scaledlogmagnitude2 - logmagnitude2)*0.5 ).exp(),2,dim=1)
logmagnitude = 0.5*torch.log(magnitude2)
scaledlogmagnitude = self.b(logmagnitude)
return x * torch.repeat_interleave(( (scaledlogmagnitude - logmagnitude) ).exp(),2,dim=1)
class ScalarVectorBatchnorm(torch.nn.Module):
def __init__(self,nscalar,nvector):
super().__init__()
self.nscalar = nscalar
self.nvector = nvector
self.bs = ScalarBatchnorm(nscalar)
self.bv = VectorBatchnorm(nvector)
def forward(self,x):
return torch.concatenate( (self.bs(x[:,:self.nscalar]),self.bv(x[:,self.nscalar:])) , 1)
class ScalarToMatrix(torch.nn.Module):
def __init__(self,in_channels, out_channels, kernel_size, padding=0, padding_mode='zeros'):
super().__init__()
# what's the main idea here?
# for the identity, we can do a regular conv, then multiply by identity
# for the xx we have to actually do the bigger convolution
# since we're doing the bigger convolution, we might as well just do it
self.in_channels = in_channels
self.out_channels = out_channels
self.kernel_size = kernel_size
self.padding = padding
self.padding_mode = padding_mode
if padding_mode == 'zeros': self.padding_mode = 'constant'
# use kernel size to get x
r = (kernel_size - 1)//2
x = torch.arange(-r,r+1)
X = torch.stack(torch.meshgrid(x,x,indexing='ij'),-1)
R = torch.sqrt(torch.sum(X**2,-1))
Xhat = X/R[...,None]
Xhat[R==0] = 0
# reshape it to the way I will want to use it
# it should match out channels on the left
# we need XhatXhat, and identity
Xhat = Xhat.permute(-1,0,1)
XhatXhat = Xhat[None,:]*Xhat[:,None] # 2x2xkxk
XhatXhat = XhatXhat.reshape(4,1,kernel_size,kernel_size) # 4x1xkxk
XhatXhat = XhatXhat.repeat((out_channels,in_channels,1,1))
rs,inds = torch.unique(R,return_inverse=True)
indsxx = inds.clone()-1
indsxx[indsxx==-1] = 0# wlil get multiplied by zero
# register buffers, this will allow them to move to devices
indsidentity = inds
identity = torch.eye(2).reshape(4,1).repeat((out_channels,in_channels))[...,None,None]
self.register_buffer('XhatXhat',XhatXhat)
self.register_buffer('identity',identity)
self.register_buffer('indsxx',indsxx)
self.register_buffer('indsidentity',indsidentity)
self.weightsxx = torch.nn.parameter.Parameter(torch.randn(out_channels,in_channels,len(rs)-1)/np.sqrt(3*in_channels*2))
self.weightsidentity = torch.nn.parameter.Parameter(torch.randn(out_channels,in_channels,len(rs))/np.sqrt(3*in_channels*2))
def forward(self,x):
# note
# the input is going to have in_channels
# the output is going to have out_channels*4
cxx = torch.repeat_interleave(self.weightsxx,4,0)[...,self.indsxx] * self.XhatXhat
cidentity = torch.repeat_interleave(self.weightsidentity,4,0)[...,self.indsidentity]*self.identity
c = cxx + cidentity
tmp = torch.nn.functional.pad(x,(self.padding,self.padding,self.padding,self.padding),mode=self.padding_mode)
return torch.nn.functional.conv2d(tmp,c) # no bias when output is matrix
class MatrixToScalar(torch.nn.Module):
def __init__(self,in_channels, out_channels, kernel_size, padding=0, bias=True,padding_mode='zeros'):
super().__init__()
# what's the main idea here?
# for the identity, we can do a regular conv, then multiply by identity
# for the xx we have to actually do the bigger convolution
# since we're doing the bigger convolution, we might as well just do it
self.in_channels = in_channels
self.out_channels = out_channels
self.kernel_size = kernel_size
self.padding = padding
self.padding_mode = padding_mode
if padding_mode == 'zeros': self.padding_mode = 'constant'
# use kernel size to get x
r = (kernel_size - 1)//2
x = torch.arange(-r,r+1)
X = torch.stack(torch.meshgrid(x,x,indexing='ij'),-1)
R = torch.sqrt(torch.sum(X**2,-1))
Xhat = X/R[...,None]
Xhat[R==0] = 0
# reshape it to the way I will want to use it
# it should match out channels on the left
# we need XhatXhat, and identity
Xhat = Xhat.permute(-1,0,1)
XhatXhat = Xhat[None,:]*Xhat[:,None]
XhatXhat = XhatXhat.reshape(1,4,kernel_size,kernel_size)
XhatXhat = XhatXhat.repeat((out_channels,in_channels,1,1))
rs,inds = torch.unique(R,return_inverse=True)
indsxx = inds.clone()-1
indsxx[indsxx==-1] = 0# wlil get multiplied by zero
# register buffers, this will allow them to move to devices
indsidentity = inds
identity = torch.eye(2).reshape(1,4).repeat((out_channels,in_channels))[...,None,None]
self.register_buffer('XhatXhat',XhatXhat)
self.register_buffer('identity',identity)
self.register_buffer('indsxx',indsxx)
self.register_buffer('indsidentity',indsidentity)
self.weightsxx = torch.nn.parameter.Parameter(torch.randn(out_channels,in_channels,len(rs)-1)/np.sqrt(3*in_channels*2))
self.weightsidentity = torch.nn.parameter.Parameter(torch.randn(out_channels,in_channels,len(rs))/np.sqrt(3*in_channels*2))
self.bias = torch.nn.parameter.Parameter(torch.randn(out_channels)/np.sqrt(3.0))
def forward(self,x):
cxx = torch.repeat_interleave(self.weightsxx,4,1)[...,self.indsxx]*self.XhatXhat
cidentity = torch.repeat_interleave(self.weightsidentity,4,1)[...,self.indsidentity]*self.identity
c = cxx + cidentity
tmp = torch.nn.functional.pad(x,(self.padding,self.padding,self.padding,self.padding),mode=self.padding_mode)
return torch.nn.functional.conv2d(tmp,c,self.bias)
class MatrixToVector(torch.nn.Module):
def __init__(self,in_channels, out_channels, kernel_size, padding=0, bias=True,padding_mode='zeros'):
''' Here we need to act with an operator that has 3 indices, and sum over two of them
'''
super().__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.kernel_size = kernel_size
self.padding = padding
self.padding_mode = padding_mode
if padding_mode == 'zeros': self.padding_mode = 'constant'
# use kernel size to get x
r = (kernel_size - 1)//2
x = torch.arange(-r,r+1)
X = torch.stack(torch.meshgrid(x,x,indexing='ij'),-1)
R = torch.sqrt(torch.sum(X**2,-1))
rs,inds = torch.unique(R,return_inverse=True)
indsxxx = inds.clone()-1
indsxxx[indsxxx==-1] = 0 # will get multiplied by zero
indsidentity = inds
# identity
identity = torch.eye(2)[:,:,None,None]
# build up Xhat
Xhat = X/R[...,None]
Xhat[R==0] = 0
Xhat = Xhat.permute(-1,0,1) # put the vector components in the front
# now we have these guys
XXX = Xhat[:,None,None]*Xhat[None,:,None]*Xhat[None,None,:]
# or
XDD = Xhat[:,None,None] * identity[None,:,:]
# or
DXD = Xhat[None,:,None]*identity[:,None,:]
# or
DDX = identity[:,:,None]*Xhat[None,None,:]
# now reshape them and tile them
XXX = XXX.reshape(2,4,kernel_size,kernel_size).repeat(out_channels,in_channels,1,1)
XDD = XDD.reshape(2,4,kernel_size,kernel_size).repeat(out_channels,in_channels,1,1)
DXD = DXD.reshape(2,4,kernel_size,kernel_size).repeat(out_channels,in_channels,1,1)
DDX = DDX.reshape(2,4,kernel_size,kernel_size).repeat(out_channels,in_channels,1,1)
# register buffers, this will allow them to move to devices
self.register_buffer('XXX',XXX)
self.register_buffer('XDD',XDD)
self.register_buffer('DXD',DXD)
self.register_buffer('DDX',DDX)
self.register_buffer('indsxxx',indsxxx)
self.weightsxxx = torch.nn.parameter.Parameter(torch.randn(out_channels,in_channels,len(rs)-1)/np.sqrt(3*in_channels*2))
self.weightsxdd = torch.nn.parameter.Parameter(torch.randn(out_channels,in_channels,len(rs)-1)/np.sqrt(3*in_channels*2))
self.weightsdxd = torch.nn.parameter.Parameter(torch.randn(out_channels,in_channels,len(rs)-1)/np.sqrt(3*in_channels*2))
self.weightsddx = torch.nn.parameter.Parameter(torch.randn(out_channels,in_channels,len(rs)-1)/np.sqrt(3*in_channels*2))
def forward(self,x):
cxxx = torch.repeat_interleave(torch.repeat_interleave(self.weightsxxx,4,1),2,0)[...,self.indsxxx]*self.XXX
cddx = torch.repeat_interleave(torch.repeat_interleave(self.weightsddx,4,1),2,0)[...,self.indsxxx]*self.DDX
cdxd = torch.repeat_interleave(torch.repeat_interleave(self.weightsdxd,4,1),2,0)[...,self.indsxxx]*self.DXD
cxdd = torch.repeat_interleave(torch.repeat_interleave(self.weightsxdd,4,1),2,0)[...,self.indsxxx]*self.XDD
c = cxxx + cddx + cdxd + cxdd
tmp = torch.nn.functional.pad(x,(self.padding,self.padding,self.padding,self.padding),mode=self.padding_mode)
return torch.nn.functional.conv2d(tmp,c)
class VectorToMatrix(torch.nn.Module):
def __init__(self,in_channels, out_channels, kernel_size, padding=0, bias=True,padding_mode='zeros'):
''' Here we need to act with an operator that has 3 indices, and sum over two of them
'''
super().__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.kernel_size = kernel_size
self.padding = padding
self.padding_mode = padding_mode
if padding_mode == 'zeros': self.padding_mode = 'constant'
# use kernel size to get x
r = (kernel_size - 1)//2
x = torch.arange(-r,r+1)
X = torch.stack(torch.meshgrid(x,x,indexing='ij'),-1)
R = torch.sqrt(torch.sum(X**2,-1))
rs,inds = torch.unique(R,return_inverse=True)
indsxxx = inds.clone()-1
indsxxx[indsxxx==-1] = 0 # will get multiplied by zero
indsidentity = inds
# identity
identity = torch.eye(2)[:,:,None,None]
# build up Xhat
Xhat = X/R[...,None]
Xhat[R==0] = 0
Xhat = Xhat.permute(-1,0,1) # put the vector components in the front
# now we have these guys
XXX = Xhat[:,None,None]*Xhat[None,:,None]*Xhat[None,None,:]
# or
XDD = Xhat[:,None,None] * identity[None,:,:]
# or
DXD = Xhat[None,:,None]*identity[:,None,:]
# or
DDX = identity[:,:,None]*Xhat[None,None,:]
# now reshape them and tile them
XXX = XXX.reshape(4,2,kernel_size,kernel_size).repeat(out_channels,in_channels,1,1)
XDD = XDD.reshape(4,2,kernel_size,kernel_size).repeat(out_channels,in_channels,1,1)
DXD = DXD.reshape(4,2,kernel_size,kernel_size).repeat(out_channels,in_channels,1,1)
DDX = DDX.reshape(4,2,kernel_size,kernel_size).repeat(out_channels,in_channels,1,1)
# register buffers, this will allow them to move to devices
self.register_buffer('XXX',XXX)
self.register_buffer('XDD',XDD)
self.register_buffer('DXD',DXD)
self.register_buffer('DDX',DDX)
self.register_buffer('indsxxx',indsxxx)
self.weightsxxx = torch.nn.parameter.Parameter(torch.randn(out_channels,in_channels,len(rs)-1)/np.sqrt(3*in_channels*2))
self.weightsxdd = torch.nn.parameter.Parameter(torch.randn(out_channels,in_channels,len(rs)-1)/np.sqrt(3*in_channels*2))
self.weightsdxd = torch.nn.parameter.Parameter(torch.randn(out_channels,in_channels,len(rs)-1)/np.sqrt(3*in_channels*2))
self.weightsddx = torch.nn.parameter.Parameter(torch.randn(out_channels,in_channels,len(rs)-1)/np.sqrt(3*in_channels*2))
def forward(self,x):
cxxx = torch.repeat_interleave(torch.repeat_interleave(self.weightsxxx,2,1),4,0)[...,self.indsxxx]*self.XXX
cddx = torch.repeat_interleave(torch.repeat_interleave(self.weightsddx,2,1),4,0)[...,self.indsxxx]*self.DDX
cdxd = torch.repeat_interleave(torch.repeat_interleave(self.weightsdxd,2,1),4,0)[...,self.indsxxx]*self.DXD
cxdd = torch.repeat_interleave(torch.repeat_interleave(self.weightsxdd,2,1),4,0)[...,self.indsxxx]*self.XDD
c = cxxx + cddx + cdxd + cxdd
tmp = torch.nn.functional.pad(x,(self.padding,self.padding,self.padding,self.padding),mode=self.padding_mode)
return torch.nn.functional.conv2d(tmp,c)
class MatrixToMatrix(torch.nn.Module):
def __init__(self,in_channels, out_channels, kernel_size, padding=0, bias=True,padding_mode='zeros'):
''' Here we need to act with an operator that has 3 indices, and sum over two of them
'''
super().__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.kernel_size = kernel_size
self.padding = padding
self.padding_mode = padding_mode
if padding_mode == 'zeros': self.padding_mode = 'constant'
# use kernel size to get x
r = (kernel_size - 1)//2
x = torch.arange(-r,r+1)
X = torch.stack(torch.meshgrid(x,x,indexing='ij'),-1)
R = torch.sqrt(torch.sum(X**2,-1))
rs,inds = torch.unique(R,return_inverse=True)
indsxxxx = inds.clone()-1
indsxxxx[indsxxxx==-1] = 0 # will get multiplied by zero
indsidentity = inds
# identity
identity = torch.eye(2)[:,:,None,None]
# build up Xhat
Xhat = X/R[...,None]
Xhat[R==0] = 0
Xhat = Xhat.permute(-1,0,1) # put the vector components in the front
# first all Xs (1)
XXXX = Xhat[:,None,None,None]*Xhat[None,:,None,None]*Xhat[None,None,:,None]*Xhat[None,None,None,:]
# now with one identity (6)
XXDD = Xhat[:,None,None,None]*Xhat[None,:,None,None]*identity[None,None,:,:]
# or
XDXD = Xhat[:,None,None,None]*identity[None,:,None,:]*Xhat[None,None,:,None]
# or
XDDX = Xhat[:,None,None,None]*identity[None,:,:,None]*Xhat[None,None,None,:]
# or
DXXD = identity[:,None,None,:]*Xhat[None,:,None,None]*Xhat[None,None,:,None]
# or
DXDX = identity[:,None,:,None]*Xhat[None,:,None,None]*Xhat[None,None,None,:]
# or
DDXX = identity[:,:,None,None]*Xhat[None,None,:,None]*Xhat[None,None,None,:]
# now with two identities (2)
DDDD0 = identity[:,:,None,None]*identity[None,None,:,:]
DDDD1 = identity[:,None,:,None]*identity[None,:,None,:]
DDDD2 = identity[:,None,None,:]*identity[None,:,:,None]
# now reshape them and tile them
XXXX = XXXX.reshape(4,4,kernel_size,kernel_size).repeat(out_channels,in_channels,1,1)
XXDD = XXDD.reshape(4,4,kernel_size,kernel_size).repeat(out_channels,in_channels,1,1)
XDXD = XDXD.reshape(4,4,kernel_size,kernel_size).repeat(out_channels,in_channels,1,1)
XDDX = XDDX.reshape(4,4,kernel_size,kernel_size).repeat(out_channels,in_channels,1,1)
DXXD = DXXD.reshape(4,4,kernel_size,kernel_size).repeat(out_channels,in_channels,1,1)
DXDX = DXDX.reshape(4,4,kernel_size,kernel_size).repeat(out_channels,in_channels,1,1)
DDXX = DDXX.reshape(4,4,kernel_size,kernel_size).repeat(out_channels,in_channels,1,1)
DDDD0 = DDDD0.reshape(4,4,1,1).repeat(out_channels,in_channels,1,1)
DDDD1 = DDDD1.reshape(4,4,1,1).repeat(out_channels,in_channels,1,1)
DDDD2 = DDDD2.reshape(4,4,1,1).repeat(out_channels,in_channels,1,1)
# register buffers, this will allow them to move to devices
self.register_buffer('XXXX',XXXX)
self.register_buffer('XXDD',XXDD)
self.register_buffer('XDXD',XDXD)
self.register_buffer('XDDX',XDDX)
self.register_buffer('DXXD',DXXD)
self.register_buffer('DXDX',DXDX)
self.register_buffer('DDXX',DDXX)
self.register_buffer('DDDD0',DDDD0)
self.register_buffer('DDDD1',DDDD1)
self.register_buffer('DDDD2',DDDD2)
self.register_buffer('indsxxxx',indsxxxx)
self.register_buffer('indsidentity',indsidentity)
self.weightsxxxx = torch.nn.parameter.Parameter(torch.randn(out_channels,in_channels,len(rs)-1)/np.sqrt(3*in_channels*2))
self.weightsxxdd = torch.nn.parameter.Parameter(torch.randn(out_channels,in_channels,len(rs)-1)/np.sqrt(3*in_channels*2))
self.weightsxdxd = torch.nn.parameter.Parameter(torch.randn(out_channels,in_channels,len(rs)-1)/np.sqrt(3*in_channels*2))
self.weightsxddx = torch.nn.parameter.Parameter(torch.randn(out_channels,in_channels,len(rs)-1)/np.sqrt(3*in_channels*2))
self.weightsdxxd = torch.nn.parameter.Parameter(torch.randn(out_channels,in_channels,len(rs)-1)/np.sqrt(3*in_channels*2))
self.weightsdxdx = torch.nn.parameter.Parameter(torch.randn(out_channels,in_channels,len(rs)-1)/np.sqrt(3*in_channels*2))
self.weightsddxx = torch.nn.parameter.Parameter(torch.randn(out_channels,in_channels,len(rs)-1)/np.sqrt(3*in_channels*2))
self.weightsdddd0 = torch.nn.parameter.Parameter(torch.randn(out_channels,in_channels,len(rs))/np.sqrt(3*in_channels*2))