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 convolution related functionality in tensorflow.ops.nn.""" 16from __future__ import absolute_import 17from __future__ import division 18from __future__ import print_function 19 20import numpy as np 21from six.moves import xrange # pylint: disable=redefined-builtin 22 23from tensorflow.python.framework import constant_op 24from tensorflow.python.framework import dtypes 25from tensorflow.python.ops import array_ops 26from tensorflow.python.ops import nn_ops 27from tensorflow.python.platform import test 28 29 30class Conv1DTest(test.TestCase): 31 32 def testBasic(self): 33 """Test that argument passing to conv1d is handled properly.""" 34 for dtype in [dtypes.float16, dtypes.float32, dtypes.float64]: 35 x = constant_op.constant([1, 2, 3, 4], dtype=dtype) 36 x = array_ops.expand_dims(x, 0) # Add batch dimension 37 x = array_ops.expand_dims(x, 2) # And depth dimension 38 filters = constant_op.constant([2, 1], dtype=dtype) 39 filters = array_ops.expand_dims(filters, 1) # in_channels 40 filters = array_ops.expand_dims(filters, 2) # out_channels 41 # Filters is 2x1x1 42 for stride in [1, 2]: 43 with self.cached_session(use_gpu=test.is_gpu_available()): 44 c = nn_ops.conv1d(x, filters, stride, padding="VALID") 45 reduced = array_ops.squeeze(c) 46 output = self.evaluate(reduced) 47 if stride == 1: 48 self.assertEqual(len(output), 3) 49 self.assertAllClose(output, 50 [2 * 1 + 1 * 2, 2 * 2 + 1 * 3, 2 * 3 + 1 * 4]) 51 else: 52 self.assertEqual(len(output), 2) 53 self.assertAllClose(output, [2 * 1 + 1 * 2, 2 * 3 + 1 * 4]) 54 55 def testConv1DTranspose(self): 56 with self.cached_session(): 57 stride = 2 58 59 # Input, output: [batch, width, depth] 60 x_shape = [2, 4, 3] 61 y_shape = [2, 9, 2] 62 63 # Filter: [kernel_width, output_depth, input_depth] 64 f_shape = [3, 2, 3] 65 66 x = constant_op.constant( 67 1.0, shape=x_shape, name="x", dtype=dtypes.float32) 68 f = constant_op.constant( 69 1.0, shape=f_shape, name="filter", dtype=dtypes.float32) 70 output = nn_ops.conv1d_transpose( 71 x, f, y_shape, strides=stride, padding="VALID") 72 value = self.evaluate(output) 73 74 cache_values = np.zeros(y_shape, dtype=np.float32) 75 76 # The amount of padding added 77 pad = 1 78 79 for n in xrange(x_shape[0]): 80 for k in xrange(f_shape[1]): 81 for w in xrange(pad, y_shape[1] - pad): 82 target = 3.0 83 # We add a case for locations divisible by the stride. 84 w_in = w % stride == 0 and w > pad and w < y_shape[1] - 1 - pad 85 if w_in: 86 target += 3.0 87 cache_values[n, w, k] = target 88 89 # copy values in the border 90 cache_values[n, 0, k] = cache_values[n, 1, k] 91 cache_values[n, -1, k] = cache_values[n, -2, k] 92 93 self.assertAllClose(cache_values, value) 94 95 96if __name__ == "__main__": 97 test.main() 98