public static List<Pair<INDArray, String>> get3dReshapedWithShape(long seed, long... shape) { Nd4j.getRandom().setSeed(seed); long[] shape2d = {shape[0] * shape[2], shape[1]}; int lenshape2d = ArrayUtil.prod(shape2d); INDArray array2d = Nd4j.linspace(1, lenshape2d, lenshape2d).reshape(shape2d); INDArray array3d = array2d.reshape(shape); return Collections.singletonList(new Pair<>(array3d, "get3dReshapedWithShape(" + seed + "," + Arrays.toString(shape) + ").get(0)")); }
/** * Get a view of the underlying array * relative to the size of the actual array. * (Sometimes there are overflows in the internals * but you want to use the internal INDArray for computing something * directly, this gives you the relevant subset that reflects the content of the list) * @return the view of the underlying ndarray relative to the collection's real size */ public INDArray array() { return container.get(NDArrayIndex.interval(0,size)).reshape(1,size); }
/** * 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); }
public INDArray mapTensorProto(OnnxProto3.TensorProto tensor) { if(tensor == null) return null; DataBuffer.Type type = nd4jTypeFromOnnxType(tensor.getDataType()); ByteString bytes = tensor.getRawData(); ByteBuffer byteBuffer = bytes.asReadOnlyByteBuffer().order(ByteOrder.nativeOrder()); ByteBuffer directAlloc = ByteBuffer.allocateDirect(byteBuffer.capacity()).order(ByteOrder.nativeOrder()); directAlloc.put(byteBuffer); directAlloc.rewind(); long[] shape = getShapeFromTensor(tensor); DataBuffer buffer = Nd4j.createBuffer(directAlloc,type, ArrayUtil.prod(shape)); INDArray arr = Nd4j.create(buffer).reshape(shape); return arr; }
public static List<Pair<INDArray, String>> get6dReshapedWithShape(int seed, int... shape) { Nd4j.getRandom().setSeed(seed); int[] shape3d = {shape[0] * shape[2], shape[4] * shape[5], shape[1] * shape[3]}; INDArray array3d = Nd4j.rand(shape3d); INDArray array6d = array3d.reshape(ArrayUtil.toLongArray(shape)); return Collections.singletonList(new Pair<>(array6d, "get6dReshapedWithShape(" + seed + "," + Arrays.toString(shape) + ").get(0)")); }
public static List<Pair<INDArray, String>> get4dReshapedWithShape(int seed, int... shape) { Nd4j.getRandom().setSeed(seed); int[] shape2d = {shape[0] * shape[2], shape[1] * shape[3]}; INDArray array2d = Nd4j.rand(shape2d); INDArray array3d = array2d.reshape(ArrayUtil.toLongArray(shape)); return Collections.singletonList(new Pair<>(array3d, "get4dReshapedWithShape(" + seed + "," + Arrays.toString(shape) + ").get(0)")); }
public static List<Pair<INDArray, String>> get5dReshapedWithShape(int seed, int... shape) { Nd4j.getRandom().setSeed(seed); int[] shape2d = {shape[0] * shape[2], shape[4], shape[1] * shape[3]}; INDArray array3d = Nd4j.rand(shape2d); INDArray array5d = array3d.reshape(ArrayUtil.toLongArray(shape)); return Collections.singletonList(new Pair<>(array5d, "get5dReshapedWithShape(" + seed + "," + Arrays.toString(shape) + ").get(0)")); }
/** * Reshapes the input in to the given rows and columns * * @param rows the row size * @param cols the column size * @return a copy of this data op with the input resized */ @Override public DataSet reshape(int rows, int cols) { DataSet ret = new DataSet(getFeatures().reshape(new long[] {rows, cols}), getLabels()); return ret; }
/** * Reshapes an ndarray to remove leading 1s * @param toStrip the ndarray to newShapeNoCopy * @return the reshaped ndarray */ public static INDArray stripOnes(INDArray toStrip) { if (toStrip.isVector()) return toStrip; else { long[] shape = Shape.squeeze(toStrip.shape()); return toStrip.reshape(shape); } }
public static List<Pair<INDArray, String>> get3dPermutedWithShape(long seed, long... shape) { Nd4j.getRandom().setSeed(seed); long[] createdShape = {shape[1], shape[2], shape[0]}; int lencreatedShape = ArrayUtil.prod(createdShape); INDArray arr = Nd4j.linspace(1, lencreatedShape, lencreatedShape).reshape(createdShape); INDArray permuted = arr.permute(2, 0, 1); return Collections.singletonList(new Pair<>(permuted, "get3dPermutedWithShape(" + seed + "," + Arrays.toString(shape) + ").get(0)")); }
public Map<String, INDArray> getGradientsFromFlattened(NeuralNetConfiguration conf, INDArray gradientView) { Map<String, INDArray> out = super.getGradientsFromFlattened(conf, gradientView); FeedForwardLayer layerConf = (FeedForwardLayer) conf.getLayer(); int nIn = layerConf.getNIn(); int nOut = layerConf.getNOut(); int nWeightParams = nIn * nOut; int nUserWeightParams = numUsers * nOut; INDArray userWeightGradientView = gradientView.get(NDArrayIndex.point(0), NDArrayIndex.interval(nWeightParams + nOut, nWeightParams + nOut + nUserWeightParams)) .reshape('f', numUsers, nOut); out.put(USER_WEIGHT_KEY, userWeightGradientView); return out; } }
public static Pair<INDArray, String> getTransposedMatrixWithShape(char ordering, int rows, int cols, int seed) { Nd4j.getRandom().setSeed(seed); INDArray out = Nd4j.linspace(1, rows * cols, rows * cols).reshape(ordering, cols, rows); return new Pair<>(out.transpose(), "getTransposedMatrixWithShape(" + rows + "," + cols + "," + seed + ")"); }
public static Pair<INDArray, String> getPermutedWithShape(char ordering, long rows, long cols, long seed) { Nd4j.getRandom().setSeed(seed); long len = rows * cols; INDArray arr = Nd4j.linspace(1, len, len).reshape(cols, rows); return new Pair<>(arr.permute(1, 0), "getPermutedWithShape(" + rows + "," + cols + "," + seed + ")"); }
/** * Replicate and tile array to fill out to the given shape * See: * https://github.com/numpy/numpy/blob/master/numpy/matlib.py#L310-L358 * @param shape the new shape of this ndarray * @return the shape to fill out to */ @Override public INDArray repmat(int[] shape) { Nd4j.getCompressor().autoDecompress(this); long rows = rows() * shape[0]; long cols = columns() * shape[1]; INDArray ret = reshape(1, length()).repeat(0, shape[0]).reshape(rows, columns()).repeat(0, shape[1]); return ret.reshape(rows, cols); }
public static Pair<INDArray, String> getTransposedMatrixWithShape(long rows, long cols, long seed) { Nd4j.getRandom().setSeed(seed); INDArray out = Nd4j.linspace(1, rows * cols, rows * cols).reshape(cols, rows); return new Pair<>(out.transpose(), "getTransposedMatrixWithShape(" + rows + "," + cols + "," + seed + ")"); }
/** * Estimate the variance of a single record with reduced # of dimensions. * @param data A single record with the same <i>N</i> features as the constructing data set * @param ndims The number of dimensions to include in calculation * @return The fraction (0 to 1) of the total variance covered by the <i>ndims</i> basis set. */ public double estimateVariance(INDArray data, int ndims) { INDArray dx = data.sub(mean); INDArray v = eigenvectors.transpose().mmul(dx.reshape(dx.columns(), 1)); INDArray t2 = Transforms.pow(v, 2); double fraction = t2.get(NDArrayIndex.interval(0, ndims)).sumNumber().doubleValue(); double total = t2.sumNumber().doubleValue(); return fraction / total; }
protected INDArray handleParamsView(INDArray outputArray, INDArray paramView) { //minor optimization when the views are the same, just return if(paramView == null || paramView == outputArray) return outputArray; INDArray flat = Nd4j.toFlattened(order(), outputArray); if (flat.length() != paramView.length()) throw new RuntimeException("ParamView length does not match initialized weights length (view length: " + paramView.length() + ", view shape: " + Arrays.toString(paramView.shape()) + "; flattened length: " + flat.length()); paramView.assign(flat); return paramView.reshape(order(), outputArray.shape()); }
/** * Create an ndarray based on the given data * @param sliceShape the shape of each slice * @param arrays the arrays of data to create * @return the ndarray of the specified shape where * number of slices is equal to array length and each * slice is the specified shape */ public static INDArray create(int[] sliceShape, float[]... arrays) { int slices = arrays.length; INDArray ret = Nd4j.create(ArrayUtil.combine(new int[] {slices}, sliceShape)); for (int i = 0; i < ret.slices(); i++) ret.putSlice(i, Nd4j.create(arrays[i]).reshape(ArrayUtil.toLongArray(sliceShape))); return ret; }
@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; }