Home | History | Annotate | Download | only in kernels
      1 /* Copyright 2015 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/core/kernels/eigen_activations.h"
     17 #include "tensorflow/core/framework/types.h"
     18 #include "tensorflow/core/platform/test.h"
     19 
     20 namespace Eigen {
     21 
     22 namespace {
     23 void EigenApprox(float a, float b) {
     24   ASSERT_TRUE(std::abs(a - b) <= std::min(std::abs(a), std::abs(b)) * 1e-3);
     25 }
     26 }  // namespace
     27 
     28 TEST(EigenBackwardSpatialConvolutionsTest, SigmoidFastDerivative) {
     29   const ptrdiff_t depth = 3;
     30   const ptrdiff_t batch = 10;
     31   const ptrdiff_t rows = 32;
     32   const ptrdiff_t cols = 48;
     33 
     34   Tensor<float, 4> input(depth, rows, cols, batch);
     35   input.setRandom();
     36 
     37   Tensor<float, 4> result(depth, rows, cols, batch);
     38   result = input.unaryExpr(scalar_sigmoid_fast_derivative_op<float>());
     39 
     40   for (int b = 0; b < batch; ++b) {
     41     for (int c = 0; c < cols; ++c) {
     42       for (int r = 0; r < rows; ++r) {
     43         for (int d = 0; d < depth; ++d) {
     44           float val = input(d, r, c, b);
     45           EigenApprox(result(d, r, c, b), (1 - val) * val);
     46         }
     47       }
     48     }
     49   }
     50 }
     51 
     52 TEST(EigenBackwardSpatialConvolutionsTest, TanhFastDerivative) {
     53   const ptrdiff_t depth = 3;
     54   const ptrdiff_t batch = 10;
     55   const ptrdiff_t rows = 32;
     56   const ptrdiff_t cols = 48;
     57 
     58   Tensor<float, 4> input(depth, rows, cols, batch);
     59   input.setRandom();
     60 
     61   Tensor<float, 4> result(depth, rows, cols, batch);
     62   result = input.unaryExpr(scalar_tanh_fast_derivative_op<float>());
     63 
     64   for (int b = 0; b < batch; ++b) {
     65     for (int c = 0; c < cols; ++c) {
     66       for (int r = 0; r < rows; ++r) {
     67         for (int d = 0; d < depth; ++d) {
     68           float val = input(d, r, c, b);
     69           EigenApprox(result(d, r, c, b), 1 - (val * val));
     70         }
     71       }
     72     }
     73   }
     74 }
     75 
     76 TEST(EigenBackwardSpatialConvolutionsTest, Clip) {
     77   const ptrdiff_t depth = 3;
     78   const ptrdiff_t batch = 10;
     79   const ptrdiff_t rows = 32;
     80   const ptrdiff_t cols = 48;
     81 
     82   Tensor<float, 4> input(depth, rows, cols, batch);
     83   input.setRandom();
     84 
     85   Tensor<float, 4> result(depth, rows, cols, batch);
     86   result = input.binaryExpr(input.constant(0.01), scalar_clip_op<float>());
     87 
     88   for (int b = 0; b < batch; ++b) {
     89     for (int c = 0; c < cols; ++c) {
     90       for (int r = 0; r < rows; ++r) {
     91         for (int d = 0; d < depth; ++d) {
     92           float val = input(d, r, c, b);
     93           EigenApprox(result(d, r, c, b),
     94                       (std::min)((std::max)(val, -0.01f), 0.01f));
     95         }
     96       }
     97     }
     98   }
     99 }
    100 
    101 }  // namespace Eigen
    102