Java Code Examples for org.nd4j.linalg.factory.Nd4j#clearNans()

The following examples show how to use org.nd4j.linalg.factory.Nd4j#clearNans() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseNDArray.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/**
 * in place (element wise) division of two matrices
 *
 * @param other  the second ndarray to divide
 * @param result the result ndarray
 * @return the result of the divide
 */
@Override
public INDArray divi(INDArray other, INDArray result) {
    if (other.isScalar()) {
        return divi(other.getDouble(0), result);
    }

    if (isScalar()) {
        return other.rdivi(getDouble(0), result);
    }


    if(!Shape.shapeEquals(this.shape(),other.shape())) {
        int[] broadcastDimensions = Shape.getBroadcastDimensions(this.shape(),other.shape());
        Nd4j.getExecutioner().exec(new BroadcastDivOp(this,other,result,broadcastDimensions),broadcastDimensions);
        return result;
    }


    LinAlgExceptions.assertSameShape(other, result);
    Nd4j.getExecutioner().exec(new OldDivOp(this, other, result, length()));

    if (Nd4j.ENFORCE_NUMERICAL_STABILITY)
        Nd4j.clearNans(result);
    return result;
}
 
Example 2
Source File: DeepGL.java    From ml-models with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray ndOp(INDArray features, INDArray adjacencyMatrix) {
    INDArray mean = adjacencyMatrix
            .mmul(features)
            .diviColumnVector(adjacencyMatrix.sum(1));
    // clear NaNs from div by 0 - these entries should have a 0 instead.
    Nd4j.clearNans(mean);
    return mean;
}
 
Example 3
Source File: BaseNDArray.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray rdivi(Number n, INDArray result) {

    if (Double.isNaN(n.doubleValue()))
        n = Nd4j.EPS_THRESHOLD;
    Nd4j.getExecutioner().exec(new ScalarReverseDivision(this, null, result, result.length(), n));
    if (Nd4j.ENFORCE_NUMERICAL_STABILITY)
        Nd4j.clearNans(result);
    return result;
}
 
Example 4
Source File: BaseNDArray.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray rsubi(Number n, INDArray result) {

    if (Double.isNaN(n.doubleValue()))
        n = Nd4j.EPS_THRESHOLD;

    Nd4j.getExecutioner().exec(new ScalarReverseSubtraction(this, null, result, result.lengthLong(), n));

    if (Nd4j.ENFORCE_NUMERICAL_STABILITY)
        Nd4j.clearNans(result);
    return result;
}
 
Example 5
Source File: BaseNDArray.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray muli(Number n, INDArray result) {
    if (Double.isNaN(n.doubleValue()))
        n = Nd4j.EPS_THRESHOLD;
    Nd4j.getExecutioner().exec(new ScalarMultiplication(this, null, result, result.lengthLong(), n));

    if (Nd4j.ENFORCE_NUMERICAL_STABILITY)
        Nd4j.clearNans(result);

    return result;
}
 
Example 6
Source File: BaseNDArray.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray subi(Number n, INDArray result) {

    if (Double.isNaN(n.doubleValue()))
        n = Nd4j.EPS_THRESHOLD;

    Nd4j.getExecutioner().exec(new ScalarSubtraction(this, null, result, result.lengthLong(), n));
    if (Nd4j.ENFORCE_NUMERICAL_STABILITY)
        Nd4j.clearNans(result);

    return result;
}
 
Example 7
Source File: BaseNDArray.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * in place (element wise) multiplication of two matrices
 *
 * @param other  the second ndarray to multiply
 * @param result the result ndarray
 * @return the result of the multiplication
 */
@Override
public INDArray muli(INDArray other, INDArray result) {
    if (other.isScalar()) {
        return muli(other.getDouble(0), result);
    }
    if (isScalar()) {
        return other.muli(getDouble(0), result);
    }



    if(!Shape.shapeEquals(this.shape(),other.shape())) {
        int[] broadcastDimensions = Shape.getBroadcastDimensions(this.shape(),other.shape());
        Nd4j.getExecutioner().exec(new BroadcastMulOp(this,other,result,broadcastDimensions),broadcastDimensions);
        return result;
    }

    LinAlgExceptions.assertSameShape(other, result);

    Nd4j.getExecutioner().exec(new OldMulOp(this, other, result, length()));

    if (Nd4j.ENFORCE_NUMERICAL_STABILITY)
        Nd4j.clearNans(result);

    return result;
}
 
Example 8
Source File: BaseNDArray.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * in place subtraction of two matrices
 *
 * @param other  the second ndarray to subtract
 * @param result the result ndarray
 * @return the result of the subtraction
 */
