提交 69332137 authored 作者: Pascal Lamblin's avatar Pascal Lamblin

Merge pull request #2648 from lamblin/conv2d_rop

R op for Conv2d
...@@ -776,9 +776,19 @@ class ConvOp(OpenMPOp): ...@@ -776,9 +776,19 @@ class ConvOp(OpenMPOp):
#execute the c version which is much faster. #execute the c version which is much faster.
if self.dx > 1 or self.dy > 1: if self.dx > 1 or self.dy > 1:
zz = zz[:, :, 0::self.dx, 0::self.dy].copy() zz = zz[:, :, 0::self.dx, 0::self.dy].copy()
z[0] = zz z[0] = zz
def R_op(self, inputs, eval_points):
rval = None
if eval_points[0] is not None:
rval = self.make_node(eval_points[0], inputs[1]).outputs[0]
if eval_points[1] is not None:
if rval is None:
rval = self.make_node(inputs[0], eval_points[1]).outputs[0]
else:
rval += self.make_node(inputs[0], eval_points[1]).outputs[0]
return [rval]
def grad(self, inp, grads): def grad(self, inp, grads):
inputs, kerns = inp inputs, kerns = inp
gz, = grads gz, = grads
......
...@@ -21,6 +21,8 @@ import numpy ...@@ -21,6 +21,8 @@ import numpy
from theano.gof import Op, Apply from theano.gof import Op, Apply
from theano.gradient import grad_undefined from theano.gradient import grad_undefined
from numpy.testing.noseclasses import KnownFailureTest from numpy.testing.noseclasses import KnownFailureTest
from theano.tensor.signal.downsample import DownsampleFactorMax
from theano.tensor.nnet import conv
''' '''
Special Op created to test what happens when you have one op that is not Special Op created to test what happens when you have one op that is not
...@@ -262,6 +264,51 @@ class test_RopLop(RopLop_checker): ...@@ -262,6 +264,51 @@ class test_RopLop(RopLop_checker):
self.x[:4].dimshuffle('x', 0), 0).sum(axis=1), self.x[:4].dimshuffle('x', 0), 0).sum(axis=1),
(1,)) (1,))
def test_conv(self):
for border_mode in ['valid', 'full']:
image_shape = (2, 2, 4, 5)
filter_shape = (2, 2, 2, 3)
image_dim = len(image_shape)
filter_dim = len(filter_shape)
input = tensor.TensorType(
theano.config.floatX,
[False] * image_dim)(name='input')
filters = tensor.TensorType(
theano.config.floatX,
[False] * filter_dim)(name='filter')
ev_input = tensor.TensorType(
theano.config.floatX,
[False] * image_dim)(name='ev_input')
ev_filters = tensor.TensorType(
theano.config.floatX,
[False] * filter_dim)(name='ev_filters')
def sym_conv2d(input, filters):
return conv.conv2d(input, filters, border_mode=border_mode)
output = sym_conv2d(input, filters).flatten()
yv = tensor.Rop(output, [input, filters], [ev_input, ev_filters])
rop_f = function([input, filters, ev_input, ev_filters],
yv, on_unused_input='ignore')
sy, _ = theano.scan(
lambda i, y, x1, x2, v1, v2:
(tensor.grad(y[i], x1) * v1).sum() + \
(tensor.grad(y[i], x2) * v2).sum(),
sequences = tensor.arange(output.shape[0]),
non_sequences=[output, input, filters,
ev_input, ev_filters])
scan_f = function([input, filters, ev_input, ev_filters], sy,
on_unused_input='ignore')
image_data = numpy.random.random(image_shape)
filter_data = numpy.random.random(filter_shape)
ev_image_data = numpy.random.random(image_shape)
ev_filter_data = numpy.random.random(filter_shape)
v1 = rop_f(image_data, filter_data, ev_image_data,
ev_filter_data)
v2 = scan_f(image_data, filter_data, ev_image_data,
ev_filter_data)
assert numpy.allclose(v1, v2), ("Rop mismatch: %s %s" %
(v1,v2))
def test_join(self): def test_join(self):
tv = numpy.asarray(self.rng.uniform(size=(10,)), tv = numpy.asarray(self.rng.uniform(size=(10,)),
theano.config.floatX) theano.config.floatX)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论