-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Nd4jTestsC.java
8968 lines (7016 loc) · 343 KB
/
Nd4jTestsC.java
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 program and the accompanying materials are made available under the
* * terms of the Apache License, Version 2.0 which is available at
* * https://www.apache.org/licenses/LICENSE-2.0.
* *
* * See the NOTICE file distributed with this work for additional
* * information regarding copyright ownership.
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* * License for the specific language governing permissions and limitations
* * under the License.
* *
* * SPDX-License-Identifier: Apache-2.0
* *****************************************************************************
*/
package org.eclipse.deeplearning4j.nd4j.linalg;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.math3.stat.descriptive.rank.Percentile;
import org.apache.commons.math3.util.FastMath;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.nd4j.common.io.ClassPathResource;
import org.nd4j.common.primitives.Pair;
import org.nd4j.common.tests.tags.NativeTag;
import org.nd4j.common.tests.tags.TagNames;
import org.nd4j.common.util.ArrayUtil;
import org.nd4j.common.util.MathUtils;
import org.nd4j.enums.WeightsFormat;
import org.nd4j.linalg.BaseNd4jTestWithBackends;
import org.nd4j.linalg.api.blas.params.GemmParams;
import org.nd4j.linalg.api.blas.params.MMulTranspose;
import org.nd4j.linalg.api.buffer.DataBuffer;
import org.nd4j.linalg.api.buffer.DataType;
import org.nd4j.linalg.api.environment.Nd4jEnvironment;
import org.nd4j.linalg.api.iter.INDArrayIterator;
import org.nd4j.linalg.api.iter.NdIndexIterator;
import org.nd4j.linalg.api.memory.conf.WorkspaceConfiguration;
import org.nd4j.linalg.api.memory.enums.LearningPolicy;
import org.nd4j.linalg.api.memory.enums.SpillPolicy;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.api.ops.BroadcastOp;
import org.nd4j.linalg.api.ops.CustomOp;
import org.nd4j.linalg.api.ops.DynamicCustomOp;
import org.nd4j.linalg.api.ops.Op;
import org.nd4j.linalg.api.ops.executioner.GridExecutioner;
import org.nd4j.linalg.api.ops.executioner.OpExecutioner;
import org.nd4j.linalg.api.ops.impl.broadcast.BroadcastAMax;
import org.nd4j.linalg.api.ops.impl.broadcast.BroadcastAMin;
import org.nd4j.linalg.api.ops.impl.broadcast.BroadcastAddOp;
import org.nd4j.linalg.api.ops.impl.broadcast.BroadcastDivOp;
import org.nd4j.linalg.api.ops.impl.broadcast.BroadcastMax;
import org.nd4j.linalg.api.ops.impl.broadcast.BroadcastMin;
import org.nd4j.linalg.api.ops.impl.broadcast.BroadcastMulOp;
import org.nd4j.linalg.api.ops.impl.broadcast.BroadcastSubOp;
import org.nd4j.linalg.api.ops.impl.broadcast.bool.BroadcastEqualTo;
import org.nd4j.linalg.api.ops.impl.broadcast.bool.BroadcastGreaterThan;
import org.nd4j.linalg.api.ops.impl.broadcast.bool.BroadcastGreaterThanOrEqual;
import org.nd4j.linalg.api.ops.impl.broadcast.bool.BroadcastLessThan;
import org.nd4j.linalg.api.ops.impl.indexaccum.custom.ArgAmax;
import org.nd4j.linalg.api.ops.impl.indexaccum.custom.ArgAmin;
import org.nd4j.linalg.api.ops.impl.indexaccum.custom.ArgMax;
import org.nd4j.linalg.api.ops.impl.indexaccum.custom.ArgMin;
import org.nd4j.linalg.api.ops.impl.layers.convolution.Conv2D;
import org.nd4j.linalg.api.ops.impl.layers.convolution.Im2col;
import org.nd4j.linalg.api.ops.impl.layers.convolution.config.Conv2DConfig;
import org.nd4j.linalg.api.ops.impl.layers.convolution.config.PaddingMode;
import org.nd4j.linalg.api.ops.impl.reduce.Mmul;
import org.nd4j.linalg.api.ops.impl.reduce.bool.All;
import org.nd4j.linalg.api.ops.impl.reduce.custom.LogSumExp;
import org.nd4j.linalg.api.ops.impl.reduce.floating.Norm1;
import org.nd4j.linalg.api.ops.impl.reduce.floating.Norm2;
import org.nd4j.linalg.api.ops.impl.reduce.same.Sum;
import org.nd4j.linalg.api.ops.impl.reduce3.CosineDistance;
import org.nd4j.linalg.api.ops.impl.reduce3.CosineSimilarity;
import org.nd4j.linalg.api.ops.impl.reduce3.EuclideanDistance;
import org.nd4j.linalg.api.ops.impl.reduce3.HammingDistance;
import org.nd4j.linalg.api.ops.impl.reduce3.ManhattanDistance;
import org.nd4j.linalg.api.ops.impl.scalar.LeakyReLU;
import org.nd4j.linalg.api.ops.impl.scalar.ReplaceNans;
import org.nd4j.linalg.api.ops.impl.scalar.comparison.ScalarEquals;
import org.nd4j.linalg.api.ops.impl.scatter.ScatterUpdate;
import org.nd4j.linalg.api.ops.impl.shape.Reshape;
import org.nd4j.linalg.api.ops.impl.transforms.any.IsMax;
import org.nd4j.linalg.api.ops.impl.transforms.bool.MatchConditionTransform;
import org.nd4j.linalg.api.ops.impl.transforms.comparison.CompareAndSet;
import org.nd4j.linalg.api.ops.impl.transforms.comparison.Eps;
import org.nd4j.linalg.api.ops.impl.transforms.custom.BatchToSpaceND;
import org.nd4j.linalg.api.ops.impl.transforms.custom.Reverse;
import org.nd4j.linalg.api.ops.impl.transforms.custom.SoftMax;
import org.nd4j.linalg.api.ops.impl.transforms.pairwise.BinaryRelativeError;
import org.nd4j.linalg.api.ops.impl.transforms.pairwise.Set;
import org.nd4j.linalg.api.ops.impl.transforms.pairwise.arithmetic.Axpy;
import org.nd4j.linalg.api.ops.impl.transforms.same.Sign;
import org.nd4j.linalg.api.ops.impl.transforms.strict.ACosh;
import org.nd4j.linalg.api.ops.impl.transforms.strict.Tanh;
import org.nd4j.linalg.api.ops.util.PrintVariable;
import org.nd4j.linalg.api.shape.LongShapeDescriptor;
import org.nd4j.linalg.api.shape.Shape;
import org.nd4j.linalg.checkutil.NDArrayCreationUtil;
import org.nd4j.linalg.exception.ND4JIllegalStateException;
import org.nd4j.linalg.factory.Nd4j;
import org.nd4j.linalg.factory.Nd4jBackend;
import org.nd4j.linalg.indexing.BooleanIndexing;
import org.nd4j.linalg.indexing.INDArrayIndex;
import org.nd4j.linalg.indexing.NDArrayIndex;
import org.nd4j.linalg.indexing.SpecifiedIndex;
import org.nd4j.linalg.indexing.conditions.Conditions;
import org.nd4j.linalg.ops.transforms.Transforms;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import static org.junit.jupiter.api.Assertions.*;
/**
* NDArrayTests
*
* @author Adam Gibson
*/
@Slf4j
@NativeTag
@Tag(TagNames.FILE_IO)
public class Nd4jTestsC extends BaseNd4jTestWithBackends {
@TempDir Path testDir;
@Override
public long getTimeoutMilliseconds() {
return 90000;
}
@BeforeEach
public void before() throws Exception {
Nd4j.getRandom().setSeed(123);
Nd4j.getExecutioner().enableDebugMode(false);
Nd4j.getExecutioner().enableVerboseMode(false);
}
@AfterEach
public void after() throws Exception {
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testEmptyStringScalar(Nd4jBackend backend) {
INDArray arr = Nd4j.empty(DataType.UTF8);
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testPutWhereWithMask(Nd4jBackend backend) {
double[][] arr = new double[][]{{1., 2.}, {1., 4.}, {1., 6}};
double[][] expected = new double[][] {
{2,2},
{2,4},
{2,6}
};
INDArray assertion = Nd4j.create(expected);
INDArray dataMatrix = Nd4j.createFromArray(arr);
INDArray compareTo = Nd4j.valueArrayOf(dataMatrix.shape(), 1.);
INDArray replacement = Nd4j.valueArrayOf(dataMatrix.shape(), 2);
INDArray mask = dataMatrix.match(compareTo, Conditions.equals(1));
INDArray out = dataMatrix.putWhereWithMask(mask, replacement);
assertEquals(assertion,out);
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testConditions(Nd4jBackend backend) {
Nd4j.getExecutioner().enableVerboseMode(true);
Nd4j.getExecutioner().enableDebugMode(true);
double[][] arr = new double[][]{{1., 2.}, {1., 4.}, {1., 6}};
INDArray dataMatrix = Nd4j.createFromArray(arr);
INDArray compareTo = Nd4j.valueArrayOf(dataMatrix.shape(), 1.);
INDArray mask1 = dataMatrix.dup().match(compareTo, Conditions.epsNotEquals(1));
INDArray mask2 = dataMatrix.dup().match(compareTo, Conditions.epsEquals(1));
assertNotEquals(mask1,mask2);
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testArangeNegative(Nd4jBackend backend) {
INDArray arr = Nd4j.arange(-2,2).castTo(DataType.DOUBLE);
INDArray assertion = Nd4j.create(new double[]{-2, -1, 0, 1});
assertEquals(assertion,arr);
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testTri(Nd4jBackend backend) {
INDArray assertion = Nd4j.create(new double[][]{
{1,1,1,0,0},
{1,1,1,1,0},
{1,1,1,1,1}
});
INDArray tri = Nd4j.tri(3,5,2);
assertEquals(assertion,tri);
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testTriu(Nd4jBackend backend) {
Nd4j.getExecutioner().enableDebugMode(true);
Nd4j.getExecutioner().enableVerboseMode(true);
INDArray input = Nd4j.linspace(1,12,12, DataType.DOUBLE).reshape(4,3);
int k = -1;
INDArray test = Nd4j.triu(input,k);
INDArray create = Nd4j.create(new double[][]{
{1,2,3},
{4,5,6},
{0,8,9},
{0,0,12}
});
assertEquals(create,test);
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testDiag(Nd4jBackend backend) {
INDArray diag = Nd4j.diag(Nd4j.linspace(1,4,4, DataType.DOUBLE).reshape(4,1));
assertArrayEquals(new long[] {4,4},diag.shape());
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testGetRowEdgeCase(Nd4jBackend backend) {
INDArray orig = Nd4j.linspace(1,300,300, DataType.DOUBLE).reshape('c', 100, 3);
INDArray col = orig.getColumn(0).reshape(100, 1);
for( int i = 0; i < 100; i++) {
INDArray row = col.getRow(i);
INDArray rowDup = row.dup();
double d = orig.getDouble(i, 0);
double d2 = col.getDouble(i);
double dRowDup = rowDup.getDouble(0);
double dRow = row.getDouble(0);
String s = String.valueOf(i);
assertEquals(d, d2, 0.0,s);
assertEquals(d, dRowDup, 0.0,s); //Fails
assertEquals(d, dRow, 0.0,s); //Fails
}
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testNd4jEnvironment(Nd4jBackend backend) {
System.out.println(Nd4j.getExecutioner().getEnvironmentInformation());
int manualNumCores = Integer.parseInt(Nd4j.getExecutioner().getEnvironmentInformation()
.get(Nd4jEnvironment.CPU_CORES_KEY).toString());
assertEquals(Runtime.getRuntime().availableProcessors(), manualNumCores);
assertEquals(Runtime.getRuntime().availableProcessors(), Nd4jEnvironment.getEnvironment().getNumCores());
System.out.println(Nd4jEnvironment.getEnvironment());
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testSerialization(Nd4jBackend backend) throws Exception {
Nd4j.getRandom().setSeed(12345);
INDArray arr = Nd4j.rand(1, 20).castTo(DataType.DOUBLE);
File dir = testDir.resolve("new-dir-" + UUID.randomUUID().toString()).toFile();
assertTrue(dir.mkdirs());
String outPath = FilenameUtils.concat(dir.getAbsolutePath(), "dl4jtestserialization.bin");
try (DataOutputStream dos = new DataOutputStream(Files.newOutputStream(Paths.get(outPath)))) {
Nd4j.write(arr, dos);
}
INDArray in;
try (DataInputStream dis = new DataInputStream(new FileInputStream(outPath))) {
in = Nd4j.read(dis);
}
INDArray inDup = in.dup();
assertEquals(arr, in); //Passes: Original array "in" is OK, but array "inDup" is not!?
assertEquals(in, inDup); //Fails
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testTensorAlongDimension2(Nd4jBackend backend) {
INDArray array = Nd4j.create(new float[100], new long[] {50, 1, 2});
assertArrayEquals(new long[] {1, 2}, array.slice(0, 0).shape());
}
@Disabled // with broadcastables mechanic it'll be ok
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testShapeEqualsOnElementWise(Nd4jBackend backend) {
assertThrows(IllegalStateException.class,() -> {
Nd4j.ones(10000, 1).sub(Nd4j.ones(1, 2));
});
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testIsMaxVectorCase(Nd4jBackend backend) {
INDArray arr = Nd4j.create(new double[] {1, 2, 4, 3}, new long[] {2, 2});
INDArray assertion = Nd4j.create(new boolean[] {false, false, true, false}, new long[] {2, 2}, DataType.BOOL);
INDArray test = Nd4j.getExecutioner().exec(new IsMax(arr))[0];
assertEquals(assertion, test);
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testArgMax(Nd4jBackend backend) {
INDArray toArgMax = Nd4j.linspace(1, 24, 24, DataType.DOUBLE).reshape(4, 3, 2);
INDArray argMaxZero = Nd4j.argMax(toArgMax, 0);
INDArray argMax = Nd4j.argMax(toArgMax, 1);
INDArray argMaxTwo = Nd4j.argMax(toArgMax, 2);
INDArray valueArray = Nd4j.valueArrayOf(new long[] {4, 2}, 2, DataType.LONG);
INDArray valueArrayTwo = Nd4j.valueArrayOf(new long[] {3, 2}, 3, DataType.LONG);
INDArray valueArrayThree = Nd4j.valueArrayOf(new long[] {4, 3}, 1, DataType.LONG);
assertEquals(valueArrayTwo, argMaxZero);
assertEquals(valueArray, argMax);
assertEquals(valueArrayThree, argMaxTwo);
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testArgMax_119(Nd4jBackend backend) {
val array = Nd4j.create(new double[]{1, 2, 119, 2});
val max = array.argMax();
assertTrue(max.isScalar());
assertEquals(2L, max.getInt(0));
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testAutoBroadcastShape(Nd4jBackend backend) {
val assertion = new long[]{2,2,2,5};
val shapeTest = Shape.broadcastOutputShape(new long[]{2,1,2,1},new long[]{2,1,5});
assertArrayEquals(assertion,shapeTest);
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
@Disabled //temporary till libnd4j implements general broadcasting
public void testAutoBroadcastAdd(Nd4jBackend backend) {
INDArray left = Nd4j.linspace(1,4,4, DataType.DOUBLE).reshape(2,1,2,1);
INDArray right = Nd4j.linspace(1,10,10, DataType.DOUBLE).reshape(2,1,5);
INDArray assertion = Nd4j.create(new double[]{2,3,4,5,6,3,4,5,6,7,7,8,9,10,11,8,9,10,11,12,4,5,6,7,8,5,6,7,8,9,9,10,11,12,13,10,11,12,13,14}).reshape(2,2,2,5);
INDArray test = left.add(right);
assertEquals(assertion,test);
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testAudoBroadcastAddMatrix(Nd4jBackend backend) {
INDArray arr = Nd4j.linspace(1,4,4, DataType.DOUBLE).reshape(2,2);
INDArray row = Nd4j.ones(1, 2);
INDArray assertion = arr.add(1.0);
INDArray test = arr.add(row);
assertEquals(assertion,test);
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testScalarOps(Nd4jBackend backend) {
INDArray n = Nd4j.create(Nd4j.ones(27).data(), new long[] {3, 3, 3});
assertEquals(27d, n.length(), 1e-1);
n.addi(Nd4j.scalar(1d));
n.subi(Nd4j.scalar(1.0d));
n.muli(Nd4j.scalar(1.0d));
n.divi(Nd4j.scalar(1.0d));
n = Nd4j.create(Nd4j.ones(27).data(), new long[] {3, 3, 3});
assertEquals(27, n.sumNumber().doubleValue(), 1e-1,getFailureMessage(backend));
INDArray a = n.slice(2);
assertEquals( true, Arrays.equals(new long[] {3, 3}, a.shape()),getFailureMessage(backend));
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testTensorAlongDimension(Nd4jBackend backend) {
val shape = new long[] {4, 5, 7};
int length = ArrayUtil.prod(shape);
INDArray arr = Nd4j.linspace(1, length, length, DataType.DOUBLE).reshape(shape);
int[] dim0s = {0, 1, 2, 0, 1, 2};
int[] dim1s = {1, 0, 0, 2, 2, 1};
double[] sums = {1350., 1350., 1582, 1582, 630, 630};
for (int i = 0; i < dim0s.length; i++) {
int firstDim = dim0s[i];
int secondDim = dim1s[i];
INDArray tad = arr.tensorAlongDimension(0, firstDim, secondDim);
tad.sumNumber();
// assertEquals("I " + i + " failed ",sums[i],tad.sumNumber().doubleValue(),1e-1);
}
INDArray testMem = Nd4j.create(10, 10);
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testMmulWithTranspose(Nd4jBackend backend) {
INDArray arr = Nd4j.linspace(1,4,4, DataType.DOUBLE).reshape(2,2);
INDArray arr2 = Nd4j.linspace(1,4,4, DataType.DOUBLE).reshape(2,2).transpose();
INDArray arrTransposeAssertion = arr.transpose().mmul(arr2);
MMulTranspose mMulTranspose = MMulTranspose.builder()
.transposeA(true)
.build();
INDArray testResult = arr.mmul(arr2,mMulTranspose);
assertEquals(arrTransposeAssertion,testResult);
INDArray bTransposeAssertion = arr.mmul(arr2.transpose());
mMulTranspose = MMulTranspose.builder()
.transposeB(true)
.build();
INDArray bTest = arr.mmul(arr2,mMulTranspose);
assertEquals(bTransposeAssertion,bTest);
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testGetDouble(Nd4jBackend backend) {
INDArray n2 = Nd4j.create(Nd4j.linspace(1, 30, 30, DataType.DOUBLE).data(), new long[] {3, 5, 2});
INDArray swapped = n2.swapAxes(n2.shape().length - 1, 1);
INDArray slice0 = swapped.slice(0).slice(1);
INDArray assertion = Nd4j.create(new double[] {2, 4, 6, 8, 10});
assertEquals(assertion, slice0);
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testWriteTxt() throws Exception {
INDArray row = Nd4j.create(new double[][] {{1, 2}, {3, 4}});
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Nd4j.write(row, new DataOutputStream(bos));
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
INDArray ret = Nd4j.read(bis);
assertEquals(row, ret);
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void test2dMatrixOrderingSwitch(Nd4jBackend backend) {
char order = Nd4j.order();
INDArray c = Nd4j.create(new double[][] {{1, 2}, {3, 4}}, 'c');
assertEquals('c', c.ordering());
assertEquals(order, Nd4j.order().charValue());
INDArray f = Nd4j.create(new double[][] {{1, 2}, {3, 4}}, 'f');
assertEquals('f', f.ordering());
assertEquals(order, Nd4j.order().charValue());
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testMatrix(Nd4jBackend backend) {
INDArray arr = Nd4j.create(new float[] {1, 2, 3, 4}, new long[] {2, 2});
INDArray brr = Nd4j.create(new float[] {5, 6}, new long[] {2});
INDArray row = arr.getRow(0);
row.subi(brr);
assertEquals(Nd4j.create(new float[] {-4, -4}), arr.getRow(0));
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testMMul(Nd4jBackend backend) {
INDArray arr = Nd4j.create(new double[][] {{1, 2, 3}, {4, 5, 6}});
INDArray assertion = Nd4j.create(new double[][] {{14, 32}, {32, 77}});
INDArray test = arr.mmul(arr.transpose());
assertEquals(assertion, test,getFailureMessage(backend));
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
@Disabled
public void testMmulOp(Nd4jBackend backend) throws Exception {
INDArray arr = Nd4j.create(new double[][] {{1, 2, 3}, {4, 5, 6}});
INDArray z = Nd4j.create(2, 2);
INDArray assertion = Nd4j.create(new double[][] {{14, 32}, {32, 77}});
MMulTranspose mMulTranspose = MMulTranspose.builder()
.transposeB(true)
.build();
DynamicCustomOp op = new Mmul(arr, arr, z, mMulTranspose);
Nd4j.getExecutioner().execAndReturn(op);
assertEquals(assertion, z,getFailureMessage(backend));
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testSubiRowVector(Nd4jBackend backend) {
INDArray oneThroughFour = Nd4j.linspace(1, 4, 4, DataType.DOUBLE).reshape('c', 2, 2);
INDArray row1 = oneThroughFour.getRow(1).dup();
oneThroughFour.subiRowVector(row1);
INDArray result = Nd4j.create(new double[] {-2, -2, 0, 0}, new long[] {2, 2});
assertEquals(result, oneThroughFour,getFailureMessage(backend));
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testAddiRowVectorWithScalar(Nd4jBackend backend) {
INDArray colVector = Nd4j.create(5, 1).assign(0.0);
INDArray scalar = Nd4j.create(1, 1).assign(0.0);
scalar.putScalar(0, 1);
assertEquals(scalar.getDouble(0), 1.0, 0.0);
colVector.addiRowVector(scalar); //colVector is all zeros after this
for (int i = 0; i < 5; i++)
assertEquals(colVector.getDouble(i), 1.0, 0.0);
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testTADOnVector(Nd4jBackend backend) {
Nd4j.getRandom().setSeed(12345);
INDArray rowVec = Nd4j.rand(1, 10);
INDArray thirdElem = rowVec.tensorAlongDimension(2, 0);
assertEquals(rowVec.getDouble(2), thirdElem.getDouble(0), 0.0);
thirdElem.putScalar(0, 5);
assertEquals(5, thirdElem.getDouble(0), 0.0);
assertEquals(5, rowVec.getDouble(2), 0.0); //Both should be modified if thirdElem is a view
//Same thing for column vector:
INDArray colVec = Nd4j.rand(10, 1);
thirdElem = colVec.tensorAlongDimension(2, 1);
assertEquals(colVec.getDouble(2), thirdElem.getDouble(0), 0.0);
thirdElem.putScalar(0, 5);
assertEquals(5, thirdElem.getDouble(0), 0.0);
assertEquals(5, colVec.getDouble(2), 0.0);
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testLength(Nd4jBackend backend) {
INDArray values = Nd4j.create(2, 2);
INDArray values2 = Nd4j.create(2, 2);
values.put(0, 0, 0);
values2.put(0, 0, 2);
values.put(1, 0, 0);
values2.put(1, 0, 2);
values.put(0, 1, 0);
values2.put(0, 1, 0);
values.put(1, 1, 2);
values2.put(1, 1, 2);
INDArray expected = Nd4j.repeat(Nd4j.scalar(DataType.DOUBLE, 2).reshape(1, 1), 2).reshape(2);
val accum = new EuclideanDistance(values, values2);
accum.setDimensions(1);
INDArray results = Nd4j.getExecutioner().exec(accum);
assertEquals(expected, results);
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testBroadCasting(Nd4jBackend backend) {
INDArray first = Nd4j.arange(0, 3).reshape(3, 1).castTo(DataType.DOUBLE);
INDArray ret = first.broadcast(3, 4);
INDArray testRet = Nd4j.create(new double[][] {{0, 0, 0, 0}, {1, 1, 1, 1}, {2, 2, 2, 2}});
assertEquals(testRet, ret);
INDArray r = Nd4j.arange(0, 4).reshape(1, 4).castTo(DataType.DOUBLE);
INDArray r2 = r.broadcast(4, 4);
INDArray testR2 = Nd4j.create(new double[][] {{0, 1, 2, 3}, {0, 1, 2, 3}, {0, 1, 2, 3}, {0, 1, 2, 3}});
assertEquals(testR2, r2);
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testGetColumns(Nd4jBackend backend) {
INDArray matrix = Nd4j.linspace(1, 6, 6, DataType.DOUBLE).reshape(2, 3);
INDArray matrixGet = matrix.getColumns(1, 2);
INDArray matrixAssertion = Nd4j.create(new double[][] {{2, 3}, {5, 6}});
assertEquals(matrixAssertion, matrixGet);
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testSort(Nd4jBackend backend) {
INDArray toSort = Nd4j.linspace(1, 4, 4, DataType.DOUBLE).reshape(2, 2);
INDArray ascending = Nd4j.sort(toSort.dup(), 1, true);
//rows are already sorted
assertEquals(toSort, ascending);
INDArray columnSorted = Nd4j.create(new double[] {2, 1, 4, 3}, new long[] {2, 2});
INDArray sorted = Nd4j.sort(toSort.dup(), 1, false);
assertEquals(columnSorted, sorted);
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testSortRows(Nd4jBackend backend) {
int nRows = 10;
int nCols = 5;
Random r = new Random(12345);
for (int i = 0; i < nCols; i++) {
INDArray in = Nd4j.linspace(1, nRows * nCols, nRows * nCols, DataType.DOUBLE).reshape(nRows, nCols);
List<Integer> order = new ArrayList<>(nRows);
//in.row(order(i)) should end up as out.row(i) - ascending
//in.row(order(i)) should end up as out.row(nRows-j-1) - descending
for (int j = 0; j < nRows; j++)
order.add(j);
Collections.shuffle(order, r);
for (int j = 0; j < nRows; j++)
in.putScalar(new long[] {j, i}, order.get(j));
INDArray outAsc = Nd4j.sortRows(in, i, true);
INDArray outDesc = Nd4j.sortRows(in, i, false);
// System.out.println("outDesc: " + Arrays.toString(outAsc.data().asFloat()));
for (int j = 0; j < nRows; j++) {
assertEquals(outAsc.getDouble(j, i), j, 1e-1);
int origRowIdxAsc = order.indexOf(j);
assertTrue(outAsc.getRow(j).equals(in.getRow(origRowIdxAsc)));
assertEquals((nRows - j - 1), outDesc.getDouble(j, i), 0.001f);
int origRowIdxDesc = order.indexOf(nRows - j - 1);
assertTrue(outDesc.getRow(j).equals(in.getRow(origRowIdxDesc)));
}
}
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testToFlattenedOrder(Nd4jBackend backend) {
INDArray concatC = Nd4j.linspace(1, 4, 4, DataType.DOUBLE).reshape('c', 2, 2);
INDArray concatF = Nd4j.create(new long[] {2, 2}, 'f');
concatF.assign(concatC);
INDArray assertionC = Nd4j.create(new double[] {1, 2, 3, 4, 1, 2, 3, 4});
INDArray testC = Nd4j.toFlattened('c', concatC, concatF);
assertEquals(assertionC, testC);
INDArray test = Nd4j.toFlattened('f', concatC, concatF);
INDArray assertion = Nd4j.create(new double[] {1, 3, 2, 4, 1, 3, 2, 4});
assertEquals(assertion, test);
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testZero(Nd4jBackend backend) {
Nd4j.ones(11).sumNumber();
Nd4j.ones(12).sumNumber();
Nd4j.ones(2).sumNumber();
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testSumNumberRepeatability(Nd4jBackend backend) {
INDArray arr = Nd4j.ones(1, 450).reshape('c', 150, 3);
double first = arr.sumNumber().doubleValue();
double assertion = 450;
assertEquals(assertion, first, 1e-1);
for (int i = 0; i < 50; i++) {
double second = arr.sumNumber().doubleValue();
assertEquals(assertion, second, 1e-1);
assertEquals( first, second, 1e-2,String.valueOf(i));
}
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testToFlattened2(Nd4jBackend backend) {
int rows = 3;
int cols = 4;
int dim2 = 5;
int dim3 = 6;
int length2d = rows * cols;
int length3d = rows * cols * dim2;
int length4d = rows * cols * dim2 * dim3;
INDArray c2d = Nd4j.linspace(1, length2d, length2d, DataType.DOUBLE).reshape('c', rows, cols);
INDArray f2d = Nd4j.create(new long[] {rows, cols}, 'f').assign(c2d).addi(0.1);
INDArray c3d = Nd4j.linspace(1, length3d, length3d, DataType.DOUBLE).reshape('c', rows, cols, dim2);
INDArray f3d = Nd4j.create(new long[] {rows, cols, dim2}).assign(c3d).addi(0.3);
c3d.addi(0.2);
INDArray c4d = Nd4j.linspace(1, length4d, length4d, DataType.DOUBLE).reshape('c', rows, cols, dim2, dim3);
INDArray f4d = Nd4j.create(new long[] {rows, cols, dim2, dim3}).assign(c4d).addi(0.3);
c4d.addi(0.4);
assertEquals(toFlattenedViaIterator('c', c2d, f2d), Nd4j.toFlattened('c', c2d, f2d));
assertEquals(toFlattenedViaIterator('f', c2d, f2d), Nd4j.toFlattened('f', c2d, f2d));
assertEquals(toFlattenedViaIterator('c', f2d, c2d), Nd4j.toFlattened('c', f2d, c2d));
assertEquals(toFlattenedViaIterator('f', f2d, c2d), Nd4j.toFlattened('f', f2d, c2d));
assertEquals(toFlattenedViaIterator('c', c3d, f3d), Nd4j.toFlattened('c', c3d, f3d));
assertEquals(toFlattenedViaIterator('f', c3d, f3d), Nd4j.toFlattened('f', c3d, f3d));
assertEquals(toFlattenedViaIterator('c', c2d, f2d, c3d, f3d), Nd4j.toFlattened('c', c2d, f2d, c3d, f3d));
assertEquals(toFlattenedViaIterator('f', c2d, f2d, c3d, f3d), Nd4j.toFlattened('f', c2d, f2d, c3d, f3d));
assertEquals(toFlattenedViaIterator('c', c4d, f4d), Nd4j.toFlattened('c', c4d, f4d));
assertEquals(toFlattenedViaIterator('f', c4d, f4d), Nd4j.toFlattened('f', c4d, f4d));
assertEquals(toFlattenedViaIterator('c', c2d, f2d, c3d, f3d, c4d, f4d),
Nd4j.toFlattened('c', c2d, f2d, c3d, f3d, c4d, f4d));
assertEquals(toFlattenedViaIterator('f', c2d, f2d, c3d, f3d, c4d, f4d),
Nd4j.toFlattened('f', c2d, f2d, c3d, f3d, c4d, f4d));
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testToFlattenedOnViews(Nd4jBackend backend) {
int rows = 8;
int cols = 8;
int dim2 = 4;
int length = rows * cols;
int length3d = rows * cols * dim2;
INDArray first = Nd4j.linspace(1, length, length, DataType.DOUBLE).reshape('c', rows, cols);
INDArray second = Nd4j.create(new long[] {rows, cols}, 'f').assign(first);
INDArray third = Nd4j.linspace(1, length3d, length3d, DataType.DOUBLE).reshape('c', rows, cols, dim2);
first.addi(0.1);
second.addi(0.2);
third.addi(0.3);
first = first.get(NDArrayIndex.interval(4, 8), NDArrayIndex.interval(0, 2, 8));
second = second.get(NDArrayIndex.interval(3, 7), NDArrayIndex.all());
third = third.permute(0, 2, 1);
INDArray noViewC = Nd4j.toFlattened('c', first.dup('c'), second.dup('c'), third.dup('c'));
INDArray noViewF = Nd4j.toFlattened('f', first.dup('f'), second.dup('f'), third.dup('f'));
assertEquals(noViewC, Nd4j.toFlattened('c', first, second, third));
//val result = Nd4j.exec(new Flatten('f', first, second, third))[0];
//assertEquals(noViewF, result);
assertEquals(noViewF, Nd4j.toFlattened('f', first, second, third));
}
private static INDArray toFlattenedViaIterator(char order, INDArray... toFlatten) {
int length = 0;
for (INDArray i : toFlatten)
length += i.length();
INDArray out = Nd4j.create(length);
int i = 0;
for (INDArray arr : toFlatten) {
NdIndexIterator iter = new NdIndexIterator(order, arr.shape());
while (iter.hasNext()) {
double next = arr.getDouble(iter.next());
out.putScalar(i++, next);
}
}
return out;
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testIsMax2(Nd4jBackend backend) {
//Tests: full buffer...
//1d
INDArray arr1 = Nd4j.create(new double[] {1, 2, 3, 1});
val res1 = Nd4j.getExecutioner().exec(new IsMax(arr1))[0];
INDArray exp1 = Nd4j.create(new boolean[] {false, false, true, false});
assertEquals(exp1, res1);
arr1 = Nd4j.create(new double[] {1, 2, 3, 1});
INDArray result = Nd4j.createUninitialized(DataType.BOOL, 4);
Nd4j.getExecutioner().execAndReturn(new IsMax(arr1, result));
assertEquals(Nd4j.create(new double[] {1, 2, 3, 1}), arr1);
assertEquals(exp1, result);
//2d
INDArray arr2d = Nd4j.create(new double[][] {{0, 1, 2}, {2, 9, 1}});
INDArray exp2d = Nd4j.create(new boolean[][] {{false, false, false}, {false, true, false}});
INDArray f = arr2d.dup('f');
INDArray out2dc = Nd4j.getExecutioner().exec(new IsMax(arr2d.dup('c')))[0];
INDArray out2df = Nd4j.getExecutioner().exec(new IsMax(arr2d.dup('f')))[0];
assertEquals(exp2d, out2dc);
assertEquals(exp2d, out2df);
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testToFlattened3(Nd4jBackend backend) {
INDArray inC1 = Nd4j.create(new long[] {10, 100}, 'c');
INDArray inC2 = Nd4j.create(new long[] {1, 100}, 'c');
INDArray inF1 = Nd4j.create(new long[] {10, 100}, 'f');
// INDArray inF1 = Nd4j.create(new long[]{784,1000},'f');
INDArray inF2 = Nd4j.create(new long[] {1, 100}, 'f');
Nd4j.toFlattened('f', inF1); //ok
Nd4j.toFlattened('f', inF2); //ok
Nd4j.toFlattened('f', inC1); //crash
Nd4j.toFlattened('f', inC2); //crash
Nd4j.toFlattened('c', inF1); //crash on shape [784,1000]. infinite loop on shape [10,100]
Nd4j.toFlattened('c', inF2); //ok
Nd4j.toFlattened('c', inC1); //ok
Nd4j.toFlattened('c', inC2); //ok
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testIsMaxEqualValues(Nd4jBackend backend) {
//Assumption here: should only have a 1 for *first* maximum value, if multiple values are exactly equal
//[1 1 1] -> [1 0 0]
//Loop to double check against any threading weirdness...
for (int i = 0; i < 10; i++) {
val res = Transforms.isMax(Nd4j.ones(3), DataType.BOOL);
assertEquals(Nd4j.create(new boolean[] {true, false, false}), res);
}
//[0 0 0 2 2 0] -> [0 0 0 1 0 0]
assertEquals(Nd4j.create(new boolean[] {false, false, false, true, false, false}), Transforms.isMax(Nd4j.create(new double[] {0, 0, 0, 2, 2, 0}), DataType.BOOL));
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testIMaxVector_1(Nd4jBackend backend) {
val array = Nd4j.ones(3);
val idx = array.argMax(0).getInt(0);
assertEquals(0, idx);
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testIMaxVector_2(Nd4jBackend backend) {
val array = Nd4j.ones(3);
val idx = array.argMax(Integer.MAX_VALUE).getInt(0);
assertEquals(0, idx);
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testIMaxVector_3(Nd4jBackend backend) {
val array = Nd4j.ones(3);
val idx = array.argMax().getInt(0);
assertEquals(0, idx);
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testIsMaxEqualValues_2(Nd4jBackend backend) {
//[0 2] [0 1]
//[2 1] -> [0 0]bg
INDArray orig = Nd4j.create(new double[][] {{0, 3}, {2, 1}});
INDArray exp = Nd4j.create(new double[][] {{0, 1}, {0, 0}});
INDArray outc = Transforms.isMax(orig.dup('c'));
assertEquals(exp, outc);
// log.info("Orig: {}", orig.dup('f').data().asFloat());
INDArray outf = Transforms.isMax(orig.dup('f'), orig.dup('f').ulike());
// log.info("OutF: {}", outf.data().asFloat());
assertEquals(exp, outf);
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testIsMaxEqualValues_3(Nd4jBackend backend) {
//[0 2] [0 1]
//[2 1] -> [0 0]
INDArray orig = Nd4j.create(new double[][] {{0, 2}, {3, 1}});
INDArray exp = Nd4j.create(new double[][] {{0, 0}, {1, 0}});
INDArray outc = Transforms.isMax(orig.dup('c'));
assertEquals(exp, outc);
INDArray outf = Transforms.isMax(orig.dup('f'), orig.dup('f').ulike());
assertEquals(exp, outf);
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testSqrt_1(Nd4jBackend backend) {
val x = Nd4j.createFromArray(9.0, 9.0, 9.0, 9.0);
val x2 = Nd4j.createFromArray(9.0, 9.0, 9.0, 9.0);
val e = Nd4j.createFromArray(3.0, 3.0, 3.0, 3.0);
val z1 = Transforms.sqrt(x, true);
val z2 = Transforms.sqrt(x2, false);
assertEquals(e, z2);
assertEquals(e, x2);
assertEquals(e, z1);
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testAssign_CF(Nd4jBackend backend) {
val orig = Nd4j.create(new double[][] {{0, 2}, {2, 1}});
val oc = orig.dup('c');
val of = orig.dup('f');
assertEquals(orig, oc);
assertEquals(orig, of);
}
@ParameterizedTest
@MethodSource("org.nd4j.linalg.BaseNd4jTestWithBackends#configs")
public void testIsMaxAlongDimension(Nd4jBackend backend) {
//1d: row vector
INDArray orig = Nd4j.create(new double[] {1, 2, 3, 1}).reshape(1,4 );
INDArray alongDim0 = Nd4j.getExecutioner().exec(new IsMax(orig.dup(), Nd4j.createUninitialized(DataType.BOOL, orig.shape()), 0))[0];
INDArray alongDim1 = Nd4j.getExecutioner().exec(new IsMax(orig.dup(), Nd4j.createUninitialized(DataType.BOOL, orig.shape()), 1))[0];
INDArray expAlong0 = Nd4j.create(new boolean[]{true, true, true, true}).reshape(1,4);
INDArray expAlong1 = Nd4j.create(new boolean[] {false, false, true, false}).reshape(1,4);
assertEquals(expAlong0, alongDim0);
assertEquals(expAlong1, alongDim1);
//1d: col vector
// System.out.println("----------------------------------");
INDArray col = Nd4j.create(new double[] {1, 2, 3, 1}, new long[] {4, 1});
INDArray alongDim0col = Nd4j.getExecutioner().exec(new IsMax(col.dup(), Nd4j.createUninitialized(DataType.BOOL, col.shape()), 0))[0];
INDArray alongDim1col = Nd4j.getExecutioner().exec(new IsMax(col.dup(), Nd4j.createUninitialized(DataType.BOOL, col.shape()),1))[0];
INDArray expAlong0col = Nd4j.create(new boolean[] {false, false, true, false}).reshape(4,1);
INDArray expAlong1col = Nd4j.create(new boolean[] {true, true, true, true}).reshape(4,1);
assertEquals(expAlong1col, alongDim1col);
assertEquals(expAlong0col, alongDim0col);