NDArray是一个基于Java的N维数组工具,我觉得最直观的理解可以认为它是Numpy里的NDArray的Java实现。
NDArray是DJL的核心数据结构,包含超过 60+ 个在Java中的方式实现与NumPy相同的结果。
另外NDArray支持硬件加速,如在CPU上的 MKLDNN 加速以及GPU上的CUDA 加速。
NDArray在DJL中的内部结构:
NDArray包含三个关键的层:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.xundh</groupId> <artifactId>djl-learning</artifactId> <version>0.1-SNAPSHOT</version> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <java.version>8</java.version> <djl.version>0.13.0-SNAPSHOT</djl.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>ai.djl</groupId> <artifactId>bom</artifactId> <version>${djl.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>ai.djl</groupId> <artifactId>api</artifactId> </dependency> <dependency> <groupId>ai.djl</groupId> <artifactId>basicdataset</artifactId> </dependency> <dependency> <groupId>ai.djl</groupId> <artifactId>model-zoo</artifactId> </dependency> <!-- Pytorch --> <dependency> <groupId>ai.djl.pytorch</groupId> <artifactId>pytorch-engine</artifactId> </dependency> <dependency> <groupId>ai.djl.pytorch</groupId> <artifactId>pytorch-native-auto</artifactId> <version>1.7.0</version> </dependency> </dependencies> </project>
package com.xundh; import ai.djl.ndarray.NDArray; import ai.djl.ndarray.NDManager; import ai.djl.ndarray.types.Shape; public class NDArrayLearning { public static void main(String[] args){ try(NDManager manager = NDManager.newBaseManager()) { // 创建 NDArray NDArray nd = manager.ones(new Shape(2, 3)); System.out.println(nd); } } }
NDArray nd = manager.ones(new Shape(2, 3));
结果:
ND: (2, 3) cpu() float32 [[1., 1., 1.], [1., 1., 1.], ]
NDArray nd = manager.randomUniform(0, 1, new Shape(1, 1, 4));
结果:
ND: (1, 1, 4) cpu() float32 [[[0.5488, 0.5928, 0.7152, 0.8443], ], ]
NDArray nd = manager.zeros(new Shape(2,3), DataType.INT64); System.out.println(nd);
结果:
ND: (2, 3) cpu() int64 [[ 0, 0, 0], [ 0, 0, 0], ]
NDArray nd = manager.eye(3,-1); System.out.println(nd);
结果:
ND: (3, 3) cpu() float32 [[0., 0., 0.], [1., 0., 0.], [0., 1., 0.], ]
NDManager支持多达20种在NumPy中NDArray创建的方法,更多参见:
https://javadoc.io/doc/ai.djl/api/latest/ai/djl/ndarray/NDManager.html
NDArray nd = manager.arange(1, 10).reshape(3, 3); nd = nd.transpose();
结果:
ND: (3, 3) cpu() int32 [[ 1, 2, 3], [ 4, 5, 6], [ 7, 8, 9], ] ND: (3, 3) cpu() int32 [[ 1, 4, 7], [ 2, 5, 8], [ 3, 6, 9], ]
NDArray nd = manager.arange(1, 10).reshape(3, 3); // 加10 nd = nd.add(10); System.out.println(nd);
结果:
ND: (3, 3) cpu() int32 [[11, 12, 13], [14, 15, 16], [17, 18, 19], ]
更多支持的计算函数参见:
https://javadoc.io/doc/ai.djl/api/latest/ai/djl/ndarray/NDArray.html
NDArray nd = manager.arange(5, 14); System.out.println(nd); nd = nd.get(nd.gte(10)); System.out.println(nd);
结果:
ND: (9) cpu() int32 [ 5, 6, 7, 8, 9, 10, 11, 12, 13] ND: (4) cpu() int32 [10, 11, 12, 13]
NDArray nd = manager.arange(1, 10).reshape(3, 3); System.out.println(nd); nd.set(new NDIndex(":, 1"), array -> array.mul(2)); System.out.println(nd);
结果:
ND: (3, 3) cpu() int32 [[ 1, 2, 3], [ 4, 5, 6], [ 7, 8, 9], ] ND: (3, 3) cpu() int32 [[ 1, 4, 3], [ 4, 10, 6], [ 7, 16, 9], ]