private static INDArray createFromCSC(double[] data, int[] rowIndices, int[] columnPointers, int[] shape){ INDArray result = Nd4j.zeros(shape); int columns = shape[1]; int dataIdx = 0; for(int i = 0; i < columns; i++){ for(int k = dataIdx; k < (i == columnPointers.length-1 ? rowIndices.length : columnPointers[i+1]); k++, dataIdx++){ int j = rowIndices[k]; result.put(j, i, data[k]); //System.out.println("i = "+i+", k = "+k+ ", data[k] = "+data[k]+"\n matrix = "+result.toString()); } } return result; } }
@Override public boolean add(Double aDouble) { if(container == null) { container = Nd4j.create(10); } else if(size == container.length()) { INDArray newContainer = Nd4j.create(container.length() * 2); newContainer.put(new INDArrayIndex[]{NDArrayIndex.interval(0,container.length())},container); container = newContainer; } container.putScalar(size++,aDouble); return true; }
/** * Create the identity ndarray * * @param n the number for the identity * @return */ @Override public INDArray eye(long n) { INDArray ret = Nd4j.create(n, n); for (int i = 0; i < n; i++) { ret.put(i, i, 1.0); } return ret.reshape(n, n); }
/** * Perform an operation along a diagonal * * @param x the ndarray to perform the operation on * @param func the operation to perform */ public static void doAlongDiagonal(INDArray x, Function<Number, Number> func) { if (x.isMatrix()) for (int i = 0; i < x.rows(); i++) x.put(i, i, func.apply(x.getDouble(i, i))); }
private void growCapacity(int idx) { if(container == null) { container = Nd4j.create(10); } else if(idx >= container.length()) { val max = Math.max(container.length() * 2,idx); INDArray newContainer = Nd4j.create(max); newContainer.put(new INDArrayIndex[]{NDArrayIndex.interval(0,container.length())},container); container = newContainer; } }
private void growCapacity(int idx) { if(container == null) { container = Nd4j.create(10); } else if(idx >= container.length()) { val max = Math.max(container.length() * 2,idx); INDArray newContainer = Nd4j.create(max); newContainer.put(new INDArrayIndex[]{NDArrayIndex.interval(0,container.length())},container); container = newContainer; } }
private void moveBackward(int index) { int numMoved = size - index - 1; INDArrayIndex[] first = new INDArrayIndex[] {NDArrayIndex.point(0), NDArrayIndex.interval(index ,index + numMoved)}; INDArrayIndex[] getRange = new INDArrayIndex[] {NDArrayIndex.point(0), NDArrayIndex.interval(index + 1 ,index + 1 + numMoved)}; INDArray get = container.get(getRange); container.put(first,get); }
private void moveBackward(int index) { int numMoved = size - index - 1; INDArrayIndex[] first = new INDArrayIndex[] {NDArrayIndex.point(0), NDArrayIndex.interval(index ,index + numMoved)}; INDArrayIndex[] getRange = new INDArrayIndex[] {NDArrayIndex.point(0), NDArrayIndex.interval(index + 1 ,index + 1 + numMoved)}; container.put(first,container.get(getRange)); }
public static INDArray mergePerOutputMasks2d(long[] outShape, INDArray[] arrays, INDArray[] masks) { val numExamplesPerArr = new long[arrays.length]; for (int i = 0; i < numExamplesPerArr.length; i++) { numExamplesPerArr[i] = arrays[i].size(0); } INDArray outMask = Nd4j.ones(outShape); //Initialize to 'all present' (1s) int rowsSoFar = 0; for (int i = 0; i < masks.length; i++) { long thisRows = numExamplesPerArr[i]; //Mask itself may be null -> all present, but may include multiple examples if (masks[i] == null) { continue; } outMask.put(new INDArrayIndex[] {NDArrayIndex.interval(rowsSoFar, rowsSoFar + thisRows), NDArrayIndex.all()}, masks[i]); rowsSoFar += thisRows; } return outMask; }
@Override public INDArray toFlattened(INDArray... matrices) { int length = 0; for (INDArray m : matrices) length += m.length(); INDArray ret = Nd4j.create(1, length); int linearIndex = 0; for (INDArray d : matrices) { ret.put(new INDArrayIndex[] {NDArrayIndex.interval(linearIndex, linearIndex + d.length())}, d); linearIndex += d.length(); } return ret; }
/** * Squeezes input data to a max and a min * * @param min the min value to occur in the dataset * @param max the max value to ccur in the dataset */ @Override public void squishToRange(double min, double max) { for (int i = 0; i < getFeatures().length(); i++) { double curr = (double) getFeatures().getScalar(i).element(); if (curr < min) getFeatures().put(i, Nd4j.scalar(min)); else if (curr > max) getFeatures().put(i, Nd4j.scalar(max)); } }
private void moveForward(int index) { int numMoved = size - index - 1; INDArrayIndex[] getRange = new INDArrayIndex[] {NDArrayIndex.point(0), NDArrayIndex.interval(index,index + numMoved)}; INDArray get = container.get(getRange); INDArrayIndex[] first = new INDArrayIndex[] {NDArrayIndex.point(0), NDArrayIndex.interval(index + 1,index + 1 + get.length())}; container.put(first,get); }
/** * Returns a vector with all of the elements in every nd array * equal to the sum of the lengths of the ndarrays * * @param matrices the ndarrays to getFloat a flattened representation of * @return the flattened ndarray */ @Override public INDArray toFlattened(Collection<INDArray> matrices) { int length = 0; for (INDArray m : matrices) length += m.length(); INDArray ret = Nd4j.create(1, length); int linearIndex = 0; for (INDArray d : matrices) { ret.put(new INDArrayIndex[] {NDArrayIndex.interval(linearIndex, linearIndex + d.length())}, d); linearIndex += d.length(); } return ret; }
private void moveForward(int index) { int numMoved = size - index - 1; INDArrayIndex[] getRange = new INDArrayIndex[] {NDArrayIndex.point(0), NDArrayIndex.interval(index,index + numMoved)}; INDArray get = container.get(getRange).dup(); INDArrayIndex[] first = new INDArrayIndex[] {NDArrayIndex.point(0), NDArrayIndex.interval(index + 1,index + 1 + get.length())}; container.put(first,get); }
@Override public INDArray toDense() { // Dummy way - going to use the conversion routines in level2 (?) INDArray result = Nd4j.zeros(shape()); int[] pointersB = pointerB.asInt(); int[] pointersE = pointerE.asInt(); for (int row = 0; row < rows(); row++) { for (int idx = pointersB[row]; idx < pointersE[row]; idx++) { result.put(row, columnsPointers.getInt(idx), values.getNumber(idx)); } } return result; }
@Override public void roundToTheNearest(int roundTo) { for (int i = 0; i < getFeatures().length(); i++) { double curr = (double) getFeatures().getScalar(i).element(); getFeatures().put(i, Nd4j.scalar(MathUtils.roundDouble(curr, roundTo))); } }
@Override protected void setup() throws LibrecException { super.setup(); inputDim = numUsers; hiddenDim = conf.getInt("rec.hidden.dimension"); learningRate = conf.getDouble("rec.iterator.learnrate"); lambdaReg = conf.getDouble("rec.weight.regularization"); numIterations = conf.getInt("rec.iterator.maximum"); hiddenActivation = conf.get("rec.hidden.activation"); outputActivation = conf.get("rec.output.activation"); // transform the sparse matrix to INDArray int[] matrixShape = {numItems, numUsers}; trainSet = Nd4j.zeros(matrixShape); trainSetMask = Nd4j.zeros(matrixShape); for (MatrixEntry me: trainMatrix) { trainSet.put(me.column(), me.row(), me.get()); trainSetMask.put(me.column(), me.row(), 1); } }
@Override public boolean remove(Object o) { int idx = BooleanIndexing.firstIndex(container,new EqualsCondition((double) o)).getInt(0); if(idx < 0) return false; container.put(new INDArrayIndex[]{NDArrayIndex.interval(idx,container.length())},container.get(NDArrayIndex.interval(idx + 1,container.length()))); container = container.reshape(1,size); return true; }
@Override public boolean remove(Object o) { int idx = BooleanIndexing.firstIndex(container,new EqualsCondition((double) o)).getInt(0); if(idx < 0) return false; container.put(new INDArrayIndex[]{NDArrayIndex.interval(idx,container.length())},container.get(NDArrayIndex.interval(idx + 1,container.length()))); container = container.reshape(1,size); return true; }
@Override protected void setup() throws LibrecException { super.setup(); inputDim = numItems; hiddenDim = conf.getInt("rec.hidden.dimension"); learningRate = conf.getDouble("rec.iterator.learnrate"); lambdaReg = conf.getDouble("rec.weight.regularization"); numIterations = conf.getInt("rec.iterator.maximum"); hiddenActivation = conf.get("rec.hidden.activation"); outputActivation = conf.get("rec.output.activation"); // transform the sparse matrix to INDArray // the sparse training matrix has been binarized int[] matrixShape = {numUsers, numItems}; trainSet = Nd4j.zeros(matrixShape); for (MatrixEntry me: trainMatrix) { trainSet.put(me.row(), me.column(), me.get()); } }