提交 23512f3d authored 作者: Pascal Lamblin's avatar Pascal Lamblin 提交者: GitHub

Merge pull request #5554 from teganmaharaj/test_abstract_conv

Test abstract conv
...@@ -1594,3 +1594,109 @@ class TestConv2dTranspose(unittest.TestCase): ...@@ -1594,3 +1594,109 @@ class TestConv2dTranspose(unittest.TestCase):
[2, 2, 4, 4, 4, 4, 4, 4, 2, 2], [2, 2, 4, 4, 4, 4, 4, 4, 2, 2],
[2, 2, 4, 4, 4, 4, 4, 4, 2, 2]]]] * 2) [2, 2, 4, 4, 4, 4, 4, 4, 2, 2]]]] * 2)
numpy.testing.assert_equal(output, expected_output) numpy.testing.assert_equal(output, expected_output)
class TestConv2dGrads(unittest.TestCase):
def setUp(self):
if (not theano.config.cxx or
theano.config.mode == "FAST_COMPILE"):
raise SkipTest("Need blas to test conv2d")
self.random_stream = numpy.random.RandomState(utt.fetch_seed())
self.inputs_shapes = [(8, 1, 12, 12), (1, 1, 5, 5), (1, 1, 5, 6), (1, 1, 6, 6)]
self.filters_shapes = [(5, 1, 2, 2), (1, 1, 3, 3)]
self.subsamples = [(1, 1), (2, 2)]
self.border_modes = ["valid", "full"]
self.filter_flip = [True, False]
self.output_grad = theano.tensor.tensor4()
self.output_grad_wrt = theano.tensor.tensor4()
self.x = theano.tensor.tensor4('x', theano.config.floatX) # inputs
self.w = theano.tensor.tensor4('w', theano.config.floatX) # filter weights
def test_conv2d_grad_wrt_inputs(self):
"""Compares calculated abstract grads wrt inputs with the fwd grads
This method checks the outputs of conv2_grad_wrt_inputs against
the outputs of T.nnet.conv forward grads to make sure the
results are the same.
"""
for (in_shape, fltr_shape) in zip(self.inputs_shapes, self.filters_shapes):
for bm in self.border_modes:
for ss in self.subsamples:
for ff in self.filter_flip:
input_val = self.random_stream.random_sample(in_shape).astype(theano.config.floatX)
filter_val = self.random_stream.random_sample(fltr_shape).astype(theano.config.floatX)
out_grad_shape = theano.tensor.nnet.abstract_conv.get_conv_output_shape(image_shape=in_shape,
kernel_shape=fltr_shape,
border_mode=bm,
subsample=ss)
out_grad_val = self.random_stream.random_sample(out_grad_shape).astype(theano.config.floatX)
conv_out = theano.tensor.nnet.conv2d(self.x,
filters=self.w,
border_mode=bm,
subsample=ss,
input_shape=in_shape,
filter_shape=fltr_shape,
filter_flip=ff
)
conv_grad = theano.grad(conv_out.sum(), wrt=self.x, known_grads={conv_out: self.output_grad})
f_old = theano.function([self.x, self.w, self.output_grad], conv_grad)
conv_wrt_i_out = theano.tensor.nnet.abstract_conv.conv2d_grad_wrt_inputs(output_grad=self.output_grad_wrt,
filters=self.w,
border_mode=bm,
subsample=ss,
input_shape=in_shape,
filter_shape=fltr_shape,
filter_flip=ff
)
f_new = theano.function([self.w, self.output_grad_wrt], conv_wrt_i_out)
# check that they're equal
utt.assert_allclose(f_new(filter_val, out_grad_val), f_old(input_val, filter_val, out_grad_val))
def test_conv2d_grad_wrt_weights(self):
"""Compares calculated abstract grads wrt weights with the fwd grads
This method checks the outputs of conv2_grad_wrt_weights against
the outputs of T.nnet.conv forward grads to make sure the
results are the same.
"""
for (in_shape, fltr_shape) in zip(self.inputs_shapes, self.filters_shapes):
for bm in self.border_modes:
for ss in self.subsamples:
for ff in self.filter_flip:
input_val = self.random_stream.random_sample(in_shape).astype(theano.config.floatX)
filter_val = self.random_stream.random_sample(fltr_shape).astype(theano.config.floatX)
out_grad_shape = theano.tensor.nnet.abstract_conv.get_conv_output_shape(image_shape=in_shape,
kernel_shape=fltr_shape,
border_mode=bm,
subsample=ss)
out_grad_val = self.random_stream.random_sample(out_grad_shape).astype(theano.config.floatX)
conv_out = theano.tensor.nnet.conv2d(self.x,
filters=self.w,
border_mode=bm,
subsample=ss,
input_shape=in_shape,
filter_shape=fltr_shape,
filter_flip=ff
)
conv_grad = theano.grad(conv_out.sum(), wrt=self.w, known_grads={conv_out: self.output_grad})
f_old = theano.function([self.x, self.w, self.output_grad], conv_grad)
conv_wrt_w_out = theano.tensor.nnet.abstract_conv.conv2d_grad_wrt_weights(self.x,
output_grad=self.output_grad_wrt,
border_mode=bm,
subsample=ss,
input_shape=in_shape,
filter_shape=fltr_shape,
filter_flip=ff
)
f_new = theano.function([self.x, self.output_grad_wrt], conv_wrt_w_out)
utt.assert_allclose(f_new(input_val, out_grad_val), f_old(input_val, filter_val, out_grad_val))
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论