src/main/java/convnet/CNN.java
/**
     * Logic for a valid convolution operation.
     *
     * @param input
     * @param kernel
     * @param bias
     * @return
     */
Logic for a valid convolution operation.
Logic for a valid convolution operation .
private Data evalValidConvolution(Data input, Data kernel, double bias) {
        int stride = 1;
        int m = input.getDimensions()[0];
        int n = input.getDimensions()[1];
        int o = input.getDimensions()[2];
        int km = kernel.getDimensions()[0];
        int kn = kernel.getDimensions()[1];
        int ko = kernel.getDimensions()[2];
        int kms = ((m - km) / stride) + 1;
        int kns = ((n - kn) / stride) + 1;
        if (o != ko) {
            throw new RuntimeException("Malformed kernel and input. Different depths.");
        }
        double[][] result = new double[kms][kns];
        for (int i = 0; i < kms; i++) {
            for (int j = 0; j < kns; j++) {
                int[] sliceH = new int[]{i, i + km};
                int[] sliceW = new int[]{j, j + kn};
                int[] sliceD = new int[]{0, ko};
                double[][][] inputSlice = input.slice3d(sliceH, sliceW, sliceD);
                result[i][j] = NumPute.dot(inputSlice, kernel.read3d()) + bias;
            }
        }
        Data activationMap = new Data();
        activationMap.write(result);
        return activationMap;
    }
private Data evalValidConvolution ( Data input , Data kernel , double bias ) { int stride = 1 ; int m = input . getDimensions ( ) [ 0 ] ; int n = input . getDimensions ( ) [ 1 ] ; int o = input . getDimensions ( ) [ 2 ] ; int km = kernel . getDimensions ( ) [ 0 ] ; int kn = kernel . getDimensions ( ) [ 1 ] ; int ko = kernel . getDimensions ( ) [ 2 ] ; int kms = ( ( m - km ) / stride ) + 1 ; int kns = ( ( n - kn ) / stride ) + 1 ; if ( o != ko ) { throw new RuntimeException ( "Malformed kernel and input. Different depths." ) ; } double [ ] [ ] result = new double [ kms ] [ kns ] ; for ( int i = 0 ; i < kms ; i ++ ) { for ( int j = 0 ; j < kns ; j ++ ) { int [ ] sliceH = new int [ ] { i , i + km } ; int [ ] sliceW = new int [ ] { j , j + kn } ; int [ ] sliceD = new int [ ] { 0 , ko } ; double [ ] [ ] [ ] inputSlice = input . slice3d ( sliceH , sliceW , sliceD ) ; result [ i ] [ j ] = NumPute . dot ( inputSlice , kernel . read3d ( ) ) + bias ; } } Data activationMap = new Data ( ) ; activationMap . write ( result ) ; return activationMap ; }
Logic for a valid convolution operation.
Logic for a valid convolution operation .