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"""Test for version 2 of the zero_out op."""
17
18from __future__ import absolute_import
19from __future__ import division
20from __future__ import print_function
21
22import tensorflow as tf
23
24
25from tensorflow.examples.adding_an_op import zero_out_grad_2  # pylint: disable=unused-import
26from tensorflow.examples.adding_an_op import zero_out_op_2
27
28
29class ZeroOut2Test(tf.test.TestCase):
30
31  def test(self):
32    with self.test_session():
33      result = zero_out_op_2.zero_out([5, 4, 3, 2, 1])
34      self.assertAllEqual(result.eval(), [5, 0, 0, 0, 0])
35
36  def test_2d(self):
37    with self.test_session():
38      result = zero_out_op_2.zero_out([[6, 5, 4], [3, 2, 1]])
39      self.assertAllEqual(result.eval(), [[6, 0, 0], [0, 0, 0]])
40
41  def test_grad(self):
42    with self.test_session():
43      shape = (5,)
44      x = tf.constant([5, 4, 3, 2, 1], dtype=tf.float32)
45      y = zero_out_op_2.zero_out(x)
46      err = tf.test.compute_gradient_error(x, shape, y, shape)
47      self.assertLess(err, 1e-4)
48
49  def test_grad_2d(self):
50    with self.test_session():
51      shape = (2, 3)
52      x = tf.constant([[6, 5, 4], [3, 2, 1]], dtype=tf.float32)
53      y = zero_out_op_2.zero_out(x)
54      err = tf.test.compute_gradient_error(x, shape, y, shape)
55      self.assertLess(err, 1e-4)
56
57
58if __name__ == '__main__':
59  tf.test.main()
60