Skip to content

Commit 3c7e858

Browse files
RobAltenaAlexDBlack
authored andcommitted
fix compiler warnings in nd4j examples.
Signed-off-by: Robert Altena <[email protected]>
1 parent 752af12 commit 3c7e858

17 files changed

+52
-93
lines changed

nd4j-examples/src/main/java/org/nd4j/examples/ConcatVstackHstackPad.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*******************************************************************************
1+
/* *****************************************************************************
22
* Copyright (c) 2015-2019 Skymind, Inc.
33
*
44
* This program and the accompanying materials are made available under the
@@ -80,9 +80,6 @@ public static void main(String[] args) {
8080
INDArray vstack = Nd4j.vstack(ones,zeros);
8181
System.out.println("### VSTACK ####");
8282
System.out.println(vstack);
83-
84-
85-
8683
}
8784

8885
}

nd4j-examples/src/main/java/org/nd4j/examples/CustomOpsExamples.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*******************************************************************************
1+
/* *****************************************************************************
22
* Copyright (c) 2015-2019 Skymind, Inc.
33
*
44
* This program and the accompanying materials are made available under the
@@ -65,7 +65,5 @@ public static void main(String[] args){
6565
Nd4j.getExecutioner().exec(op);
6666

6767
System.out.println(output1 + "\n\n" + output2);
68-
6968
}
70-
7169
}

nd4j-examples/src/main/java/org/nd4j/examples/Nd4jEx10_ElementWiseOperation.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*******************************************************************************
1+
/* *****************************************************************************
22
* Copyright (c) 2015-2019 Skymind, Inc.
33
*
44
* This program and the accompanying materials are made available under the
@@ -16,7 +16,6 @@
1616

1717
package org.nd4j.examples;
1818

19-
import org.nd4j.linalg.api.buffer.DataBuffer;
2019
import org.nd4j.linalg.api.ndarray.INDArray;
2120
import org.nd4j.linalg.factory.Nd4j;
2221

@@ -27,13 +26,10 @@
2726
*
2827
* Created by cvn on 9/6/14.
2928
*/
30-
31-
3229
public class Nd4jEx10_ElementWiseOperation {
3330

3431
public static void main(String[] args) {
35-
//Nd4j.dtype = DataBuffer.DOUBLE;
36-
INDArray nd1 = Nd4j.create(new double[]{1,2,3,4,5,6},new int[]{2,3});
32+
INDArray nd1 = Nd4j.create(new double[]{1,2,3,4,5,6}, 2,3);
3733
System.out.println("nd1:\n"+nd1);
3834

3935
//create nd-array variable ndv to be able to print result of nondestructive operations. add scalar to matrix and assign ndv the sum.
@@ -53,7 +49,7 @@ public static void main(String[] args) {
5349

5450
//add column vector to matrix
5551

56-
INDArray nd2 = Nd4j.create(new double[]{10,20},new int[]{2,1}); //vector as column
52+
INDArray nd2 = Nd4j.create(new double[]{10,20}, 2,1); //vector as column
5753
System.out.println("nd2:\n"+nd2);
5854

5955
ndv = nd1.addColumnVector(nd2);
@@ -62,7 +58,7 @@ public static void main(String[] args) {
6258

6359
// add row vector to matrix
6460

65-
INDArray nd3 = Nd4j.create(new double[]{30,40,50},new int[]{1, 3}); //vector as row
61+
INDArray nd3 = Nd4j.create(new double[]{30,40,50}, 1, 3); //vector as row
6662
System.out.println("nd3:\n"+nd3);
6763

6864
ndv = nd1.addRowVector(nd3);
@@ -71,14 +67,11 @@ public static void main(String[] args) {
7167

7268
//multiply two matrices of equal dimensions elementwise.
7369

74-
INDArray nd4 = Nd4j.create(new double[]{1,2,1,2,1,2},new int[]{2,3});
70+
INDArray nd4 = Nd4j.create(new double[]{1,2,1,2,1,2}, 2,3);
7571
System.out.println("nd4:\n"+nd4);
7672

7773
ndv = nd1.mul(nd4);
7874

7975
System.out.println("nd1.mul(nd4):\n"+ndv);
80-
81-
8276
}
83-
8477
}

nd4j-examples/src/main/java/org/nd4j/examples/Nd4jEx11_Axpy.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*******************************************************************************
1+
/* *****************************************************************************
22
* Copyright (c) 2015-2019 Skymind, Inc.
33
*
44
* This program and the accompanying materials are made available under the
@@ -16,7 +16,6 @@
1616

1717
package org.nd4j.examples;
1818

19-
import org.nd4j.linalg.api.buffer.DataBuffer;
2019
import org.nd4j.linalg.api.buffer.DataType;
2120
import org.nd4j.linalg.api.ndarray.INDArray;
2221
import org.nd4j.linalg.factory.Nd4j;

nd4j-examples/src/main/java/org/nd4j/examples/Nd4jEx12_INDArrayFromCSCMatrix.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*******************************************************************************
1+
/* *****************************************************************************
22
* Copyright (c) 2015-2019 Skymind, Inc.
33
*
44
* This program and the accompanying materials are made available under the
@@ -26,7 +26,6 @@
2626
*
2727
* @author Audrey Loeffel
2828
*/
29-
3029
public class Nd4jEx12_INDArrayFromCSCMatrix {
3130

3231
public static void main(String[] args) {
@@ -36,33 +35,29 @@ public static void main(String[] args) {
3635
int[] columnPointers = {0, 1, 3, 5};
3736
double[] dataCSC = {11, 12, 22, 13, 23, 14, 34};
3837
int[] shapeCSC = {5, 4};
39-
INDArray matrix = createFromCSC(dataCSC, rowIndices, columnPointers, shapeCSC);
40-
System.out.println("nd array is: \n" + matrix);
41-
long[] matrixCSCShape = matrix.shape();
42-
System.out.println("Its shape is [" + matrixCSCShape[0] +","+ matrixCSCShape[1]+"]");
38+
createFromCSC(dataCSC, rowIndices, columnPointers, shapeCSC);
4339

4440
System.out.println("-------- CSC with empty column to INDArray -------");
4541
int[] rowIndices2 = {1, 2};
4642
int[] columnPointers2 = {0, 0, 1};
4743
double[] dataCSC2 = {22, 33};
4844
int[] shapeCSC2 = {4, 3};
49-
INDArray matrix2 = createFromCSC(dataCSC2, rowIndices2, columnPointers2, shapeCSC2);
50-
System.out.println("nd array is: \n" + matrix2);
51-
long[] matrixCSCShape2 = matrix2.shape();
52-
System.out.println("Its shape is [" + matrixCSCShape2[0] +","+ matrixCSCShape2[1]+"]");
45+
createFromCSC(dataCSC2, rowIndices2, columnPointers2, shapeCSC2);
5346
}
5447

55-
private static INDArray createFromCSC(double[] data, int[] rowIndices, int[] columnPointers, int[] shape){
48+
private static void createFromCSC(double[] data, int[] rowIndices, int[] columnPointers, int[] shape){
5649
INDArray result = Nd4j.zeros(shape);
5750
int columns = shape[1];
5851
int dataIdx = 0;
5952
for(int i = 0; i < columns; i++){
6053
for(int k = dataIdx; k < (i == columnPointers.length-1 ? rowIndices.length : columnPointers[i+1]); k++, dataIdx++){
6154
int j = rowIndices[k];
6255
result.put(j, i, data[k]);
63-
//System.out.println("i = "+i+", k = "+k+ ", data[k] = "+data[k]+"\n matrix = "+result.toString());
6456
}
6557
}
66-
return result;
58+
// Normally one would return the result. For this example we just print the result.
59+
System.out.println("nd array is: \n" + result);
60+
long[] matrixCSCShape2 = result.shape();
61+
System.out.println("Its shape is [" + matrixCSCShape2[0] +","+ matrixCSCShape2[1]+"]");
6762
}
6863
}

nd4j-examples/src/main/java/org/nd4j/examples/Nd4jEx13_LargeMatrices.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*******************************************************************************
1+
/* *****************************************************************************
22
* Copyright (c) 2015-2019 Skymind, Inc.
33
*
44
* This program and the accompanying materials are made available under the
@@ -18,8 +18,6 @@
1818

1919
import org.nd4j.linalg.api.ndarray.INDArray;
2020
import org.nd4j.linalg.factory.Nd4j;
21-
import org.slf4j.Logger;
22-
import org.slf4j.LoggerFactory;
2321

2422
/**
2523
* --- Nd4j Example 13: Large Matrix ---
@@ -28,15 +26,10 @@
2826
*
2927
* @author Adam Gibson
3028
*/
31-
3229
public class Nd4jEx13_LargeMatrices {
3330

34-
private static Logger log = LoggerFactory.getLogger(Nd4jEx13_LargeMatrices.class);
35-
3631
public static void main(String[] args) {
37-
INDArray n = Nd4j.linspace(1,10000000,10000000);
32+
INDArray n = Nd4j.linspace(1,10000000,10000000).reshape(1, 10000000);
3833
System.out.println("MMUL" + n.mmul(n.transpose()));
39-
4034
}
41-
4235
}

nd4j-examples/src/main/java/org/nd4j/examples/Nd4jEx14_Normalizers.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*******************************************************************************
1+
/* *****************************************************************************
22
* Copyright (c) 2015-2019 Skymind, Inc.
33
*
44
* This program and the accompanying materials are made available under the

nd4j-examples/src/main/java/org/nd4j/examples/Nd4jEx15_Workspaces.java

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*******************************************************************************
1+
/* *****************************************************************************
22
* Copyright (c) 2015-2019 Skymind, Inc.
33
*
44
* This program and the accompanying materials are made available under the
@@ -38,24 +38,24 @@
3838
*
3939
4040
*/
41+
@SuppressWarnings("unused") //variable names clarify the example.
4142
public class Nd4jEx15_Workspaces {
4243
private static final Logger log = org.slf4j.LoggerFactory.getLogger(Nd4jEx15_Workspaces.class);
4344

44-
public static void main(String[] args) throws Exception {
45-
/**
45+
public static void main(String[] args) {
46+
/*
4647
* Each workspace is tied to a JVM Thread via ID. So, same ID in different threads will point to different actual workspaces
4748
* Each workspace is created using some configuration, and different workspaces can either share the same config, or have their own
4849
*/
4950

50-
// we create config with 10MB memory space preallocated
51+
// we create config with 10MB memory space pre allocated
5152
WorkspaceConfiguration initialConfig = WorkspaceConfiguration.builder()
5253
.initialSize(10 * 1024L * 1024L)
5354
.policyAllocation(AllocationPolicy.STRICT)
5455
.policyLearning(LearningPolicy.NONE)
5556
.build();
5657

57-
58-
INDArray result = null;
58+
INDArray result;
5959

6060
// we use
6161
try(MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(initialConfig, "SOME_ID")) {
@@ -68,22 +68,19 @@ public static void main(String[] args) throws Exception {
6868
// please note, new array mean will be also attached to this workspace
6969
INDArray mean = array.mean(1);
7070

71-
/**
71+
/*
7272
* PLEASE NOTE: if after doing some operations on the workspace, you want to bring result away from it, you should either leverage it, or detach
7373
*/
74-
7574
result = mean.detach();
7675
}
7776

7877
// Since we've detached array, we expect FALSE printed out here. So, result array is managed by GC now.
7978
log.info("Array attached? {}", result.isAttached());
8079

8180

82-
83-
/**
81+
/*
8482
* Workspace can be initially preallocated as shown above, or can be learning their desired size over time, or after first loop
8583
*/
86-
8784
WorkspaceConfiguration learningConfig = WorkspaceConfiguration.builder()
8885
.policyAllocation(AllocationPolicy.STRICT) // <-- this option disables overallocation behavior
8986
.policyLearning(LearningPolicy.FIRST_LOOP) // <-- this option makes workspace learning after first loop
@@ -93,7 +90,7 @@ public static void main(String[] args) throws Exception {
9390
try (MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(learningConfig, "OTHER_ID")) {
9491
INDArray array = Nd4j.create(100);
9592

96-
/**
93+
/*
9794
* At first iteration, workspace will be spilling all allocations as separate memory chunks.
9895
* But after first iteration is finished - workspace will be allocated, to match all required allocations in this loop
9996
* So, further iterations will be reusing workspace memory over and over again
@@ -102,7 +99,7 @@ public static void main(String[] args) throws Exception {
10299
}
103100

104101

105-
/**
102+
/*
106103
* Workspaces can be nested. And INDArrays can migrate between them, if needed
107104
*/
108105

@@ -123,7 +120,7 @@ public static void main(String[] args) throws Exception {
123120
}
124121

125122

126-
/**
123+
/*
127124
* You can break your workspace flow, if, for some reason you need part of calculations to be handled with GC
128125
*/
129126
try(MemoryWorkspace ws1 = Nd4j.getWorkspaceManager().getAndActivateWorkspace(initialConfig, "SOME_ID")) {
@@ -132,17 +129,14 @@ public static void main(String[] args) throws Exception {
132129

133130
try(MemoryWorkspace ws = Nd4j.getWorkspaceManager().scopeOutOfWorkspaces()) {
134131
// anything allocated within this try block will be managed by GC
135-
136132
array2 = Nd4j.create(10, 10).assign(2.0f);
137133
}
138134

139-
140135
// at this point sumRes contains valid data, allocated in current workspace. We expect 300 printed here.
141136
log.info("Sum: {}", array1.addi(array2).sumNumber().floatValue());
142137
}
143138

144-
145-
/**
139+
/*
146140
* It's also possible to build workspace that'll be acting as circular buffer.
147141
*/
148142
WorkspaceConfiguration circularConfig = WorkspaceConfiguration.builder()
@@ -153,7 +147,7 @@ public static void main(String[] args) throws Exception {
153147
.build();
154148

155149
for (int x = 0; x < 10; x++) {
156-
// since this workspace is ciruclar, we know that all pointers allocated before buffer ended - will be viable.
150+
// since this workspace is circular, we know that all pointers allocated before buffer ended - will be viable.
157151
try (MemoryWorkspace ws1 = Nd4j.getWorkspaceManager().getAndActivateWorkspace(circularConfig, "CIRCULAR_ID")) {
158152
INDArray array = Nd4j.create(100);
159153
// so, you can use this array anywhere as long as YOU're sure buffer wasn't reset.

nd4j-examples/src/main/java/org/nd4j/examples/Nd4jEx1_INDArrayBasics.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*******************************************************************************
1+
/* *****************************************************************************
22
* Copyright (c) 2015-2019 Skymind, Inc.
33
*
44
* This program and the accompanying materials are made available under the
@@ -64,7 +64,7 @@ public static void main(String[] args) {
6464
System.out.println("Is a vector: " + myArray.isVector());
6565
System.out.println("Is a scalar: " + myArray.isScalar());
6666
System.out.println("Is a matrix: " + myArray.isMatrix());
67-
System.out.println("Is a square matrix: " + myArray.isSquare());;
67+
System.out.println("Is a square matrix: " + myArray.isSquare());
6868

6969

7070

nd4j-examples/src/main/java/org/nd4j/examples/Nd4jEx2_CreatingINDArrays.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*******************************************************************************
1+
/* *****************************************************************************
22
* Copyright (c) 2015-2019 Skymind, Inc.
33
*
44
* This program and the accompanying materials are made available under the
@@ -55,7 +55,7 @@ public static void main(String[] args){
5555
System.out.println("rowVector: " + rowVector);
5656
System.out.println("rowVector.shape(): " + Arrays.toString(rowVector.shape())); //1 row, 3 columns
5757

58-
INDArray columnVector = Nd4j.create(vectorDouble, new int[]{3,1}); //Manually specify: 3 rows, 1 column
58+
INDArray columnVector = Nd4j.create(vectorDouble, 3,1); //Manually specify: 3 rows, 1 column
5959
System.out.println("columnVector: " + columnVector); //Note for printing: row/column vectors are printed as one line
6060
System.out.println("columnVector.shape(): " + Arrays.toString(columnVector.shape())); //3 row, 1 columns
6161

@@ -71,6 +71,7 @@ public static void main(String[] args){
7171
//It is also possible to create random INDArrays:
7272
//Be aware however that by default, random values are printed with truncated precision using INDArray.toString()
7373
int[] shape = new int[]{nRows, nColumns};
74+
long[] shape_long = new long []{nRows, nColumns};
7475
INDArray uniformRandom = Nd4j.rand(shape);
7576
System.out.println("\n\n\nUniform random array:");
7677
System.out.println(uniformRandom);
@@ -82,8 +83,8 @@ public static void main(String[] args){
8283

8384
//We can make things repeatable using RNG seed:
8485
long rngSeed = 12345;
85-
INDArray uniformRandom2 = Nd4j.rand(shape, rngSeed);
86-
INDArray uniformRandom3 = Nd4j.rand(shape, rngSeed);
86+
INDArray uniformRandom2 = Nd4j.rand(rngSeed, shape_long);
87+
INDArray uniformRandom3 = Nd4j.rand(rngSeed, shape_long);
8788
System.out.println("\nUniform random arrays with same fixed seed:");
8889
System.out.println(uniformRandom2);
8990
System.out.println();

0 commit comments

Comments
 (0)