package hex.deeplearning;

import java.util.Arrays;
import java.util.Random;

import junit.framework.Assert;

import org.junit.Test;

public class DropoutTest {

  @Test
  public void test() throws Exception {
    final int units = 1000;
    float[] a = new float[units];
    double sum1=0, sum2=0, sum3=0, sum4=0;

    final int loops = 10000;
    for (int l = 0; l < loops; ++l) {
      long seed = new Random().nextLong();

      Dropout d = new Dropout(units, 0.3);
      Arrays.fill(a, 1f);
      d.randomlySparsifyActivation(a, seed);
      sum1 += water.util.Utils.sum(a);

      d = new Dropout(units, 0.0);
      Arrays.fill(a, 1f);
      d.randomlySparsifyActivation(a, seed + 1);
      sum2 += water.util.Utils.sum(a);

      d = new Dropout(units, 1.0);
      Arrays.fill(a, 1f);
      d.randomlySparsifyActivation(a, seed + 2);
      sum3 += water.util.Utils.sum(a);

      d = new Dropout(units, 0.314);
      d.fillBytes(seed+3);
//      Log.info("loop: " + l + " sum4: " + sum4);
      for (int i=0; i<units; ++i) {
        if (d.unit_active(i)) {
          sum4++;
          assert(d.unit_active(i));
        }
        else assert(!d.unit_active(i));
      }
//      Log.info(d.toString());
    }
    sum1 /= loops;
    sum2 /= loops;
    sum3 /= loops;
    sum4 /= loops;
    Assert.assertTrue(Math.abs(sum1-700)<1);
    Assert.assertTrue(sum2 == units);
    Assert.assertTrue(sum3 == 0);
    Assert.assertTrue(Math.abs(sum4-686)<1);
  }

}

@@ -32,104 +32,103 @@
 rt j
-ava.util.Arrays;%0Aimport java.util.Random;%0A%0Aimport junit.framework.Assert
+unit.framework.Assert;%0Aimport org.junit.Test;%0A%0Aimport java.util.Arrays
 ;%0A
-%0A
 import 
-org.junit.Test
+java.util.Random
 ;%0A%0Ap
@@ -234,40 +234,67 @@
 00;%0A
+%0A
     
-float%5B%5D a = new float%5B
+Neurons.DenseVector a = new Neurons.DenseVector(
 units
-%5D
+)
 ;%0A  
@@ -500,32 +500,38 @@
    Arrays.fill(a
+.raw()
 , 1f);%0A      d.r
@@ -594,32 +594,38 @@
 util.Utils.sum(a
+.raw()
 );%0A%0A      d = ne
@@ -658,32 +658,38 @@
    Arrays.fill(a
+.raw()
 , 1f);%0A      d.r
@@ -756,32 +756,38 @@
 util.Utils.sum(a
+.raw()
 );%0A%0A      d = ne
@@ -828,16 +828,22 @@
 s.fill(a
+.raw()
 , 1f);%0A 
@@ -926,16 +926,22 @@
 ls.sum(a
+.raw()
 );%0A%0A    

Fix Dropout Test to use new Sparsify.