deeplearing4j学习之矩阵学习

本文通过示例介绍了如何使用ND4j库创建不同类型的矩阵,包括全零矩阵、全一矩阵、随机矩阵等,并演示了如何通过堆叠操作组合矩阵。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近要学习deeplearning4j,先看了下矩阵情况学习,先看下创建矩阵的情况:

package com.meituan.nd4jexcise;
import java.util.Arrays;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.factory.Nd4j;
public class Nd4jEx2_CreatingINDArrays {
	public static void main(String[] args) {
		 int nRows = 3;
	       int nColumns = 5;
	       INDArray  allZeros=Nd4j.zeros(nRows, nColumns);
	       System.out.println("ND4j.zeros(nRows,nColumns)");
	       System.out.println(allZeros);
	       
	       
	       
	       INDArray allOnes=Nd4j.ones(nRows,nColumns);
	        System.out.println("\nNd4j.ones(nRows, nColumns)");
	        System.out.println(allOnes);
	       INDArray  allTens=Nd4j.valueArrayOf(nRows, nColumns,10.0);//产生全部是10的矩阵
	       System.out.println("\nNd4j.valueArrayOf(nRows, nColumns, 10.0)");
	        System.out.println(allTens);
	      
	        
	        double[] vectorDouble=new double[]{1,2,3};
	        INDArray  rowVecArray=Nd4j.create(vectorDouble);
	        System.out.println("rowVector :"+rowVecArray);
	        System.out.println("rowVector.shape():      " + Arrays.toString(rowVecArray.shape()));
	        
	        INDArray columnVector=Nd4j.create(vectorDouble,new int[]{1,3});
	        System.out.println("columnVector:           " + columnVector);      //Note for printing: row/column vectors are printed as one line
	        System.out.println("columnVector.shape():   " + Arrays.toString(columnVector.shape()));    //3 row, 1 columns
	        
	        double[][] matrixDouble = new double[][]{
	                {1.0, 2.0, 3.0},
	                {4.0, 5.0, 6.0}};
	            INDArray matrix = Nd4j.create(matrixDouble);
	            System.out.println("\nINDArray defined from double[][]:");
	            System.out.println(matrix);
	            
	            int[] shape = new int[]{nRows, nColumns};
	            INDArray uniformRandom=Nd4j.rand(shape);
	            long rngSeed = 12345;
	            INDArray uniformRandom2 = Nd4j.rand(shape, rngSeed);
	            INDArray uniformRandom3 = Nd4j.rand(shape, rngSeed);
	            System.out.println("\nUniform random arrays with same fixed seed:");
	            System.out.println(uniformRandom2);
	            System.out.println();
	            System.out.println(uniformRandom3);
	            //Of course, we aren't restricted to 2d. 3d or higher is easy:
	            INDArray threeDimArray = Nd4j.ones(3,4,5);      //3x4x5 INDArray
	            INDArray fourDimArray = Nd4j.ones(3,4,5,6);     //3x4x5x6 INDArray
	            INDArray fiveDimArray = Nd4j.ones(3,4,5,6,7);   //3x4x5x6x7 INDArray
	            System.out.println("\n\n\nCreating INDArrays with more dimensions:");
	            System.out.println("3d array shape:         " + Arrays.toString(threeDimArray.shape()));
	            System.out.println("4d array shape:         " + Arrays.toString(fourDimArray.shape()));
	            System.out.println("5d array shape:         " + Arrays.toString(fiveDimArray.shape()));
	            //We can create INDArrays by combining other INDArrays, too:
	            INDArray rowVector1 = Nd4j.create(new double[]{1,2,3});
	            INDArray rowVector2 = Nd4j.create(new double[]{4,5,6});
	            INDArray vStack = Nd4j.vstack(rowVector1, rowVector2);      //Vertical stack:   [1,3]+[1,3] to [2,3]
	            INDArray hStack = Nd4j.hstack(rowVector1, rowVector2);      //Horizontal stack: [1,3]+[1,3] to [1,6]
	            System.out.println("\n\n\nCreating INDArrays from other INDArrays, using hstack and vstack:");
	            System.out.println("vStack:\n" + vStack);
	            System.out.println("hStack:\n" + hStack);
	            //There's some other miscellaneous methods, too:
	            INDArray identityMatrix = Nd4j.eye(3);
	            System.out.println("\n\n\nNd4j.eye(3):\n" + identityMatrix);
	            INDArray linspace = Nd4j.linspace(1,10,9);                 //Values 1 to 10, in 10 steps
	            System.out.println("Nd4j.linspace(1,10,10):\n" + linspace);
	            INDArray diagMatrix = Nd4j.diag(rowVector2);                //Create square matrix, with rowVector2 along the diagonal
	            System.out.println("Nd4j.diag(rowVector2):\n" + diagMatrix);
	        
	      
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值