@Override public INDArray mmul(INDArray other) { long[] shape = {rows(), other.columns()}; INDArray result = createUninitialized(shape, 'f'); if (result.isScalar()) return Nd4j.scalar(Nd4j.getBlasWrapper().dot(this, other)); return mmuli(other, result); }
public static void main(String[] args) { Nd4j.setDataType(DataBuffer.Type.DOUBLE); INDArray arr = Nd4j.create(300); double numTimes = 10000000; double total = 0; for(int i = 0; i < numTimes; i++) { long start = System.nanoTime(); Nd4j.getBlasWrapper().axpy(new Integer(1), arr,arr); long after = System.nanoTime(); long add = Math.abs(after - start); System.out.println("Took " + add); total += Math.abs(after - start); } System.out.println("Avg time " + (total / numTimes)); } }
@Override public int outcome() { return Nd4j.getBlasWrapper().iamax(getLabels()); }
/** * in place subtraction of two matrices * * @param other the second ndarray to subtract * @param result the result ndarray * @return the result of the subtraction */ @Override public IComplexNDArray subi(INDArray other, INDArray result) { IComplexNDArray cOther = (IComplexNDArray) other; IComplexNDArray cResult = (IComplexNDArray) result; if (other.isScalar()) return subi(cOther.getComplex(0), result); if (result == this) Nd4j.getBlasWrapper().axpy(Nd4j.NEG_UNIT, cOther, cResult); else if (result == other) { if (data.dataType() == (DataBuffer.Type.DOUBLE)) { Nd4j.getBlasWrapper().scal(Nd4j.NEG_UNIT.asDouble(), cResult); Nd4j.getBlasWrapper().axpy(Nd4j.UNIT, this, cResult); } else { Nd4j.getBlasWrapper().scal(Nd4j.NEG_UNIT.asFloat(), cResult); Nd4j.getBlasWrapper().axpy(Nd4j.UNIT, this, cResult); } } else { Nd4j.getBlasWrapper().copy(this, result); Nd4j.getBlasWrapper().axpy(Nd4j.NEG_UNIT, cOther, cResult); } return cResult; }
Nd4j.getBlasWrapper().level2().gemv(BlasBufferUtil.getCharForTranspose(temp), BlasBufferUtil.getCharForTranspose(this), Nd4j.UNIT, this, otherArray, Nd4j.ZERO, temp); } else { Nd4j.getBlasWrapper().level3().gemm(BlasBufferUtil.getCharForTranspose(temp), BlasBufferUtil.getCharForTranspose(this), BlasBufferUtil.getCharForTranspose(other), Nd4j.UNIT, this, otherArray, Nd4j.ZERO, temp); Nd4j.getBlasWrapper().copy(temp, resultArray); Nd4j.getBlasWrapper().level2().gemv(BlasBufferUtil.getCharForTranspose(resultArray), BlasBufferUtil.getCharForTranspose(this), Nd4j.UNIT, this, otherArray, Nd4j.ZERO, resultArray); Nd4j.getBlasWrapper().level3().gemm(BlasBufferUtil.getCharForTranspose(resultArray), BlasBufferUtil.getCharForTranspose(this), BlasBufferUtil.getCharForTranspose(other), Nd4j.UNIT, this, otherArray, Nd4j.ZERO, resultArray);
LinkedList<INDArray> y = (LinkedList<INDArray>) searchState.get("y"); double sy = Nd4j.getBlasWrapper().dot(previousParameters, previousGradient) + Nd4j.EPS_THRESHOLD; double yy = Nd4j.getBlasWrapper().dot(previousGradient, previousGradient) + Nd4j.EPS_THRESHOLD; throw new IllegalStateException("Gradients and parameters length not equal"); alpha[i] = rhoi * Nd4j.getBlasWrapper().dot(si, searchDir); Nd4j.getBlasWrapper().level1().axpy(searchDir.length(), -alpha[i], yi, searchDir); //q = q-alpha[i]*yi double rhoi = rhoIter.next(); double beta = rhoi * Nd4j.getBlasWrapper().dot(yi, searchDir); //beta = rho_i * y_i^T * r Nd4j.getBlasWrapper().level1().axpy(gradient.length(), alpha[i] - beta, si, searchDir);
/** * finds the element of a vector that has the minimum absolute value. * * @param arr * @return */ @Override public int iamin(INDArray arr) { if (arr.isSparse()) { return Nd4j.getSparseBlasWrapper().level1().iamin(arr); } else { throw new UnsupportedOperationException(); } }
Nd4j.getBlasWrapper().lapack().gesvd(A, s, null, VT);
/** * computes the Euclidean norm of a vector. * * @param arr * @return */ @Override public double nrm2(INDArray arr) { if (arr.isSparse()) { return Nd4j.getSparseBlasWrapper().level1().nrm2(arr); } if (Nd4j.getExecutioner().getProfilingMode() == OpExecutioner.ProfilingMode.ALL) OpProfiler.getInstance().processBlasCall(false, arr); if (arr.isSparse()) { return Nd4j.getSparseBlasWrapper().level1().nrm2(arr); } if (arr.data().dataType() == DataBuffer.Type.DOUBLE) { DefaultOpExecutioner.validateDataType(DataBuffer.Type.DOUBLE, arr); return dnrm2(arr.length(), arr, BlasBufferUtil.getBlasStride(arr)); } else { DefaultOpExecutioner.validateDataType(DataBuffer.Type.FLOAT, arr); return snrm2(arr.length(), arr, BlasBufferUtil.getBlasStride(arr)); } // TODO: add nrm2 for half, as call to appropriate NativeOp<HALF> }
/** * in place subtraction of two matrices * * @param other the second ndarray to subtract * @param result the result ndarray * @return the result of the subtraction */ @Override public IComplexNDArray subi(INDArray other, INDArray result) { IComplexNDArray cOther = (IComplexNDArray) other; IComplexNDArray cResult = (IComplexNDArray) result; if (other.isScalar()) return subi(cOther.getComplex(0), result); if (result == this) Nd4j.getBlasWrapper().axpy(Nd4j.NEG_UNIT, cOther, cResult); else if (result == other) { if (data.dataType() == (DataBuffer.Type.DOUBLE)) { Nd4j.getBlasWrapper().scal(Nd4j.NEG_UNIT.asDouble(), cResult); Nd4j.getBlasWrapper().axpy(Nd4j.UNIT, this, cResult); } else { Nd4j.getBlasWrapper().scal(Nd4j.NEG_UNIT.asFloat(), cResult); Nd4j.getBlasWrapper().axpy(Nd4j.UNIT, this, cResult); } } else { Nd4j.getBlasWrapper().copy(this, result); Nd4j.getBlasWrapper().axpy(Nd4j.NEG_UNIT, cOther, cResult); } return cResult; }
Nd4j.getBlasWrapper().lapack().gesvd(A, s, null, VT);
Nd4j.getBlasWrapper().level2().gemv(BlasBufferUtil.getCharForTranspose(temp), BlasBufferUtil.getCharForTranspose(this), Nd4j.UNIT, this, otherArray, Nd4j.ZERO, temp); } else { Nd4j.getBlasWrapper().level3().gemm(BlasBufferUtil.getCharForTranspose(temp), BlasBufferUtil.getCharForTranspose(this), BlasBufferUtil.getCharForTranspose(other), Nd4j.UNIT, this, otherArray, Nd4j.ZERO, temp); Nd4j.getBlasWrapper().copy(temp, resultArray); Nd4j.getBlasWrapper().level2().gemv(BlasBufferUtil.getCharForTranspose(resultArray), BlasBufferUtil.getCharForTranspose(this), Nd4j.UNIT, this, otherArray, Nd4j.ZERO, resultArray); Nd4j.getBlasWrapper().level3().gemm(BlasBufferUtil.getCharForTranspose(resultArray), BlasBufferUtil.getCharForTranspose(this), BlasBufferUtil.getCharForTranspose(other), Nd4j.UNIT, this, otherArray, Nd4j.ZERO, resultArray);
/** * Perform a copy matrix multiplication * * @param other the other matrix to perform matrix multiply with * @return the result of the matrix multiplication */ @Override public INDArray mmul(INDArray other) { // FIXME: for 1D case, we probably want vector output here? long[] shape = {rows(), other.rank() == 1 ? 1 : other.columns()}; INDArray result = createUninitialized(shape, 'f'); if (result.isScalar()) return Nd4j.scalar(Nd4j.getBlasWrapper().dot(this, other)); return mmuli(other, result); }
@Override public Map<Integer, Double> labelCounts() { Map<Integer, Double> ret = new HashMap<>(); if (labels == null) return ret; long nTensors = labels.tensorssAlongDimension(1); for (int i = 0; i < nTensors; i++) { INDArray row = labels.tensorAlongDimension(i, 1); INDArray javaRow = labels.javaTensorAlongDimension(i, 1); int maxIdx = Nd4j.getBlasWrapper().iamax(row); int maxIdxJava = Nd4j.getBlasWrapper().iamax(javaRow); if (maxIdx < 0) throw new IllegalStateException("Please check the iamax implementation for " + Nd4j.getBlasWrapper().getClass().getName()); if (ret.get(maxIdx) == null) ret.put(maxIdx, 1.0); else ret.put(maxIdx, ret.get(maxIdx) + 1.0); } return ret; }
/** * finds the element of a * vector that has the largest absolute value. * * @param arr * @return */ @Override public int iamax(INDArray arr) { if (arr.isSparse()) { return Nd4j.getSparseBlasWrapper().level1().iamax(arr); } if (Nd4j.getExecutioner().getProfilingMode() == OpExecutioner.ProfilingMode.ALL) OpProfiler.getInstance().processBlasCall(false, arr); if (arr.data().dataType() == DataBuffer.Type.DOUBLE) { DefaultOpExecutioner.validateDataType(DataBuffer.Type.DOUBLE, arr); return idamax(arr.length(), arr, BlasBufferUtil.getBlasStride(arr)); } else { DefaultOpExecutioner.validateDataType(DataBuffer.Type.FLOAT, arr); return isamax(arr.length(), arr, BlasBufferUtil.getBlasStride(arr)); } }
@Override public INDArray sample(int[] shape) { int numRows = 1; for (int i = 0; i < shape.length - 1; i++) numRows *= shape[i]; int numCols = shape[shape.length - 1]; val flatShape = new int[]{numRows, numCols}; val flatRng = Nd4j.getExecutioner().exec(new GaussianDistribution(Nd4j.createUninitialized(flatShape, Nd4j.order()), 0.0, 1.0), random); long m = flatRng.rows(); long n = flatRng.columns(); val s = Nd4j.create(m < n ? m : n); val u = m < n ? Nd4j.create(m, n) : Nd4j.create(m, m); val v = Nd4j.create(n, n, 'f'); Nd4j.getBlasWrapper().lapack().gesvd(flatRng, s, u, v); // FIXME: int cast if (gains == null) { if (u.rows() == numRows && u.columns() == numCols) { return v.get(NDArrayIndex.interval(0, numRows), NDArrayIndex.interval(0, numCols)).mul(gain).reshape(ArrayUtil.toLongArray(shape)); } else { return u.get(NDArrayIndex.interval(0, numRows), NDArrayIndex.interval(0, numCols)).mul(gain).reshape(ArrayUtil.toLongArray(shape)); } } else { throw new UnsupportedOperationException(); } }
/** * 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; }