• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"""Tests for Softsign and SoftsignGrad."""
16
17import numpy as np
18
19from tensorflow.python.framework import constant_op
20from tensorflow.python.framework import test_util
21from tensorflow.python.ops import gradient_checker
22from tensorflow.python.ops import nn_ops
23import tensorflow.python.ops.nn_grad  # pylint: disable=unused-import
24from tensorflow.python.platform import test
25
26
27class SoftsignTest(test.TestCase):
28
29  def _npSoftsign(self, np_features):
30    return np_features / (1 + np.abs(np_features))
31
32  def _testSoftsign(self, np_features, use_gpu=False):
33    np_softsign = self._npSoftsign(np_features)
34    with self.cached_session(use_gpu=use_gpu):
35      softsign = nn_ops.softsign(np_features)
36      tf_softsign = self.evaluate(softsign)
37    self.assertAllClose(np_softsign, tf_softsign)
38    self.assertShapeEqual(np_softsign, softsign)
39
40  def testNumbers(self):
41    for t in [np.float64, np.double]:
42      self._testSoftsign(
43          np.array([[-9, 7, -5, 3, -1], [1, -3, 5, -7, 9]]).astype(t),
44          use_gpu=False)
45      self._testSoftsign(
46          np.array([[-9, 7, -5, 3, -1], [1, -3, 5, -7, 9]]).astype(t),
47          use_gpu=True)
48
49  @test_util.run_deprecated_v1
50  def testGradient(self):
51    with self.cached_session():
52      x = constant_op.constant(
53          [-0.9, -0.7, -0.5, -0.3, -0.1, 0.1, 0.3, 0.5, 0.7, 0.9],
54          shape=[2, 5],
55          name="x")
56      y = nn_ops.softsign(x, name="softsign")
57      x_init = np.asarray(
58          [[-0.9, -0.7, -0.5, -0.3, -0.1], [0.1, 0.3, 0.5, 0.7, 0.9]],
59          dtype=np.float32,
60          order="F")
61      err = gradient_checker.compute_gradient_error(
62          x, [2, 5], y, [2, 5], x_init_value=x_init)
63    print("softsign (float) gradient err = ", err)
64    self.assertLess(err, 1e-4)
65
66  @test_util.run_deprecated_v1
67  def testNoInts(self):
68    with self.cached_session():
69      with self.assertRaisesRegex(
70          TypeError,
71          "'features' has DataType int32 not in list of allowed values"):
72        nn_ops.softsign(constant_op.constant(7)).eval()
73
74
75if __name__ == "__main__":
76  test.main()
77