/* 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.initializers;

import org.junit.jupiter.api.Test;
import org.tensorflow.Operand;
import org.tensorflow.framework.initializers.VarianceScaling.Distribution;
import org.tensorflow.framework.utils.TestSession;
import org.tensorflow.ndarray.Shape;
import org.tensorflow.op.Ops;
import org.tensorflow.types.TFloat32;
import org.tensorflow.types.TFloat64;

/** Test the He initializer */
public class HeTest {
  private static final long SEED = 1000L;
  private final TestSession.Mode[] tfModes = {TestSession.Mode.EAGER, TestSession.Mode.GRAPH};
  int counter;

  /** Test of call method, of class He. */
  @Test
  public void testCallNormalFloat() {
    float[] expected = {-0.7408917F, -0.41477704F, -0.11133519F, -0.45044965F};
    for (TestSession.Mode tfMode : tfModes)
      try (TestSession session = TestSession.createTestSession(tfMode)) {
        Ops tf = session.getTF();
        Shape shape = Shape.of(2, 2);
        He<TFloat32> instance = new He<>(Distribution.TRUNCATED_NORMAL, SEED);

        Operand<TFloat32> operand = instance.call(tf, tf.constant(shape), TFloat32.class);
        session.evaluate(expected, operand);
      }
  }

  @Test
  public void testCallNormalDouble() {
    double[] expected = {
      2.117256561466521, -1.7661437620712939, -0.7650439080001085, 0.6889186518780481
    };
    for (TestSession.Mode tfMode : tfModes)
      try (TestSession session = TestSession.createTestSession(tfMode)) {
        Ops tf = session.getTF();
        Shape shape = Shape.of(2, 2);
        He<TFloat64> instance = new He<>(Distribution.TRUNCATED_NORMAL, SEED);

        Operand<TFloat64> operand = instance.call(tf, tf.constant(shape), TFloat64.class);
        session.evaluate(expected, operand);
      }
  }

  /** Test of call method, of class He. */
  @Test
  public void testCallFloat() {
    float[] expected = {1.3104724f, 1.1583493f, 1.5936272f, 0.93282115f};
    for (TestSession.Mode tfMode : tfModes)
      try (TestSession session = TestSession.createTestSession(tfMode)) {
        Ops tf = session.getTF();
        Shape shape = Shape.of(2, 2);
        He<TFloat32> instance = new He<>(Distribution.UNIFORM, SEED);

        Operand<TFloat32> operand = instance.call(tf, tf.constant(shape), TFloat32.class);
        session.evaluate(expected, operand);
      }
  }

  @Test
  public void testCallDouble() {
    double[] expected = {
      .09147407402970674, 0.6246627788317102, 0.09490870950065552, 0.8879452169740599
    };
    for (TestSession.Mode tfMode : tfModes)
      try (TestSession session = TestSession.createTestSession(tfMode)) {
        Ops tf = session.getTF();
        Shape shape = Shape.of(2, 2);
        He<TFloat64> instance = new He<>(Distribution.UNIFORM, SEED);

        Operand<TFloat64> operand = instance.call(tf, tf.constant(shape), TFloat64.class);
        session.evaluate(expected, operand);
      }
  }

  @Test
  public void testCallNormalReproducible() {
    for (TestSession.Mode tfMode : tfModes)
      try (TestSession session = TestSession.createTestSession(tfMode)) {
        Ops tf = session.getTF();
        Shape shape = Shape.of(2, 2);
        He<TFloat64> instance = new He<>(Distribution.TRUNCATED_NORMAL, SEED);

        Operand<TFloat64> operand1 = instance.call(tf, tf.constant(shape), TFloat64.class);
        Operand<TFloat64> operand2 = instance.call(tf, tf.constant(shape), TFloat64.class);
        session.evaluate(operand1, operand2);
      }
  }

  @Test
  public void testCallUniformReproducible() {
    for (TestSession.Mode tfMode : tfModes)
      try (TestSession session = TestSession.createTestSession(tfMode)) {
        Ops tf = session.getTF();
        Shape shape = Shape.of(2, 2);
        He<TFloat64> instance = new He<>(Distribution.UNIFORM, SEED);

        Operand<TFloat64> operand1 = instance.call(tf, tf.constant(shape), TFloat64.class);
        Operand<TFloat64> operand2 = instance.call(tf, tf.constant(shape), TFloat64.class);
        session.evaluate(operand1, operand2);
      }
  }

  @Test
  public void testCallNORMALReproducible() {
    for (TestSession.Mode tfMode : tfModes)
      try (TestSession session = TestSession.createTestSession(tfMode)) {
        Ops tf = session.getTF();
        Shape shape = Shape.of(2, 2);
        He<TFloat64> instance = new He<>(Distribution.NORMAL, SEED);

        Operand<TFloat64> operand1 = instance.call(tf, tf.constant(shape), TFloat64.class);
        Operand<TFloat64> operand2 = instance.call(tf, tf.constant(shape), TFloat64.class);
        session.evaluate(operand1, operand2);
      }
  }
}
