提交 c3b54b9b authored 作者: Arnaud Bergeron's avatar Arnaud Bergeron

Fix tests and code so that they can be happy forever after.

上级 05a63694
...@@ -31,6 +31,7 @@ from .bn import batch_normalization ...@@ -31,6 +31,7 @@ from .bn import batch_normalization
import warnings import warnings
from .abstract_conv import conv2d as abstract_conv2d from .abstract_conv import conv2d as abstract_conv2d
def conv2d(input, filters, input_shape=None, filter_shape=None, def conv2d(input, filters, input_shape=None, filter_shape=None,
border_mode='valid', subsample=(1, 1), filter_flip=True, border_mode='valid', subsample=(1, 1), filter_flip=True,
image_shape=None, **kwargs): image_shape=None, **kwargs):
...@@ -139,5 +140,3 @@ def conv2d(input, filters, input_shape=None, filter_shape=None, ...@@ -139,5 +140,3 @@ def conv2d(input, filters, input_shape=None, filter_shape=None,
return abstract_conv2d(input, filters, input_shape, filter_shape, return abstract_conv2d(input, filters, input_shape, filter_shape,
border_mode, subsample, filter_flip) border_mode, subsample, filter_flip)
...@@ -424,7 +424,7 @@ class CorrMM_gradWeights(BaseCorrMM): ...@@ -424,7 +424,7 @@ class CorrMM_gradWeights(BaseCorrMM):
if shape is None: if shape is None:
raise ValueError('shape must be given if subsample != (1, 1)' raise ValueError('shape must be given if subsample != (1, 1)'
' or border_mode == "half"') ' or border_mode == "half"')
height_width = [shape[0], shape[1]] height_width = [as_tensor_variable(shape[0]), as_tensor_variable(shape[1])]
else: else:
height_width = [] height_width = []
...@@ -519,7 +519,7 @@ class CorrMM_gradInputs(BaseCorrMM): ...@@ -519,7 +519,7 @@ class CorrMM_gradInputs(BaseCorrMM):
raise TypeError('topgrad must be 4D tensor') raise TypeError('topgrad must be 4D tensor')
if self.subsample != (1, 1) and shape is None: if self.subsample != (1, 1) and shape is None:
raise ValueError('shape must be given if subsample != (1, 1)') raise ValueError('shape must be given if subsample != (1, 1)')
height_width = [shape[0], shape[1]] if self.subsample != (1, 1) else [] height_width = [as_tensor_variable(shape[0]), as_tensor_variable(shape[1])] if self.subsample != (1, 1) else []
broadcastable = [topgrad.type.broadcastable[0], kern.type.broadcastable[1], broadcastable = [topgrad.type.broadcastable[0], kern.type.broadcastable[1],
False, False] False, False]
......
...@@ -162,7 +162,7 @@ PyArrayObject* corrMM(PyArrayObject* bottom, ...@@ -162,7 +162,7 @@ PyArrayObject* corrMM(PyArrayObject* bottom,
"CorrMM shape inconsistency:\n" "CorrMM shape inconsistency:\n"
" bottom shape: %%d %%d %%d %%d\n" " bottom shape: %%d %%d %%d %%d\n"
" weight shape: %%d %%d %%d %%d\n" " weight shape: %%d %%d %%d %%d\n"
" top shape: %%d %%d %%d %%d (expected %%d %%d %%d %%d)\n", " top shape: %%ld %%ld %%ld %%ld (expected %%d %%d %%d %%d)\n",
batchSize, nChannels, bottomHeight, bottomWidth, batchSize, nChannels, bottomHeight, bottomWidth,
nFilters, nChannels, kH, kW, nFilters, nChannels, kH, kW,
PyArray_DIMS(top)[0], PyArray_DIMS(top)[1], PyArray_DIMS(top)[0], PyArray_DIMS(top)[1],
...@@ -182,7 +182,7 @@ PyArrayObject* corrMM(PyArrayObject* bottom, ...@@ -182,7 +182,7 @@ PyArrayObject* corrMM(PyArrayObject* bottom,
if (NULL == col) if (NULL == col)
{ {
PyErr_Format(PyExc_RuntimeError, PyErr_Format(PyExc_RuntimeError,
"CorrMM failed to allocate working memory of %%d x %%d\n", "CorrMM failed to allocate working memory of %%ld x %%ld\n",
col_dim[0], col_dim[1]); col_dim[0], col_dim[1]);
return NULL; return NULL;
} }
......
...@@ -4,12 +4,12 @@ import itertools ...@@ -4,12 +4,12 @@ import itertools
import theano import theano
from theano import tensor from theano import tensor
from theano.tests import unittests_tools as utt from theano.tests import unittest_tools as utt
from theano.tensor.nnet.abstract_conv import conv, get_conv_output_shape from theano.tensor.nnet import corr, abstract_conv as conv
from theano.tensor.nnet import corr from theano.tensor.nnet.abstract_conv import get_conv_output_shape
from theano.tensor.nnet.conv import ConvOp
from theano.tensor.nnet.corr import (CorrMM, CorrMM_gradWeights, from theano.tensor.nnet.corr import (CorrMM, CorrMM_gradWeights,
CorrMM_gradInputs) CorrMM_gradInputs)
from theano.tensor.nnet.conv import ConvOp
from theano.tensor.nnet.ConvGrad3D import ConvGrad3D from theano.tensor.nnet.ConvGrad3D import ConvGrad3D
from theano.tensor.nnet.ConvTransp3D import ConvTransp3D from theano.tensor.nnet.ConvTransp3D import ConvTransp3D
...@@ -21,18 +21,21 @@ def conv_corr(inputs, filters, border_mode="valid", subsample=(1, 1), ...@@ -21,18 +21,21 @@ def conv_corr(inputs, filters, border_mode="valid", subsample=(1, 1),
return corr.CorrMM(border_mode, subsample)(inputs, filters) return corr.CorrMM(border_mode, subsample)(inputs, filters)
def conv_corr_gw(inputs, filters, border_mode="valid", subsample=(1, 1), def conv_corr_gw(inputs, topgrad, filters_shape, border_mode="valid",
conv_mode='conv'): subsample=(1, 1), conv_mode='conv'):
rval = corr.CorrMM_gradWeights(border_mode, subsample)(inputs, topgrad,
filters_shape[2:])
if conv_mode == 'conv': if conv_mode == 'conv':
filters = filters[:, :, ::-1, ::-1] rval = rval[:, :, ::-1, ::-1]
return corr.CorrMM(border_mode, subsample)(inputs, filters) return rval
def conv_corr_gi(inputs, filters, border_mode="valid", subsample=(1, 1), def conv_corr_gi(filters, topgrad, inputs_shape, border_mode="valid",
conv_mode='conv'): subsample=(1, 1), conv_mode='conv'):
if conv_mode == 'conv': if conv_mode == 'conv':
filters = filters[:, :, ::-1, ::-1] filters = filters[:, :, ::-1, ::-1]
return corr.CorrMM(border_mode, subsample)(inputs, filters) return corr.CorrMM_gradInputs(border_mode, subsample)(filters, topgrad,
inputs_shape[2:])
class TestGetConvOutShape(unittest.TestCase): class TestGetConvOutShape(unittest.TestCase):
...@@ -279,7 +282,7 @@ class TestCpuConv2d(TestConv2d): ...@@ -279,7 +282,7 @@ class TestCpuConv2d(TestConv2d):
gradweight_OK = False gradweight_OK = False
gradinput_OK = False gradinput_OK = False
if b not in ('valid', 'full'): if b not in ((0, 0), 'valid', 'full'):
fwd_OK = False fwd_OK = False
gradweight_OK = False gradweight_OK = False
gradinput_OK = False gradinput_OK = False
...@@ -294,7 +297,7 @@ class TestCpuConv2d(TestConv2d): ...@@ -294,7 +297,7 @@ class TestCpuConv2d(TestConv2d):
if fwd_OK: if fwd_OK:
self.run_fwd(inputs_shape=i, filters_shape=f, subsample=s, self.run_fwd(inputs_shape=i, filters_shape=f, subsample=s,
verify_grad=True, mode=mode, device='cpu', verify_grad=True, mode=mode,
provide_shape=provide_shape, border_mode=b, provide_shape=provide_shape, border_mode=b,
filter_flip=flip, target_op=ConvOp) filter_flip=flip, target_op=ConvOp)
else: else:
...@@ -312,7 +315,7 @@ class TestCpuConv2d(TestConv2d): ...@@ -312,7 +315,7 @@ class TestCpuConv2d(TestConv2d):
if gradweight_OK: if gradweight_OK:
self.run_gradweight(inputs_shape=i, filters_shape=f, self.run_gradweight(inputs_shape=i, filters_shape=f,
output_shape=o, subsample=s, output_shape=o, subsample=s,
verify_grad=False, mode=mode, device='cpu', verify_grad=False, mode=mode,
provide_shape=provide_shape, border_mode=b, provide_shape=provide_shape, border_mode=b,
filter_flip=flip, filter_flip=flip,
target_op=(ConvOp, ConvGrad3D)) target_op=(ConvOp, ConvGrad3D))
...@@ -332,7 +335,7 @@ class TestCpuConv2d(TestConv2d): ...@@ -332,7 +335,7 @@ class TestCpuConv2d(TestConv2d):
if gradinput_OK: if gradinput_OK:
self.run_gradinput(inputs_shape=i, filters_shape=f, self.run_gradinput(inputs_shape=i, filters_shape=f,
output_shape=o, subsample=s, output_shape=o, subsample=s,
verify_grad=False, mode=mode, device='cpu', verify_grad=False, mode=mode,
provide_shape=provide_shape, border_mode=b, provide_shape=provide_shape, border_mode=b,
filter_flip=flip, filter_flip=flip,
target_op=(ConvOp, ConvTransp3D)) target_op=(ConvOp, ConvTransp3D))
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论