@Override
public INDArray subi(INDArray other, INDArray result) {
    if (other.isScalar()) {
        return subi(other.getDouble(0), result);
    }
    if (isScalar()) {
        return other.rsubi(getDouble(0), result);
    }


    if(!Shape.shapeEquals(this.shape(),other.shape())) {
        int[] broadcastDimensions = Shape.getBroadcastDimensions(this.shape(),other.shape());
        Nd4j.getExecutioner().exec(new BroadcastSubOp(this,other,result,broadcastDimensions),broadcastDimensions);
        return result;
    }


    LinAlgExceptions.assertSameShape(other, result);


    Nd4j.getExecutioner().exec(new OldSubOp(this, other,result));

    if (Nd4j.ENFORCE_NUMERICAL_STABILITY)
        Nd4j.clearNans(result);

    return result;
}
 
Example 9
Source File: BaseNDArray.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * in place addition of two matrices
 *
 * @param other  the second ndarray to add
 * @param result the result ndarray
 * @return the result of the addition
 */
@Override
public INDArray addi(INDArray other, INDArray result) {
    if (other.isScalar()) {
        return result.addi(other.getDouble(0), result);
    }

    if (isScalar()) {
        return other.addi(getDouble(0), result);
    }

    if(!Shape.shapeEquals(this.shape(),other.shape())) {
        int[] broadcastDimensions = Shape.getBroadcastDimensions(this.shape(),other.shape());
        result = Nd4j.createUninitialized(Shape.broadcastOutputShape(this.shape(),other.shape()));
        Nd4j.getExecutioner().exec(new BroadcastAddOp(this,other,result,broadcastDimensions),broadcastDimensions);
        return result;
    }

    LinAlgExceptions.assertSameShape(other, result);

    Nd4j.getExecutioner().exec(new OldAddOp(this, other, result, length()));


    if (Nd4j.ENFORCE_NUMERICAL_STABILITY)
        Nd4j.clearNans(result);

    return result;
}
 
Example 10
Source File: BaseNDArray.java    From nd4j with Apache License 2.0 4 votes vote down vote up
/**
 * Perform an copy matrix multiplication
 *
 * @param other  the other matrix to perform matrix multiply with
 * @param result the result ndarray
 * @return the result of the matrix multiplication
 */
@Override
public INDArray mmuli(INDArray other, INDArray result) {
    LinAlgExceptions.assertMultiplies(this, other);


    if (other.isScalar()) {
        return muli(other.getDouble(0), result);
    }
    if (isScalar()) {
        return other.muli(getDouble(0), result);
    }

    /* check sizes and resize if necessary */


    if (result == this || result == other) {
        /* actually, blas cannot do multiplications in-place. Therefore, we will fake by
         * allocating a temporary object on the side and copy the result later.
         */
        INDArray temp = Nd4j.create(result.shape(), Nd4j.getStrides(result.shape(), 'f'));

        if (other.columns() == 1 || other.rank() == 1) {
            Nd4j.getBlasWrapper().level2().gemv(BlasBufferUtil.getCharForTranspose(result),
                    BlasBufferUtil.getCharForTranspose(this), 1.0, this, other, 0.0, temp);
        }

        else {
            Nd4j.getBlasWrapper().level3().gemm(BlasBufferUtil.getCharForTranspose(result),
                    BlasBufferUtil.getCharForTranspose(this), BlasBufferUtil.getCharForTranspose(temp), 1.0,
                    this, other, 0.0, temp);
            Nd4j.getBlasWrapper().level3().gemm(
                    BlasBufferUtil.getCharForTranspose(result),
                    BlasBufferUtil.getCharForTranspose(this),
                    BlasBufferUtil.getCharForTranspose(temp),
                    1.0,
                    this,
                    other,
                    0.0,
                    temp);
        }

        result.assign(temp);


    } else {

        //We require that the result array is 'f' (fortran) order
        // However, user might have called mmuli with a c order array for the result
        // In which case, we need to allocate a temporary f order array, and later do an assign to the real result array

        boolean requiresTemp = result.ordering() == 'c';
        INDArray gemmResultArr;
        if (requiresTemp) {
            //Can use createUninitialized due to beta==0.0 parameter in gemm
            gemmResultArr = Nd4j.createUninitialized(result.shape(), 'f');
        } else {
            gemmResultArr = result;
        }

        if (other.columns() == 1 || other.rank() == 1) {
            Nd4j.getBlasWrapper().level2().gemv(
                    ordering(),
                    BlasBufferUtil.getCharForTranspose(other),
                    1.0,
                    this,
                    other,
                    0.0,
                    gemmResultArr);
        } else {
            //gemm doesn't support strides so vectors and views
            //don't work
            Nd4j.getBlasWrapper().level3().gemm(ordering(),
                    BlasBufferUtil.getCharForTranspose(other),
                    BlasBufferUtil.getCharForTranspose(gemmResultArr),
                    1.0,
                    this,
                    other,
                    0.0,
                    gemmResultArr);
        }

        if (requiresTemp) {
            result.assign(gemmResultArr);
        }
    }

    // 1D edge case: reshape back to vector
    if (other.rank() == 1)
        result = result.reshape(result.length());

    if (Nd4j.ENFORCE_NUMERICAL_STABILITY)
        Nd4j.clearNans(result);
    return result;
}
 
