简体   繁体   中英

Sigmoid function of a 2D array

Is there a way to find the sigmoid of a 2D array without using an external library like JAMA ? I have tried the following code, but in failure.

public static double[][] sigmoid(double[][] x, boolean deriv){
    for (int i = 0; i <x.length ; i++)
    {
        for (int j = 0; j < x[1].length; j++){
            if(deriv == false){
                return sigmoid(x[i][j], false) * (1 - sigmoid(x[i][j], false));
            }
            return (1/(1 + Math.pow(Math.E, (-1 * x[i][j]))));
        }
    }
}

It says, cannot convert double to double[][]. Any method to solve this would be appreciated. thank you!

This is the function of an element-wise sigmoid operation on your array x:

public static double sigmoid(double t) {
    return 1 / (1 + Math.pow(Math.E, (-1 * t)));
}

public static double[][] sigmoid(double[][] x, boolean deriv) {
    double[][] = result = new double[x.length][x[0].length];

    for (int i = 0; i < x.length; i++) {
        for (int j = 0; j < x[i].length; j++) {
            double sigmoidCell = sigmoid(x[i][j]);

            if (deriv == true) {
                result[i][j] = sigmoidCell * (1 - sigmoidCell);
            } else {
                result[i][j] = sigmoidCell;
            }
        }
    }

    return result;
}

In your method, there are some syntax errors, as well as a recursive statement which will never end because deriv is always false. Also the recursive statement calculates a double, not return any 2d array.

If you're doing more than this, I suggest you create methods for subtraction, dot-multiplication and creating ones matrices.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM