org.nd4j.linalg.exception.ND4JIllegalArgumentException Java Examples

The following examples show how to use org.nd4j.linalg.exception.ND4JIllegalArgumentException. 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: SDIndex.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
public static SDIndex interval(Integer begin, Integer strides, Integer end){
    if(strides == 0){
        throw new ND4JIllegalArgumentException("Invalid index : strides can not be 0.");
    }
    SDIndex sdIndex = new SDIndex();
    sdIndex.indexType = IndexType.INTERVAL;
    if(begin != null) {
        sdIndex.intervalBegin = begin.longValue();
    }
    if(end != null){
        sdIndex.intervalEnd = end.longValue();
    }
    if(strides != null){
        sdIndex.intervalStrides = strides.longValue();
    }
    return sdIndex;
}
 
Example #2
Source File: RPUtils.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 * Query all trees using the given input and data
 * @param toQuery the query vector
 * @param X the input data to query
 * @param trees the trees to query
 * @param n the number of results to search for
 * @param similarityFunction the similarity function to use
 * @return the indices (in order) in the ndarray
 */
public static List<Pair<Double,Integer>> queryAllWithDistances(INDArray toQuery,INDArray X,List<RPTree> trees,int n,String similarityFunction) {
    if(trees.isEmpty()) {
        throw new ND4JIllegalArgumentException("Trees is empty!");
    }

    List<Integer> candidates = getCandidates(toQuery, trees,similarityFunction);
    val sortedCandidates = sortCandidates(toQuery,X,candidates,similarityFunction);
    int numReturns = Math.min(n,sortedCandidates.size());
    List<Pair<Double,Integer>> ret = new ArrayList<>(numReturns);
    for(int i = 0; i < numReturns; i++) {
        ret.add(sortedCandidates.get(i));
    }

    return ret;
}
 
Example #3
Source File: RPUtils.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 * Query all trees using the given input and data
 * @param toQuery the query vector
 * @param X the input data to query
 * @param trees the trees to query
 * @param n the number of results to search for
 * @param similarityFunction the similarity function to use
 * @return the indices (in order) in the ndarray
 */
public static INDArray queryAll(INDArray toQuery,INDArray X,List<RPTree> trees,int n,String similarityFunction) {
    if(trees.isEmpty()) {
        throw new ND4JIllegalArgumentException("Trees is empty!");
    }

    List<Integer> candidates = getCandidates(toQuery, trees,similarityFunction);
    val sortedCandidates = sortCandidates(toQuery,X,candidates,similarityFunction);
    int numReturns = Math.min(n,sortedCandidates.size());

    INDArray result = Nd4j.create(numReturns);
    for(int i = 0; i < numReturns; i++) {
        result.putScalar(i,sortedCandidates.get(i).getSecond());
    }


    return result;
}
 
Example #4
Source File: ArrowConverter.java    From DataVec with Apache License 2.0 5 votes vote down vote up
/**
 * Create an ndarray from a matrix.
 * The included batch must be all the same number of rows in order
 * to work. The reason for this is {@link INDArray} must be all the same dimensions.
 * Note that the input columns must also be numerical. If they aren't numerical already,
 * consider using an {@link org.datavec.api.transform.TransformProcess} to transform the data
 * output from {@link org.datavec.arrow.recordreader.ArrowRecordReader} in to the proper format
 * for usage with this method for direct conversion.
 *
 * @param arrowWritableRecordBatch the incoming batch. This is typically output from
 *                                 an {@link org.datavec.arrow.recordreader.ArrowRecordReader}
 * @return an {@link INDArray} representative of the input data
 */
public static INDArray toArray(ArrowWritableRecordBatch arrowWritableRecordBatch) {
    List<FieldVector> columnVectors = arrowWritableRecordBatch.getList();
    Schema schema = arrowWritableRecordBatch.getSchema();
    for(int i = 0; i < schema.numColumns(); i++) {
        switch(schema.getType(i)) {
            case Integer:
                break;
            case Float:
                break;
            case Double:
                break;
            case Long:
                break;
            default:
                throw new ND4JIllegalArgumentException("Illegal data type found for column " + schema.getName(i));
        }
    }

    int rows  = arrowWritableRecordBatch.getList().get(0).getValueCount();
    int cols = schema.numColumns();
    INDArray arr  = Nd4j.create(rows,cols);
    for(int i = 0; i < cols; i++) {
        INDArray put = ArrowConverter.convertArrowVector(columnVectors.get(i),schema.getType(i));
        switch(arr.data().dataType()) {
            case FLOAT:
                arr.putColumn(i,Nd4j.create(put.data().asFloat()).reshape(rows,1));
                break;
            case DOUBLE:
                arr.putColumn(i,Nd4j.create(put.data().asDouble()).reshape(rows,1));
                break;
        }

    }

    return arr;
}
 
