-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests_ops.py
1181 lines (1044 loc) · 40 KB
/
tests_ops.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
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
"""
This file contains all the test cases checking the functionality of all the operations defined in the ops.py file
All the tests can be run by command "pytest tests_ops.py"
"""
#Import required packages
import pytest
import numpy as np
from autodiff.core.ops import *
from autodiff.core.node import *
import autodiff as ad
from autodiff.core.grad import grad
def test_ReduceSumToShape1_as_scalar():
"""
Aim: Test whether ReduceSumToShape(which is a wrapper of ReduceSumKeepDims) exactly reduces an array to a scalar
Expected Result: 25
Obtained Result: 25
Remarks:None
"""
X= np.ones((5,5))
X = Variable(X,"X")
val = ReduceSumToShape(X,())
assert val() == 25 and isinstance(val,ReduceSumKeepDims)
def test_ReduceSumToShape2_as_column():
"""
Aim: Test whether ReduceSumToShape(which is a wrapper of ReduceSumKeepDims) exactly reduces an array to a column vector
Expected Result: [5,5,5,5,5]
Obtained Result: [5,5,5,5,5]
Remarks:None
"""
X = np.ones((5,5))
X = Variable(X,"X")
val = ReduceSumToShape(X,(5,))
print(val())
flag = np.array_equal(val(),[5., 5., 5., 5., 5.])
assert flag == True and isinstance(val,ReduceSumKeepDims)
def test_ReduceSumToShape3_as_row():
"""
Aim: Test whether ReduceSumToShape(which is a wrapper of ReduceSumKeepDims) exactly reduces an array into a single row
Expected Result: [[5,5,5,5,5]]
Obtained Result: [[5,5,5,5,5]]
Remarks:None
"""
X = np.ones((5,5))
X = Variable(X,"X")
val = ReduceSumToShape(X,(1,5))
print(val())
flag = np.array_equal(val(),np.full((1,5),5.))
assert flag == True and isinstance(val,ReduceSumKeepDims)
def test_add1_2equal():
"""
Aim:Test addition operation (if the value is correct and derivatives are passed as expected)
Expected Result:Add: [[2,4],[8,10]] dzdx=dzdy=[[1,1],[1,1]]
Obtained Result:[[2,4],[8,10]]
Remark: For the case where two variables are of same shape
"""
x = np.array([[1,2],[4,5]])
y = np.array([[1,2],[4,5]])
X = Variable(x,"X")
Y = Variable(y,"Y")
Z = x+y
Z1 = Add(X,Y)
dzdx,dzdy = grad(Z1,[X,Y])
assert np.array_equal(Z,Z1()) == True and isinstance(Z1,Add) and np.array_equal(dzdx(),np.ones_like(x)) and np.array_equal(dzdy(),np.ones_like(x))
def test_add2_matrix_vector():
"""
Aim:Test addition operation (if the value is correct and derivatives are passed as expected)
Expected Result:add = [[2,3][5,6]] and dzdx=[2,2],dzdy=[[1,1],[1,1]]
Obtained Result:
Remarks: For the case where one variable is matrix and other is 1D-vector
"""
x = np.ones((2,))
y = np.array([[1,2],[4,5]])
Z = x+y
X = Variable(x,"X")
Y = Variable(y,"Y")
Z1 = Add(X,Y)
dzdx,dzdy = grad(Z1,[X,Y])
print(dzdx())
print(np.full_like(x,1.0))
assert np.array_equal(Z,Z1()) == True and isinstance(Z1,Add) and np.array_equal(dzdx(),np.full_like(x,2)) and np.array_equal(dzdy(),np.full_like(y,1.0,dtype=type(dzdy())))
def test_add3_matrix_vector_fractions():
"""
Aim:Test addition operation (if the value is correct and derivatives are passed as expected)
Expected:[ 2., 3.],
[-3., 1.00324465]] and dzdx=[[2,2]] dzdy=[[1,1],[1,1]]
Obtained:[ 2., 3.],
[-3., 1.00324465]] and dzdx=[[2,2]] dzdy=[[1,1],[1,1]]
Remarks: For a 2D array and another 2D array as a row vector
"""
x = (np.ones((1,2)))
y = np.array([[1,2.0],[-4,5/1541]])
Z = x+y
X =Variable(x,"X")
Y = Variable(y,"Y")
Z1 = Add(X,Y)
dzdx,dzdy = grad(Z1,[X,Y])
print(dzdx())
assert np.array_equal(Z,Z1()) == True and isinstance(Z1,Add) and np.array_equal(dzdx(),np.full_like(x,2)) and np.array_equal(dzdy(),np.ones_like(y) )
def test_add4_irrational():
"""
Aim:Test addition operation (if the value is correct and derivatives are passed as expected)
Expected:add=13.141592653589793 dzdx=dzdy=1
Obtained:add=13.141592653589793 dzdx=dzdy=1
Remark: This test case ascertains the behaviour of Add when irrational numbers are passed and derivatives are also passed as expected
"""
x = np.pi
y = 10
Z = x+y
X = Variable(x,"X")
Y = Variable(y,"Y")
Z1 = Add(X,Y)
dzdx,dzdy = grad(Z1,[X,Y])
assert np.array_equal(Z,Z1()) == True and isinstance(Z1,Add) and dzdx()==dzdy()==1
"""
def test_add5_complex_meant_to_fail():
X = np.pi
print("This is meant to fail to show that Complex numbers are not supported")
Y = Variable(10j)
Z = X+Y
#Z1 = Add(X,Y)
assert isinstance(Y,TypeError)
"""
def test_Mul1_irrational():
"""
Aim:Test the Mul operation (if the value is correct and derivatives are passed as expected)
Expected:pi ,dzdx=pi,dzdy=1
Obtained:pi,dzdx=pi,dzdy=1
Remark:multiplying an irrational integer to check robustness of kernel
"""
x = 1
y = np.pi
Z = x*y
X = Variable(x,"X")
Y = Variable(y,"Y")
Z1 = Mul(X,Y)
dzdx,dzdy = grad(Z1,[X,Y])
assert isinstance(Z1,Mul) and Z1()==np.pi and dzdx()==y and dzdy()==x
def test_Mul2_matrix_scalar():
"""
Aim:Test the Mul operation (if the value is correct and derivatives are passed as expected)
Obtained: [[3 6],[6 6]] and dzdx=[[3 3] [3 3]] , dzdy=7
Expected: [[3 6],[6 6]] and dzdx=[[3 3] [3 3]] , dzdy=7
Remarks: Multiplying a scalar with an array to check that derivatives are broadcasted correctly
"""
x = np.array([[1,2],[2,2]])
y = 3
Z = x*y
X = Variable(x,"X")
Y= Variable(y,"Y")
Z1 = Mul(X,Y)
dzdx,dzdy = grad(Z1,[X,Y])
print(dzdx())
print(dzdy())
assert isinstance(Z1,Mul) and np.array_equal(Z1(),Z)==True and np.array_equal(dzdx(),np.full_like(x,y)) and np.array_equal(dzdy(),7)
def test_Mul3_matrix_vector():
"""
Aim:Test the Mul operation (if the value is correct and derivatives are passed as expected)
Obtained: mul=[[1 0][0 0]] and dzdy=[[1 0] [1 0]]dzdx=[1,0.423310825130748]
Expected: mul=[[1 0][0 0]] and dzdy=[[1 0] [1 0]]dzdx=[1,0.423310825130748]
Remarks: Multiplying a scalar with a vector to check that derivatives are broadcasted correctly
"""
x = np.array([1,0])
y = np.array([[1,np.pi],[0,-np.exp(1)]])
Z = x*y
X = Variable(x,"X")
Y = Variable(y,"Y")
Z1 = Mul(X,Y)
dzdx,dzdy = grad(Z1,[X,Y])
print(dzdx())
print(dzdy())
assert isinstance(Z1,Mul) and np.array_equal(Z1(),Z)==True and np.array_equal(dzdy(),np.array([[1.,0.],[1.,0.]])) and np.array_equal(dzdx(),np.array([1,np.pi-np.exp(1)]))
def test_Negate_zero():
"""
Aim: To check the negation operation((if the value is correct and derivatives are passed as expected))
Expected: 0, dydx = -1
Obtained:0, dydx = -1
Remarks:This is a typical case , because the negation of zero is itself but the gradient passed should change it's sign.
"""
X = Variable(0,"X")
Y = Negate(X)
dy = grad(Y,[X])[0]
assert isinstance(Y,Negate) and Y()==0 and dy()==-1
def test_Negate_array():
"""
Aim: To check the negation operation((if the value is correct and derivatives are passed as expected))
Expected: [[-np.pi,-2.0],[+3,-4.2222222]], dydx = [[-1 -1][-1 -1]]
Obtained:[[-np.pi,-2.0],[+3,-4.2222222]], dydx = [[-1 -1][-1 -1]]
Remarks: taking all possible types of numbers and negating them
"""
x = np.array([[np.pi,2.0],[-3,4.2222222]])
X = Variable(x,"X")
Y = Negate(X)
dy = grad(Y,[X])[0]
assert isinstance(Y,Negate) and np.array_equal(Y(),-1*x) == True and np.array_equal(dy(),np.full((2,2),-1.))
def test_recipr_scalar():
"""
Aim: To test the operation Reciprocal(which basically does element-wise reciprocal)
Obtained: 0.19999999999996 and dy = -0.04
Expected: 0.2 and dy = -0.04
Remarks: This small deviation marks the distinctive feature employed in all the operations which might have to deal with unwanted infinities. In this case division by zero is avoided by adding a very small innate error of 1e-12
"""
x= 5
X = Variable(x,"X")
Y= Recipr(X)
dy = grad(Y,[X])[0]
assert isinstance(Y,Recipr) and np.abs(Y() - (1/(5+1e-12))) < 0.0000001 and np.abs(dy() - np.array(-0.04)) < 0.000000001
def test_recipr_irrational_array():
"""
Aim:To test the operation Reciprocal(which basically does element-wise reciprocal)
Expected:[[0.31830989, 0.31818182], dy = [[1.01321184e-01, 1.01239669e-01],
[2.71828183, 0.082085 ]] [7.38905610e+00, 6.73794700e-03]]
Obtained:[[0.31830989, 0.31818182], dy= [[1.01321184e-01, 1.01239669e-01],
[2.71828183, 0.082085 ]] [7.38905610e+00, 6.73794700e-03]]
Remarks:Deviation due to buffer avoiding infinities and invoking itself for passage of derivatives is also tested.
"""
x = np.array([[np.pi,22/7],[np.exp(-1),np.exp(2.5)]])
X = Variable(x,"X")
Y = Recipr(X)
dy = grad(Y,[X])[0]
print(dy())
print(-np.reciprocal((x+1e-12)*(x+1e-12)))
assert isinstance(Y,Recipr) and np.array_equal(Y(),np.reciprocal(x+1e-12)) and np.all(np.less(np.abs(dy()+np.reciprocal((x+1e-12)*(x+1e-12))) , np.full_like(dy(),0.0000001)))
def test_einsum_onearg_identity():
"""
Aim:To test the operation Einsum
Expected: random array of shape (2,3,5,7)
Obtained: the same random array of shape (2,3,5,7)
Remarks: Same indices on both sides of arrow of an einsum means Identity wrapper of einsum is tested here
"""
X = np.random.randn(2,3,5,7)
Xv = Variable(X,"Xv")
Z = Einsum("ijkl->ijkl",Xv)
assert isinstance(Z,Einsum) and np.array_equal(Z(),X) == True
def test_einsum_onearg_sum_1axis():
"""
Aim:To test the operation Einsum
Expected: contracted random array(4th order tensor ) along the last dimension
Obtained: contracted random array(4th order tensor ) along the last dimension
Remarks: this test tests the single contraction of a 4th order tensor with itself(i.e reduce sum keep dimensions 1,2,3)
"""
X = np.random.randn(2,3,5,7)
Xv = Variable(X,"Xv")
Z = Einsum("ijkl->ijk",Xv)
assert isinstance(Z,Einsum) and np.array_equal(Z(),np.einsum("ijkl->ijk",X)) == True
def test_einsum_onearg_sum_2axis():
"""
Aim:To test the operation Einsum
Expected: contracted array along the last two dimensions
Obtained: contracted array along the last two dimensions
Remarks: this test tests double contraction of a 4th order tensor with itself(i.e reduce sum keep dimensions 1,2)
"""
X = np.random.randn(2,3,5,7)
Xv = Variable(X,"Xv")
Z = Einsum("ijkl->ij",Xv)
assert isinstance(Z,Einsum) and np.array_equal(Z(),np.einsum("ijkl->ij",X)) == True
def test_einsum_onearg_sum_3axis():
"""
Aim:To test the operation Einsum
Expected: contracted array along the last three dimensions
Obtained: contracted array along the last three dimensions
Remarks: this test tests the triple contraction of a 4th order tensor with itself(i.e reduce sum keep dimensions 1)
"""
X = np.random.randn(2,3,5,7)
Xv = Variable(X,"Xv")
Z = Einsum("ijkl->i",Xv)
assert isinstance(Z,Einsum) and np.array_equal(Z(),np.einsum("ijkl->i",X)) == True
def test_einsum_onearg_sum_allaxis():
"""
Aim:To test the operation Einsum
Expected:contracted array along the last all dimensions
Obtained:contracted array along the last all dimensions
Remarks: this tests einsum as reduce sum keep dims none
"""
X = np.random.randn(2,3,5,7)
Xv = Variable(X,"Xv")
Z = Einsum("ijkl->",Xv)
assert isinstance(Z,Einsum) and np.array_equal(Z(),np.einsum("ijkl->",X)) == True
def test_einsum_matmul():
"""
Aim:To test the operation Einsum (Value and its derivatives)
Expected: z=[[ 433, 344], dzdx = [[115, 108] dzdy =[[55, 55],
[3452, 3176]] [115, 108]] [10, 10]]
obtained:z=[[ 433, 344], dzdx = [[115, 108], dzdy=[[55, 55],
[3452, 3176]] [115, 108]] [10, 10]]
Remarks: This tests usage of Einsum for matrix multiplication(tests the wrapper function Wrapper Matmul)
"""
x = np.array([[3,4],[52,6]])
y = np.array([[59,56],[64,44]])
z = np.dot(x,y)
X = Variable(x,"X")
Y = Variable(y,"Y")
Z = Einsum("ij,jk->ik",X,Y)
dzdx , dzdy = grad(Z,[X,Y])
assert isinstance(Z,Einsum) and np.array_equal(Z(),z) and np.array_equal(dzdx(),np.einsum("ik,jk->ij",np.ones_like(z),y)) \
and np.array_equal(dzdy(),np.einsum("ij,jk->jk",x,np.ones_like(z)))
def test_einsum_matmul3():
"""
Aim:To test the operation Einsum (Value and its derivatives)
Expected: z=[[ 51503, 43169], dzdx =[[20928, 7972], dzdy=[[11000, 8965] dzdw =[[3285, 3285],
[673332, 462756]] [20928, 7972]] [ 2000, 1630]] [3520, 3520]]
Obtained: z=[[ 51503, 43169], dzdx =[[20928, 7972], dzdy=[[11000, 8965] dzdw= [[3285, 3285],
[673332, 462756]] [20928, 7972]] [ 2000, 1630]] [3520, 3520]]
Remarks: This test tests usage of einsum for multplication of 3 matrices
"""
x = np.array([[3,4],[52,6]])
y = np.array([[59,56],[4,44]])
w = np.array([[151,49],[65,98]])
z = np.dot(np.dot(x,y),w)
X = Variable(x,"X")
Y = Variable(y,"Y")
W = Variable(w,"W")
Z = Einsum("ij,jk,kl->il",X,Y,W)
dzdx,dzdy,dzdw = grad(Z,[X,Y,W])
print(dzdx())
print(np.einsum("il,jk,kl->ij",np.ones_like(z),y,w))
assert isinstance(Z,Einsum) and np.array_equal(Z(),z) and np.array_equal(dzdx(),np.einsum("il,jk,kl->ij",np.ones_like(z),y,w)) \
and np.array_equal(dzdy(),np.einsum("ij,il,kl->jk",x,np.ones_like(z),w)) and np.array_equal(dzdw(),np.einsum("ij,jk,il->kl",x,y,np.ones_like(z)))
def test_einsum_different_indices():
"""
Aim:To test the operation Einsum (Value and its derivatives)
Expected: third order tensor and second order derivatives obtained by outer product of three second order tensors
Obtained: third order tensor and second order derivatives obtained by outer product of three second order tensors
Remarks: This test tests usage of einsum for outer product of three second order tensors to get third order tensor
"""
x = np.array([[3,4],[52,6]])
y = np.array([[59,54],[44,84]])
w = np.array([[11,29],[75,9]])
z = np.einsum("ij,jk,kl->ijl",x,y,w)
X = Variable(x,"X")
Y = Variable(y,"Y")
W = Variable(w,"W")
Z = Einsum("ij,jk,kl->ijl",X,Y,W)
dzdx,dzdy,dzdw = grad(Z,[X,Y,W])
assert isinstance(Z,Einsum) and np.array_equal(Z(),z) and np.array_equal(dzdx(),np.einsum("ijl,jk,kl->ij",np.ones_like(z),y,w)) \
and np.array_equal(dzdy(),np.einsum("ij,ijl,kl->jk",x,np.ones_like(z),w)) and np.array_equal(dzdw(),np.einsum("ij,jk,ijl->kl",x,y,np.ones_like(z)))
def test_einsum_4thorder():
"""
Aim:To test the operation Einsum (Value and its derivatives)
Expected: fourth order tensor and second order derivatives obtained by outer product of three second order tensors
Obtained: fourth order tensor and second order derivatives obtained by outer product of three second order tensors
Remarks: This test tests usage of einsum for outer product of three second order tensors to get fourth order tensor
"""
x = np.array([[35,24],[52,6]])
y = np.array([[59,56],[44,44]])
w = np.array([[11,45],[75,28]])
z = np.einsum("ij,jk,kl->ijkl",x,y,w)
X = Variable(x,"X")
Y = Variable(y,"Y")
W = Variable(w,"W")
Z = Einsum("ij,jk,kl->ijkl",X,Y,W)
dzdx,dzdy,dzdw = grad(Z,[X,Y,W])
assert isinstance(Z,Einsum) and np.array_equal(Z(),z) and np.array_equal(dzdx(),np.einsum("ijkl,jk,kl->ij",np.ones_like(z),y,w)) \
and np.array_equal(dzdy(),np.einsum("ij,ijkl,kl->jk",x,np.ones_like(z),w)) and np.array_equal(dzdw(),np.einsum("ij,jk,ijkl->kl",x,y,np.ones_like(z)))
def test_pow_scalar():
"""
Aim: Test the operation power(value and derivatives)
Expected: val = 9 and dy =6
Obtained: val = 9 and dy =6
Remarks: tests specific x**n situation n being a scalar whose derivative will be n*x**n-1
"""
x = 3
y = 3**2
X = Variable(x,"X")
Y = Pow(X,2)
dy = grad(Y,[X])[0]
assert isinstance(Y,Pow) and Y()==9 and dy()==6
def test_pow_scalar_irrational():
"""
Aim: Test the operation power(value and derivatives)
Obtained:val= 0.03170146783514191 ,dy = -0.03319769948629831
Expected:val = 0.03170146783514191, dy = -0.03319769948629831
Remarks: tests specific x**n stuation n being an irrational number.
"""
x = 3
y = 3**-np.pi
X = Variable(x,"X")
Y = Pow(X,-np.pi)
dy = grad(Y,[X])[0]
assert isinstance(Y,Pow) and Y()==y and dy()==-np.pi*3**(-np.pi-1)
def test_pow_array_with_scalar():
"""
Aim: Test the operation power(value and derivatives)
obtained: value is element wise squared of a random array,derivative is 2*random array
Expected: value is element wise squared of a random array,derivative is 2*random array
Remarks: tests how pow operation for a higher dimensional array
"""
x = np.random.rand(3,3,3)
y = x**2
X = Variable(x,"X")
Y = Pow(X,2)
dy = grad(Y,[X])[0]
assert isinstance(Y,Pow) and np.array_equal(Y(),y) and np.array_equal(dy(),2*x)
def test_pow_array_with_itself():
"""
Aim: Test the operation power(value and derivatives)
Expected: Each value in third order tesnor raised to itself , derivative is additionally multiplied with log of value
Obtained:Each value in third order tesnor raised to itself, derivative is very slightly less than expected
Remarks: tests how pow operation works for type x**x whose derivative is x**x*log(x) and the slight deviation is result of avoiding the unwanted infinities in log .
"""
x = np.random.rand(3,3,3)
y = x**x
X = Variable(x,"X")
Y = Pow(X,X)
#print(Y())
dy = grad(Y,[X])[0]
print(dy())
print((x**x)*(np.log(x)+1))
assert isinstance(Y,Pow) and np.array_equal(Y(),y) and np.array_equal(dy(),(x**x)*(np.log(x+1e-12)+1))
def test_pow_array_with_another():
"""
Aim:Test the operation power(value and derivatives)
Expected:Each value in third order tesnor raised to another value of another tensor with same index , derivative is additionally multiplied with log of value
Obtained:Each value in third order tesnor raised to another value of another tensor with same index , derivative is additionally multiplied with log of value , small deviation due to avoiding infinities.
Remarks: tests both scenarios x**a and a**x and derivatives
"""
x = np.random.rand(4,4,4)
z = np.random.rand(4,4,4)
X = Variable(x,"X")
Z = Variable(z,"Z")
y = x**z
print(y)
Y = Pow(X,Z)
print(Y())
dydx ,dydz= grad(Y,[X,Z])
assert isinstance(Y,Pow) and np.array_equal(Y(),y) and np.array_equal(dydx(),z*(x**(z-1))) and np.array_equal(dydz(),(x**z)*np.log(x+1e-12))
def test_log_scalar():
"""
Aim: test the log operation(value and derivative)
Expected: 1.6094379124341003 and dy = 0.2
Obtained: 1.6094379124343003 and dy = 0.20000000000100002
Remarks: The slight deviation is caused by a very small value added during the operation to avoid infinities
"""
x = 5
y = np.log(x+1e-12)
X = Variable(x,"X")
Y = Log(X)
dy = grad(Y,[X])[0]
assert isinstance(Y,Log) and Y()==y and dy()==1/(5+1e-12)
def test_log_0():
"""
Aim: test the log operation(value and derivative)
Expected: Not defined/Does not exist
Obtained: -27.631021115928547 and dy = 10000000000000
Remarks: This shows the anomalous behaviour at discontinuities , they are avoided by adding a very small number.This is employed because they are more likely to occur in NN and discontinuites need to be dealt with.
"""
x = 0
y = np.log(x+1e-12)
X = Variable(x,"X")
Y = Log(X)
dy = grad(Y,[X])[0]
assert isinstance(Y,Log) and Y()==y and dy()==1/1e-12
def test_log_array():
"""
Aim: Test the log operation(value and derivative)
Expected: np.log of the array
Obtained: np.log(array+1e-12)
Remarks: The small error constitutes weight to avoid infinities
"""
x = np.array([[np.pi,np.exp(1)],[232,848]])
y = np.log(x+1e-12)
X = Variable(x,"X")
Y = Log(X)
dy=grad(Y,[X])[0]
print(dy())
print(1/x)
assert isinstance(Y,Log) and np.array_equal(Y(),y) and np.array_equal(dy(),1/(x+1e-12))
def test_identity():
"""
Aim: Test the identity function (function and derivative)
Expected: The same function which is passed as argument
Obtained:The same function which is passed as argument
Remarks: Sometimes derivatives musr also pass without any changes , This Identity is used in such conditions
"""
x = np.array([[np.pi,np.exp(1)],[232.3864641,-84.448]])
X = Variable(x,"X")
y = Identity(X)
dy=grad(y,[X])[0]
print(dy())
print(y())
assert isinstance(y,Identity) and np.array_equal(x,y()) and np.array_equal(dy(),np.full_like(x,1.))
def test_exp_scalar():
"""
Aim: Test the exponent function (value and derivative)
Expected: exp(5)
Obtained:exp(5)
Remarks: The derivative of exp(x) is again exp(x)
"""
x = 5
y = np.exp(x)
X = Variable(x,"X")
Y = Exp(X)
dy=grad(Y,[X])[0]
assert isinstance(Y,Exp) and Y()==y and dy()==y
def test_exp_array():
"""
Aim: Test the exponent function(value and derivative)
Expected: exp(array)
Obtained: exp(array)
Remarks: The derivative is equal to value
"""
x = np.random.rand(2,2)
y = np.exp(x)
X = Variable(x,"X")
Y = Exp(X)
dy=grad(Y,[X])[0]
assert isinstance(Y,Exp) and np.array_equal(Y(),y) and np.array_equal(dy(),y)
def test_sine_scalar():
"""
Aim: test the sine function(value and derivative)
Expected:1
Obtained:1
Remarks: Exact derivatives are obtained because of no presence of discontinuities
"""
x = np.pi/2
y = np.sin(x)
X = Variable(x,"X")
Y = ad.Sine(X)
dy = grad(Y,[X])[0]
print(dy())
assert isinstance(Y,Sine) and y==Y() and dy()==np.cos(x)
def test_sine_array():
"""
Aim:test the sine function (value and derivative)
Expected:sine(array)
Obtained:sine(array)
Remarks:Exact derivatives are obtained because of no presence of discontinuities
"""
x = np.random.rand(4,5,6)
y = np.sin(x)
X = Variable(x,"X")
Y = ad.Sine(X)
dy = grad(Y,[X])[0]
print(dy())
assert isinstance(Y,Sine) and np.array_equal(Y(),y) and np.array_equal(dy(),np.cos(x))
def test_cosine_array():
"""
Aim: test cosine function (value and derivative)
Expected:cosine(array)
Obtained:cosine(array)
Remarks:Exact derivatives are obtained because of no presence of discontinuities
"""
x = np.random.rand(4,5)
y = np.cos(x)
X = Variable(x,"X")
Y = ad.Cosine(X)
dy = grad(Y,[X])[0]
print(dy())
print(-np.sin(x))
assert isinstance(Y,Cosine) and np.array_equal(Y(),y) and np.array_equal(dy(),-np.sin(x))
def test_cosine_scalar():
"""
Aim:test cosine function (value and derivative)
Expected:1
Obtained:1
Remarks:Exact derivatives are obtained because of no presence of discontinuities
"""
x = 0
y = np.cos(x)
Y = Cosine(x)
dy = grad(Y,[x])[0]
print(dy())
print(-np.sin(x))
assert isinstance(Y,Cosine) and y==Y() and dy()==-np.sin(x)
def test_tan_scalar():
"""
Aim:test the tan function (value and derivative)
Expected: infinity
Obtained:1e20
Remarks: discontinuity occurs at this point for value and derivative and numpy kernel actually evades discontinuity in value but for derivative additional functionality is implemented.
"""
x = np.pi/2
X = Variable(x,"X")
y = np.tan(x)
Y = Tan(X)
dy = grad(Y,[X])[0]
print(dy())
temp=((1.0/(np.cos(x+1e-12)**2)))
print(temp)
assert isinstance(Y,Tan) and y == Y() and (temp-dy()) < 0.000000000001
def test_tan_scalar1():
"""
Aim:the tan function (value and derivative)
Expected:0
Obtained:0
Remarks:derivatives are not exact to avoid discontinuity
"""
x = 0
X = Variable(x,"X")
y = np.tan(x)
Y = Tan(X)
dy = grad(Y,[X])[0]
print(dy())
temp=((1.0/(np.cos(x+1e-12)**2)))
print(temp)
assert isinstance(Y,Tan) and y == Y() and (temp-dy()) < 0.000000000001
def test_tan_array():
"""
Aim:the tan function (value and derivative)
Expected:tan(array)
Obtained:tan(array)
Remarks:derivatives are not exact to avoid discontinuity
"""
x = np.random.rand(4,5)
y = np.tan(x)
X = Variable(x,"X")
Y = Tan(X)
dy = grad(Y,[X])[0]
print(dy())
temp=((1.0/(np.cos(x)**2)))
print(temp)
assert isinstance(Y,Tan) and np.array_equal(Y(),y) and np.all(np.less(np.abs(dy()-temp),0.0000000001))
def test_cosec_array():
"""
Aim:test the cosec function and derivative
Expected:cosec(array)
Obtained:cosec(array+1e-12)
Remarks:derivatives are not exact to avoid discontinuity
"""
x = np.random.rand(4,5)
y = 1.0/np.sin(x+1e-12)
X = Variable(x,"X")
Y = Cosec(X)
dy = grad(Y,[X])[0]
print(dy())
temp=((1.0/(np.sin(x+1e-12)*np.tan(x+1e-12))))
print(temp)
assert isinstance(Y,Cosec) and np.array_equal(Y(),y) and np.all(np.less(np.abs(dy()+temp),0.0000000001))
def test_cosec_array_higher():
"""
Aim:test the cosec function and derivative
Expected:cosec(array)
Obtained:cosec(array+1e-12)
Remarks:derivatives are not exact to avoid discontinuity
"""
x = np.random.rand(4,5,5)
y = 1.0/np.sin(x+1e-12)
X = Variable(x,"X")
Y = Cosec(X)
dy = grad(Y,[X])[0]
print(dy())
temp=((1.0/(np.sin(x+1e-12)*np.tan(x+1e-12))))
print(temp)
assert isinstance(Y,Cosec) and np.array_equal(Y(),y) and np.all(np.less(np.abs(dy()+temp),0.0000000001))
def test_cosec_scalar():
"""
Aim:test the cosec function and derivative
Expected:infinity
Obtained:1e12
Remarks:derivatives are not exact to avoid discontinuity
"""
x = 0
y = 1.0/(np.sin(x+1e-12))
X = Variable(x,"X")
Y = Cosec(X)
print(Y())
print(y)
dy = grad(Y,[X])[0]
print(dy())
temp=((1.0/(np.sin(-1e-12)*np.tan(x+1e-12))))
print(temp)
assert isinstance(Y,Cosec) and np.array_equal(Y(),y) and dy() < -1e23 and temp < -1e23
def test_cosec_scalar_1():
"""
Aim:test the cosec function and derivative
Expected:1
Obtained:1
Remarks:derivatives are not exact to avoid discontinuity
"""
x = np.pi/2
y = 1.0/(np.sin(x+1e-12))
X = Variable(x,"X")
Y = Cosec(X)
print(Y())
print(y)
dy = grad(Y,[X])[0]
print(dy())
temp=((-1.0/(np.sin(x+1e-12)*np.tan(x+1e-12))))
print(temp)
assert isinstance(Y,Cosec) and np.array_equal(Y(),y) and np.all(np.less(np.abs(dy()-temp),0.0000000001))
def test_sec_scalar_1():
"""
Aim:test the sec function and derivative
Expected:1
Obtained:1
Remarks:difference in derivative constitutes is due to avoiding discontinuities
"""
x = 0
y = 1.0/(np.cos(x+1e-12))
X = Variable(x,"X")
Y = Sec(X)
print(Y())
print(y)
dy = grad(Y,[X])[0]
print(dy())
temp=np.tan(x+1e-12)/np.cos(x+1e-12)
print(temp)
assert isinstance(Y,Sec) and np.array_equal(Y(),y) and np.all(np.less(np.abs(dy()-temp),0.0000000001))
def test_sec_scalar():
"""
Aim:test the sec function and derivative
Expected:infinity
Obtained:1e12
Remarks:difference in derivative and also value constitutes is due to avoiding discontinuities
"""
x = np.pi/2
y = 1.0/(np.cos(x+1e-12))
X = Variable(x,"X")
Y = Sec(X)
print(Y())
print(y)
dy = grad(Y,[X])[0]
print(dy())
temp= Sec(X)*Tan(X)
print(temp())
assert isinstance(Y,Sec) and np.array_equal(Y(),y) and np.all(np.less(np.abs(dy()-temp()),0.0000000001))
def test_sec_array():
"""
Aim:test the sec function and derivative
Expected:sec(array)
Obtained:sec(array+1e-12)
Remarks:difference in derivative constitutes is due to avoiding discontinuities
"""
x = np.random.randn(10,20)
y = 1.0/(np.cos(x+1e-12))
X = Variable(x,"X")
Y = Sec(X)
print(Y())
print(y)
dy = grad(Y,[X])[0]
print(dy())
temp= Sec(X)*Tan(X)
print(temp())
assert isinstance(Y,Sec) and np.array_equal(Y(),y) and np.all(np.less(np.abs(dy()-temp()),0.0000000001))
def test_cot_scalar():
"""
Aim:test the cot function and derivative
Expected:0
Obtained:0(almost upto 12 digits)
Remarks:difference in derivative constitutes is due to avoiding discontinuities
"""
x = np.pi/2
y = 1.0/(np.tan(x+1e-12))
X = Variable(x,"X")
Y = Cot(X)
print(Y())
print(y)
dy = grad(Y,[X])[0]
print(dy())
temp= -1/((np.sin(x+1e-12)**2))
print(temp)
assert isinstance(Y,Cot) and np.array_equal(Y(),y) and np.all(np.less(np.abs(dy()-temp),0.0000000001))
def test_cot_scalar():
"""
Aim:test the cot function and derivative
Expected:infinity
Obtained:1e12
Remarks:discontinuity occurs at this point for value and derivative and numpy kernel actually evades discontinuity in value but for derivative additional functionality is implemented.
"""
x = 0
y = 1.0/(np.tan(x+1e-12))
X = Variable(x,"X")
Y = Cot(X)
print(Y())
print(y)
dy = grad(Y,[X])[0]
print(dy())
temp= -1/((np.sin(x+1e-12)**2))
print(temp)
assert isinstance(Y,Cot) and np.array_equal(Y(),y) and dy() < -1e23 and temp < -1e23
def test_cot_array():
"""
Aim: test the cot function and derivative
Expected: cot (array)
Obtained: cot(array+1e-12)
Remarks:difference in derivative constitutes is due to avoiding discontinuities
"""
x = np.random.randn(22,88)
y = 1.0/(np.tan(x+1e-12))
X = Variable(x,"X")
Y = Cot(X)
print(Y())
print(y)
dy = grad(Y,[X])[0]
print(dy())
temp= -1/((np.sin(x+1e-12)**2))
print(temp)
assert isinstance(Y,Cot) and np.array_equal(Y(),y) and np.all(np.less(np.abs(dy()-temp),0.0000001))
#The sigmoid function is a typical case where the derivative is in terms of the function itself. This property is emphasized by the test cases.
def test_sigmoid_scalar():
"""
Aim: Test sigmoid function (value and derivative)
Expected:0.5
Obtained:0.5
Remarks: The values will be exact since the function is itself used as derivative
"""
x = 0
y = 1/(1+np.exp(-x))
X =Variable(x,"X")
Y = Sigmoid(X)
dy = grad(Y,[X])[0]
temp = Sigmoid(X)*(1-Sigmoid(X))
assert isinstance(Y,Sigmoid) and Y()==y and dy()==temp()
def test_sigmoid_array():
"""
Aim: test sigmoid function (value and derivative)
Expected:sigmoid(array)
Obtained:sigmoid(array)
Remarks:The values will be exact since the function is itself used as derivative
"""
x = np.random.rand(3,3,4)
y = 1/(1+np.exp(-x))
X =Variable(x,"X")
Y = Sigmoid(X)
dy = grad(Y,[X])[0]
temp = Sigmoid(X)*(1-Sigmoid(X))
assert isinstance(Y,Sigmoid) and np.array_equal(Y(),y) and np.array_equal(dy(),temp())
#Note for all Inverse trigonometric functions.
#The inverse trigonometric functions work only in specific ranges and there is a possibility that while avoiding infinities the value might be in undefined regions
#This is handled by not using the epsilon in value and derivative also, but the derivative uses functions which already have implemented in a way to avoid infinities. Hence he infinities are handled without any hindrance to teh possible ranges.
#The same is emphasized in the test cases below
def test_arcsin_scalar():
"""
Aim:Test the arcsin function (value and derivative)
Expected: pi/2
Obtained: pi/2
Remarks:discontinuity occurs at derivative but it is evaded by eps.
"""
x = 1
y = np.arcsin(x)
X =Variable(x,"X")
Y = ArcSin(X)
dy = grad(Y,[X])[0]
print(dy())
x = x
temp = 1/(np.sqrt(1-(x*x))+1e-12)
print(temp)
assert isinstance(Y,ArcSin) and Y()==y and dy()-temp < 1e-20
def test_arcsin_array():
"""
Aim:Test the arcsin function (value and derivative)
Expected:arcsin(array)
Obtained:arcsin(array)
Remarks:discontinuity occurs at derivative but it is evaded by eps.
"""
x = np.random.rand(5,5)
y = np.arcsin(x)
X =Variable(x,"X")
Y = ArcSin(X)
dy = grad(Y,[X])[0]
print(dy())
temp = 1/np.sqrt(1-(x*x))
assert isinstance(Y,ArcSin) and np.array_equal(Y(),y) and np.all(np.less(np.abs(dy()-temp),0.000000001))
def test_arccos_scalar():
"""
Aim: Test the arccos function (value and derivative)
Expected:0
Obtained:0
Remarks: typical case where functionality of derivative breaks but the value is more frequent to occur hence it is checked to prevent it.
"""
x = 1
y = np.arccos(x)
X =Variable(x,"X")
Y = ArcCos(X)
dy = grad(Y,[X])[0]
print(dy())
x = x
temp = -1/(np.sqrt(1-(x*x))+1e-12)
print(temp)
assert isinstance(Y,ArcCos) and Y()==y and np.abs(dy()-temp) < 1e-20
def test_arccos_array():
"""
Aim:test the arccos function (value and derivative)
Expected: arccos(array)
Obtained: arccos(array)
Remarks: To avoid discontinuity of derivative , the values won't exactly match
"""
x = np.random.rand(5,5)