current position:Home>Dl4j practice 5: basic skills of matrix operation, docker introduction video tutorial

Dl4j practice 5: basic skills of matrix operation, docker introduction video tutorial

2022-01-27 02:33:10 Alibaba_ Open Source

  • Matrix properties :

 Insert picture description here

  • Create operation :

 Insert picture description here

  • Read operations :

 Insert picture description here

  • Write operations :

 Insert picture description here

  • Matrix computing :

 Insert picture description here

Source download

  • The complete source code of this actual combat can be found in GitHub Download to , The address and link information is shown in the following table (https://github.com/zq2599/blog_demos):

| name | link | remarks |

| :-- | :-- | :-- |

| Project home page | https://github.com/zq2599/blog_demos | The project is in progress. GitHub Home page on |

| git Warehouse address (https) | https://github.com/zq2599/blog_demos.git | The warehouse address of the source code of the project ,https agreement |

| git Warehouse address (ssh) | [email protected]:zq2599/blog_demos.git | The warehouse address of the source code of the project ,ssh agreement |

  • This git Multiple folders in project ,《DL4J actual combat 》 The source code of the series is dl4j-tutorials Under the folder , As shown in the red box below :

 Insert picture description here

  • dl4j-tutorials There are several subprojects under the folder , The actual combat code is in ndarray-experience Under the table of contents , The red box is shown below :

 Insert picture description here

Create a project

  • In the parent project dl4j-tutorials The new name is ndarray-experience The sub project of , Its pom.xml as follows :
<?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”>

dlfj-tutorials

com.bolingcavalry

1.0-SNAPSHOT

4.0.0

ndarray-experience

org.nd4j

${nd4j.backend}

ch.qos.logback

logback-classic

  • The next code is written in ndarray-experience In Engineering

The most basic method

  • Let's start with two basic methods , They are often used in later learning :

  • rand: Rank , dimension , for example 2 That's ok 3 The two-dimensional matrix of a column ,rand Method returns a value equal to 2

  • shape: The size of each dimension of the matrix , Such as 2 That's ok 3 The two-dimensional matrix of a column ,shape Method returns a value equal to [2, 3]

  • Prepare a static method , Can be INDArray Print out the details of the instance , It's used rand and shape Method :

private static void disp(String type, INDArray indArray) {

StringBuilder stringBuilder = new StringBuilder("*****************************************************\n");

stringBuilder.append(type)

.append("\n dimension : ").append(indArray.rank())

.append("\n shape : ").append(Arrays.toString(indArray.shape()))

.append("\n Complete matrix : \n").append(indArray);

System.out.println(stringBuilder);

}

Create a matrix

  1. All zero matrix :zeros

// establish 2 That's ok 3 All zero matrices of columns

INDArray indArray0 = Nd4j.zeros(2, 3);

disp(“ All zero matrix ”, indArray0);

  • Execution results

All zero matrix

dimension : 2

shape : [2, 3]

Complete matrix :

[[ 0, 0, 0],

[ 0, 0, 0]]

  1. whole 1 matrix :ones

// establish 2 That's ok 3 All one matrix of columns

INDArray indArray1 = Nd4j.ones(2, 3);

disp(“ All one matrix ”, indArray1);

  • Execution results

All one matrix

dimension : 2

shape : [2, 3]

Complete matrix :

[[ 1.0000, 1.0000, 1.0000],

[ 1.0000, 1.0000, 1.0000]]

  1. All matrices of specified values :valueArrayOf

// establish 2 That's ok 3 Columns are all matrices of specified values

INDArray indArray2 = Nd4j.valueArrayOf(new int[] {2, 3}, 888);

disp(“ All specified values (888) Matrix ”, indArray2);

  • Execution results

All specified values (888) Matrix

dimension : 2

shape : [2, 3]

Complete matrix :

[[ 888.0000, 888.0000, 888.0000],

[ 888.0000, 888.0000, 888.0000]]

  1. rand: Random matrix (0 To 1 Random number between )

// establish 2 That's ok 3 Random matrix of columns

INDArray indArray2 = Nd4j.rand(2, 3);

disp(“ Random matrix ”, indArray2);

  • Execution results

Random matrix

dimension : 2

shape : [2, 3]

Complete matrix :

[[ 0.7236, 0.5159, 0.1908],

[ 0.9458, 0.4413, 0.4173]]

  1. The matrix of random Gaussian distribution ( The average value is 0, The standard deviation is 1):randn

// establish 2 That's ok 3 The random Gaussian distribution matrix of a column

INDArray indArray3 = Nd4j.randn(2, 3);

disp(“ Random Gaussian distribution matrix ”, indArray3);

  • Execution results

Random Gaussian distribution matrix

dimension : 2

shape : [2, 3]

Complete matrix :

[[ -0.4287, -0.5790, 0.5004],

[ -0.5122, 1.0551, -0.1998]]

  1. A sequence of equal differences :linspace

// Create a sequence of equal differences ,

// from 1 To 6、 The length is 10 Equal difference sequence of

INDArray indArray4 = Nd4j.linspace(1,6, 10);

disp(“ A sequence of equal differences ”, indArray4);

  • Execution results

A sequence of equal differences

dimension : 1

shape : [10]

Complete matrix :

[ 1.0000, 1.5556, 2.1111, 2.6667, 3.2222, 3.7778, 4.3333, 4.8889, 5.4444, 6.0000]

  1. Create a matrix from an array :create(float[] data, int[] shape)

// Create... From an array 2 That's ok 3 Columns of the matrix

INDArray indArray6 = Nd4j.create(new float[] {1, 2, 3, 4, 5, 6}, new int[] {2,3});

disp(“ Create a matrix from an array ”, indArray6);

  • Execution results

Create a matrix from an array

dimension : 2

shape : [2, 3]

Complete matrix :

[[ 1.0000, 2.0000, 3.0000],

[ 4.0000, 5.0000, 6.0000]]

  1. The three dimensional matrix

// The three dimensional matrix

INDArray indArray7 = Nd4j.valueArrayOf(new int[] {2, 2, 3}, 888);

disp(“ The three dimensional matrix ”, indArray7);

The three dimensional matrix

dimension : 3

shape : [2, 2, 3]

Complete matrix :

[[[ 888.0000, 888.0000, 888.0000],

[ 888.0000, 888.0000, 888.0000]],

[[ 888.0000, 888.0000, 888.0000],

[ 888.0000, 888.0000, 888.0000]]]

  1. Create a square two-dimensional matrix , And the element values on the diagonal are 1.0:

// establish 3 That's ok 3 The two-dimensional matrix of a column , The diagonal value is 1.0

INDArray indArray10 = Nd4j.eye(3);

disp(“3*3 matrix , And the diagonal is 1.0”, indArray10);

  • Execution results

3*3 matrix , And the diagonal is 1.0

dimension : 2

shape : [3, 3]

Complete matrix :

[[ 1.0000, 0, 0],

[ 0, 1.0000, 0],

[ 0, 0, 1.0000]]

Read operations

  • Next, try reading related operations , Let's review what we created with arrays 2 That's ok 3 Columns of the matrix , The contents are as follows :

[[ 1.0000, 2.0000, 3.0000],

[ 4.0000, 5.0000, 6.0000]]

  1. Read the specified location :

System.out.println(" Read the value of the first row and first column : " + indArray6.getDouble(1,1));

  • Execution results

Read the value of the first row and first column : 5.0

  1. Designated line :

System.out.println(" Read first line : " + indArray6.getRow(1));

  • Execution results

Read first line : [ 4.0000, 5.0000, 6.0000]

  1. Specified column :

System.out.println(" Read the second column : " + indArray6.getColumn(2));

  • Execution results

Read the second column : [ 3.0000, 6.0000]

  1. Specify multiple columns :

System.out.println(" Read the second 、 The three column : " + indArray6.getColumns(1,2));

  • Execution results

Read the second 、 The three column : [[ 2.0000, 3.0000],

[ 5.0000, 6.0000]]

Write operations

  • Next, try reading related operations , Let's review what we created with arrays 2 That's ok 3 Columns of the matrix , The contents are as follows :

[[ 1.0000, 2.0000, 3.0000],

[ 4.0000, 5.0000, 6.0000]]

  1. Modify the specified location , After looking at the source code, I found that ,put The method content is actually calling putScalar Method :

indArray6.put(1,1, 123);

indArray6.putScalar(0,0, 456);

disp(“a. After modification ”, indArray6);

  • Execution results

a. After modification

dimension : 2

shape : [2, 3]

Complete matrix :

[[ 456.0000, 2.0000, 3.0000],

[ 4.0000, 123.0000, 6.0000]]

  1. Modify entire line :

// Prepare one-dimensional arrays

INDArray row1 = Nd4j.create(new float[] {9,8,7});

// Replace the entire row of a matrix with a one-dimensional array

indArray6.putRow(1, row1);

disp(“b. After modification ”, indArray6);

  • Execution results

b. After modification

dimension : 2

shape : [2, 3]

Complete matrix :

[[ 456.0000, 2.0000, 3.0000],

[ 9.0000, 8.0000, 7.0000]]

Matrix computing

  • Matrix computing , Let's start with four basic operations
  1. Add, subtract, multiply and divide , The input parameter is a scalar , Will do calculations with all the elements in the matrix

// Get the raw data ready ,2 That's ok 3 Column matrices

indArray6 = Nd4j.create(new float[] {1, 2, 3, 4, 5, 6}, new int[] {2,3});

// Add

disp(“ Add ”, indArray6.add(1));

// Subtraction

disp(“ Subtraction ”, indArray6.sub(1));

// Multiplication

disp(“ Multiplication ”, indArray6.mul(2));

// division

disp(“ division ”, indArray6.div(2));

  • Execution results

Add

dimension : 2

shape : [2, 3]

Complete matrix :

[[ 2.0000, 3.0000, 4.0000],

[ 5.0000, 6.0000, 7.0000]]


Subtraction

dimension : 2

shape : [2, 3]

Complete matrix :

[[ 0, 1.0000, 2.0000],

[ 3.0000, 4.0000, 5.0000]]


Multiplication

dimension : 2

shape : [2, 3]

Complete matrix :

[[ 2.0000, 4.0000, 6.0000],

[ 8.0000, 10.0000, 12.0000]]


division

dimension : 2

shape : [2, 3]

Complete matrix :

[[ 0.5000, 1.0000, 1.5000],

[ 2.0000, 2.5000, 3.0000]]

  1. Ahead add Method , After execution, a new NDArray example , Does not affect the original object , But if you call addi, Will change the content of the original object :

INDArray indArray8 = Nd4j.create(new float[] {1, 2, 3, 4, 5, 6}, new int[] {2,3});

disp(“ Before replacement ”, indArray8);

indArray8.addi(1);

disp(“ After replacement ”, indArray8);

  • Execution results

Before replacement

dimension : 2

shape : [2, 3]

Complete matrix :

[[ 1.0000, 2.0000, 3.0000],

[ 4.0000, 5.0000, 6.0000]]


After replacement

dimension : 2

shape : [2, 3]

Complete matrix :

[[ 2.0000, 3.0000, 4.0000],

[ 5.0000,

《 A big factory Java Analysis of interview questions + Back end development learning notes + The latest architecture explanation video + Practical project source code handout 》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 Full content open source sharing

6.0000, 7.0000]]

  1. an :Nd4j.toFlattened,2 That's ok 3 The two-dimensional matrix of a column is expanded into one-dimensional

disp(“ an ”, Nd4j.toFlattened(indArray6));

  • Execution results

an

dimension : 1

shape : [6]

Complete matrix :

[ 1.0000, 2.0000, 3.0000, 4.0000, 5.0000, 6.0000]

  1. transformation :reshape, It's equivalent to using the original data , But another one shape Enter the reference

disp(“ transformation ”, indArray6.reshape(3,2));

  • Execution results

transformation

dimension : 2

shape : [3, 2]

Complete matrix :

[[ 1.0000, 2.0000],

[ 3.0000, 4.0000],

[ 5.0000, 6.0000]]

copyright notice
author[Alibaba_ Open Source],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201270233089006.html

Random recommended