百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 编程字典 > 正文

Java程序员学深度学习 DJL上手4 NDArray基本操作

toyiye 2024-08-22 02:19 7 浏览 0 评论


一、介绍NDArray

NDArray是一个基于Java的N维数组工具,我觉得最直观的理解可以认为它是Numpy里的NDArray的Java实现。
NDArray是DJL的核心数据结构,包含超过 60+ 个在Java中的方式实现与NumPy相同的结果。
另外NDArray支持硬件加速,如在CPU上的 MKLDNN 加速以及GPU上的CUDA 加速。

NDArray在DJL中的内部结构:

NDArray包含三个关键的层:

  • 界面层
  • DJL各种深度学习算法为NDArray开发的引擎
  • C++层

二、准备环境

  • idea
  • maven

1. 创建项目

2. pom.xml

<?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>

3. NDArrayLearning.java

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);
        }
    }
}

三、创建数组

1. 创建全1的N维数组

NDArray nd = manager.ones(new Shape(2, 3));

结果:

ND: (2, 3) cpu() float32
[[1., 1., 1.],
 [1., 1., 1.],
]

2. 创建随机数的N维数组

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],
 ],
]

3. 创建全0数组

            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],
]

4. 创建对角为1的矩阵

            NDArray nd = manager.eye(3,-1);
            System.out.println(nd);

结果:

ND: (3, 3) cpu() float32
[[0., 0., 0.],
 [1., 0., 0.],
 [0., 1., 0.],
]

5. 创建顺序数组

            NDArray nd = manager.arange(2,5);
            System.out.println(nd);

结果:

ND: (3) cpu() int32
[ 2,  3,  4]

6. 计算大小和形状

            NDArray nd = manager.arange(2,5);
            System.out.println(nd);
            System.out.println(nd.size());
            System.out.println(nd.getShape());

结果:

ND: (3) cpu() int32
[ 2,  3,  4]
3
(3)
ND: (2, 3) cpu() float32
[[1., 1., 1.],
 [1., 1., 1.],
]

结果:

6
(2, 3)

NDManager支持多达20种在NumPy中NDArray创建的方法,更多参见:
https://javadoc.io/doc/ai.djl/api/latest/ai/djl/ndarray/NDManager.html

四、数组计算

1. 转置

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],
]

2. 所有元素加

            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],
]

3. 转换形状

            NDArray nd = manager.arange(12);
            System.out.println(nd);
            System.out.println(nd.getShape());
            System.out.println(nd.reshape(3,4));

结果:

ND: (12) cpu() int32
[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11]

(12)
ND: (3, 4) cpu() int32
[[ 0,  1,  2,  3],
 [ 4,  5,  6,  7],
 [ 8,  9, 10, 11],
]

4. 两个数组相加

            NDArray x = manager.create(new float[]{1f, 2f, 4f, 8f});
            NDArray y = manager.create(new float[]{2f, 2f, 2f, 2f});
            System.out.println(x.add(y));

结果:

ND: (4) cpu() float32
[ 3.,  4.,  6., 10.]

5. 取幂运算

x.exp();

6. 两个数组连接

x = manager.arange(12f).reshape(3, 4);
y = manager.create(new float[]{2, 1, 4, 3, 1, 2, 3, 4, 4, 3, 2, 1}, new Shape(3, 4));
x.concat(y) // default axis = 0

默认x轴位置在0结果:

ND: (6, 4) gpu(0) float32
[[ 0.,  1.,  2.,  3.],
 [ 4.,  5.,  6.,  7.],
 [ 8.,  9., 10., 11.],
 [ 2.,  1.,  4.,  3.],
 [ 1.,  2.,  3.,  4.],
 [ 4.,  3.,  2.,  1.],
]

7. 判断相等

x.eq(y)

结果示例:

[[false,  true, false,  true],
 [false, false, false, false],
 [false, false, false, false],
]

更多支持的计算函数参见:
https://javadoc.io/doc/ai.djl/api/latest/ai/djl/ndarray/NDArray.html

