def testCausalConv2DLayerStridedWithPaddingFPropV2(self, seq_len): """Check strided convs get the same values for different length dim.""" with self.session(use_gpu=True): batch_size = 5 expected_seq_len = 3 params = conv_layers.CausalConv2DLayerWithPadding.Params() params.v2_padding = True params.weight_norm = False params.filter_stride = [2, 2] params.name = 'conv' params.filter_shape = [3, 1, 1, 1] params.params_init = py_utils.WeightInit.Constant(1.0) conv_layer = params.Instantiate() # Set up the padding for the sequence length. (starting at 5). in_padding = tf.constant([ [0, 0, 0, 0, 0], [0, 0, 0, 0, 1], [0, 0, 0, 1, 1], [0, 0, 1, 1, 1], [0, 1, 1, 1, 1], ], tf.float32) in_padding = tf.pad( in_padding, [[0, 0], [0, seq_len - 5]], constant_values=1.0) inputs = 1.0 + tf.tile( tf.reshape(tf.range(seq_len, dtype=tf.float32), [1, seq_len, 1, 1]), [batch_size, 1, 3, 1]) inputs = py_utils.ApplyPadding( tf.reshape(in_padding, [batch_size, seq_len, 1, 1]), inputs) inputs = py_utils.Debug(inputs) output, out_padding = conv_layer.FPropDefaultTheta(inputs, in_padding) output = py_utils.Debug(output) out_padding = py_utils.Debug(out_padding) self.evaluate(tf.global_variables_initializer()) output, out_padding = self.evaluate([output, out_padding]) self.assertEqual((batch_size, expected_seq_len, 2, 1), output.shape) self.assertAllClose([ [0, 0, 0], [0, 0, 1], [0, 0, 1], [0, 1, 1], [0, 1, 1], ], out_padding) self.assertAllClose( [ [[[1], [1]], [[6], [6]], [[12], [12]]], [[[1], [1]], [[6], [6]], [[7], [7]]], [[[1], [1]], [[6], [6]], [[3], [3]]], # NOTE: not padded. [[[1], [1]], [[3], [3]], [[0], [0]]], [[[1], [1]], [[1], [1]], [[0], [0]]], ], output)
def testComputeConvOutputPaddingV2(self, padding, expected_padding, stride, padding_algorithm): """Test Convolution padding computation.""" padding = tf.constant([padding], tf.float32) expected_padding = tf.constant([expected_padding], tf.float32) with self.session(use_gpu=True): conv_padding = conv_layers._ComputeConvOutputPaddingV2( padding, window=3, stride=stride, padding_algorithm=padding_algorithm) self.evaluate(tf.global_variables_initializer()) conv_padding = py_utils.Debug(conv_padding) conv_padding = self.evaluate(conv_padding) tf.logging.info('expected_padding {expected_padding}') self.assertAllClose(expected_padding, conv_padding)
def testConv2DLayerStridedWithPaddingFProp(self, seq_len): """Check strided convs get the same values for different length dim.""" # TODO(isaace): THIS TEST SHOWS THAT THERE IS A BUG IN THE CODE. with self.session(use_gpu=True): batch_size = 3 expected_seq_len = 3 params = conv_layers.Conv2DLayerWithPadding.Params() params.weight_norm = False params.filter_stride = [2, 2] params.name = 'conv' params.filter_shape = [3, 3, 1, 1] params.params_init = py_utils.WeightInit.Constant(1.0) conv_layer = params.Instantiate() # Set up the padding for the sequence length. (starting at 5). in_padding = tf.constant([ [0, 0, 0, 0, 0], [0, 0, 0, 0, 1], [0, 0, 0, 1, 1], ], tf.float32) in_padding = tf.pad( in_padding, [[0, 0], [0, seq_len - 5]], constant_values=1.0) inputs = 1.0 + tf.tile( tf.reshape(tf.range(seq_len, dtype=tf.float32), [1, seq_len, 1, 1]), [batch_size, 1, 3, 1]) inputs = py_utils.ApplyPadding( tf.reshape(in_padding, [batch_size, seq_len, 1, 1]), inputs) inputs = py_utils.Debug(inputs) output, out_padding = conv_layer.FPropDefaultTheta(inputs, in_padding) output = py_utils.Debug(output) out_padding = py_utils.Debug(out_padding) self.evaluate(tf.global_variables_initializer()) output, out_padding = self.evaluate([output, out_padding]) self.assertEqual((batch_size, expected_seq_len, 2, 1), output.shape) self.assertAllClose([ [0, 0, 1], [0, 0, 1], [0, 1, 1], ], out_padding) # This here shows a bug in the implementation; the output should be the # same. Also there are bugs with the output not having the correct # padding. if seq_len == 5: self.assertAllClose([ [[[6], [6]], [[18], [18]], [[18], [18]]], [[[6], [6]], [[18], [18]], [[8], [8]]], [[[6], [6]], [[10], [10]], [[0], [0]]], ], output) elif seq_len == 6: self.assertAllClose([ [[[12], [12]], [[24], [24]], [[10], [10]]], [[[12], [12]], [[14], [14]], [[0], [0]]], [[[12], [12]], [[6], [6]], [[0], [0]]], ], output) else: raise ValueError('Test does not handle length {seq_len}')