aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/java/src/test/java/org/tensorflow/op/core/GradientsTest.java
blob: 2ffc69c20929880e3b5ab6103a664b26c4af089a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package org.tensorflow.op.core;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.Arrays;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.tensorflow.Graph;
import org.tensorflow.Output;
import org.tensorflow.Session;
import org.tensorflow.Tensor;
import org.tensorflow.Tensors;
import org.tensorflow.TestUtil;
import org.tensorflow.op.Scope;

@RunWith(JUnit4.class)
public class GradientsTest {

  @Test
  public void createGradients() {
    try (Graph g = new Graph(); 
        Session sess = new Session(g)) {
      Scope scope = new Scope(g);

      Output<Float> x = TestUtil.placeholder(g, "x1", Float.class);
      Output<Float> y0 = TestUtil.square(g, "y0", x);
      Output<Float> y1 = TestUtil.square(g, "y1", y0);
      
      Gradients grads = Gradients.create(scope, y1, Arrays.asList(x, y0));
      
      assertNotNull(grads);
      assertNotNull(grads.dy());
      assertEquals(2, grads.dy().size());

      try (Tensor<Float> c = Tensors.create(3.0f);
          TestUtil.AutoCloseableList<Tensor<?>> outputs = new TestUtil.AutoCloseableList<>(
              sess.runner()
                  .feed(x, c)
                  .fetch(grads.dy(0))
                  .fetch(grads.dy(1))
                  .run())) {
     
        assertEquals(108.0f, outputs.get(0).floatValue(), 0.0f);
        assertEquals(18.0f, outputs.get(1).floatValue(), 0.0f);
      }
    }
  }

  @Test
  public void createGradientsWithSum() {
    try (Graph g = new Graph();
        Session sess = new Session(g)) {
      Scope scope = new Scope(g);

      Output<Float> x = TestUtil.placeholder(g, "x1", Float.class);
      Output<Float> y0 = TestUtil.square(g, "y0", x);
      Output<Float> y1 = TestUtil.square(g, "y1", y0);
      
      Gradients grads = Gradients.create(scope, Arrays.asList(y0, y1), Arrays.asList(x));
      
      assertNotNull(grads);
      assertNotNull(grads.dy());
      assertEquals(1, grads.dy().size());

      try (Tensor<Float> c = Tensors.create(3.0f);
          TestUtil.AutoCloseableList<Tensor<?>> outputs = new TestUtil.AutoCloseableList<>(
              sess.runner()
                  .feed(x, c)
                  .fetch(grads.dy(0))
                  .run())) {
     
        assertEquals(114.0f, outputs.get(0).floatValue(), 0.0f);
      }
    }
  }

  @Test
  public void createGradientsWithInitialValues() {
    try (Graph g = new Graph();
        Session sess = new Session(g)) {
      Scope scope = new Scope(g);

      Output<Float> x = TestUtil.placeholder(g, "x1", Float.class);
      Output<Float> y0 = TestUtil.square(g, "y0", x);
      Output<Float> y1 = TestUtil.square(g, "y1", y0);
      
      Gradients grads0 = Gradients.create(scope, y1, Arrays.asList(y0));
      Gradients grads1 = Gradients.create(scope, y0, Arrays.asList(x), Gradients.dx(grads0.dy()));
      
      assertNotNull(grads1);
      assertNotNull(grads1.dy());
      assertEquals(1, grads1.dy().size());

      try (Tensor<Float> c = Tensors.create(3.0f);
          TestUtil.AutoCloseableList<Tensor<?>> outputs = new TestUtil.AutoCloseableList<>(
              sess.runner()
                  .feed(x, c)
                  .fetch(grads1.dy(0))
                  .run())) {
     
        assertEquals(108.0f, outputs.get(0).floatValue(), 0.0f);
      }
    }
  }

  @Test
  public void createGradientsWithScopeName() {
    try (Graph g = new Graph()) {
      Scope scope = new Scope(g);

      Output<Float> x = TestUtil.placeholder(g, "x1", Float.class);
      Output<Float> y = TestUtil.square(g, "y", x);
      
      Scope gradScope = scope.withSubScope("grads").withSubScope("test");
      Gradients grads = Gradients.create(gradScope, y, Arrays.asList(x));
      
      assertTrue(grads.dy(0).op().name().startsWith("grads/test/"));
    }
  }
}