@Test
  public void testAdamUpdater() {
    INDArray m, v;
    double lr = 0.01;
    int iteration = 0;
    double beta1 = 0.8;
    double beta2 = 0.888;

    NeuralNetConfiguration conf =
        new NeuralNetConfiguration.Builder()
            .learningRate(lr)
            .iterations(iteration)
            .adamMeanDecay(beta1)
            .adamVarDecay(beta2)
            .layer(
                new DenseLayer.Builder()
                    .nIn(nIn)
                    .nOut(nOut)
                    .updater(org.deeplearning4j.nn.conf.Updater.ADAM)
                    .build())
            .build();

    int numParams = LayerFactories.getFactory(conf).initializer().numParams(conf, true);
    INDArray params = Nd4j.create(1, numParams);
    Layer layer = LayerFactories.getFactory(conf).create(conf, null, 0, params, true);
    Updater updater = UpdaterCreator.getUpdater(layer);
    int updaterStateSize = updater.stateSizeForLayer(layer);
    INDArray updaterState = Nd4j.create(1, updaterStateSize);
    updater.setStateViewArray(layer, updaterState, true);

    updater.update(layer, gradient, iteration, 1);

    double beta1t = FastMath.pow(beta1, iteration);
    double beta2t = FastMath.pow(beta2, iteration);
    double alphat = lr * FastMath.sqrt(1 - beta2t) / (1 - beta1t);
    if (Double.isNaN(alphat) || alphat == 0.0) alphat = epsilon;

    Gradient gradientDup = new DefaultGradient();
    gradientDup.setGradientFor(DefaultParamInitializer.WEIGHT_KEY, weightGradient);
    gradientDup.setGradientFor(DefaultParamInitializer.BIAS_KEY, biasGradient);

    for (Map.Entry<String, INDArray> entry : gradientDup.gradientForVariable().entrySet()) {
      val = entry.getValue();
      m = Nd4j.zeros(val.shape());
      v = Nd4j.zeros(val.shape());

      m.muli(beta1).addi(val.mul(1.0 - beta1));
      v.muli(beta2).addi(val.mul(val).mul(1.0 - beta2));
      gradExpected = m.mul(alphat).divi(Transforms.sqrt(v).addi(epsilon));
      if (!gradExpected.equals(gradient.getGradientFor(entry.getKey()))) {
        System.out.println(Arrays.toString(gradExpected.dup().data().asFloat()));
        System.out.println(
            Arrays.toString(gradient.getGradientFor(entry.getKey()).dup().data().asFloat()));
      }
      assertEquals(gradExpected, gradient.getGradientFor(entry.getKey()));
    }

    assertEquals(beta1, layer.conf().getLayer().getAdamMeanDecay(), 1e-4);
    assertEquals(beta2, layer.conf().getLayer().getAdamVarDecay(), 1e-4);
  }
Example #2
0
  @Test
  public void testDirSerializer() throws IOException {

    long[][] l = new long[16][];
    l[3] = new long[] {0, 0, 12, 13, 14, 0, Long.MAX_VALUE, 0};
    l[6] = new long[] {1, 2, 3, 4, 5, 6, 7, 8};

    DataOutput2 out = new DataOutput2();
    HTreeMap.DIR_SERIALIZER.serialize(out, l);

    DataInput2 in = swap(out);

    long[][] b = HTreeMap.DIR_SERIALIZER.deserialize(in, -1);

    assertEquals(null, b[0]);
    assertEquals(null, b[1]);
    assertEquals(null, b[2]);
    assertEquals(
        Arrays.toString(new long[] {0, 0, 12, 13, 14, 0, Long.MAX_VALUE, 0}),
        Arrays.toString(b[3]));
    assertEquals(null, b[4]);
    assertEquals(null, b[5]);
    assertEquals(Arrays.toString(new long[] {1, 2, 3, 4, 5, 6, 7, 8}), Arrays.toString(b[6]));
    assertEquals(null, b[7]);
  }
Example #3
0
 public TestFile file(Object... path) {
   try {
     return new TestFile(this, path);
   } catch (RuntimeException e) {
     throw new RuntimeException(
         String.format(
             "Could not locate file '%s' relative to '%s'.", Arrays.toString(path), this),
         e);
   }
 }
Example #4
0
  private static <C> void assertListsEqual(
      List<C> expectedList, List<C> actualList, Comparator<C> comparator, String name) {
    List<C> notFoundExpected = new ArrayList<C>();
    List<C> notFoundActual = new ArrayList<C>();
    for (C expected : expectedList) {
      boolean found = false;
      for (C actual : actualList) {
        if (comparator.compare(actual, expected) == 0) {
          found = true;
          break;
        }
      }
      if (!found) {
        notFoundExpected.add(expected);
      }
    }

    for (C actual : actualList) {
      boolean found = false;
      for (C expected : expectedList) {
        if (comparator.compare(actual, expected) == 0) {
          found = true;
          break;
        }
      }
      if (!found) {
        notFoundActual.add(actual);
      }
    }

    if (!notFoundExpected.isEmpty()) {
      fail("Not found expected " + name + " " + Arrays.toString(notFoundExpected.toArray()));
    }

    if (!notFoundActual.isEmpty()) {
      fail("Not expected " + name + " " + Arrays.toString(notFoundActual.toArray()));
    }
  }
Example #5
0
  @Test
  public void testMutualExclusion() {
    List<String[]> testList = q.getMutualExclusion();

    assertEquals("[testArg, testArg2]", Arrays.toString(testList.get(0)));
  }