java从一个单独的类中调用数学函数来计算神经网络会导致网络不能正常工作

wnavrhmk  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(223)

我一直在遵循一个关于创建一个简单的三层神经网络的教程,可以在这里找到,我的代码几乎是一对一的时刻。在本教程中,他们的网络计算是在他们的“矩阵”类中完成的。另一节课叫我“数学”。他们的榜样行得通,而我的榜样不行。我想知道为什么会这样,因为在我看来,它应该工作。为了便于查找,我将包括教程的类和我的。
示例代码:

public class Matrix {
double[][] data;
int rows,cols;

public Matrix(int row, int col) {
    data = new double[row][col];
    rows = row;
    cols = col;
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
            data[i][j] = Math.random() * 2 - 1;
        }
    }
}        
public void add(Matrix B){
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            this.data[i][j] += B.data[i][j];
        }
    }
}

public static Matrix sub(Matrix A, Matrix B){
    Matrix temp = new Matrix(A.rows, A.cols);
    for (int i = 0; i < A.rows; i++) {
        for (int j = 0; j < A.cols; j++) {
            temp.data[i][j] = A.data[i][j] - B.data[i][j];
        }
    }
    return temp;
}

public void multiplyElements(Matrix a){
    for (int i = 0; i < a.rows; i++) {
        for (int j = 0; j < a.cols; j++) {
            this.data[i][j] *= a.data[i][j];
        }
    }
}

public void multiplyScalar(double a){
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            this.data[i][j] *= a;
        }
    }
}

public static Matrix DotProduct(Matrix A,Matrix B){
    Matrix temp = new Matrix(A.rows, B.cols);
    for (int i = 0; i < temp.rows; i++) {
        for (int j = 0; j < temp.cols; j++) {
            double sum = 0;
            for (int k = 0; k < A.cols; k++) {
                sum += A.data[i][k] * B.data[k][j];
            }
            temp.data[i][j] = sum;
        }
    }
    return temp;
}

public void Activate(){
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++)
            this.data[i][j] = 1 / (1 + Math.exp(-this.data[i][j]));
    }
}

public static Matrix Transpose(Matrix A){
    Matrix temp = new Matrix(A.cols, A.rows);
    for (int i = 0; i < A.rows; i++) {
        for (int j = 0; j < A.cols; j++) {
            temp.data[j][i] = A.data[i][j];
        }
    }
    return temp;
}

public static Matrix Clone(double[] A){
    Matrix temp = new Matrix(A.length,1);
    for(int i =0;i<A.length;i++)
        temp.data[i][0]=A[i];
    return temp;
}

public Matrix DxActivate(){
    Matrix temp = new Matrix(rows, cols);
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++)
            temp.data[i][j] = this.data[i][j] * (1 - this.data[i][j]);
    }
    return temp;
}

我的代码:

public class Matrix {
double[][] data;
int rows, cols;
static Maths M = new Maths();

public Matrix(int row, int col) {
    data = new double[row][col];
    rows = row;
    cols = col;
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
            data[i][j] = Math.random() * 2 - 1;
        }
    }
}

public void add(Matrix B) {
    this.data = M.AddIndices(this.data, B.data);
}

public static Matrix sub(Matrix A, Matrix B) {
    Matrix temp = new Matrix(A.data.length, A.data[0].length);
    temp.data = M.SubtractIndices(A.data, B.data);
    return temp;
}

public void multiplyElements(Matrix B) {
    this.data = M.MultiplyIndexes(this.data, B.data);
}

public void multiplyScalar(double S) {
    this.data = M.MultiplyByScalar(this.data, S);
}

public static Matrix DotProduct(Matrix A, Matrix B) {
    Matrix temp = new Matrix(A.data.length, B.data[0].length);
    temp.data = M.DotProduct(A.data, B.data);
    return temp;
}

public void Activate() {
    this.data = M.ApplySigmoid(this.data);
}

public Matrix Transpose() {
    Matrix temp = new Matrix(this.data[0].length, this.data.length);
    temp.data = M.Transpose(this.data);
    return temp;
}

public static Matrix Clone(double[] A) {
    Matrix temp = new Matrix(A.length, 1);
    for (int i = 0; i < A.length; i++)
        temp.data[i][0] = A[i];
    return temp;
}

public Matrix DxActivate() {
    Matrix temp = new Matrix(this.data.length, this.data[0].length);
    temp.data = M.Apply_dx_Sigmoid(this.data);
    return temp;
}

}
在每次调用数学类m中的函数时,该函数都将返回一个二维双数组。所调用的每个函数都与示例代码中的函数相同,只是创建了一个2d双数组,而不是创建一个矩阵示例。
我相信问题在于数据是如何传递/存储的,但这并不是特别重要,因为对于我来说,作为一个简单的temp.data=returning数组,应该正确地存储它。
谢谢你的帮助和知识。
编辑:我发现了我的问题,我的点积函数工作不正常。谢谢您

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题