Example #5
Source File: TwoPointApproximation.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param f
 * @param x
 * @param relStep
 * @param f0
 * @param bounds
 * @return
 */
public static INDArray approximateDerivative(Function<INDArray,INDArray> f,
                                             INDArray x,
                                             INDArray relStep,INDArray f0,
                                             INDArray bounds)  {
    if(x.rank() > 2) {
        throw new ND4JIllegalArgumentException("Argument must be a vector or scalar");
    }

    INDArray h = computeAbsoluteStep(relStep,x);
    INDArray[] upperAndLower = prepareBounds(bounds, x);
    INDArray[] boundaries = adjustSchemeToBounds(x,h,1,upperAndLower[0],upperAndLower[1]);
    return denseDifference(f,x,f0,h,boundaries[1]);

}
 
Example #6
Source File: Choose.java    From nd4j with Apache License 2.0 5 votes vote down vote up
public Choose(SameDiff sameDiff, SDVariable[] args, Condition condition) {
    super(null, sameDiff, args);
    if(condition == null) {
        throw new ND4JIllegalArgumentException("Must specify a condition.");
    }

    this.inPlace = true;
    this.inplaceCall = true;
    addIArgument(condition.condtionNum());
    this.condition = condition;
}
 
Example #7
Source File: Choose.java    From nd4j with Apache License 2.0 5 votes vote down vote up
public Choose(String opName, INDArray[] inputs, Condition condition) {
    super(opName, inputs, null);
    if(condition == null) {
        throw new ND4JIllegalArgumentException("Must specify a condition.");
    }

    addInputArgument(inputs);
    addIArgument(condition.condtionNum());
    addOutputArgument(Nd4j.create(inputs[0].length()),Nd4j.scalar(1.0));
}
 