Example 11
Source File: BaseSparseNDArray.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Override
public INDArray mmuli(INDArray other, INDArray result) {
    LinAlgExceptions.assertMultiplies(this, other);


    if (other.isScalar()) {
        return muli(other.getDouble(0), result);
    }
    if (isScalar()) {
        return other.muli(getDouble(0), result);
    }

    /* check sizes and resize if necessary */



    //We require that the result array is 'f' (fortran) order
    // However, user might have called mmuli with a c order array for the result
    // In which case, we need to allocate a temporary f order array, and later do an assign to the real result array

    boolean requiresTemp = result.ordering() == 'c';
    INDArray gemmResultArr;
    if (requiresTemp) {
        //Can use createUninitialized due to beta==0.0 parameter in gemm
        gemmResultArr = Nd4j.createUninitialized(result.shape(), 'f');
    } else {
        gemmResultArr = result;
    }

    if (other.columns() == 1) {
        Nd4j.getBlasWrapper().level2().gemv(ordering(), BlasBufferUtil.getCharForTranspose(other), 1.0, this, other,
                        0.0, gemmResultArr);
    } else {
        //gemm doesn't support strides so vectors and views
        //don't work
        if (isView() && isVector()) {
            return dup().mmuli(other, gemmResultArr);
        }

        Nd4j.getBlasWrapper().level3().gemm(ordering(), BlasBufferUtil.getCharForTranspose(other),
                        BlasBufferUtil.getCharForTranspose(gemmResultArr), 1.0, this, other, 0.0, gemmResultArr);
    }

    if (requiresTemp) {
        result.assign(gemmResultArr);
    }


    if (Nd4j.ENFORCE_NUMERICAL_STABILITY)
        Nd4j.clearNans(result);

    return result;
}
 
Example 12
Source File: WordVectorSerializer.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
/**
 * This method can be used to load previously saved model from InputStream (like a HDFS-stream)
 * <p>
 * Deprecation note: Please, consider using readWord2VecModel() or loadStaticModel() method instead
 *
 * @param stream        InputStream that contains previously serialized model
 * @param skipFirstLine Set this TRUE if first line contains csv header, FALSE otherwise
 * @return
 * @throws IOException
 * @deprecated Use readWord2VecModel() or loadStaticModel() method instead
 */
@Deprecated
public static WordVectors loadTxtVectors(@NonNull InputStream stream, boolean skipFirstLine) throws IOException {
    AbstractCache<VocabWord> cache = new AbstractCache.Builder<VocabWord>().build();

    BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
    String line = "";
    List<INDArray> arrays = new ArrayList<>();

    if (skipFirstLine)
        reader.readLine();

    while ((line = reader.readLine()) != null) {
        String[] split = line.split(" ");
        String word = split[0].replaceAll(WHITESPACE_REPLACEMENT, " ");
        VocabWord word1 = new VocabWord(1.0, word);

        word1.setIndex(cache.numWords());

        cache.addToken(word1);

        cache.addWordToIndex(word1.getIndex(), word);

        cache.putVocabWord(word);

        float[] vector = new float[split.length - 1];

        for (int i = 1; i < split.length; i++) {
            vector[i - 1] = Float.parseFloat(split[i]);
        }

        INDArray row = Nd4j.create(vector);

        arrays.add(row);
    }

    InMemoryLookupTable<VocabWord> lookupTable =
            (InMemoryLookupTable<VocabWord>) new InMemoryLookupTable.Builder<VocabWord>()
                    .vectorLength(arrays.get(0).columns()).cache(cache).build();

    INDArray syn = Nd4j.vstack(arrays);

    Nd4j.clearNans(syn);
    lookupTable.setSyn0(syn);

    return fromPair(Pair.makePair((InMemoryLookupTable) lookupTable, (VocabCache) cache));
}
 
Example 13
Source File: BaseNDArray.java    From nd4j with Apache License 2.0 3 votes vote down vote up
@Override
public INDArray divi(Number n, INDArray result) {

    if (Double.isNaN(n.doubleValue()))
        n = Nd4j.EPS_THRESHOLD;
    Nd4j.getExecutioner().exec(new ScalarDivision(this, null, result, result.lengthLong(), n));


    if (Nd4j.ENFORCE_NUMERICAL_STABILITY)
        Nd4j.clearNans(result);

    return result;
}