提交 3b740511 authored 作者: Tegan Maharaj's avatar Tegan Maharaj

set up class TestConv2dGrads methods for testing grads wrt input, weights, and both

上级 aefab959
...@@ -1592,14 +1592,34 @@ class TestConv2dTranspose(unittest.TestCase): ...@@ -1592,14 +1592,34 @@ class TestConv2dTranspose(unittest.TestCase):
#TM copying the TestConvTypes setup, maybe doing it wrong #TM copying the TestConvTypes setup, not sure exactly what unittest.TestCase is
class TestConv2dGrads(unittest.TestCase): class TestConv2dGrads(unittest.TestCase):
def setUp(self): def __init__(self):
#TM anything to go here? self.random_stream = numpy.random.RandomState(unittest_tools.fetch_seed())
def get_outputs_of_conv2d_grad_wrt_inputs
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 = T.tensor4()
self.filters = T.tensor4()
self.x = T.tensor4('x', theano.config.floatX) #inputs
self.w = T.tensor4('w', theano.config.floatX) #weights
#first two must be wrong; I'm not sure how to use TestCase
if unittest.TestCase == 'inputs':
test_conv2d_grad_wrt_inputs()
elif unittest.TestCase =='weights':
test_conv2d_grad_wrt_weights():
else:
self.test_conv2_grads_wrt_input_and_weights()
def test_conv2d_grad_wrt_inputs(): def test_conv2d_grad_wrt_inputs():
"""Compares calculated abstract grads wrt inputs with the fwd grads """Compares calculated abstract grads wrt inputs with the fwd grads
...@@ -1608,6 +1628,21 @@ class TestConv2dGrads(unittest.TestCase): ...@@ -1608,6 +1628,21 @@ class TestConv2dGrads(unittest.TestCase):
results are the same. results are the same.
""" """
for (in_shape, fltr_shape) in zip(inputs_shapes, filters_shapes):
for bm in border_modes:
for ss in subsamples:
for ff in filter_flip:
conv_out = T.nnet.conv.conv2d(x,
filters = filters,
border_mode = bm,
subsample = ss,
image_shape = in_shape
filter_shape = fltr_shape
)
conv_grad = theano.grad(wrt=[x], known_grads={conv_out: output_grad})
f_prime = theano.function([x, output_grad, filters], conv_grad)
utt.assert_allclose(conv_grad, f_prime)
def test_conv2d_grad_wrt_weights(): def test_conv2d_grad_wrt_weights():
"""Compares calculated abstract grads wrt weights with the fwd grads """Compares calculated abstract grads wrt weights with the fwd grads
...@@ -1616,3 +1651,48 @@ class TestConv2dGrads(unittest.TestCase): ...@@ -1616,3 +1651,48 @@ class TestConv2dGrads(unittest.TestCase):
results are the same. results are the same.
""" """
for (in_shape, fltr_shape) in zip(inputs_shapes, filters_shapes):
for bm in border_modes:
for ss in subsamples:
for ff in filter_flip:
conv_out = T.nnet.conv.conv2d(w,
filters = filters,
border_mode = bm,
subsample = ss,
image_shape = in_shape
filter_shape = fltr_shape
)
conv_grad = theano.grad(wrt=[w], known_grads={conv_out: output_grad})
f_prime = theano.function([w, output_grad, filters], conv_grad)
utt.assert_allclose(conv_grad, f_prime)
def test_conv2_grads_wrt_input_and_weights():
"""Compares calculated abstract grads wrt [inputs, weights] with the fwd grads
This method checks the outputs of conv2_grad wrt inputs and weights
against the outputs of T.nnet.conv forward grads to make sure the
results are the same.
"""
#for (input_shape, filter_shape) in zip(inputs_shapes, filter_shapes):
#make_rand_inputs = theano.function([x], random_stream(input_shape))
#make_rand_filters = theano.function([filters], random_stream(filter_shape))
for (in_shape, fltr_shape) in zip(inputs_shapes, filters_shapes):
for wrt in [x,w]: #for both inputs (x) and weights (w)
for bm in border_modes:
for ss in subsamples:
for ff in filter_flip:
conv_out = T.nnet.conv.conv2d(wrt,
filters = filters,
border_mode = bm,
subsample = ss,
image_shape = in_shape
filter_shape = fltr_shape
)
conv_grad = theano.grad(wrt=[wrt], known_grads={conv_out: output_grad})
f_prime = theano.function([wrt, output_grad, filters], conv_grad)
utt.assert_allclose(conv_grad, f_prime)
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论