/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=======================================================================*/
package org.tensorflow.framework.optimizers;

import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.*;
import org.tensorflow.Graph;
import org.tensorflow.framework.utils.TestSession;
import org.tensorflow.ndarray.Shape;
import org.tensorflow.op.Op;
import org.tensorflow.op.Ops;
import org.tensorflow.op.core.Constant;
import org.tensorflow.op.core.Variable;
import org.tensorflow.types.TFloat32;
import org.tensorflow.types.family.TType;

/** Test cases for AdaGradDA Optimizer */
public class AdaGradDATest {

  private final TestSession.Mode tfMode = TestSession.Mode.GRAPH;

  public AdaGradDATest() {}

  @BeforeAll
  public static void setUpClass() {}

  @AfterAll
  public static void tearDownClass() {}

  @BeforeEach
  public void setUp() {}

  @AfterEach
  public void tearDown() {}

  @Test
  public void testBasic() {
    float[] var0Init = {0.0F, 0.0F};
    float[] var1Init = {0.0F, 0.0F};
    float[] grads0Init = {0.1F, 0.2F};
    float[] grads1Init = {0.01F, 0.02F};
    try (TestSession session = TestSession.createTestSession(tfMode)) {
      Graph graph = session.getGraph();

      float learningRate = 3.0F;

      AdaGradDA instance = new AdaGradDA(graph, learningRate);
      Ops tf = instance.getTF();

      Shape shape0 = Shape.of(var0Init.length);
      Shape shape1 = Shape.of(var1Init.length);
      Variable<TFloat32> var0 = tf.withName("var0").variable(shape0, TFloat32.class);
      Variable<TFloat32> var1 = tf.withName("var1").variable(shape1, TFloat32.class);

      tf.withInitScope().assign(var0, tf.constant(var0Init));
      tf.withInitScope().assign(var1, tf.constant(var1Init));

      Constant<TFloat32> grads0 = tf.constant(grads0Init);
      Constant<TFloat32> grads1 = tf.constant(grads1Init);

      /* build the GradsAnvVars */
      List<Optimizer.GradAndVar<? extends TType>> gradsAndVars = new ArrayList<>();
      gradsAndVars.add(new Optimizer.GradAndVar<>(grads0.asOutput(), var0.asOutput()));
      gradsAndVars.add(new Optimizer.GradAndVar<>(grads1.asOutput(), var1.asOutput()));

      Op adaUpdate = instance.applyGradients(gradsAndVars, "AdGradDATest");

      /* initialize the accumulators */
      session.initialize();

      session.evaluate(var0Init, var0);
      session.evaluate(var1Init, var1);
      session.run(adaUpdate);
      float[] expected0 = {-0.904534F, -1.603567F};
      session.evaluate(expected0, var0);
      float[] expected1 = {-0.094821f, -0.189358f};
      session.evaluate(expected1, var1);
    }
  }
}
