Java Code Examples for org.nd4j.linalg.factory.Nd4j#ones()
The following examples show how to use
org.nd4j.linalg.factory.Nd4j#ones() .
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: CustomOpTests.java From deeplearning4j with Apache License 2.0 | 6 votes |
@Test public void testResizeBilinearEdgeCase(){ INDArray in = Nd4j.ones(DataType.FLOAT, 1, 1, 1, 3); INDArray size = Nd4j.createFromArray(8, 8); INDArray out = Nd4j.create(DataType.FLOAT, 1, 8, 8, 3); DynamicCustomOp op = DynamicCustomOp.builder("resize_bilinear") .addInputs(in, size) .addOutputs(out) .addIntegerArguments(1) //1 = center. Though TF works with align_corners == false or true .build(); Nd4j.getExecutioner().exec(op); INDArray exp = Nd4j.ones(DataType.FLOAT, 1, 8, 8, 3); assertEquals(exp, out); }
Example 2
Source File: ComputationGraphTestRNN.java From deeplearning4j with Apache License 2.0 | 6 votes |
@Test public void testTbpttMasking() { //Simple "does it throw an exception" type test... ComputationGraphConfiguration conf = new NeuralNetConfiguration.Builder().seed(12345) .graphBuilder().addInputs("in") .addLayer("out", new RnnOutputLayer.Builder(LossFunctions.LossFunction.MSE) .activation(Activation.IDENTITY).nIn(1).nOut(1).build(), "in") .setOutputs("out").backpropType(BackpropType.TruncatedBPTT).tBPTTForwardLength(8) .tBPTTBackwardLength(8).build(); ComputationGraph net = new ComputationGraph(conf); net.init(); MultiDataSet data = new MultiDataSet(new INDArray[] {Nd4j.linspace(1, 10, 10, Nd4j.dataType()).reshape(1, 1, 10)}, new INDArray[] {Nd4j.linspace(2, 20, 10, Nd4j.dataType()).reshape(1, 1, 10)}, null, new INDArray[] {Nd4j.ones(1, 10)}); net.fit(data); }
Example 3
Source File: AggregatesTests.java From nd4j with Apache License 2.0 | 6 votes |
@Test public void testBatchedAggregate1() throws Exception { INDArray arrayX1 = Nd4j.ones(10); INDArray arrayY1 = Nd4j.zeros(10); INDArray arrayX2 = Nd4j.ones(10); INDArray arrayY2 = Nd4j.zeros(10); INDArray exp1 = Nd4j.create(10).assign(1f); INDArray exp2 = Nd4j.create(10).assign(1f); AggregateAxpy axpy1 = new AggregateAxpy(arrayX1, arrayY1, 1.0f); AggregateAxpy axpy2 = new AggregateAxpy(arrayX2, arrayY2, 1.0f); List<Aggregate> batch = new ArrayList<>(); batch.add(axpy1); batch.add(axpy2); Nd4j.getExecutioner().exec(batch); assertEquals(exp1, arrayY1); assertEquals(exp2, arrayY2); }
Example 4
Source File: NativeOpExecutionerTest.java From nd4j with Apache License 2.0 | 6 votes |
@Test public void execBroadcastOp() throws Exception { INDArray array = Nd4j.ones(1024, 1024); INDArray arrayRow = Nd4j.linspace(1, 1024, 1024); float sum = (float) array.sumNumber().doubleValue(); array.addiRowVector(arrayRow); long time1 = System.nanoTime(); for (int x = 0; x < 100000; x++) { array.addiRowVector(arrayRow); } long time2 = System.nanoTime(); /* time1 = System.nanoTime(); array.addiRowVector(arrayRow); time2 = System.nanoTime(); */ System.out.println("Execution time: " + ((time2 - time1) / 100000) ); assertEquals(1002, array.getFloat(0), 0.1f); assertEquals(2003, array.getFloat(1), 0.1f); }
Example 5
Source File: EndlessTests.java From nd4j with Apache License 2.0 | 5 votes |
@Test public void testAccumForeverAlongDimension(){ INDArray arr = Nd4j.ones(100,100); for (int i = 0; i < RUN_LIMIT; i++ ) { arr.sum(0); } }
Example 6
Source File: DummyTransportTest.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testUpdatesPropagation_1() throws Exception { val counter = new AtomicInteger(0); val connector = new DummyTransport.Connector(); val transportA = new DummyTransport("alpha", connector); val transportB = new DummyTransport("beta", connector); val transportG = new DummyTransport("gamma", connector); val transportD = new DummyTransport("delta", connector); connector.register(transportA, transportB, transportG, transportD); transportB.sendMessage(new HandshakeRequest(), "alpha"); transportG.sendMessage(new HandshakeRequest(), "alpha"); transportD.sendMessage(new HandshakeRequest(), "alpha"); val f = new MessageCallable<GradientsUpdateMessage>() { @Override public void apply(GradientsUpdateMessage message) { val update = message.getPayload(); counter.addAndGet(update.sumNumber().intValue()); } }; transportA.addPrecursor(GradientsUpdateMessage.class, f); transportB.addPrecursor(GradientsUpdateMessage.class, f); transportG.addPrecursor(GradientsUpdateMessage.class, f); transportD.addPrecursor(GradientsUpdateMessage.class, f); val array = Nd4j.ones(10, 10); val msg = new GradientsUpdateMessage("message", array); msg.setOriginatorId("beta"); transportB.propagateMessage(msg, PropagationMode.BOTH_WAYS); // we expect that each of the nodes gets this message assertEquals(400, counter.get()); }
Example 7
Source File: TensorFlowImportTest.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testTensorArray_119_1() throws Exception { val tg = TFGraphMapper.importGraph(new ClassPathResource("tf_graphs/tensor_array.pb.txt").getInputStream()); assertNotNull(tg); val input_matrix = Nd4j.ones(3, 2); val array = tg.outputSingle(Collections.singletonMap("input_matrix", input_matrix), tg.outputs().get(0)); val exp = Nd4j.create(new float[] {1, 1, 2, 2, 3, 3}, new int[]{3, 2}); assertEquals(exp, array); }
Example 8
Source File: ConcatTests.java From nd4j with Apache License 2.0 | 5 votes |
@Test public void testConcatHorizontally() { INDArray rowVector = Nd4j.ones(5); INDArray other = Nd4j.ones(5); INDArray concat = Nd4j.hstack(other, rowVector); assertEquals(rowVector.rows(), concat.rows()); assertEquals(rowVector.columns() * 2, concat.columns()); }
Example 9
Source File: SpecialTests.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testScalarShuffle2() { List<DataSet> listData = new ArrayList<>(); for (int i = 0; i < 3; i++) { INDArray features = Nd4j.ones(14, 25); INDArray label = Nd4j.create(14, 50); DataSet dataset = new DataSet(features, label); listData.add(dataset); } DataSet data = DataSet.merge(listData); data.shuffle(); }
Example 10
Source File: OpExecutionerTests.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testComparisonOps() { INDArray linspace = Nd4j.linspace(1, 6, 6, DataType.DOUBLE); INDArray ones = Nd4j.ones(DataType.BOOL, 6); INDArray zeros = Nd4j.zeros(DataType.BOOL, 6); INDArray res = Nd4j.createUninitialized(DataType.BOOL, 6); assertEquals(ones, Nd4j.getExecutioner().exec(new ScalarGreaterThan(linspace, res,0))); assertEquals(zeros, Nd4j.getExecutioner().exec(new ScalarGreaterThan(linspace, res, 7))); assertEquals(zeros, Nd4j.getExecutioner().exec(new ScalarLessThan(linspace, res, 0))); assertEquals(ones, Nd4j.getExecutioner().exec(new ScalarLessThan(linspace, res,7))); }
Example 11
Source File: BooleanIndexingTest.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testMatchConditionAlongDimension1() { INDArray array = Nd4j.ones(3, 10); array.getRow(2).assign(0.0); boolean result[] = BooleanIndexing.and(array, Conditions.equals(0.0), 1); boolean comp[] = new boolean[] {false, false, true}; // System.out.println("Result: " + Arrays.toString(result)); assertArrayEquals(comp, result); }
Example 12
Source File: EndlessTests.java From nd4j with Apache License 2.0 | 5 votes |
@Test public void testTransformsForeverSingle2(){ INDArray arr = Nd4j.ones(100,100); for (int i = 0; i < RUN_LIMIT; i++ ) { Nd4j.getExecutioner().exec(new OldSoftMax(arr)); } }
Example 13
Source File: CustomOpsTests.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testOnesLike_1() { val x = Nd4j.create(DataType.FLOAT, 3, 4, 5); val e = Nd4j.ones(DataType.INT32, 3, 4, 5); val z = Nd4j.exec(new OnesLike(x, DataType.INT32))[0]; assertEquals(e, z); }
Example 14
Source File: EndlessTests.java From nd4j with Apache License 2.0 | 5 votes |
@Test public void testReduce3AlongDim(){ INDArray first = Nd4j.ones(10,10); INDArray second = Nd4j.ones(10,10); for (int i = 0; i < RUN_LIMIT; i++ ) { Nd4j.getExecutioner().exec(new CosineSimilarity(first,second),0); } }
Example 15
Source File: NativeOpExecutionerTest.java From nd4j with Apache License 2.0 | 5 votes |
@Test public void testConditionalUpdate() { INDArray arr = Nd4j.linspace(-2, 2, 5); INDArray ones = Nd4j.ones(5); System.out.println("arr: " + arr); System.out.println("ones: " + ones); Nd4j.getExecutioner().exec(new CompareAndSet(ones, arr, ones, Conditions.equals(0.0))); System.out.println("After:"); System.out.println("arr: " + arr); System.out.println("ones: " + ones); }
Example 16
Source File: NDArrayTestsFortran.java From deeplearning4j with Apache License 2.0 | 4 votes |
@Test public void testOneTensor() { INDArray arr = Nd4j.ones(1, 1, 1, 1, 1, 1, 1); INDArray matrixToBroadcast = Nd4j.ones(1, 1); assertEquals(matrixToBroadcast.broadcast(arr.shape()), arr); }
Example 17
Source File: AttentionLayerTest.java From deeplearning4j with Apache License 2.0 | 4 votes |
@Test public void testLearnedSelfAttentionLayer() { int nIn = 3; int nOut = 2; int tsLength = 4; int layerSize = 4; int numQueries = 3; for (boolean inputMask : new boolean[]{false, true}) { for (int mb : new int[]{3, 1}) { for (boolean projectInput : new boolean[]{false, true}) { INDArray in = Nd4j.rand(DataType.DOUBLE, new int[]{mb, nIn, tsLength}); INDArray labels = TestUtils.randomOneHot(mb, nOut); String maskType = (inputMask ? "inputMask" : "none"); INDArray inMask = null; if (inputMask) { inMask = Nd4j.ones(mb, tsLength); for (int i = 0; i < mb; i++) { int firstMaskedStep = tsLength - 1 - i; if (firstMaskedStep == 0) { firstMaskedStep = tsLength; } for (int j = firstMaskedStep; j < tsLength; j++) { inMask.putScalar(i, j, 0.0); } } } String name = "testLearnedSelfAttentionLayer() - mb=" + mb + ", tsLength = " + tsLength + ", maskType=" + maskType + ", projectInput = " + projectInput; System.out.println("Starting test: " + name); MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder() .dataType(DataType.DOUBLE) .activation(Activation.TANH) .updater(new NoOp()) .weightInit(WeightInit.XAVIER) .list() .layer(new LSTM.Builder().nOut(layerSize).build()) .layer( projectInput ? new LearnedSelfAttentionLayer.Builder().nOut(4).nHeads(2).nQueries(numQueries).projectInput(true).build() : new LearnedSelfAttentionLayer.Builder().nHeads(1).nQueries(numQueries).projectInput(false).build() ) .layer(new GlobalPoolingLayer.Builder().poolingType(PoolingType.MAX).build()) .layer(new OutputLayer.Builder().nOut(nOut).activation(Activation.SOFTMAX) .lossFunction(LossFunctions.LossFunction.MCXENT).build()) .setInputType(InputType.recurrent(nIn)) .build(); MultiLayerNetwork net = new MultiLayerNetwork(conf); net.init(); boolean gradOK = GradientCheckUtil.checkGradients(new GradientCheckUtil.MLNConfig().net(net).input(in) .labels(labels).inputMask(inMask).subset(true).maxPerParam(100)); assertTrue(name, gradOK); } } } }
Example 18
Source File: CompressionTests.java From nd4j with Apache License 2.0 | 4 votes |
@Test public void testThresholdCompression5() throws Exception { INDArray initial = Nd4j.ones(1000); INDArray exp_0 = initial.dup(); Nd4j.getExecutioner().commit(); //Nd4j.getCompressor().getCompressor("THRESHOLD").configure(1e-3); INDArray compressed = Nd4j.getExecutioner().thresholdEncode(initial, 1.0f, 100); assertEquals(104, compressed.data().length()); assertNotEquals(exp_0, initial); assertEquals(900, initial.sumNumber().doubleValue(), 0.01); }
Example 19
Source File: SporadicTests.java From nd4j with Apache License 2.0 | 4 votes |
@Test public void testReplicate3() throws Exception { INDArray array = Nd4j.ones(10, 10); INDArray exp = Nd4j.create(10).assign(10f); log.error("Array length: {}", array.length()); int numDevices = Nd4j.getAffinityManager().getNumberOfDevices(); final DeviceLocalNDArray locals = new DeviceLocalNDArray(array); Thread[] threads = new Thread[numDevices]; for (int t = 0; t < numDevices; t++) { threads[t] = new Thread(new Runnable() { @Override public void run() { AllocationPoint point = AtomicAllocator.getInstance().getAllocationPoint(locals.get()); log.error("Point deviceId: {}; current deviceId: {}", point.getDeviceId(), Nd4j.getAffinityManager().getDeviceForCurrentThread()); INDArray sum = locals.get().sum(1); INDArray localExp = Nd4j.create(10).assign(10f); assertEquals(localExp, sum); } }); threads[t].start(); } for (int t = 0; t < numDevices; t++) { threads[t].join(); } for (int t = 0; t < numDevices; t++) { AllocationPoint point = AtomicAllocator.getInstance().getAllocationPoint(locals.get(t)); log.error("Point deviceId: {}; current deviceId: {}", point.getDeviceId(), Nd4j.getAffinityManager().getDeviceForCurrentThread()); exp.addi(0.0f); assertEquals(exp, locals.get(t).sum(0)); log.error("Point after: {}", point.getDeviceId()); } }
Example 20
Source File: DataSetUtil.java From deeplearning4j with Apache License 2.0 | 4 votes |
public static INDArray mergeMasks4d(INDArray[] featuresOrLabels, INDArray[] masks) { long[] outShape = null; long mbCountNoMask = 0; for (int i = 0; i < masks.length; i++) { if(masks[i] == null) { mbCountNoMask += featuresOrLabels[i].size(0); continue; } if(masks[i].rank() != 4) throw new IllegalStateException("Cannot merge mask arrays: expected mask array of rank 4. Got mask array of rank " + masks[i].rank() + " with shape " + Arrays.toString(masks[i].shape())); if(outShape == null) outShape = masks[i].shape().clone(); else { INDArray m = masks[i]; if(m.size(1) != outShape[1] || m.size(2) != outShape[2] || m.size(3) != outShape[3]){ throw new IllegalStateException("Mismatched mask shapes: masks should have same depth/height/width for all examples." + " Prior examples had shape [mb," + masks[1] + "," + masks[2] + "," + masks[3] + "], next example has shape " + Arrays.toString(m.shape())); } outShape[0] += m.size(0); } } if(outShape == null) return null; //No masks to merge outShape[0] += mbCountNoMask; INDArray outMask = Nd4j.ones(outShape); //Initialize to 'all present' (1s) int exSoFar = 0; for (int i = 0; i < masks.length; i++) { if (masks[i] == null) { exSoFar += featuresOrLabels[i].size(0); continue; } long nEx = masks[i].size(0); outMask.put(new INDArrayIndex[] {NDArrayIndex.interval(exSoFar, exSoFar + nEx), NDArrayIndex.all()}, masks[i]); exSoFar += nEx; } return outMask; }