Java Code Examples for org.nd4j.linalg.api.ndarray.INDArray#add()
The following examples show how to use
org.nd4j.linalg.api.ndarray.INDArray#add() .
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: TwoPointApproximationTest.java From nd4j with Apache License 2.0 | 6 votes |
@Test public void testLinspaceDerivative() throws Exception { String basePath = "/two_points_approx_deriv_numpy/"; INDArray linspace = Nd4j.createNpyFromInputStream(new ClassPathResource(basePath + "x.npy").getInputStream()); INDArray yLinspace = Nd4j.createNpyFromInputStream(new ClassPathResource(basePath + "y.npy").getInputStream()); Function<INDArray,INDArray> f = new Function<INDArray, INDArray>() { @Override public INDArray apply(INDArray indArray) { return indArray.add(1); } }; INDArray test = TwoPointApproximation .approximateDerivative(f,linspace,null,yLinspace, Nd4j.create(new double[] {Float.MIN_VALUE ,Float.MAX_VALUE})); INDArray npLoad = Nd4j.createNpyFromInputStream(new ClassPathResource(basePath + "approx_deriv_small.npy").getInputStream()); assertEquals(npLoad,test); System.out.println(test); }
Example 2
Source File: CheckUtil.java From deeplearning4j with Apache License 2.0 | 6 votes |
/**Same as checkMmul, but for matrix addition */ public static boolean checkAdd(INDArray first, INDArray second, double maxRelativeDifference, double minAbsDifference) { RealMatrix rmFirst = convertToApacheMatrix(first); RealMatrix rmSecond = convertToApacheMatrix(second); INDArray result = first.add(second); RealMatrix rmResult = rmFirst.add(rmSecond); if (!checkShape(rmResult, result)) return false; boolean ok = checkEntries(rmResult, result, maxRelativeDifference, minAbsDifference); if (!ok) { INDArray onCopies = Shape.toOffsetZeroCopy(first).add(Shape.toOffsetZeroCopy(second)); printFailureDetails(first, second, rmResult, result, onCopies, "add"); } return ok; }
Example 3
Source File: MixedDataTypesTests.java From deeplearning4j with Apache License 2.0 | 6 votes |
@Test public void testSimple(){ Nd4j.create(1); for(DataType dt : new DataType[]{DataType.DOUBLE, DataType.FLOAT, DataType.HALF, DataType.INT, DataType.LONG}) { // System.out.println("----- " + dt + " -----"); INDArray arr = Nd4j.ones(dt,1, 5); // System.out.println("Ones: " + arr); arr.assign(1.0); // System.out.println("assign(1.0): " + arr); // System.out.println("DIV: " + arr.div(8)); // System.out.println("MUL: " + arr.mul(8)); // System.out.println("SUB: " + arr.sub(8)); // System.out.println("ADD: " + arr.add(8)); // System.out.println("RDIV: " + arr.rdiv(8)); // System.out.println("RSUB: " + arr.rsub(8)); arr.div(8); arr.mul(8); arr.sub(8); arr.add(8); arr.rdiv(8); arr.rsub(8); } }
Example 4
Source File: CheckUtil.java From nd4j with Apache License 2.0 | 6 votes |
/**Same as checkMmul, but for matrix addition */ public static boolean checkAdd(INDArray first, INDArray second, double maxRelativeDifference, double minAbsDifference) { RealMatrix rmFirst = convertToApacheMatrix(first); RealMatrix rmSecond = convertToApacheMatrix(second); INDArray result = first.add(second); RealMatrix rmResult = rmFirst.add(rmSecond); if (!checkShape(rmResult, result)) return false; boolean ok = checkEntries(rmResult, result, maxRelativeDifference, minAbsDifference); if (!ok) { INDArray onCopies = Shape.toOffsetZeroCopy(first).add(Shape.toOffsetZeroCopy(second)); printFailureDetails(first, second, rmResult, result, onCopies, "add"); } return ok; }
Example 5
Source File: NDArrayTestsFortran.java From nd4j with Apache License 2.0 | 6 votes |
@Test public void testElementWiseOps() { INDArray n1 = Nd4j.scalar(1); INDArray n2 = Nd4j.scalar(2); INDArray nClone = n1.add(n2); assertEquals(Nd4j.scalar(3), nClone); INDArray n1PlusN2 = n1.add(n2); assertFalse(getFailureMessage(), n1PlusN2.equals(n1)); INDArray n3 = Nd4j.scalar(3); INDArray n4 = Nd4j.scalar(4); INDArray subbed = n4.sub(n3); INDArray mulled = n4.mul(n3); INDArray div = n4.div(n3); assertFalse(subbed.equals(n4)); assertFalse(mulled.equals(n4)); assertEquals(Nd4j.scalar(1), subbed); assertEquals(Nd4j.scalar(12), mulled); assertEquals(Nd4j.scalar(1.333333333333333333333), div); }
Example 6
Source File: TwoPointApproximation.java From nd4j with Apache License 2.0 | 6 votes |
/** * * @param func * @param x0 * @param f0 * @param h * @param oneSided * @return */ public static INDArray denseDifference(Function<INDArray,INDArray> func, INDArray x0,INDArray f0, INDArray h,INDArray oneSided) { INDArray hVecs = Nd4j.diag(h.reshape(1,h.length())); INDArray dx,df,x; INDArray jTransposed = Nd4j.create(x0.length(),f0.length()); for(int i = 0; i < h.length(); i++) { INDArray hVecI = hVecs.slice(i); x = (x0.add(hVecI)); dx = x.slice(i).sub(x0.slice(i)); df = func.apply(x).sub(f0); INDArray div = df.div(dx); jTransposed.putSlice(i,div); } if(f0.length() == 1) jTransposed = jTransposed.ravel(); return jTransposed; }
Example 7
Source File: GraphRunnerTest.java From deeplearning4j with Apache License 2.0 | 5 votes |
private void runGraphRunnerTest(GraphRunner graphRunner) throws Exception { String json = graphRunner.sessionOptionsToJson(); if( json != null ) { org.tensorflow.framework.ConfigProto.Builder builder = org.tensorflow.framework.ConfigProto.newBuilder(); JsonFormat.parser().merge(json, builder); org.tensorflow.framework.ConfigProto build = builder.build(); assertEquals(build,graphRunner.getSessionOptionsConfigProto()); } assertNotNull(graphRunner.getInputOrder()); assertNotNull(graphRunner.getOutputOrder()); org.tensorflow.framework.ConfigProto configProto1 = json == null ? null : GraphRunner.fromJson(json); assertEquals(graphRunner.getSessionOptionsConfigProto(),configProto1); assertEquals(2,graphRunner.getInputOrder().size()); assertEquals(1,graphRunner.getOutputOrder().size()); INDArray input1 = Nd4j.linspace(1,4,4).reshape(4); INDArray input2 = Nd4j.linspace(1,4,4).reshape(4); Map<String,INDArray> inputs = new LinkedHashMap<>(); inputs.put("input_0",input1); inputs.put("input_1",input2); for(int i = 0; i < 2; i++) { Map<String,INDArray> outputs = graphRunner.run(inputs); INDArray assertion = input1.add(input2); assertEquals(assertion,outputs.get("output")); } }
Example 8
Source File: GridExecutionerTest.java From nd4j with Apache License 2.0 | 5 votes |
@Test public void testReverseFlow2() { CudaGridExecutioner executioner = ((CudaGridExecutioner) Nd4j.getExecutioner()); INDArray n1 = Nd4j.scalar(1); INDArray n2 = Nd4j.scalar(2); INDArray n3 = Nd4j.scalar(3); INDArray n4 = Nd4j.scalar(4); System.out.println("0: ------------------------"); INDArray nClone = n1.add(n2); assertEquals(Nd4j.scalar(3), nClone); INDArray n1PlusN2 = n1.add(n2); assertFalse(n1PlusN2.equals(n1)); System.out.println("2: ------------------------"); System.out.println(n4); INDArray subbed = n4.sub(n3); INDArray mulled = n4.mul(n3); INDArray div = n4.div(n3); System.out.println("Subbed: " + subbed); System.out.println("Mulled: " + mulled); System.out.println("Div: " + div); System.out.println("4: ------------------------"); assertFalse(subbed.equals(n4)); assertFalse(mulled.equals(n4)); assertEquals(0, executioner.getQueueLength()); assertEquals(Nd4j.scalar(1), subbed); assertEquals(Nd4j.scalar(12), mulled); assertEquals(Nd4j.scalar(1.333333333333333333333), div); }
Example 9
Source File: OperationProfilerTests.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test(expected = ND4JIllegalStateException.class) public void testScopePanic1() { Nd4j.getExecutioner().setProfilingMode(OpExecutioner.ProfilingMode.SCOPE_PANIC); INDArray array; try (MemoryWorkspace workspace = Nd4j.getWorkspaceManager().getAndActivateWorkspace("WS119")) { array = Nd4j.create(10); assertTrue(array.isAttached()); } array.add(1.0); }
Example 10
Source File: TestPythonTransformProcess.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test(timeout = 60000L) public void testNDArrayMixed() throws Exception{ long[] shape = new long[]{3, 2}; INDArray arr1 = Nd4j.rand(DataType.DOUBLE, shape); INDArray arr2 = Nd4j.rand(DataType.DOUBLE, shape); INDArray expectedOutput = arr1.add(arr2.castTo(DataType.DOUBLE)); Builder schemaBuilder = new Builder(); schemaBuilder .addColumnNDArray("col1", shape) .addColumnNDArray("col2", shape); Schema initialSchema = schemaBuilder.build(); schemaBuilder.addColumnNDArray("col3", shape); Schema finalSchema = schemaBuilder.build(); String pythonCode = "col3 = col1 + col2"; TransformProcess tp = new TransformProcess.Builder(initialSchema).transform( PythonTransform.builder().code(pythonCode) .outputSchema(finalSchema) .build() ).build(); List<Writable> inputs = Arrays.asList( (Writable) new NDArrayWritable(arr1), new NDArrayWritable(arr2) ); List<Writable> outputs = tp.execute(inputs); assertEquals(arr1, ((NDArrayWritable)outputs.get(0)).get()); assertEquals(arr2, ((NDArrayWritable)outputs.get(1)).get()); assertEquals(expectedOutput,((NDArrayWritable)outputs.get(2)).get()); }
Example 11
Source File: SameDiffTests.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testAutoBroadcastAddMatrixVector() { SameDiff sameDiff = SameDiff.create(); INDArray arr = Nd4j.linspace(1, 4, 4).reshape(2, 2); INDArray row = Nd4j.ones(2); INDArray assertion = arr.add(1.0); SDVariable left = sameDiff.var("arr", arr); SDVariable right = sameDiff.var("row", row); SDVariable test = left.add(right); assertEquals(assertion, test.eval()); }
Example 12
Source File: LossMSLE.java From nd4j with Apache License 2.0 | 5 votes |
@Override public INDArray computeGradient(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask) { if (labels.size(1) != preOutput.size(1)) { throw new IllegalArgumentException( "Labels array numColumns (size(1) = " + labels.size(1) + ") does not match output layer" + " number of outputs (nOut = " + preOutput.size(1) + ") "); } //INDArray output = Nd4j.getExecutioner().execAndReturn(Nd4j.getOpFactory().createTransform(activationFn, preOutput.dup())); INDArray output = activationFn.getActivation(preOutput.dup(), true); INDArray p1 = output.add(1.0); INDArray dlda = p1.rdiv(2.0 / labels.size(1)); INDArray logRatio = Transforms.log(p1.divi(labels.add(1.0)), false); dlda.muli(logRatio); if (weights != null) { dlda.muliRowVector(weights); } if (mask != null && LossUtil.isPerOutputMasking(dlda, mask)) { //For *most* activation functions: we don't actually need to mask dL/da in addition to masking dL/dz later //but: some, like softmax, require both (due to dL/dz_i being a function of dL/da_j, for i != j) //We could add a special case for softmax (activationFn instanceof ActivationSoftmax) but that would be // error prone - though buy us a tiny bit of performance LossUtil.applyMask(dlda, mask); } //dL/dz INDArray gradients = activationFn.backprop(preOutput, dlda).getFirst(); //TODO activation functions with weights if (mask != null) { LossUtil.applyMask(gradients, mask); } return gradients; }
Example 13
Source File: ComplexNDArrayUtil.java From nd4j with Apache License 2.0 | 5 votes |
/** * Center an array * * @param arr the arr to center * @param shape the shape of the array * @return the center portion of the array based on the * specified shape */ public static IComplexNDArray center(IComplexNDArray arr, long[] shape) { if (arr.length() < ArrayUtil.prod(shape)) return arr; for (int i = 0; i < shape.length; i++) if (shape[i] < 1) shape[i] = 1; INDArray shapeMatrix = NDArrayUtil.toNDArray(shape); INDArray currShape = NDArrayUtil.toNDArray(arr.shape()); INDArray startIndex = Transforms.floor(currShape.sub(shapeMatrix).divi(Nd4j.scalar(2))); INDArray endIndex = startIndex.add(shapeMatrix); INDArrayIndex[] indexes = Indices.createFromStartAndEnd(startIndex, endIndex); if (shapeMatrix.length() > 1) return arr.get(indexes); else { IComplexNDArray ret = Nd4j.createComplex(new int[] {(int) shapeMatrix.getDouble(0)}); int start = (int) startIndex.getDouble(0); int end = (int) endIndex.getDouble(0); int count = 0; for (int i = start; i < end; i++) { ret.putScalar(count++, arr.getComplex(i)); } return ret; } }
Example 14
Source File: NDArrayTestsFortran.java From nd4j with Apache License 2.0 | 5 votes |
@Test public void testAddScalar() { INDArray div = Nd4j.valueArrayOf(new long[] {1, 4}, 4); INDArray rdiv = div.add(1); INDArray answer = Nd4j.valueArrayOf(new long[] {1, 4}, 5); assertEquals(answer, rdiv); }
Example 15
Source File: IntDataBufferTests.java From nd4j with Apache License 2.0 | 5 votes |
@Test(expected = ND4JIllegalStateException.class) public void testOpDiscarded() throws Exception { DataBuffer dataBuffer = Nd4j.createBuffer(new int[] {1, 2, 3, 4, 5}); DataBuffer shapeBuffer = Nd4j.getShapeInfoProvider().createShapeInformation(new int[] {1, 5}).getFirst(); INDArray intArray = Nd4j.createArrayFromShapeBuffer(dataBuffer, shapeBuffer); intArray.add(10f); }
Example 16
Source File: SameDiffTests.java From nd4j with Apache License 2.0 | 5 votes |
@Test public void testEvalVariable() { SameDiff sameDiff = SameDiff.create(); INDArray ones = Nd4j.ones(4); INDArray twos = ones.add(ones); SDVariable inputOne = sameDiff.var("inputone", ones); SDVariable inputResult = inputOne.add("extravarname", inputOne); assertEquals(twos, inputResult.eval()); }
Example 17
Source File: NativeOpExecutionerTest.java From nd4j with Apache License 2.0 | 5 votes |
@Test(expected = ND4JIllegalStateException.class) public void testMismatch() { INDArray y = Nd4j.create(100, 100); INDArray x = Nd4j.create(50, 50); x.add(1.0, y); }
Example 18
Source File: OperationProfilerTests.java From nd4j with Apache License 2.0 | 4 votes |
@Test public void testScopePanic3() throws Exception { Nd4j.getExecutioner().setProfilingMode(OpExecutioner.ProfilingMode.SCOPE_PANIC); INDArray array; try (MemoryWorkspace workspace = Nd4j.getWorkspaceManager().getAndActivateWorkspace("WS121")) { array = Nd4j.create(10); assertTrue(array.isAttached()); assertEquals(1, workspace.getGenerationId()); try (MemoryWorkspace workspaceInner = Nd4j.getWorkspaceManager().getAndActivateWorkspace("WS122")) { array.add(1.0); } } }
Example 19
Source File: TestSessions.java From deeplearning4j with Apache License 2.0 | 4 votes |
@Test public void testSwitchSimple(){ SameDiff sd = SameDiff.create(); SDVariable x = sd.placeHolder("x", DataType.FLOAT, 3,3); SDVariable b = sd.placeHolder("b", DataType.BOOL); SDVariable[] switchOut = sd.switchOp(x,b); //Order: false then true SDVariable falsePlusOne = switchOut[0].add("addFalseBranch", 1); SDVariable truePlusTen = switchOut[1].add("addTrueBranch", 10.0); SDVariable merge = sd.merge(falsePlusOne, truePlusTen); INDArray xArr = Nd4j.create(DataType.FLOAT, 3,3); INDArray bArr = Nd4j.scalar(true); INDArray expTrue = xArr.add(10.0); INDArray expFalse = xArr.add(1.0); Map<String,INDArray> m = new HashMap<>(); m.put("x", xArr); m.put("b", bArr); InferenceSession is = new InferenceSession(sd); String n = merge.name(); // System.out.println("----------------------------------"); Map<String,INDArray> outMap = is.output(Collections.singletonList(n), m, null, Collections.<String>emptyList(), null, At.defaultAt(Operation.TRAINING)); assertEquals(1, outMap.size()); assertEquals(expTrue, outMap.get(n)); // System.out.println("----------------------------------"); //Check false case: bArr.assign(0); is = new InferenceSession(sd); outMap = is.output(Collections.singletonList(n), m, null, Collections.<String>emptyList(), null, At.defaultAt(Operation.TRAINING)); assertEquals(1, outMap.size()); assertEquals(expFalse, outMap.get(n)); }
Example 20
Source File: OperationProfilerTests.java From deeplearning4j with Apache License 2.0 | 4 votes |
@Test(expected = ND4JIllegalStateException.class) public void testScopePanic2() { Nd4j.getExecutioner().setProfilingMode(OpExecutioner.ProfilingMode.SCOPE_PANIC); INDArray array; try (MemoryWorkspace workspace = Nd4j.getWorkspaceManager().getAndActivateWorkspace("WS120")) { array = Nd4j.create(10); assertTrue(array.isAttached()); assertEquals(1, workspace.getGenerationId()); } try (MemoryWorkspace workspace = Nd4j.getWorkspaceManager().getAndActivateWorkspace("WS120")) { assertEquals(2, workspace.getGenerationId()); array.add(1.0); assertTrue(array.isAttached()); } }