提交 30c18aa4 authored 作者: Tegan Maharaj's avatar Tegan Maharaj

fixed bugs

上级 4eda607b
......@@ -1605,10 +1605,9 @@ class TestConv2dGrads(unittest.TestCase):
self.output_grad = theano.tensor.tensor4()
self.output_grad_wrt = theano.tensor.tensor4()
self.filters = theano.tensor.tensor4()
self.x = theano.tensor.tensor4('x', theano.config.floatX) # inputs
self.w = theano.tensor.tensor4('w', theano.config.floatX) # weights
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
......@@ -1616,32 +1615,48 @@ class TestConv2dGrads(unittest.TestCase):
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:
if self.filter_flip:
fltr_shape = fltr_shape[::1] # conv2d doesn't seem to have filter_flip
# get random values of the right shapes
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)
# old conv
conv_out = theano.tensor.nnet.conv.conv2d(self.x,
filters=self.filters,
filters=self.w,
border_mode=bm,
subsample=ss,
image_shape=in_shape,
filter_shape=fltr_shape
)
# grad of old conv
conv_grad = theano.grad(conv_out.sum(), wrt=[self.x], known_grads={conv_out: self.output_grad})
f_prime = theano.function([self.x, self.output_grad, self.filters], conv_grad)
conv_wrt_i_out = theano.tensor.nnet.conv.abstract_conv.conv2d_grad_wrt_inputs(self.output_grad_wrt,
filters=self.filters,
border_mode=bm,
subsample=ss,
input_shape=in_shape,
filter_shape=fltr_shape,
filter_flip=ff
)
f = theano.function([self.x, self.output_grad_wrt, self.filters], conv_wrt_i_out)
utt.assert_allclose(f, f_prime)
f_old = theano.function([self.x, self.w, self.output_grad], conv_grad)
# new conv + grad (wrt i)
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
......@@ -1656,23 +1671,30 @@ class TestConv2dGrads(unittest.TestCase):
for ff in self.filter_flip:
if self.filter_flip:
fltr_shape = fltr_shape[::1] # conv2d doesn't seem to have filter_flip
conv_out = theano.tensor.nnet.conv.conv2d(self.w,
filters=self.filters,
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.conv.conv2d(self.x,
filters=self.w,
border_mode=bm,
subsample=ss,
image_shape=in_shape,
filter_shape=fltr_shape
)
conv_grad = theano.grad(conv_out.sum(), wrt=[self.w], known_grads={conv_out: self.output_grad})
f_prime = theano.function([self.w, self.output_grad, self.filters], conv_grad)
conv_wrt_w_out = theano.tensor.nnet.conv.abstract_conv.conv2d_grad_wrt_weights(self.output_grad_wrt,
filters=self.filters,
border_mode=bm,
subsample=ss,
input_shape=in_shape,
filter_shape=fltr_shape,
filter_flip=ff
)
f = theano.function([self.w, self.output_grad_wrt, self.filters], conv_wrt_w_out)
utt.assert_allclose(f, f_prime)
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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论