1# Copyright 2017 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 Local Response Normalization ops.""" 16 17import copy 18 19import numpy as np 20 21from tensorflow.compiler.tests import xla_test 22from tensorflow.python.framework import constant_op 23from tensorflow.python.framework import dtypes 24from tensorflow.python.framework import ops 25from tensorflow.python.ops import array_ops 26from tensorflow.python.ops import gen_nn_ops 27from tensorflow.python.ops import nn 28from tensorflow.python.platform import googletest 29 30CPU_DEVICE = "/job:localhost/replica:0/task:0/cpu:0" 31 32 33# Local response normalization tests. The forward tests are copied from 34# tensorflow/python/kernel_tests/lrn_op_test.py 35class LRNTest(xla_test.XLATestCase): 36 37 def _LRN(self, input_image, lrn_depth_radius=5, bias=1.0, alpha=1.0, 38 beta=0.5): 39 """Compute expected result.""" 40 output = copy.deepcopy(input_image) 41 batch_size = input_image.shape[0] 42 rows = input_image.shape[1] 43 cols = input_image.shape[2] 44 depth = input_image.shape[3] 45 for b in range(batch_size): 46 for r in range(rows): 47 for c in range(cols): 48 for d in range(depth): 49 begin = max(0, d - lrn_depth_radius) 50 end = min(depth, d + lrn_depth_radius + 1) 51 patch = input_image[b, r, c, begin:end] 52 output[b, r, c, d] /= ( 53 np.power(bias + alpha * np.sum(patch * patch), beta)) 54 return output 55 56 def _RunAndVerify(self, dtype): 57 with self.session(): 58 # random shape 59 shape = np.random.randint(1, 16, size=4) 60 # Make depth at least 2 to make it meaningful 61 shape[3] += 1 62 p = array_ops.placeholder(dtype, shape=shape) 63 # random depth_radius, bias, alpha, beta 64 lrn_depth_radius = np.random.randint(1, shape[3]) 65 bias = 1.0 + np.random.rand() 66 alpha = 2.0 * np.random.rand() 67 beta = 2.0 * np.random.rand() 68 with self.test_scope(): 69 lrn_t = nn.local_response_normalization( 70 p, 71 name="lrn", 72 depth_radius=lrn_depth_radius, 73 bias=bias, 74 alpha=alpha, 75 beta=beta) 76 params = {p: np.random.rand(*shape).astype("f")} 77 result = lrn_t.eval(feed_dict=params) 78 expected = self._LRN( 79 params[p], 80 lrn_depth_radius=lrn_depth_radius, 81 bias=bias, 82 alpha=alpha, 83 beta=beta) 84 err = np.amax(np.abs(result - expected)) 85 print("LRN error for bias ", bias, "alpha ", alpha, " beta ", beta, " is ", 86 err) 87 if dtype == dtypes.float32: 88 self.assertTrue(err < 1e-4) 89 else: 90 self.assertTrue(err < 1e-2) 91 self.assertShapeEqual(expected, lrn_t) 92 93 def testCompute(self): 94 for _ in range(2): 95 self._RunAndVerify(dtypes.float32) 96 97 def testLrnGrad(self): 98 # Test for LRNGrad that compares against the CPU implementation. 99 shape = [1, 2, 3, 4] 100 total_size = np.prod(shape) 101 in_image_vals = np.arange(1, total_size + 1, dtype=np.float32) 102 out_image_vals = np.arange(1, total_size + 1, dtype=np.float32) 103 out_grads_vals = np.arange(1, total_size + 1, dtype=np.float32) 104 depth_radius = np.random.randint(1, shape[3]) 105 bias = 1.0 + np.random.rand() 106 alpha = 1.0 * np.random.rand() 107 beta = 1.0 * np.random.rand() 108 109 with self.session(): 110 in_image = constant_op.constant(in_image_vals, shape=shape) 111 out_image = constant_op.constant(out_image_vals, shape=shape) 112 out_grads = constant_op.constant(out_grads_vals, shape=shape) 113 with ops.device(CPU_DEVICE): 114 expected = gen_nn_ops.lrn_grad(out_grads, in_image, out_image, 115 depth_radius, bias, alpha, beta) 116 with self.test_scope(): 117 actual = gen_nn_ops.lrn_grad(out_grads, in_image, out_image, 118 depth_radius, bias, alpha, beta) 119 expected_val = self.evaluate(expected) 120 actual_val = self.evaluate(actual) 121 self.assertAllClose(actual_val, expected_val, rtol=1e-3) 122 123 124if __name__ == "__main__": 125 googletest.main() 126