Java Code Examples for org.nd4j.linalg.api.shape.Shape#rank()

The following examples show how to use org.nd4j.linalg.api.shape.Shape#rank() . 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
/**
 * Get the vector along a particular dimension
 *
 * @param index     the index of the vector to get
 * @param dimension the dimension to get the vector from
 * @return the vector along a particular dimension
 */
@Override
public INDArray vectorAlongDimension(int index, int dimension) {
    if (dimension < 0)
        dimension = Shape.rank(javaShapeInformation) + dimension;

    //return the whole thing
    if (dimension == Shape.rank(javaShapeInformation) - 1 && size(dimension) == 1 && rank() > 2
            || rank() > 2 && dimension == 0 && size(dimension) == 1) {
        return this;
    }

    INDArray ret = tensorAlongDimension(index, dimension);
    if (isMatrix() && ret.isVector() && dimension == 1 && !ret.isRowVector())
        return ret.reshape(ArrayUtil.reverseCopy(ret.shape()));
    else if (isMatrix() && ret.isVector() && dimension == 0 && !ret.isColumnVector())
        return ret.reshape(ArrayUtil.reverseCopy(ret.shape()));
    return ret;
}
 
Example 2
Source File: BaseNDArray.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/**
 * Test whether a matrix is scalar.
 */
@Override
public boolean isScalar() {
    if (Shape.rank(javaShapeInformation) == 0) {
        return true;
    } else if (Shape.rank(javaShapeInformation) > 2) {
        return false;
    } else if (Shape.rank(javaShapeInformation) == 1) {
        return shapeOf().getInt(0) == 1;
    } else if (Shape.rank(javaShapeInformation) == 2) {
        return shape()[0] == 1 && shape()[1] == 1 || length() == 1;
    }

    else
        return false;

}
 
Example 3
Source File: BaseSparseNDArray.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isScalar() {
    if (isScalar != null)
        return isScalar;
    if (Shape.rank(shapeInformation) > 2) {
        isScalar = false;
    } else if (Shape.rank(shapeInformation) == 1) {
        isScalar = shapeOf().getInt(0) == 1;
    } else if (Shape.rank(shapeInformation) == 2) {
        isScalar = shapeOf().getInt(0) == 1 && shapeOf().getInt(1) == 1;
    }

    else
        isScalar = false;

    return isScalar;
}
 
Example 4
Source File: BaseNDArray.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public int stride(int dimension) {
    int rank = Shape.rank(javaShapeInformation);
    if (dimension < 0)
        return strideOf().getInt(dimension + rank);
    return strideOf().getInt(dimension);
}
 
Example 5
Source File: BaseNDArray.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
@Deprecated
public long linearIndex(long i) {
    long idx = i;
    for (int j = 0; j < Shape.rank(javaShapeInformation) - 1; j++) {
        if (size((int) i) == 1)
            continue;
        idx += i * stride(j);
    }
    return Shape.offset(javaShapeInformation) + (idx);
}
 
Example 6
Source File: BaseNDArray.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Checks whether the matrix is a vector.
 */
@Override
public boolean isVector() {
    if (Shape.rank(javaShapeInformation) == 1)
        return true;

    return isRowVector() || isColumnVector();
}
 
Example 7
Source File: BaseSparseNDArray.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public int stride(int dimension) {
    int rank = Shape.rank(shapeInformation);
    if (dimension < 0)
        return strideOf().getInt(dimension + rank);
    return strideOf().getInt(dimension);
}
 
Example 8
Source File: JvmShapeInfo.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public JvmShapeInfo(@NonNull long[] javaShapeInformation) {
    this.javaShapeInformation = javaShapeInformation;
    this.shape = Shape.shape(javaShapeInformation);
    this.stride = Shape.stride(javaShapeInformation);
    this.length = Shape.isEmpty(javaShapeInformation) ? 0 : Shape.length(javaShapeInformation);
    this.ews = Shape.elementWiseStride(javaShapeInformation);
    this.extras = Shape.extras(javaShapeInformation);
    this.order = Shape.order(javaShapeInformation);
    this.rank = Shape.rank(javaShapeInformation);
}
 
Example 9
Source File: BaseNDArray.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Override
public int rank() {
    return Shape.rank(javaShapeInformation);
}
 
Example 10
Source File: BaseNDArray.java    From nd4j with Apache License 2.0 4 votes vote down vote up
/**
 * An <b>in-place</b> version of permute. The array  shape information (shape, strides)
 * is modified by this operation (but not the data itself)
 * See: http://www.mathworks.com/help/matlab/ref/permute.html
 *
 * @param rearrange the dimensions to swap to
 * @return the current array
 */
