aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/core/kernels/eigen_activations_test.cc
blob: 34952f5abb8526f0317ba8a674948fada4dc0ce7 (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
/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/core/kernels/eigen_activations.h"
#include "tensorflow/core/framework/types.h"
#include "tensorflow/core/platform/test.h"

namespace Eigen {

namespace {
void EigenApprox(float a, float b) {
  ASSERT_TRUE(std::abs(a - b) <= std::min(std::abs(a), std::abs(b)) * 1e-3);
}
}  // namespace

TEST(EigenBackwardSpatialConvolutionsTest, SigmoidFastDerivative) {
  const ptrdiff_t depth = 3;
  const ptrdiff_t batch = 10;
  const ptrdiff_t rows = 32;
  const ptrdiff_t cols = 48;

  Tensor<float, 4> input(depth, rows, cols, batch);
  input.setRandom();

  Tensor<float, 4> result(depth, rows, cols, batch);
  result = input.unaryExpr(scalar_sigmoid_fast_derivative_op<float>());

  for (int b = 0; b < batch; ++b) {
    for (int c = 0; c < cols; ++c) {
      for (int r = 0; r < rows; ++r) {
        for (int d = 0; d < depth; ++d) {
          float val = input(d, r, c, b);
          EigenApprox(result(d, r, c, b), (1 - val) * val);
        }
      }
    }
  }
}

TEST(EigenBackwardSpatialConvolutionsTest, TanhFastDerivative) {
  const ptrdiff_t depth = 3;
  const ptrdiff_t batch = 10;
  const ptrdiff_t rows = 32;
  const ptrdiff_t cols = 48;

  Tensor<float, 4> input(depth, rows, cols, batch);
  input.setRandom();

  Tensor<float, 4> result(depth, rows, cols, batch);
  result = input.unaryExpr(scalar_tanh_fast_derivative_op<float>());

  for (int b = 0; b < batch; ++b) {
    for (int c = 0; c < cols; ++c) {
      for (int r = 0; r < rows; ++r) {
        for (int d = 0; d < depth; ++d) {
          float val = input(d, r, c, b);
          EigenApprox(result(d, r, c, b), 1 - (val * val));
        }
      }
    }
  }
}

TEST(EigenBackwardSpatialConvolutionsTest, Clip) {
  const ptrdiff_t depth = 3;
  const ptrdiff_t batch = 10;
  const ptrdiff_t rows = 32;
  const ptrdiff_t cols = 48;

  Tensor<float, 4> input(depth, rows, cols, batch);
  input.setRandom();

  Tensor<float, 4> result(depth, rows, cols, batch);
  result = input.binaryExpr(input.constant(0.01), scalar_clip_op<float>());

  for (int b = 0; b < batch; ++b) {
    for (int c = 0; c < cols; ++c) {
      for (int r = 0; r < rows; ++r) {
        for (int d = 0; d < depth; ++d) {
          float val = input(d, r, c, b);
          EigenApprox(result(d, r, c, b),
                      (std::min)((std::max)(val, -0.01f), 0.01f));
        }
      }
    }
  }
}

}  // namespace Eigen