• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 package org.tensorflow.op.core;
17 
18 import static org.junit.Assert.assertEquals;
19 
20 import java.util.ArrayList;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.junit.runners.JUnit4;
24 import org.tensorflow.Graph;
25 import org.tensorflow.Operand;
26 import org.tensorflow.Session;
27 import org.tensorflow.Tensor;
28 import org.tensorflow.Shape;
29 import org.tensorflow.op.Ops;
30 
31 @RunWith(JUnit4.class)
32 public final class GeneratedOperationsTest {
33 
34   @Test
tensorInputTensorOutput()35   public void tensorInputTensorOutput() {
36     try (Graph g = new Graph();
37         Session sess = new Session(g)) {
38       Ops ops = Ops.create(g);
39       Operand<Integer> x = ops.math().add(ops.constant(1), ops.constant(2));
40       try (Tensor<Integer> result = sess.runner().fetch(x).run().get(0).expect(Integer.class)) {
41         assertEquals(3, result.intValue());
42       }
43     }
44   }
45 
46   @Test
testListInputTensorOutput()47   public void testListInputTensorOutput() {
48     try (Graph g = new Graph();
49         Session sess = new Session(g)) {
50       Ops ops = Ops.create(g);
51       ArrayList<Operand<Integer>> inputs = new ArrayList<Operand<Integer>>();
52       inputs.add(ops.constant(1));
53       inputs.add(ops.constant(2));
54       inputs.add(ops.constant(3));
55       Operand<Integer> x = ops.math().addN(inputs);
56       try (Tensor<Integer> result = sess.runner().fetch(x).run().get(0).expect(Integer.class)) {
57         assertEquals(6, result.intValue());
58       }
59     }
60   }
61 
62   /**
63    * Test for Ops.withControlDependencies.
64    *
65    * <p>Creates an add node with a control dependency to an assign node. In other words, the assign
66    * node is a control input to the add node. When the add node is run, the assign node is expected
67    * to have run beforehand due to the control dependency.
68    */
69   @Test
testControlDependencies()70   public void testControlDependencies() {
71     try (Graph g = new Graph();
72         Session sess = new Session(g)) {
73       Ops ops = Ops.create(g);
74       Operand<Integer> variable = ops.variable(Shape.scalar(), Integer.class);
75       Operand<?> initVariable = ops.assign(variable, ops.constant(0));
76       ArrayList<Operand<?>> controls = new ArrayList<Operand<?>>();
77       controls.add(ops.assign(variable, ops.constant(3)));
78       Operand<Integer> x =
79           ops.withControlDependencies(controls).math().add(variable, ops.constant(0));
80       sess.runner().addTarget(initVariable).run();
81       try (Tensor<Integer> result = sess.runner().fetch(x).run().get(0).expect(Integer.class); ) {
82         assertEquals(3, result.intValue());
83       }
84     }
85   }
86 }
87