Home | History | Annotate | Download | only in gradients
      1 /* Copyright 2017 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 #include "tensorflow/cc/framework/grad_op_registry.h"
     17 #include "tensorflow/cc/framework/gradient_checker.h"
     18 #include "tensorflow/cc/framework/testutil.h"
     19 #include "tensorflow/cc/gradients/grad_testutil.h"
     20 #include "tensorflow/cc/ops/standard_ops.h"
     21 #include "tensorflow/core/framework/tensor_testutil.h"
     22 #include "tensorflow/core/lib/core/status_test_util.h"
     23 #include "tensorflow/core/lib/random/random.h"
     24 
     25 namespace tensorflow {
     26 namespace {
     27 
     28 using ops::Const;
     29 using ops::DynamicPartition;
     30 using ops::DynamicStitch;
     31 using ops::Placeholder;
     32 
     33 class DataFlowGradTest : public ::testing::Test {
     34  protected:
     35   DataFlowGradTest() : scope_(Scope::NewRootScope()) {}
     36 
     37   void RunTest(const OutputList& xs, const std::vector<TensorShape>& x_shapes,
     38                const OutputList& ys, const std::vector<TensorShape>& y_shapes) {
     39     TF_ASSERT_OK(scope_.status());
     40     float max_error;
     41     TF_ASSERT_OK((ComputeGradientError<float, float, float>(
     42         scope_, xs, x_shapes, ys, y_shapes, &max_error)));
     43     EXPECT_LT(max_error, 1e-4);
     44   }
     45 
     46   Scope scope_;
     47 };
     48 
     49 TEST_F(DataFlowGradTest, DynamicPartitionGrad) {
     50   TensorShape data_shape({2, 3, 2});
     51   auto data = Placeholder(scope_, DT_FLOAT, Placeholder::Shape(data_shape));
     52   auto partitions = Const(scope_, {{2, 1, 0}, {1, 2, 0}});
     53   auto y = DynamicPartition(scope_, data, partitions, 3);
     54   TensorShape partition_shape({2, 2});
     55   RunTest({data}, {data_shape}, y.outputs,
     56           {partition_shape, partition_shape, partition_shape});
     57 }
     58 
     59 TEST_F(DataFlowGradTest, DynamicStitchGrad) {
     60   TensorShape d1_shape({2});
     61   TensorShape d2_shape({2, 2});
     62   std::vector<Output> indices = {Const(scope_, 2), Const(scope_, {1, 0})};
     63   std::vector<Output> data = {
     64       Placeholder(scope_, DT_FLOAT, Placeholder::Shape(d1_shape)),
     65       Placeholder(scope_, DT_FLOAT, Placeholder::Shape(d2_shape))};
     66   auto y = DynamicStitch(scope_, indices, data);
     67   TensorShape y_shape({3, 2});
     68   RunTest(data, {d1_shape, d2_shape}, {y}, {y_shape});
     69 }
     70 
     71 }  // namespace
     72 }  // namespace tensorflow
     73