Onnx interop Key features and brief samples.
File f = new File ("path/to/your/model.onnx");
INDArray x = Nd4j.scalar(1.0f).reshape(1,1);
INDArray y = Nd4j.scalar(1.0f).reshape(1,1);
OnnxRuntimeRunner onnxRuntimeRunner = OnnxRuntimeRunner.builder()
.modelUri(f.getAbsolutePath())
.build();
Map<String,INDArray> inputs = new LinkedHashMap<>();
inputs.put("x",x);
inputs.put("y",y);
Map<String, INDArray> exec = onnxRuntimeRunner.exec(inputs);
INDArray z = exec.get("z");<dependency>
<groupId>org.nd4j</groupId>
<artifactId>nd4j-onnxruntime</groupId>
<version>${nd4j.version}</version>
</dependency>TVM Key features and brief samples.
File f = new File ("path/to/your/model.pb");
INDArray x = Nd4j.scalar(1.0f).reshape(1,1);
TvmRunner tvmRunner = TvmRunner.builder()
.modelUri(f.getAbsolutePath())
.build();
Map<String,INDArray> inputs = new LinkedHashMap<>();
inputs.put("x",x);
Map<String, INDArray> exec = tvmRunner.exec(inputs);
INDArray z = exec.get("0");<dependency>
<groupId>org.nd4j</groupId>
<artifactId>nd4j-tvm</groupId>
<version>${nd4j.version}</version>
</dependency> [3.0]
[1.0 ,2.0] * [4.0] = (1.0 * 3.0) + (2.0 * 4.0) = 11INDArray nd = Nd4j.create(new float[]{1,2},new int[]{2}); //row vector
INDArray nd2 = Nd4j.create(new float[]{3,4},new int[]{2, 1}); //column vectornd.mmul(nd2);INDArray nd4 = Nd4j.create(new float[]{3,4,5,6},new int[]{2, 2});
nd.mmul(nd4);
[3.0 ,5.0]
[1.0 ,2.0] * [4.0 ,6.0] = [(1.0 * 3.0) + (2.0 * 4.0), (1.0 * 5.0) + (2.0 * 6.0)] = [11, 17]INDArray nd3 = Nd4j.create(new float[]{1,3,2,4},new int[]{2,2});
nd3.mmul(nd4);[1.0 ,2.0] [3.0 ,5.0] [(1.0 * 3.0) + (2.0 * 4.0), (1.0 * 5.0) + (2.0 * 6.0), [11, 17]
[3.0 ,4.0] * [4.0 ,6.0] = (3.0 * 3.0) + (4.0 * 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8.0 ,2.0 ,8.0 ,2.0 ,8.0 ,2.0 ,8.0 ,2.0 ,8.0]
[3.0 ,9.0 ,3.0 ,9.0 ,3.0 ,9.0 ,3.0 ,9.0 ,3.0 ,9.0 ,3.0 ,9.0]
[4.0 ,10.0 ,4.0 ,10.0 ,4.0 ,10.0 ,4.0 ,10.0 ,4.0 ,10.0 ,4.0 ,10.0]
[5.0 ,11.0 ,5.0 ,11.0 ,5.0 ,11.0 ,5.0 ,11.0 ,5.0 ,11.0 ,5.0 ,11.0]
[6.0 ,12.0 ,6.0 ,12.0 ,6.0 ,12.0 ,6.0 ,12.0 ,6.0 ,12.0 ,6.0 ,12.0]Tensorflow interop Key features and brief samples.
File f = new File ("path/to/your/model.pb");
INDArray x = Nd4j.scalar(1.0f).reshape(1,1);
INDArray y = Nd4j.scalar(1.0f).reshape(1,1);
List<String> inputNames = Arrays.asList("x","y");
List<String> outputNames = Arrays.asList("z");
GraphRunner tensorflowRunner = GraphRunner.builder()
.inputNames(inputNames)
.graphPath(f)
.outputNames(outputNames).
build();
Map<String,INDArray> inputs = new LinkedHashMap<>();
inputs.put("x",x);
inputs.put("y",y);
Map<String, INDArray> exec = tensorflowRunner.exec(inputs);
INDArray z = exec.get("z");<dependency>
<groupId>org.nd4j</groupId>
<artifactId>nd4j-tensorflow</groupId>
<version>${nd4j.version}</version>
</dependency>INDArray nd = Nd4j.create(new float[]{1,2,3,4},new int[]{2,2});System.out.println(nd);[[1.0 ,3.0]
[2.0 ,4.0]
][[1.0 ,2.0]
[3.0 ,4.0]
]nd.add(1);[[1.0 + 1 ,3.0 + 1]
[2.0 + 1,4.0 + 1]
][[2.0 ,4.0]
[3.0 ,5.0]
]nd.mul(5);[[10.0 ,20.0]
[15.0 ,25.0]
]nd.subi(3);
nd.divi(2);[[3.5 ,8.5]
[6.0 ,11.0]
]INDArray nd = Nd4j.create(new float[]{1,2,3,4},new int[]{2,2});
INDArray nd2 = Nd4j.create(new float[]{5,6},new int[]{2,1}); //vector as column
INDArray nd3 = Nd4j.create(new float[]{5,6},new int[]{2}); //vector as row[[1.00, 2.00],
[3.00, 4.00]] nd.addColumnVector(nd2);[1.0 ,2.0] [5.0] [6.0 ,7.0]
[3.0 ,4.0] + [6.0] = [9.0 ,10.0]nd.addRowVector(nd3);[1.0 ,2.0] [6.0 ,8.0]
[3.0 ,4.0] + [5.0 ,6.0] = [8.0 ,10.0]System.out.println(nd);[5.0 ,6.0] [1.0 ,3.0] [c , c] [1.0 ,3.0] [1c ,3c]
c * [2.0 ,4.0] = [c , c] * [2.0 ,4.0] = [2c ,4c][1.0 ,3.0] [5.0] [1.0 ,3.0] [5.0 ,5.0] [6.0 ,8.0]
[2.0 ,4.0] + [6.0] = [2.0 ,4.0] + [6.0 ,6.0] = [8.0 ,10.0][1.0 ,3.0] [1.0 ,3.0] [5.0 ,6.0] [6.0 ,9.0]
[2.0 ,4.0] + [5.0 ,6.0] = [2.0 ,4.0] + [5.0 ,6.0] = [7.0 ,10.0]INDArray nd4 = Nd4j.create(new float[]{5,6,7,8},new int[]{2,2});
nd.add(nd4);[1.0 ,3.0] [5.0 ,7.0] [6.0 ,10.0]
[2.0 ,4.0] + [6.0 ,8.0] = [8.0 ,12.0]nd.muli(nd4);
[1.0 ,3.0] [5.0 ,7.0] [5.0 ,21.0]
[2.0 ,4.0] * [6.0 ,8.0] = [12.0 ,32.0]