/** * Takes a set of data on each row, with the same number of features as the constructing data * and returns the data in the coordinates of the basis set about the mean. * @param data Data of the same features used to construct the PCA object * @return The record in terms of the principal component vectors, you can set unused ones to zero. */ public INDArray convertToComponents(INDArray data) { INDArray dx = data.subRowVector(mean); return Nd4j.tensorMmul(eigenvectors.transpose(), dx, new int[][] {{1}, {1}}).transposei(); }
@Builder public MMulTranspose(boolean transposeA, boolean transposeB, boolean transposeResult, INDArray a, INDArray b) { this.transposeA = transposeA; this.transposeB = transposeB; this.transposeResult = transposeResult; if(transposeResult) { this.transposeA = !transposeA; this.transposeB = !transposeB; this.a = b; this.b = a; } this.transposeA = transposeA; this.transposeB = transposeB; if(this.transposeA && a != null) this.a = a.transpose(); else this.a = a; if(this.transposeB && b != null) this.b = b.transpose(); else this.b = b; } }
@Override public INDArray z() { return x().transpose(); }
public static void main(String[] args) { INDArray n = Nd4j.linspace(1,10000000,10000000); System.out.println("MMUL" + n.mmul(n.transpose())); }
ndv = nd.transpose(); // the two and the three switch - a simple transpose System.out.println(ndv); System.out.println(nd2); ndv = nd2.transpose(); // this will make a long-rowed matrix into a tall-columned matrix System.out.println(ndv); ndv = nd2.transpose(); // one more transpose just for fun. System.out.println(ndv);
/** * Compute the right pseudo inverse. Input matrix must have full row rank. * * See also: <a href="https://en.wikipedia.org/wiki/Moore%E2%80%93Penrose_inverse#Definition">Moore–Penrose inverse</a> * * @param arr Input matrix * @param inPlace Whether to store the result in {@code arr} * @return Right pseudo inverse of {@code arr} * @exception IllegalArgumentException Input matrix {@code arr} did not have full row rank. */ public static INDArray pRightInvert(INDArray arr, boolean inPlace) { try{ final INDArray inv = arr.transpose().mmul(invert(arr.mmul(arr.transpose()), inPlace)); if (inPlace) arr.assign(inv); return inv; } catch (SingularMatrixException e){ throw new IllegalArgumentException( "Full row rank condition for right pseudo inverse was not met."); } } }
/** * Compute the left pseudo inverse. Input matrix must have full column rank. * * See also: <a href="https://en.wikipedia.org/wiki/Moore%E2%80%93Penrose_inverse#Definition">Moore–Penrose inverse</a> * * @param arr Input matrix * @param inPlace Whether to store the result in {@code arr} * @return Left pseudo inverse of {@code arr} * @exception IllegalArgumentException Input matrix {@code arr} did not have full column rank. */ public static INDArray pLeftInvert(INDArray arr, boolean inPlace) { try { final INDArray inv = invert(arr.transpose().mmul(arr), inPlace).mmul(arr.transpose()); if (inPlace) arr.assign(inv); return inv; } catch (SingularMatrixException e) { throw new IllegalArgumentException( "Full column rank condition for left pseudo inverse was not met."); } }
@Override public void exec(INDArray... inputs) { val inputArguments = inputArguments(); if(inputArguments == null || inputArguments.length < 1) { throw new IllegalStateException("No arguments found."); } INDArray weights = inputArguments[0]; INDArray right = inputArguments[1]; val outputArguments = outputArguments(); if(outputArguments == null || outputArguments.length < 1) { if(inputArguments.length == 1) addOutputArgument(inputs[0].mmul(weights.transpose())); else addOutputArgument(inputs[0].mmul(weights.transpose()).addiColumnVector(right)); } else { inputs[0].mmul(weights.transpose(),outputArguments[0]); } }
@Override public List<long[]> calculateOutputShape() { List<long[]> ret = new ArrayList<>(); ret.add(Shape.getMatrixMultiplyShape(inputArguments()[0].shape(),new long[]{nOut,nIn})); ret.add(Shape.getMatrixMultiplyShape(inputArguments()[0].shape(),inputArguments()[1].transpose().shape())); if(biasWeightInitScheme != null) { ret.add(new long[]{nOut,1}); } return ret; }
/** * Rotate a matrix 90 degrees * * @param toRotate the matrix to rotate * @return the rotated matrix */ @Override public void rot90(INDArray toRotate) { if (!toRotate.isMatrix()) throw new IllegalArgumentException("Only rotating matrices"); INDArray start = toRotate.transpose(); for (int i = 0; i < start.rows(); i++) start.putRow(i, reverse(start.getRow(i))); }
@Override public Pair<Gradient, INDArray> backpropGradient(INDArray epsilon) { //If this layer is layer L, then epsilon is (w^(L+1)*(d^(L+1))^T) (or equivalent) INDArray z = this.preOutput(true); //Note: using preOutput(INDArray) can't be used as this does a setInput(input) and resets the 'appliedDropout' flag //INDArray activationDerivative = Nd4j.getExecutioner().execAndReturn(Nd4j.getOpFactory().createTransform(conf().getLayer().getActivationFunction(), z).derivative()); // INDArray activationDerivative = conf().getLayer().getActivationFn().getGradient(z); // INDArray delta = epsilon.muli(activationDerivative); INDArray delta = layerConf().getActivationFn().backprop(z, epsilon).getFirst(); //TODO handle activation function params if (maskArray != null) { applyMask(delta); } Gradient ret = new DefaultGradient(); INDArray weightGrad = gradientViews.get(CDAEParamInitializer.WEIGHT_KEY); //f order Nd4j.gemm(input, delta, weightGrad, true, false, 1.0, 0.0); INDArray userWeightGrad = gradientViews.get(CDAEParamInitializer.USER_WEIGHT_KEY); //f order userWeightGrad.assign(delta); INDArray biasGrad = gradientViews.get(CDAEParamInitializer.BIAS_KEY); biasGrad.assign(delta.sum(0)); //biasGrad is initialized/zeroed first ret.gradientForVariable().put(CDAEParamInitializer.WEIGHT_KEY, weightGrad); ret.gradientForVariable().put(CDAEParamInitializer.BIAS_KEY, biasGrad); ret.gradientForVariable().put(CDAEParamInitializer.USER_WEIGHT_KEY, userWeightGrad); INDArray epsilonNext = params.get(CDAEParamInitializer.WEIGHT_KEY).mmul(delta.transpose()).transpose(); //epsilonNext = null; return new Pair<>(ret, epsilonNext); }
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> 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; }
/** * Returns a column vector where each entry is the nth bilinear * product of the nth slices of the two tensors. */ @Override public INDArray bilinearProducts(INDArray curr, INDArray in) { assert curr.shape().length == 3; if (in.columns() != 1) { throw new AssertionError("Expected a column vector"); } if (in.rows() != curr.size(curr.shape().length - 1)) { throw new AssertionError("Number of rows in the input does not match number of columns in tensor"); } if (curr.size(curr.shape().length - 2) != curr.size(curr.shape().length - 1)) { throw new AssertionError("Can only perform this operation on a SimpleTensor with square slices"); } INDArray ret = Nd4j.create(curr.slices(), 1); INDArray inT = in.transpose(); for (int i = 0; i < curr.slices(); i++) { INDArray slice = curr.slice(i); INDArray inTTimesSlice = inT.mmul(slice); ret.putScalar(i, Nd4j.getBlasWrapper().dot(inTTimesSlice, in)); } return ret; }
INDArray V = VT.transpose(); INDArray factor = Nd4j.create(n, nDims, 'f'); for (int i = 0; i < nDims; i++) {