[Deep Learning from scratch] 2. There is no such thing as NumPy in Java.

Introduction

[Deep Learning from scratch in Java] 1. Differentiation and partial differentiation for the time being](https://qiita.com/xaatw0/items/edb111d4ca764c1976b0) It is a continuation from. Of course, you can't use NumPy in Java, maybe. A similar Java library is ND4J, which can also use GPU, but I don't use it because it seems to be the main subject. And Java has arrays. So, we will implement the addition and multiplication of the array calculation. Subtraction should be added by minus, and division should be divided by 1 and multiplied by 1 ~~ (cut-out) ~~. Deep Learning, almost irrelevant.

Verification of two-dimensional array

Validate the two-dimensional array of arguments before performing the operation. From the viewpoint, the number of elements is not 0, and the length of the array in the second dimension (?) Is the same.

ArrayUtil.java


public void validate(double[][] x){

	if (x.length ==0 || x[0].length ==0){
		throw new IllegalArgumentException();
	}

	if (Arrays.stream(x).skip(1).anyMatch(p -> x[0].length != p.length)){
		throw new IllegalArgumentException();
	}
}

Addition of two-dimensional array

When adding numbers, add to all the elements of the two-dimensional array

ArrayUtil.java


public double[][] plus(double[][] x, double y){
	validate(x);

	final int resultRow = x.length;
	final int resultCol = x[0].length;

	double[][] result = new double[resultRow][resultCol];
	for (int i = 0; i < resultRow; i++){
		for (int j = 0; j < resultCol; j++){
			result[i][j] = x[i][j] + y;
		}
	}

	return result;
}

When adding a one-dimensional array, the first dimension of the two-dimensional array is added to the same element

ArrayUtil.java


public double[][] plus(double[][] x, double[] y){
	validate(x);

	if (x[0].length != y.length){
		throw new IllegalArgumentException();
	}

	final int resultRow = x.length;
	final int resultCol = x[0].length;

	double[][] result = new double[resultRow][resultCol];
	for (int i = 0; i < resultRow; i++){
		for (int j = 0; j < resultCol; j++){
			result[i][j] = x[i][j] + y[j];
		}
	}

	return result;
}

When adding a one-dimensional array, add to the same element of the two-dimensional array

ArrayUtil.java


public double[][] plus(double[][] x, double[][] y){
	validate(x);
	validate(y);

	if (x.length != y.length || x[0].length != y[0].length){
		throw new IllegalArgumentException();
	}

	final int resultRow = x.length;
	final int resultCol = x[0].length;

	double[][] result = new double[resultRow][resultCol];
	for (int i = 0; i < resultRow; i++){
		for (int j = 0; j < resultCol; j++){
			result[i][j] = x[i][j] + y[i][j];
		}
	}

	return result;
}

Multiplication of a two-dimensional array

When multiplying by a number, multiply by all elements of the two-dimensional array

ArrayUtil.java


public double[][] multi(double[][] x, double y){
	validate(x);

	double[][] result = new double[x.length][x[0].length];
	for (int i = 0; i < result.length; i++){
		for (int j = 0; j < result[i].length; j++){
			result[i][j] = x[i][j] * y;
		}
	}

	return result;
}

When multiplying a two-dimensional array, multiply or add. Refer to P54 "3.3.2 Matrix inner product" or "■ Matrix product AB definition" on page 54 of the book.

ArrayUtil.java


public double[][] multi(double[][] x, double[][] y){

	validate(x);
	validate(y);

	int cntCalc = x[0].length;
	if (cntCalc != y.length){
		throw new IllegalArgumentException();
	}

	final int resultRow = x.length;
	final int resultCol = y[0].length;

	double[][] result = new double[resultRow][resultCol];
	for (int i = 0; i < resultRow; i++){
		for (int j = 0; j < resultCol; j++){
			final int row = i;
			final int col = j;
			result[row][col] = IntStream.range(0, cntCalc).mapToDouble(k -> x[row][k] * y[k][col]).sum();
		}
	}

	return result;
}

in conclusion

I haven't reached deep learning yet.

Recommended Posts

[Deep Learning from scratch] 2. There is no such thing as NumPy in Java.
Study Deep Learning from scratch in Java.
[Deep Learning from scratch] in Java 3. Neural network
Deep Learning Java from scratch 6.4 Regularization
Deep Learning Java from scratch Chapter 1 Introduction
Deep Learning Java from scratch 6.1 Parameter update
Deep Learning Java from scratch Chapter 2 Perceptron
Deep Learning Java from scratch 6.3 Batch Normalization
Deep Learning from scratch Java Chapter 4 Neural network learning
Deep Learning Java from scratch Chapter 3 Neural networks
[Deep Learning from scratch] in Java 1. For the time being, differentiation and partial differentiation
In 2021, there is no topic in Java these days (Poem)
Deep Learning Java from scratch 6.2 Initial values of weights
Deep Learning Java from scratch Chapter 5 Error back propagation method
Is there no type in Ruby?
First steps for deep learning in Java
I tried to implement deep learning in Java
Fastest PC setup for deep learning from scratch
There seems to be no else-if in java
[Java] Something is displayed as "-0.0" in the output
Java11: Run Java code in a single file as is
When there is no output to stdout in docker log
Why is there no unsigned right shift operator in C / C ++?
Object-oriented child !? I tried Deep Learning in Java (trial edition)
Let's touch on Deep Java Library (DJL), a library that can handle Deep Learning in Java released from AWS.