Example #8
Source File: Choose.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Note that iArgs (integer arguments) and  tArgs(double/float arguments)
 * may end up being used under the following conditions:
 * scalar operations (if a scalar is specified the you do not need to specify an ndarray)
 * otherwise, if an ndarray is needed as a second input then put it in the inputs
 *
 * Usually, you only need 1 input (the equivalent of the array you're trying to do indexing on)
 *
 * @param inputs the inputs in to the op
 * @param iArgs the integer arguments as needed
 * @param tArgs the arguments
 * @param condition the condition to filter on
 */
public Choose(INDArray[] inputs,List<Integer> iArgs, List<Double> tArgs,Condition condition) {
    super(null, inputs, null);
    if(condition == null) {
        throw new ND4JIllegalArgumentException("Must specify a condition.");
    }

    if(!iArgs.isEmpty())
        addIArgument(Ints.toArray(iArgs));

    if(!tArgs.isEmpty())
        addTArgument(Doubles.toArray(tArgs));
    addIArgument(condition.condtionNum());
    addOutputArgument(Nd4j.create(inputs[0].shape(), inputs[0].ordering()),Nd4j.trueScalar(1.0));
}
 
Example #9
Source File: ExpandDims.java    From nd4j with Apache License 2.0 5 votes vote down vote up
public ExpandDims(SameDiff sameDiff, SDVariable[] args, int axis) {
    super(null, sameDiff, args);
    if (axis == Integer.MAX_VALUE) {
        throw new ND4JIllegalArgumentException("Cannot perform ExpandDims with axis == Integer.MAX_VALUE");
    }
    this.axis = axis;
    addIArgument(this.axis);
}
 
Example #10
Source File: SDIndex.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public static SDIndex interval(Long begin, Long strides, Long end){
    if(strides == 0){
        throw new ND4JIllegalArgumentException("Invalid index : strides can not be 0.");
    }
    SDIndex sdIndex = new SDIndex();
    sdIndex.indexType = IndexType.INTERVAL;
    sdIndex.intervalBegin = begin;
    sdIndex.intervalEnd = end;
    sdIndex.intervalStrides = strides;
    return sdIndex;
}
 
Example #11
Source File: Choose.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public Choose(SameDiff sameDiff, SDVariable[] args, Condition condition) {
    super(null, sameDiff, args);
    if(condition == null) {
        throw new ND4JIllegalArgumentException("Must specify a condition.");
    }

    this.inPlace = true;
    this.inplaceCall = true;
    addIArgument(condition.condtionNum());
    this.condition = condition;
}
 
Example #12
Source File: Choose.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public Choose(String opName, INDArray[] inputs, Condition condition) {
    super(opName, inputs, null);
    if(condition == null) {
        throw new ND4JIllegalArgumentException("Must specify a condition.");
    }

    addInputArgument(inputs);
    addIArgument(condition.condtionNum());
}
 
Example #13
Source File: Choose.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Note that iArgs (integer arguments) and  tArgs(double/float arguments)
 * may end up being used under the following conditions:
 * scalar operations (if a scalar is specified the you do not need to specify an ndarray)
 * otherwise, if an ndarray is needed as a second input then put it in the inputs
 *
 * Usually, you only need 1 input (the equivalent of the array you're trying to do indexing on)
 *
 * @param inputs the inputs in to the op
 * @param iArgs the integer arguments as needed
 * @param tArgs the arguments
 * @param condition the condition to filter on
 */
public Choose(INDArray[] inputs,List<Integer> iArgs, List<Double> tArgs,Condition condition) {
    super(null, inputs, null);
    if(condition == null) {
        throw new ND4JIllegalArgumentException("Must specify a condition.");
    }

    if(!iArgs.isEmpty())
        addIArgument(Ints.toArray(iArgs));

    if(!tArgs.isEmpty())
        addTArgument(Doubles.toArray(tArgs));
    addIArgument(condition.condtionNum());
}
 
Example #14
Source File: ExpandDims.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public ExpandDims(SameDiff sameDiff, SDVariable[] args, int axis) {
    super(null, sameDiff, args);
    if (axis == Integer.MAX_VALUE) {
        throw new ND4JIllegalArgumentException("Cannot perform ExpandDims with axis == Integer.MAX_VALUE");
    }
    this.jaxis = axis;
    addIArgument(this.jaxis);
}
 
Example #15
Source File: NativeOpExecutioner.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
/**
 * This method takes arbitrary
 * sized list of {@link Aggregate},
 * and packs them into batches
 * Note here that this is mainly used for random number generation
 * for {@link RandomOp} and things like {@link org.nd4j.linalg.api.rng.distribution.Distribution}
 * @param op the list of {@link Aggregate} to
 *              execute upon
 */
@Override
public void exec(Aggregate op) {
    // long st = profilingHookIn(op);

    if (memoryBlocks.get() == null)
        memoryBlocks.set(new HashMap<Integer, AggregateMemoryBlock>());

    if (memoryBlocks.get().get(op.opNum()) == null)
        memoryBlocks.get().put(op.opNum(), new AggregateMemoryBlock(op));

    AggregateMemoryBlock block = memoryBlocks.get().get(op.opNum());

    int numArguments = op.getArguments().size();
    int numIndexArguments = op.getIndexingArguments().size();
    int numRealArguments = op.getRealArguments().size();
    int numShapes = op.getShapes().size();
    int numIntArrays = op.getIntArrayArguments().size();

    PointerPointer arguments = block.getArgumentsPointer(); //new PointerPointer(numArguments);
    List<IntPointer> pointers = new ArrayList<>();
    PointerPointer intArrays = block.getArraysPointer(); //new PointerPointer(numIntArrays);
    val dataType = op.getArguments().get(0).dataType();

    for (int x = 0; x < numArguments; x++) {
        arguments.put(x, op.getArguments().get(x) == null ? null
                : op.getArguments().get(x).data().addressPointer());
    }

    PointerPointer shapes = block.getShapesPointer(); //new PointerPointer(numShapes);

    for (int x = 0; x < numShapes; x++) {
        if (op.getShapes().get(x).dataType() != DataType.LONG)
            throw new RuntimeException("ShapeBuffers should have LONG data opType");

        shapes.put(x, op.getShapes().get(x) == null ? null : op.getShapes().get(x).addressPointer());
    }

    //int[] indexes = new int[numIndexArguments];
    IntPointer pointer = block.getIndexingPointer();
    for (int x = 0; x < numIndexArguments; x++) {
        pointer.put(x, op.getIndexingArguments().get(x));
    }

    //IntPointer pointer = new IntPointer(indexes);

    double[] reals = new double[numRealArguments];
    for (int x = 0; x < numRealArguments; x++) {
        //reals[x] = op.getRealArguments().get(x).doubleValue();
        switch (dataType) {
            case FLOAT:
                ((FloatPointer) block.getRealArgumentsPointer()).put(x, op.getRealArguments().get(x).floatValue());
                break;
            case DOUBLE:
                ((DoublePointer) block.getRealArgumentsPointer()).put(x, op.getRealArguments().get(x).doubleValue());
                break;
            default:
                throw new ND4JIllegalArgumentException("Only FLOAT and DOUBLE datatypes are supported");
        }
    }

    for (int x = 0; x < numIntArrays; x++) {
        IntPointer intPtr = block.getIntArrays().get(x); //new IntPointer(op.getIntArrayArguments().get(x));
        intPtr.put(op.getIntArrayArguments().get(x), 0, op.getIntArrayArguments().get(x).length);
        intArrays.put(x, intPtr);
        pointers.add(intPtr);
    }

    //INDArray realsBuffer = Nd4j.create(reals);



    loop.execAggregate(null, op.opNum(), arguments, numArguments, shapes, numShapes, pointer,
                numIndexArguments, intArrays, numIntArrays, block.getRealArgumentsPointer(),
                numRealArguments, FlatBuffersMapper.getDataTypeAsByte(dataType));

    if (loop.lastErrorCode() != 0)
        throw new RuntimeException(loop.lastErrorMessage());
}