@Override
public INDArray permutei(int... rearrange) {
    boolean alreadyInOrder = true;
    val shapeInfo = shapeInfo();
    int rank = Shape.rank(javaShapeInformation);
    for (int i = 0; i < rank; i++) {
        if (rearrange[i] != i) {
            alreadyInOrder = false;
            break;
        }
    }

    if (alreadyInOrder)
        return this;

    checkArrangeArray(rearrange);
    val newShape = doPermuteSwap(Shape.shapeOf(shapeInfo), rearrange);
    val newStride = doPermuteSwap(Shape.stride(shapeInfo), rearrange);
    char newOrder = Shape.getOrder(newShape, newStride, elementStride());

    //Set the shape information of this array: shape, stride, order.
    //Shape info buffer: [rank, [shape], [stride], offset, elementwiseStride, order]
    /*for( int i=0; i<rank; i++ ){
        shapeInfo.put(1+i,newShape[i]);
        shapeInfo.put(1+i+rank,newStride[i]);
    }
    shapeInfo.put(3+2*rank,newOrder);
    */
    val ews = shapeInfo.get(2 * rank + 2);
    /*
    if (ews < 1 && !attemptedToFindElementWiseStride)
        throw new RuntimeException("EWS is -1");
        */

    val si = Nd4j.getShapeInfoProvider().createShapeInformation(newShape, newStride, 0, ews, newOrder);
    setShapeInformation(si);


    if (shapeInfo.get(2 * rank + 2) > 0) {
        //for the backend to work - no ews for permutei
        //^^ not true anymore? Not sure here. Marking this for raver
        setShapeInformation(Nd4j.getShapeInfoProvider().createShapeInformation(newShape, newStride,
                this.offset(), -1, newOrder));
    }

    //this.shape = null;
    //this.stride = null;


    return this;
}
 
Example 11
Source File: BaseSparseNDArray.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Override
public int rank() {
    return Shape.rank(shapeInformation);
}
 
Example 12
Source File: NearestNeighborsServer.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public void start() throws Exception {
    instance = this;

    String[] pathArr = instanceArgs.ndarrayPath.split(",");
    //INDArray[] pointsArr = new INDArray[pathArr.length];
    // first of all we reading shapes of saved eariler files
    int rows = 0;
    int cols = 0;
    for (int i = 0; i < pathArr.length; i++) {
        DataBuffer shape = BinarySerde.readShapeFromDisk(new File(pathArr[i]));

        log.info("Loading shape {} of {}; Shape: [{} x {}]", i + 1, pathArr.length, Shape.size(shape, 0),
                Shape.size(shape, 1));

        if (Shape.rank(shape) != 2)
            throw new DL4JInvalidInputException("NearestNeighborsServer assumes 2D chunks");

        rows += Shape.size(shape, 0);

        if (cols == 0)
            cols = Shape.size(shape, 1);
        else if (cols != Shape.size(shape, 1))
            throw new DL4JInvalidInputException(
                    "NearestNeighborsServer requires equal 2D chunks. Got columns mismatch.");
    }

    final List<String> labels = new ArrayList<>();
    if (instanceArgs.labelsPath != null) {
        String[] labelsPathArr = instanceArgs.labelsPath.split(",");
        for (int i = 0; i < labelsPathArr.length; i++) {
            labels.addAll(FileUtils.readLines(new File(labelsPathArr[i]), "utf-8"));
        }
    }
    if (!labels.isEmpty() && labels.size() != rows)
        throw new DL4JInvalidInputException(String.format("Number of labels must match number of rows in points matrix (expected %d, found %d)", rows, labels.size()));

    final INDArray points = Nd4j.createUninitialized(rows, cols);

    int lastPosition = 0;
    for (int i = 0; i < pathArr.length; i++) {
        log.info("Loading chunk {} of {}", i + 1, pathArr.length);
        INDArray pointsArr = BinarySerde.readFromDisk(new File(pathArr[i]));

        points.get(NDArrayIndex.interval(lastPosition, lastPosition + pointsArr.rows())).assign(pointsArr);
        lastPosition += pointsArr.rows();

        // let's ensure we don't bring too much stuff in next loop
        System.gc();
    }

    VPTree tree = new VPTree(points, instanceArgs.similarityFunction, instanceArgs.invert);

    //Set play secret key, if required
    //http://www.playframework.com/documentation/latest/ApplicationSecret
    String crypto = System.getProperty("play.crypto.secret");
    if (crypto == null || "changeme".equals(crypto) || "".equals(crypto)) {
        byte[] newCrypto = new byte[1024];

        new Random().nextBytes(newCrypto);

        String base64 = Base64.getEncoder().encodeToString(newCrypto);
        System.setProperty("play.crypto.secret", base64);
    }

    Router r = Router.router(vertx);
    r.route().handler(BodyHandler.create());  //NOTE: Setting this is required to receive request body content at all
    createRoutes(r, labels, tree, points);

    vertx.createHttpServer()
            .requestHandler(r)
            .listen(instanceArgs.port);
}