Java Code Examples for org.nd4j.linalg.api.shape.Shape#toOffsetZeroCopy()
The following examples show how to use
org.nd4j.linalg.api.shape.Shape#toOffsetZeroCopy() .
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: NDArrayTestsFortran.java From nd4j with Apache License 2.0 | 5 votes |
@Test public void testToOffsetZeroCopy() { List<Pair<INDArray, String>> testInputs = NDArrayCreationUtil.getAllTestMatricesWithShape(4, 5, 123); for (Pair<INDArray, String> pair : testInputs) { String msg = pair.getSecond(); INDArray in = pair.getFirst(); INDArray dup = Shape.toOffsetZeroCopy(in); INDArray dupc = Shape.toOffsetZeroCopy(in, 'c'); INDArray dupf = Shape.toOffsetZeroCopy(in, 'f'); INDArray dupany = Shape.toOffsetZeroCopyAnyOrder(in); assertEquals(msg, in, dup); assertEquals(msg, in, dupc); assertEquals(msg, in, dupf); assertEquals(msg, dupc.ordering(), 'c'); assertEquals(msg, dupf.ordering(), 'f'); assertEquals(msg, in, dupany); assertEquals(dup.offset(), 0); assertEquals(dupc.offset(), 0); assertEquals(dupf.offset(), 0); assertEquals(dupany.offset(), 0); assertEquals(dup.length(), dup.data().length()); assertEquals(dupc.length(), dupc.data().length()); assertEquals(dupf.length(), dupf.data().length()); assertEquals(dupany.length(), dupany.data().length()); } }
Example 2
Source File: BaseNDArray.java From nd4j with Apache License 2.0 | 5 votes |
@Override public INDArray dup(char order) { WorkspaceUtils.assertValidArray(this, "Cannot duplicate INDArray"); if (this.isCompressed() && this.ordering() == order) { INDArray ret = Nd4j.createArrayFromShapeBuffer(data().dup(), this.shapeInfoDataBuffer()); ret.markAsCompressed(true); return ret; } Nd4j.getCompressor().autoDecompress(this); return Shape.toOffsetZeroCopy(this, order); }
Example 3
Source File: NDArrayTestsFortran.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testToOffsetZeroCopy() { List<Pair<INDArray, String>> testInputs = NDArrayCreationUtil.getAllTestMatricesWithShape(4, 5, 123, DataType.DOUBLE); int cnt = 0; for (Pair<INDArray, String> pair : testInputs) { String msg = pair.getSecond(); INDArray in = pair.getFirst(); INDArray dup = Shape.toOffsetZeroCopy(in); INDArray dupc = Shape.toOffsetZeroCopy(in, 'c'); INDArray dupf = Shape.toOffsetZeroCopy(in, 'f'); INDArray dupany = Shape.toOffsetZeroCopyAnyOrder(in); assertEquals(msg + ": " + cnt, in, dup); assertEquals(msg, in, dupc); assertEquals(msg, in, dupf); assertEquals(msg, dupc.ordering(), 'c'); assertEquals(msg, dupf.ordering(), 'f'); assertEquals(msg, in, dupany); assertEquals(dup.offset(), 0); assertEquals(dupc.offset(), 0); assertEquals(dupf.offset(), 0); assertEquals(dupany.offset(), 0); assertEquals(dup.length(), dup.data().length()); assertEquals(dupc.length(), dupc.data().length()); assertEquals(dupf.length(), dupf.data().length()); assertEquals(dupany.length(), dupany.data().length()); cnt++; } }
Example 4
Source File: TimeSeriesUtils.java From deeplearning4j with Apache License 2.0 | 5 votes |
public static INDArray reshape2dTo3d(INDArray in, int miniBatchSize) { if (in.rank() != 2) throw new IllegalArgumentException("Invalid input: expect NDArray with rank 2"); //Based on: RnnToFeedForwardPreProcessor val shape = in.shape(); if (in.ordering() != 'f') in = Shape.toOffsetZeroCopy(in, 'f'); INDArray reshaped = in.reshape('f', miniBatchSize, shape[0] / miniBatchSize, shape[1]); return reshaped.permute(0, 2, 1); }
Example 5
Source File: BaseComplexNDArray.java From nd4j with Apache License 2.0 | 4 votes |
@Override public IComplexNDArray dup() { return (IComplexNDArray) Shape.toOffsetZeroCopy(this); }
Example 6
Source File: CheckUtil.java From nd4j with Apache License 2.0 | 4 votes |
public static boolean checkGemm(INDArray a, INDArray b, INDArray c, boolean transposeA, boolean transposeB, double alpha, double beta, double maxRelativeDifference, double minAbsDifference) { long commonDimA = (transposeA ? a.rows() : a.columns()); long commonDimB = (transposeB ? b.columns() : b.rows()); if (commonDimA != commonDimB) throw new IllegalArgumentException("Common dimensions don't match: a.shape=" + Arrays.toString(a.shape()) + ", b.shape=" + Arrays.toString(b.shape()) + ", tA=" + transposeA + ", tb=" + transposeB); long outRows = (transposeA ? a.columns() : a.rows()); long outCols = (transposeB ? b.rows() : b.columns()); if (c.rows() != outRows || c.columns() != outCols) throw new IllegalArgumentException("C does not match outRows or outCols"); if (c.offset() != 0 || c.ordering() != 'f') throw new IllegalArgumentException("Invalid c"); INDArray aConvert = transposeA ? a.transpose() : a; RealMatrix rmA = convertToApacheMatrix(aConvert); INDArray bConvet = transposeB ? b.transpose() : b; RealMatrix rmB = convertToApacheMatrix(bConvet); RealMatrix rmC = convertToApacheMatrix(c); RealMatrix rmExpected = rmA.scalarMultiply(alpha).multiply(rmB).add(rmC.scalarMultiply(beta)); INDArray cCopy1 = Nd4j.create(c.shape(), 'f'); cCopy1.assign(c); INDArray cCopy2 = Nd4j.create(c.shape(), 'f'); cCopy2.assign(c); INDArray out = Nd4j.gemm(a, b, c, transposeA, transposeB, alpha, beta); if (out != c) { System.out.println("Returned different array than c"); return false; } if (!checkShape(rmExpected, out)) return false; boolean ok = checkEntries(rmExpected, out, maxRelativeDifference, minAbsDifference); if (!ok) { INDArray aCopy = Shape.toOffsetZeroCopy(a); INDArray bCopy = Shape.toOffsetZeroCopy(b); INDArray onCopies = Nd4j.gemm(aCopy, bCopy, cCopy1, transposeA, transposeB, alpha, beta); printGemmFailureDetails(a, b, cCopy2, transposeA, transposeB, alpha, beta, rmExpected, out, onCopies); } return ok; }
Example 7
Source File: CheckUtil.java From deeplearning4j with Apache License 2.0 | 4 votes |
public static boolean checkGemm(INDArray a, INDArray b, INDArray c, boolean transposeA, boolean transposeB, double alpha, double beta, double maxRelativeDifference, double minAbsDifference) { long commonDimA = (transposeA ? a.rows() : a.columns()); long commonDimB = (transposeB ? b.columns() : b.rows()); if (commonDimA != commonDimB) throw new IllegalArgumentException("Common dimensions don't match: a.shape=" + Arrays.toString(a.shape()) + ", b.shape=" + Arrays.toString(b.shape()) + ", tA=" + transposeA + ", tb=" + transposeB); long outRows = (transposeA ? a.columns() : a.rows()); long outCols = (transposeB ? b.rows() : b.columns()); if (c.rows() != outRows || c.columns() != outCols) throw new IllegalArgumentException("C does not match outRows or outCols"); if (c.offset() != 0 || c.ordering() != 'f') throw new IllegalArgumentException("Invalid c"); INDArray aConvert = transposeA ? a.transpose() : a; RealMatrix rmA = convertToApacheMatrix(aConvert); INDArray bConvet = transposeB ? b.transpose() : b; RealMatrix rmB = convertToApacheMatrix(bConvet); RealMatrix rmC = convertToApacheMatrix(c); RealMatrix rmExpected = rmA.scalarMultiply(alpha).multiply(rmB).add(rmC.scalarMultiply(beta)); INDArray cCopy1 = Nd4j.create(c.shape(), 'f'); cCopy1.assign(c); INDArray cCopy2 = Nd4j.create(c.shape(), 'f'); cCopy2.assign(c); INDArray out = Nd4j.gemm(a, b, c, transposeA, transposeB, alpha, beta); if (out != c) { System.out.println("Returned different array than c"); return false; } if (!checkShape(rmExpected, out)) return false; boolean ok = checkEntries(rmExpected, out, maxRelativeDifference, minAbsDifference); if (!ok) { INDArray aCopy = Shape.toOffsetZeroCopy(a); INDArray bCopy = Shape.toOffsetZeroCopy(b); INDArray onCopies = Nd4j.gemm(aCopy, bCopy, cCopy1, transposeA, transposeB, alpha, beta); printGemmFailureDetails(a, b, cCopy2, transposeA, transposeB, alpha, beta, rmExpected, out, onCopies); } return ok; }