五、Get和Set

1. 筛选大于10的

            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]

2. 一个3×3的矩阵,把第二列的数据都乘以2

            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],
]

相关推荐

# Python 3 # Python 3字典Dictionary(1)

Python3字典字典是另一种可变容器模型,且可存储任意类型对象。字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中,格式如...

Python第八课:数据类型中的字典及其函数与方法

Python3字典字典是另一种可变容器模型,且可存储任意类型对象。字典的每个键值...

Python中字典详解(python 中字典)

字典是Python中使用键进行索引的重要数据结构。它们是无序的项序列(键值对),这意味着顺序不被保留。键是不可变的。与列表一样,字典的值可以保存异构数据,即整数、浮点、字符串、NaN、布尔值、列表、数...

Python3.9又更新了:dict内置新功能,正式版十月见面

机器之心报道参与:一鸣、JaminPython3.8的热乎劲还没过去,Python就又双叒叕要更新了。近日,3.9版本的第四个alpha版已经开源。从文档中,我们可以看到官方透露的对dic...

Python3 基本数据类型详解(python三种基本数据类型)

文章来源:加米谷大数据Python中的变量不需要声明。每个变量在使用前都必须赋值,变量赋值以后该变量才会被创建。在Python中,变量就是变量,它没有类型,我们所说的"类型"是变...

一文掌握Python的字典(python字典用法大全)

字典是Python中最强大、最灵活的内置数据结构之一。它们允许存储键值对,从而实现高效的数据检索、操作和组织。本文深入探讨了字典,涵盖了它们的创建、操作和高级用法,以帮助中级Python开发...

超级完整|Python字典详解(python字典的方法或操作)

一、字典概述01字典的格式Python字典是一种可变容器模型,且可存储任意类型对象,如字符串、数字、元组等其他容器模型。字典的每个键值key=>value对用冒号:分割,每个对之间用逗号,...

Python3.9版本新特性:字典合并操作的详细解读

处于测试阶段的Python3.9版本中有一个新特性:我们在使用Python字典时,将能够编写出更可读、更紧凑的代码啦!Python版本你现在使用哪种版本的Python?3.7分?3.5分?还是2.7...

python 自学,字典3(一些例子)(python字典有哪些基本操作)

例子11;如何批量复制字典里的内容2;如何批量修改字典的内容3;如何批量修改字典里某些指定的内容...

Python3.9中的字典合并和更新,几乎影响了所有Python程序员

全文共2837字,预计学习时长9分钟Python3.9正在积极开发,并计划于今年10月发布。2月26日,开发团队发布了alpha4版本。该版本引入了新的合并(|)和更新(|=)运算符,这个新特性几乎...

Python3大字典:《Python3自学速查手册.pdf》限时下载中

最近有人会想了,2022了,想学Python晚不晚,学习python有前途吗?IT行业行业薪资高,发展前景好,是很多求职群里严重的香饽饽,而要进入这个高薪行业,也不是那么轻而易举的,拿信工专业的大学生...

python学习——字典(python字典基本操作)

字典Python的字典数据类型是基于hash散列算法实现的,采用键值对(key:value)的形式,根据key的值计算value的地址,具有非常快的查取和插入速度。但它是无序的,包含的元素个数不限,值...

324页清华教授撰写【Python 3 菜鸟查询手册】火了,小白入门字典

如何入门学习python...

Python3.9中的字典合并和更新,了解一下

全文共2837字,预计学习时长9分钟Python3.9正在积极开发,并计划于今年10月发布。2月26日,开发团队发布了alpha4版本。该版本引入了新的合并(|)和更新(|=)运算符,这个新特性几乎...

python3基础之字典(python中字典的基本操作)

字典和列表一样,也是python内置的一种数据结构。字典的结构如下图:列表用中括号[]把元素包起来,而字典是用大括号{}把元素包起来,只不过字典的每一个元素都包含键和值两部分。键和值是一一对应的...

取消回复欢迎 发表评论:

请填写验证码