-
Notifications
You must be signed in to change notification settings - Fork 8
/
tensor.lisp
1181 lines (1046 loc) · 28.6 KB
/
tensor.lisp
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
(defpackage th.tensor-examples
(:use #:common-lisp
#:mu
#:th))
(in-package :th.tensor-examples)
;; plain storage
(prn (storage.float))
;; creates an empty tensor of default tensor class (float)
(prn (tensor))
;; creates a tensor of default tensor class type with specified sizes;
;; elements are not initialized.
(prn (tensor 2 2))
;; creates a tensor with contents
(prn (tensor '(1 2 3 4)))
(prn (tensor '((1 2 3 4))))
;; creates a tensor with multidimensional contents
(prn (tensor '((1 2) (3 4) (5 6))))
;; creates tensor with other tensor, content storage is shared.
(let ((x (tensor '(4 3 2 1))))
(prn "X = (4 3 2 1)")
(prn x)
(prn "SAME CONTENT AS X")
(prn (tensor x)))
;; create tensor with sizes and strides, elements are not initialized.
(prn (tensor '(2 2) '(2 1)))
;; type specific construction functions; same as above.
(prn (tensor.byte '(1 2 3 4)))
(prn (tensor.char '(1 2 3 4)))
(prn (tensor.short '(1 2 3 4)))
(prn (tensor.int '(1 2 3 4)))
(prn (tensor.long '(1 2 3 4)))
(prn (tensor.float '(1 2 3 4)))
(prn (tensor.double '(1 2 3 4)))
;; clone a tensor creates a new tensor with independent storage.
(let ((x (tensor '((1 2) (3 4)))))
(prn "'((1 2) (3 4))")
(prn ($clone x)))
;; make a tensor with contiguously allocated memory if it is not allocated contiguously.
(prn ($contiguous! (tensor.float '((1 2 3) (4 5 6)))))
;; tensor types can be changed to each other
(prn (tensor.byte (tensor.double '((1.234 2.345) (3.456 4.567)))))
(prn (tensor.double (tensor.int '((1 2 3) (4 5 6)))))
(prn (tensor.long (tensor.float '(1.2 3.4 5.6 7.8))))
;; check whether it is tensor or not
(prn ($tensorp 0))
(prn ($tensorp (tensor)))
;; query number of dimensions
(prn ($ndim (tensor)))
(prn ($ndim (tensor 2 3 4)))
;; query tensor size
(prn ($size (tensor 2 3 4)))
;; query tensor size along given dimension
(prn ($size (tensor 2 3 4) 2))
;; size of scalar value is nil
(prn ($size 1))
;; stride of a tensor
(prn ($stride (tensor 2 3 4)))
;; stride of a tensor along dimension
(prn ($stride (tensor 2 3 4) 2))
;; stride of a nil object
(prn ($stride nil))
;; storage of a tensor; a tensor is a specific view on the storage
(let* ((x (tensor 4 5))
(s ($storage x)))
(loop :for i :from 0 :below ($count s)
:do (setf ($ s i) i))
(prn "'((0 1 2 3 4) ... (15 16 17 18 19))")
(prn x))
;; contiguous or not
(prn ($contiguousp (tensor)))
(prn ($contiguousp (tensor 2 2 2)))
;; size comparison using size value
(prn (equal ($size (tensor 2 3)) ($size (tensor 2 3))))
(prn (equal ($size (tensor)) ($size (tensor 2 3))))
(prn (equal ($size (tensor 2 2)) '(2 2)))
;; size comparison
(prn ($sizep (tensor 2 2) (tensor 2 2)))
(prn ($sizep (tensor 2 3) (tensor)))
;; number of elements in a tensor
(prn ($count (tensor 3 3 4)))
;; query the element at the index location
(let ((x (tensor '((1 2 3) (4 5 6)))))
(prn "'((1 2 3) (4 5 6))")
(prn x)
(prn "1")
(prn ($ x 0 0)))
;; creates a new view on the same storage of the given tensor
(let ((x (tensor))
(y (tensor '((1 2) (3 4)))))
(prn "((1 2) (3 4))")
(prn ($set! x y))
(prn "T")
(prn ($setp x y)))
;; copy elements from other tensor; both should have the same number of elements.
(let ((x (tensor '(1 2 3 4)))
(y (tensor '((5 4) (3 2)))))
;; now the elements of x replaced with those of y
($copy! x y)
(prn "(5 4 3 2)")
(prn x)
(prn "((5 4) (3 2))")
(prn y)
;; new int tensor from x, then copies elements from list
;; new int tensor will have same size of x
(prn "(123 234 345 456)")
(prn ($copy! (tensor.int x) '(123 234 345 456)))
(prn "(5 4 3 2)")
;; storage is not shared
(prn x))
;; fill values
(let ((x (tensor 3 3)))
($fill x 123)
(prn "((123 123 123) ... (123 123 123))")
(prn x)
(prn "((0 0 0) ... (0 0 0))")
;; mutable method
(prn ($zero! x))
(prn "((0 0 0) ... (0 0 0))")
(prn x)
(prn "((1 1 1) ... (1 1 1))")
;; immutable method
(prn ($one x))
(prn "((0 0 0) ... (0 0 0))")
(prn x))
;; resizing a tensor allocates if more memory storage is required
;; note that $view only changes sizes or shape but without changing allocated memory.
(let ((x (tensor '((1 2 3) (3 4 5))))
(y (tensor 3 3)))
(prn "((1 2 3 3) (4 5 ? ?) ... (? ? ? ?)))")
(prn ($resize! x '(4 4)))
(prn "((1 2) (3 3))")
(prn ($resize! x '(2 2)))
;; resize as y
(prn "((1 2 3) (3 4 5) (? ? ?))")
(prn ($resize! x y)))
;; select - choose sub tensor at index along dimension
(let ((x (tensor '((1 2 3) (4 5 6) (7 8 9)))))
(prn "(4 5 6) - 2nd(1) row along 1st(0) dimension")
(prn ($select x 0 1))
(prn "(3 6 9) - 3rd(2) column along 2nd(1) dimension")
(prn ($select x 1 2))
(setf ($select x 0 1) '(-11 -22 -33))
(prn "x with 2nd row changed as (-11 -22 -33)")
(prn x))
;; subdimensional select using $
(let ((x (tensor '((1 2 3) (4 5 6) (7 8 9)))))
(prn "1st row - (1 2 3)")
(prn ($ x 0))
(setf ($ x 1) '(6 5 4))
(prn "2nd row changes as (6 5 4)")
(prn x))
;; narrow - start and size along dimension
;; returns a new tensor or view built with narrowing from start selected as size along dimension
(let ((x (tensor 5 6)))
($zero! x)
(prn "5x5 matrix filled with zeros.")
(prn x)
;; along 1st(0) dimension, from 2nd row, select 3 rows, then fill it as one.
(-> ($narrow x 0 1 3)
($fill! 1))
(prn "along 1st(0) dimension, from 2nd(1) row, total 3 rows are filled with one.")
(prn x)
(-> ($narrow x 1 1 4)
($fill! 2))
(prn "along 2nd(1) dimension, from 2nd(1) column, total 4 columns are filled with two.")
(prn x)
(setf ($narrow x 1 0 2) '(0 11 22 33 44 55 66 77 88 99))
(prn "along 2nd(1) dimension, from 1st(0) column, total 2 columns are copied from list.")
(prn x))
;; subview - multiple start and size per dimension, kind of multiple narrows
;; each pair of start and size along each dimensions
(let ((x (tensor '((1 2 3 4 5 6)
(2 3 4 5 6 7)
(3 4 5 6 7 8)
(4 5 6 7 8 9)
(0 0 0 0 0 0)))))
(prn "5x6 matrix.")
(prn x)
(prn "((4 5 6) (5 6 7) (6 7 8)) - from 2nd row, 3 rows, from 3rd column, 3 columns.")
(prn ($subview x 1 3 2 3))
(setf ($subview x 1 3 2 3) '(11 12 13 14 15 16 17 18 19))
(prn "((11 12 13) (14 15 16) (17 18 19))")
(prn ($subview x 1 3 2 3))
(prn "matrix changes.")
(prn x))
;; orderly subview using $
(let ((x (tensor '((1 2 3 4 5 6)
(2 3 4 5 6 7)
(3 4 5 6 7 8)
(4 5 6 7 8 9)
(0 0 0 0 0 0)))))
(prn "((4 5 6) (5 6 7) (6 7 8)) - from 2nd row, 3 rows, from 3rd column, 3 columns.")
(prn ($ x '(1 3) '(2 3)))
(setf ($ x '(1 3) '(2 3)) '(11 12 13 14 15 16 17 18 19))
(prn "((11 12 13) (14 15 16) (17 18 19)) in x")
(prn x))
;; query with dimension index size pairs or subview using $
(let ((x (tensor '((1 2 3 4 5 6)
(2 3 4 5 6 7)
(3 4 5 6 7 8)
(4 5 6 7 8 9)
(0 0 0 0 0 0)))))
(prn "original x")
(prn x)
(prn "subview of size 3x3, from 2nd row and 3rd column")
(prn ($ x '(1 3) '(2 3)))
(setf ($ x '(1 3) '(2 3)) '(11 12 13 14 15 16 17 18 19))
(prn "3x3 subview changed as 11 to 19")
(prn ($ x '(1 3) '(2 3)))
(prn "changed x")
(prn x))
;; general selection using $
(let ((x (zeros 5 6)))
(prn "5x6 zero matrix.")
(prn x)
(setf ($ x 0 2) 1)
(prn "1 at 1st row, 3rd column")
(prn x)
(setf ($ x 4) 9)
(prn "5th row as 9")
(prn x)
(setf ($ x '(:all (5 1))) 8)
(prn "6th column as 8")
(prn x)
(setf ($ x '((1 1) (1 3))) 2)
(prn "1x3 from 2nd row and 2nd column, filled with 2.")
(prn x)
(setf ($ x '((0 5) (3 1))) -1)
(prn "5x1 from 1st row and 4th column, filled with -1.")
(prn x)
(setf ($ x '((0 5) (1 1))) (range 1 5))
(prn "5x1 from 1st row and 2nd column, copied from (1 ... 5)")
(prn x)
(setf ($ x ($lt x 0)) 5)
(prn "element as 5 if oringinal one is less than 0.")
(prn x))
;; index-select - creates a new storage, not sharing
;; collects subtensor along dimension at indices
(let ((x (tensor '((1 2 3) (4 5 6) (7 8 9)))))
(prn "original x")
(prn x)
(prn "1st(0) and 2nd(1) along 1st(0) dimension")
(prn ($index x 0 '(0 1)))
(let ((y ($index x 1 '(1 2))))
(prn "2nd(1) and 3rd(2) along 2nd(1) dimension")
(prn y)
($fill! y 0)
(prn "zero filled")
(prn y)
(prn "x unchanged")
(prn x)))
;; index-copy - set copies elements into selected index location
(let ((x (tensor '((1 2 3 4) (2 3 4 5) (3 4 5 6) (4 5 6 7) (5 6 7 8))))
(y (tensor 5 2)))
($fill! ($select y 1 0) -1)
($fill! ($select y 1 1) -2)
(prn "original x")
(prn x)
(prn "original y")
(prn y)
(setf ($index x 1 '(3 0)) y)
(prn "4th(3) and 1st(0) columns along 2nd(1) dimension copied from y.")
(prn x))
;; index-fill
(let ((x (tensor '((1 2 3 4) (2 3 4 5) (3 4 5 6) (4 5 6 7) (5 6 7 8)))))
(prn "original x")
(prn x)
(setf ($index x 1 '(0 3)) 123)
(prn "1st(0) and 4th(3) columns along 2nd(1) dimension set as 123")
(prn x))
;; gather
(let ((x (tensor 5 5)))
(loop :for i :from 0 :below ($count x)
:do (setf ($ ($storage x) i) i))
(prn "original 5x5 matrix")
(prn x)
(prn "by incrementing index, collect nth 1st(0) dimensional values.")
(prn "1st row will be (0 0), (1 1), (2 2), (3 3), (4 4)")
(prn "2nd row will be (1 0), (2 1), (3 2), (4 3), (0 4)")
(prn ($gather x 0 '((0 1 2 3 4) (1 2 3 4 0))))
(prn "by incrementing index, collect nth 2nd(1) dimensional values.")
(prn "1st column will be (0 0), (1 1), (2 2), (3 3), (4 4)")
(prn "2nd column will be (1 0), (2 1), (3 2), (4 3), (0 4)")
(prn ($gather x 1 '((0 1) (1 2) (2 3) (3 4) (4 0)))))
;; scatter
(let ((x (tensor 5 5))
(y (tensor '((1 2 3 4 5) (-5 -4 -3 -2 -1)))))
($zero! x)
(prn "5x5 zeros")
(prn x)
($scatter! x 0 '((0 1 2 3 4) (1 2 3 4 0)) y)
(prn "as in gather, but set from y")
(prn x)
($scatter! x 1 '((0 1) (1 2) (2 3) (3 4) (4 0)) 9)
(prn "as in gather, but fill a value")
(prn x))
;; masked-select
(let ((x (tensor 3 4))
(mask '((1 0 1 0 0 0) (1 1 0 0 0 1)))
(z (tensor)))
(loop :for i :from 0 :below ($count x)
:do (setf ($ ($storage x) i) (1+ i)))
(prn "original x")
(prn x)
(prn "only at value 1(true)")
(prn ($masked x mask))
($set! z ($masked x mask))
(prn "same as above.")
(prn z)
($fill! z -99)
(prn "z value changed as -99")
(prn z)
(prn "x unchanged.")
(prn x))
;; masked-copy
(let ((x (tensor 3 4))
(mask '((1 0 1 0 0 0) (1 1 0 0 0 1)))
(z (tensor '(1 2 3 4 5))))
($zero! x)
(prn "x matrix, 3x4")
(prn x)
(setf ($masked x mask) z)
(prn "set by z")
(prn x))
;; masked-fill
(let ((x (tensor 3 4))
(mask '((1 0 1 0 0 0) (1 1 0 0 0 1))))
($zero! x)
(prn "original 3x4 matrix")
(prn x)
(setf ($masked x mask) 5)
(prn "filled as 5")
(prn x))
;; nonzero - returns locations of non zero elements
(prn ($nonzero (tensor '((1 2 0 3 4) (0 0 1 0 0)))))
;; repeat - repeat content as given times
(prn ($repeat (tensor '(1 2)) 3 2))
(prn ($repeat (tensor '((1 2) (3 4) (5 6))) 2 3))
;; squeeze - removes singletone dimensions
(let ((x (tensor 2 1 2 1 2)))
(prn "original size")
(prn ($size x))
(prn "no 1s")
(prn ($size ($squeeze x)))
(prn "no 1 in 2nd(1) dimension")
(prn ($size ($squeeze x 1))))
;; unsqueeze - add a singleton dimension
(let ((x (tensor '(1 2 3 4))))
(prn "vector")
(prn x)
(prn "along 1st(0) dimension")
(prn ($unsqueeze x 0))
(prn "along 2nd(1) dimension")
(prn ($unsqueeze x 1)))
;; view - different from resize(allocation), reshape(new storage), just a view
(let ((x (tensor '(0 0 0 0))))
(prn "original vector")
(prn x)
(prn "2x2")
(prn ($view x 2 2))
(prn "as the size of other 2x2 tensor")
(prn ($view x (tensor 2 2))))
;; transpose
(let ((x (tensor '((1 2 3) (4 5 6)))))
(prn "original 2x3")
(prn x)
(prn "transposed 3x2")
(prn ($transpose x)))
;; transpose - shares storage but different view(tensor)
(let ((x (tensor 3 4)))
($zero! x)
($fill! ($select x 1 2) 7)
(prn "original x")
(prn x)
(let ((y ($transpose x)))
($fill! ($select y 1 2) 8)
(prn "modified transposed x or y")
(prn y)
(prn "original x")
(prn x)))
;; permute - multidimensional transposing
(let ((x (ones 3 4 2 5)))
(prn "original size")
(prn ($size x))
(prn "permute size with 2nd, 3rd, 1st and 4th dimensions - 4,2,3,5")
(prn ($size ($permute x 1 2 0 3))))
;; unfold - slice with size by step along dimension
(let ((x (ones 7)))
(loop :for i :from 1 :to 7 :do (setf ($ x (1- i)) i))
(prn "vector, 1 to 7")
(prn x)
(prn "slice along 1st(0) dimension, size of 2, by step 1")
(prn ($unfold x 0 2 1))
(prn "slice along 1st(0) dimension, size of 2, by step 2")
(prn ($unfold x 0 2 2)))
;; fmap - just elementwise function application
(let ((x (zeros 3 3))
(n 0))
($fmap (lambda (v) (+ v (* 0.5 pi (incf n)))) x)
(prn x)
($fmap (lambda (v) (round (sin v))) x)
(prn x))
;; fmap - more, shape is irrelevant when they have same count.
(let ((x (tensor 3 3))
(y (tensor 9))
(z (tensor '(0 1 2 3 4 5 6 7 8))))
(loop :for i :from 1 :to 9 :do (setf ($ ($storage x) (1- i)) i
($ ($storage y) (1- i)) i))
(prn x)
(prn y)
(prn z)
(prn "1*1, 2*2, 3*3, ...")
(prn ($fmap (lambda (vx vy) (* vx vy)) x y))
(prn "1 + 1 + 0, 2 + 2 + 1, 3 + 3 + 2, ...")
(prn ($fmap (lambda (xx yy zz) (+ xx yy zz)) x y z))
(prn x))
;; split - split tenor with size along dimension
(let ((x (zeros 3 4 5)))
(prn "by 2 along 0 - 2x4x5, 1x4x5")
(prn ($split x 2 0))
(prn "by 2 along 1 - 3x2x5, 3x2x5")
(prn ($split x 2 1))
(prn "by 2 along 2 - 3x4x2, 3x4x2, 3x4x1")
(prn ($split x 2 2)))
;; chunk - n parition of approaximately same size along dimension
(let ((x (ones 3 4 5)))
(prn "2 partitions along 0 - 2x4x5, 1x4x5")
(prn ($chunk x 2 0))
(prn "2 partitions along 1 - 3x2x5, 3x2x5")
(prn ($chunk x 2 1))
(prn "2 paritions along 2 - 3x4x3, 3x4x2")
(prn ($chunk x 2 2)))
;; concat tensors
(prn ($cat (ones 3) (zeros 3)))
(prn ($cat (ones 1 3) (zeros 1 3) 1))
(prn ($cat (ones 1 3) (zeros 1 3) 0))
(prn ($cat (ones 3 3) (zeros 1 3)))
(prn ($cat (ones 3 4) (zeros 3 2) 1))
;; diagonal matrix
(prn ($diag (tensor '(1 2 3 4))))
(prn ($diag (ones 3 3)))
;; identity matrix
(prn (eye 2))
(prn (eye 3 4))
(prn ($eye (tensor.byte) 3))
(prn ($eye (tensor.byte 10 20) 3 4))
;; linspace
(prn (linspace 1 2))
(prn (linspace 1 2 11))
;; logspace
(prn (logspace 1 2))
(prn (logspace 1 2 11))
;; uniform random
(prn (rnd 3 3))
;; normal random
(prn (rndn 2 4))
;; range
(prn (range 2 5))
(prn (range 2 5 1.2))
;; arange
(prn (arange 2 5))
(prn (range 2 5))
;; randperm
(prn (rndperm 10))
(prn (rndperm 5))
;; reshape
(let ((x (ones 2 3))
(y nil))
(prn x)
(setf y ($reshape x 3 2))
(prn y)
($fill! y 2)
(prn y)
(prn x))
;; tril and triu
(let ((x (ones 4 4)))
(prn ($tril x))
(prn ($tril x -1))
(prn ($triu x))
(prn ($triu x 1)))
;; abs
(let ((x (tensor '((-1 2) (3 -4)))))
(prn x)
(prn ($abs x))
(prn x)
(prn ($abs! x))
(prn x))
;; sign
(let ((x (tensor '((-1 2) (3 -4)))))
(prn x)
(prn ($sign x))
(prn x)
(prn ($sign! x))
(prn x))
;; acos
(let ((x (tensor '((-1 1) (1 -1)))))
(prn x)
(prn ($acos x))
(prn x)
(prn ($acos! x))
(prn x))
;; asin
(let ((x (tensor '((-1 1) (1 -1)))))
(prn x)
(prn ($asin x))
(prn x)
(prn ($asin! x))
(prn x))
;; atan
(let ((y (tensor '((-11 1) (1 -11)))))
(prn y)
(prn ($atan y))
(prn y)
(prn ($atan! y))
(prn y))
;; atan2
(let ((y (tensor '((-11 1) (1 -11))))
(x (tensor '((1 1) (1 1)))))
(prn y)
(prn ($atan2 y x))
(prn y)
(prn ($atan2! y x))
(prn y))
;; ceil
(let ((x (tensor '((1 1.1 1.7) (-0.8 -1.1 -2.3)))))
(prn x)
(prn ($ceil x))
(prn x)
(prn ($ceil! x))
(prn x))
;; cos
(let ((x (tensor '((-3.14 0) (3.14 0)))))
(prn x)
(prn ($cos x))
(prn x)
(prn ($cos! x))
(prn x))
;; cosh
(let ((x (tensor '((-3.14 0) (3.14 0)))))
(prn x)
(prn ($cosh x))
(prn x)
(prn ($cosh! x))
(prn x))
;; exp
(let ((x (tensor '((0 1 2) (-1 -2 -3)))))
(prn x)
(prn ($exp x))
(prn x)
(prn ($exp! x))
(prn x))
;; floor
(let ((x (tensor '((1 1.1 1.7) (-0.8 -1.1 -2.3)))))
(prn x)
(prn ($floor x))
(prn x)
(prn ($floor! x))
(prn x))
;; log
(let ((x ($exp (tensor '((0 1 2) (-1 -2 -3))))))
(prn x)
(prn ($log x))
(prn x)
(prn ($log! x))
(prn x))
;; log1p
(let ((x ($exp (tensor '((0 1 2) (-1 -2 -3))))))
(prn x)
(prn ($log1p x))
(prn x)
(prn ($log1p! x))
(prn x))
;; neg
(let ((x (tensor '((0 1 2) (-1 -2 -3)))))
(prn x)
(prn ($neg x))
(prn x)
(prn ($neg! x))
(prn x))
;; cinv
(let ((x (tensor '((3 2 1) (-1 -2 -3)))))
(prn x)
(prn ($cinv x))
(prn x)
(prn ($cinv! x))
(prn x))
;; expt
(let ((x (tensor '((2 3) (1 2))))
(y (tensor '((2 2) (3 3))))
(n 2))
(prn x)
(prn ($expt x n))
(prn ($expt n x))
(prn ($expt x y))
(prn x)
(prn ($expt! x n))
(prn x)
(prn ($expt! n x))
(prn x))
;; round
(let ((x (tensor '((1.1 1.8) (-1.1 -1.8)))))
(prn x)
(prn ($round x))
(prn x)
(prn ($round! x))
(prn x))
;; sin
(let ((x (tensor '((-3.14 0) (3.14 0)))))
(prn x)
(prn ($sin x))
(prn x)
(prn ($sin! x))
(prn x))
;; sinh
(let ((x (tensor '((-3.14 0) (3.14 0)))))
(prn x)
(prn ($sinh x))
(prn x)
(prn ($sinh! x))
(prn x))
;; sqrt
(let ((x (tensor '((1 2 4) (3 5 9)))))
(prn x)
(prn ($sqrt x))
(prn x)
(prn ($sqrt! x))
(prn x))
;; rsqrt
(let ((x (tensor '((1 2 4) (3 5 9)))))
(prn x)
(prn ($rsqrt x))
(prn x)
(prn ($rsqrt! x))
(prn x))
;; tan
(let ((x (tensor '((1 2) (3 4)))))
(prn x)
(prn ($tan x))
(prn x)
(prn ($tan! x))
(prn x))
;; tanh
(let ((x (tensor '((1 2) (-3 -4)))))
(prn x)
(prn ($tanh x))
(prn x)
(prn ($tanh! x))
(prn x))
;; sigmoid
(let ((x (tensor '((-2 -1) (1 2)))))
(prn x)
(prn ($sigmoid x))
(prn x)
(prn ($sigmoid! x))
(prn x))
;; equal
(let ((x (tensor '(1 2 3)))
(y (tensor '(1 2 3))))
(prn ($equal x y)))
;; add, sub, and mul
(let ((x (tensor '((1 2) (3 4))))
(y (tensor '((2 3) (4 5))))
(a 10))
(prn ($add x a))
(prn ($add x y))
(prn ($sub x a))
(prn ($sub x y))
(prn ($mul x a))
(prn ($mul x y)))
;; clamp
(let ((x (tensor '((1 2 3 4 5) (2 3 4 5 6) (3 4 5 6 7))))
(min 2)
(max 5))
(prn x)
(prn ($clamp x min max))
(prn x)
(prn ($clamp! x min max))
(prn x))
;; add-cmul
(let ((x (tensor 2 2))
(y (tensor 4))
(z (tensor 2 2)))
($fill! x 1)
($fill! y 3)
($fill! z 5)
(prn ($addmul x y z 2)))
;; div
(let ((x (ones 2 2))
(y (range 1 4)))
(prn ($div x y)))
;; add-cdiv
(let ((x (-> (tensor 2 2) ($fill! 1)))
(y (range 1 4))
(z (-> (tensor 2 2) ($fill! 5))))
(prn ($adddiv x y z 2)))
;; fmod, remainder
(let ((x (tensor '(-3 3))))
(prn ($fmod x 2))
(prn ($fmod x -2))
(prn ($rem x 2))
(prn ($rem x -2))
(prn ($fmod (tensor '((3 3) (-3 -3))) (tensor '((2 -2) (2 -2)))))
(prn ($rem (tensor '((3 3) (-3 -3))) (tensor '((2 -2) (2 -2))))))
;; dot
(let ((x (-> (tensor 2 2) ($fill! 3)))
(y (-> (tensor 4) ($fill! 2))))
(prn ($dot x y)))
;; add-mv
(let ((y (ones 3))
(m (-> (tensor 3 2) ($fill! 3)))
(x (-> (tensor 2) ($fill! 2))))
(prn ($mv m x))
(prn ($addmv y m x)))
;; add-r
(let ((x (range 1 3))
(y (range 1 2))
(m (ones 3 2)))
(prn ($addr m x y))
(prn ($addr! m x y 2 1)))
;; add-mm
(let ((c (ones 4 4))
(a (-> (range 1 12) ($resize! '(4 3))))
(b (-> (range 1 12) ($resize! '(3 4)))))
(prn ($addmm c a b)))
;; add-bmm
(let ((c (ones 4 4))
(ba (-> (range 1 24) ($resize! '(2 4 3))))
(bb (-> (range 1 24) ($resize! '(2 3 4)))))
(prn ($addbmm c ba bb)))
;; badd-bmm
(let ((bc (ones 2 4 4))
(ba (-> (range 1 24) ($resize! '(2 4 3))))
(bb (-> (range 1 24) ($resize! '(2 3 4)))))
(prn ($baddbmm bc ba bb)))
;; operators
(prn ($+ 5 (rnd 3)))
(let ((x (-> (tensor 2 2) ($fill! 2)))
(y (-> (tensor 4) ($fill! 3))))
(prn ($+ x y))
(prn ($- y x))
(prn ($+ x 3))
(prn ($- x)))
(let ((m (-> (tensor 2 2) ($fill! 2)))
(n (-> (tensor 2 4) ($fill! 3)))
(x (-> (tensor 2) ($fill! 4)))
(y (-> (tensor 2) ($fill! 5))))
(prn ($* x y))
(prn ($@ m x))
(prn ($@ m n)))
(prn ($/ (ones 2 2) 3))
;; cross
(let ((x (rndn 4 3))
(y (rndn 4 3))
(z (tensor)))
(prn x)
(prn y)
(prn ($xx x y))
(prn ($xx! z x y 1))
(prn z))
;; cumulative product
(let ((x (range 1 5))
(m (tensor.long '((1 4 7) (2 5 8) (3 6 9)))))
(prn x)
(prn ($cumprd x))
(prn m)
(prn ($cumprd m))
(prn ($cumprd m 0)))
;; cumulative sum
(let ((x (range 1 5))
(m (tensor.long '((1 4 7) (2 5 8) (3 6 9)))))
(prn x)
(prn ($cumsum x))
(prn m)
(prn ($cumsum m))
(prn ($cumsum m 1)))
;; max and min
(let ((x (rndn 4 4))
(vals (tensor))
(indices (tensor.long)))
(prn x)
(prn ($max x))
(prn ($min x))
(prn ($max! vals indices x))
(prn ($max! vals indices x 1)))
;; mean
(let ((x (rndn 3 4)))
(prn x)
(prn ($mean x))
(prn ($mean x 0))
(prn ($mean x 1)))
;; cmax
(let ((a (tensor '(1 2 3)))
(b (tensor '(3 2 1))))
(prn ($cmax a b))
(prn ($cmax a b 2 3)))
;; cmin
(let ((a (tensor '(1 2 3)))
(b (tensor '(3 2 1))))
(prn ($cmin a b))
(prn ($cmin a b 2 3)))
;; median
(let ((x (rndn 3 4))
(vals (tensor))
(indices (tensor.long)))
(prn x)
(prn ($median x))
(prn ($median! vals indices x))
(prn ($median! vals indices x 1)))
;; product
(let ((a (tensor '(((1 2) (3 4)) ((5 6) (7 8))))))
(prn a)
(prn ($prd a))
(prn ($prd a 0))
(prn ($prd a 1)))
;; sort
(let ((x (rndn 3 3))
(vals (tensor))
(indices (tensor.long)))
(prn x)
(prn ($sort! vals indices x)))
;; conv2
(let ((x (rnd 100 100))
(k (rnd 10 10)))
(prn ($size ($conv2 x k)))
(prn ($size ($conv2 x k :full))))
(let ((x (rnd 500 100 100))
(k (rnd 500 10 10)))
(prn ($size ($conv2 x k)))
(prn ($size ($conv2 x k :full))))
;; conv3 - slow, in this laptop, it takes ~6secs
(let ((x (rnd 100 100 100))
(k (rnd 10 10 10)))
(prn ($size ($conv3 x k)))
(prn ($size ($conv3 x k :full))))
;; gesv
(let ((a (-> (tensor '((6.80 -2.11 5.66 5.97 8.23)
(-6.05 -3.30 5.36 -4.44 1.08)
(-0.45 2.58 -2.70 0.27 9.04)
(8.32 2.71 4.35 -7.17 2.14)
(-9.67 -5.14 -7.26 6.08 -6.87)))
($transpose)))
(b (-> (tensor '((4.02 6.19 -8.22 -7.57 -3.03)
(-1.56 4.00 -8.67 1.75 2.86)
(9.81 -4.09 -4.57 -8.61 8.99)))
($transpose)))
(x (tensor))
(lu (tensor)))
(prn a)
(prn b)
(prn ($gesv! x lu b a))
(prn x)
(prn ($@ a x))
(prn ($dist b ($@ a x))))
;; trtrs
(let ((a (-> (tensor '((6.80 -2.11 5.66 5.97 8.23)
(0 -3.30 5.36 -4.44 1.08)
(0 0 -2.70 0.27 9.04)
(0 0 0 -7.17 2.14)
(0 0 0 0 -6.87)))))
(b (-> (tensor '((4.02 6.19 -8.22 -7.57 -3.03)
(-1.56 4.00 -8.67 1.75 2.86)
(9.81 -4.09 -4.57 -8.61 8.99)))
($transpose)))
(x (tensor)))
(prn a)
(prn b)
(prn ($trtrs! x b a))
(prn x)
(prn ($@ a x))
(prn ($dist b ($@ a x))))
;; potrf
(let ((a (tensor '((1.2705 0.9971 0.4948 0.1389 0.2381)
(0.9971 0.9966 0.6752 0.0686 0.1196)
(0.4948 0.6752 1.1434 0.0314 0.0582)
(0.1389 0.0686 0.0314 0.0270 0.0526)
(0.2381 0.1196 0.0582 0.0526 0.3957))))
(chu (tensor))