|
17 | 17 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
18 | 18 | import static org.junit.jupiter.api.Assertions.assertNotNull;
|
19 | 19 |
|
| 20 | +import java.util.Arrays; |
20 | 21 | import org.junit.jupiter.api.Test;
|
21 | 22 | import org.tensorflow.op.Ops;
|
22 | 23 | import org.tensorflow.op.core.Init;
|
23 | 24 | import org.tensorflow.op.core.Placeholder;
|
24 | 25 | import org.tensorflow.op.math.Add;
|
25 | 26 | import org.tensorflow.op.math.Sub;
|
| 27 | +import org.tensorflow.proto.framework.DataType; |
26 | 28 | import org.tensorflow.types.TFloat32;
|
27 | 29 |
|
28 | 30 | public class ConcreteFunctionTest {
|
@@ -134,4 +136,54 @@ public void testNestedFunctionGraph() {
|
134 | 136 | }
|
135 | 137 | }
|
136 | 138 | }
|
| 139 | + |
| 140 | + private static Signature square(Ops tf) { |
| 141 | + Placeholder<TFloat32> input = tf.placeholder(TFloat32.class); |
| 142 | + Operand<TFloat32> output = tf.math.square(input); |
| 143 | + return Signature.builder().methodName("square").key("square").input("x", input).output("y", output).build(); |
| 144 | + } |
| 145 | + |
| 146 | + // call op gradients are not defined in c++ |
| 147 | +// @Test |
| 148 | + public void testGradientsGraph() { |
| 149 | + try (Graph g = new Graph(); |
| 150 | + ConcreteFunction square = ConcreteFunction.create(ConcreteFunctionTest::square); |
| 151 | + Session s = new Session(g)) { |
| 152 | + Ops tf = Ops.create(g); |
| 153 | + |
| 154 | + Output<TFloat32> x1 = tf.placeholder(TFloat32.class).output(); |
| 155 | + Output<TFloat32> x2 = tf.placeholder(TFloat32.class).output(); |
| 156 | + Output<TFloat32> y0 = (Output<TFloat32>) square.call(tf, x1); |
| 157 | + Output<TFloat32> y1 = (Output<TFloat32>) square.call(tf, y0); |
| 158 | + Output<TFloat32> y2 = tf.math.addN(Arrays.asList(y0, x2)).sum(); |
| 159 | + |
| 160 | + Output<?>[] grads0 = g.addGradients(y1, new Output[]{x1}); |
| 161 | + assertNotNull(grads0); |
| 162 | + assertEquals(1, grads0.length); |
| 163 | + assertEquals(DataType.DT_FLOAT, grads0[0].dataType()); |
| 164 | + |
| 165 | + Output<?>[] grads1 = g.addGradients(y2, new Output[]{x1, x2}); |
| 166 | + assertNotNull(grads1); |
| 167 | + assertEquals(2, grads1.length); |
| 168 | + assertEquals(DataType.DT_FLOAT, grads1[0].dataType()); |
| 169 | + assertEquals(DataType.DT_FLOAT, grads1[1].dataType()); |
| 170 | + |
| 171 | + try (TFloat32 c1 = TFloat32.scalarOf(3.0f); |
| 172 | + TFloat32 c2 = TFloat32.scalarOf(2.0f); |
| 173 | + AutoCloseableList<Tensor> outputs = new AutoCloseableList<>( |
| 174 | + s.runner() |
| 175 | + .feed(x1, c1) |
| 176 | + .feed(x2, c2) |
| 177 | + .fetch(grads0[0]) |
| 178 | + .fetch(grads1[0]) |
| 179 | + .fetch(grads1[1]) |
| 180 | + .run())) { |
| 181 | + |
| 182 | + assertEquals(3, outputs.size()); |
| 183 | + assertEquals(108.0f, ((TFloat32) outputs.get(0)).getFloat(), 0.0f); |
| 184 | + assertEquals(6.0f, ((TFloat32) outputs.get(1)).getFloat(), 0.0f); |
| 185 | + assertEquals(1.0f, ((TFloat32) outputs.get(2)).getFloat(), 0.0f); |
| 186 | + } |
| 187 | + } |
| 188 | + } |
137 | 189 | }
|
0 commit comments