[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.
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();
}
}
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;
}
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;
}
I haven't reached deep learning yet.
Recommended Posts