nn_grad_test.cc revision 008910f1122d115a6d7430bfcc63cf4296c7467d
1/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7    http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations 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
25namespace tensorflow {
26using namespace ops;  // NOLINT(build/namespaces)
27
28namespace {
29
30class NNGradTest : public ::testing::Test {
31 protected:
32  NNGradTest() : scope_(Scope::NewRootScope()) {}
33
34  void RunTest(const Output& x, const TensorShape& x_shape, const Output& y,
35               const TensorShape& y_shape) {
36    float max_error;
37    TF_ASSERT_OK(ComputeGradientError(scope_, {x}, {x_shape}, {y}, {y_shape},
38                                      &max_error));
39    EXPECT_LT(max_error, 1e-4);
40  }
41
42  void RunTest(const Output& x, const Tensor& x_init_value, const Output& y,
43               const TensorShape& y_shape) {
44    float max_error;
45    TF_ASSERT_OK(
46        ComputeGradientError(scope_, x, x_init_value, y, y_shape, &max_error));
47    EXPECT_LT(max_error, 1e-4);
48  }
49
50  void RunTest(const OutputList& xs, const std::vector<TensorShape>& x_shapes,
51               const OutputList& ys, const std::vector<TensorShape>& y_shapes) {
52    TF_ASSERT_OK(scope_.status());
53    float max_error;
54    TF_ASSERT_OK(
55        ComputeGradientError(scope_, xs, x_shapes, ys, y_shapes, &max_error));
56    EXPECT_LT(max_error, 1e-4);
57  }
58
59  Scope scope_;
60};
61
62TEST_F(NNGradTest, SoftmaxGrad) {
63  TensorShape shape({32, 10});
64  auto x = Placeholder(scope_, DT_FLOAT, Placeholder::Shape(shape));
65  auto y = Softmax(scope_, x);
66  RunTest(x, shape, y, shape);
67}
68
69TEST_F(NNGradTest, LogSoftmaxGrad) {
70  TensorShape shape({5, 3});
71  auto x = Placeholder(scope_, DT_FLOAT, Placeholder::Shape(shape));
72  auto y = LogSoftmax(scope_, x);
73  // Avoid numerical instability when computing finite differences.
74  Tensor x_init_value = test::AsTensor<float>(
75          {-0.9f, -0.7f, -0.5f, -0.3f, -0.1f,
76           0.1f, 0.3f, 0.5f, 0.7f, 0.8f,
77           -0.1f, 0.1f, 0.1f, 0.1f, 1.2f},
78          {5, 3});
79  RunTest(x, x_init_value, y, shape);
80}
81
82TEST_F(NNGradTest, ReluGrad) {
83  TensorShape shape({5, 2});
84  auto x = Placeholder(scope_, DT_FLOAT, Placeholder::Shape(shape));
85  auto y = Relu(scope_, x);
86  // Avoid input values where ReLU gradient is not well defined (around zero).
87  Tensor x_init_value = test::AsTensor<float>(
88      {-0.9f, -0.7f, -0.5f, -0.3f, -0.1f, 0.1f, 0.3f, 0.5f, 0.7f, 0.9f},
89      {5, 2});
90  RunTest(x, x_init_value, y, shape);
91}
92
93TEST_F(NNGradTest, Relu6Grad) {
94  TensorShape shape({5, 2});
95  auto x = Placeholder(scope_, DT_FLOAT, Placeholder::Shape(shape));
96  auto y = Relu6(scope_, x);
97  // Avoid input values where ReLU gradient is not well defined (around zero
98  // and six).
99  Tensor x_init_value = test::AsTensor<float>(
100      {-0.9f, -0.7f, -0.5f, -0.3f, -0.1f, 6.1f, 6.3f, 6.5f, 6.7f, 6.9f},
101      {5, 2});
102  RunTest(x, x_init_value, y, shape);
103}
104
105TEST_F(NNGradTest, EluGrad) {
106  TensorShape shape({5, 2});
107  auto x = Placeholder(scope_, DT_FLOAT, Placeholder::Shape(shape));
108  auto y = Elu(scope_, x);
109  Tensor x_init_value = test::AsTensor<float>(
110      {-0.9f, -0.7f, -0.5f, -0.3f, -0.1f, 0.1f, 0.3f, 0.5f, 0.7f, 0.9f},
111      {5, 2});
112  RunTest(x, x_init_value, y, shape);
113}
114
115TEST_F(NNGradTest, SeluGrad) {
116  TensorShape shape({5, 2});
117  auto x = Placeholder(scope_, DT_FLOAT, Placeholder::Shape(shape));
118  auto y = Selu(scope_, x);
119  Tensor x_init_value = test::AsTensor<float>(
120      {-0.9f, -0.7f, -0.5f, -0.3f, -0.1f, 0.1f, 0.3f, 0.5f, 0.7f, 0.9f},
121      {5, 2});
122  RunTest(x, x_init_value, y, shape);
123}
124
125TEST_F(NNGradTest, BiasAddGradHelper) {
126  TensorShape shape({4, 5});
127  TensorShape bias_shape({5});
128  auto x = Placeholder(scope_, DT_FLOAT, Placeholder::Shape(shape));
129  auto bias = Placeholder(scope_, DT_FLOAT, Placeholder::Shape(bias_shape));
130  auto y = BiasAdd(scope_, x, bias);
131  RunTest({x,bias}, {shape, bias_shape}, {y}, {shape});
132}
133
134}  // namespace
135}  // namespace tensorflow
136