提交 14b93781 authored 作者: Alexander Matyasko's avatar Alexander Matyasko

Update tests for downsample Rop

上级 9866ce20
...@@ -593,7 +593,7 @@ class Pool(OpenMPOp): ...@@ -593,7 +593,7 @@ class Pool(OpenMPOp):
if eval_points[0] is None: if eval_points[0] is None:
return [None] return [None]
x, ws, stride, pad = inputs x, ws, stride, pad = inputs
rop = MaxPoolRop(ignore_border=self.ignore_border) rop = MaxPoolRop(ignore_border=self.ignore_border, ndim=self.ndim)
return [rop(x, eval_points[0], ws, stride=stride, pad=pad)] return [rop(x, eval_points[0], ws, stride=stride, pad=pad)]
def c_headers(self): def c_headers(self):
......
...@@ -17,6 +17,7 @@ from theano.tests import unittest_tools as utt ...@@ -17,6 +17,7 @@ from theano.tests import unittest_tools as utt
from theano import function from theano import function
import theano import theano
from theano import tensor from theano import tensor
import itertools
import numpy 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
...@@ -258,25 +259,42 @@ class test_RopLop(RopLop_checker): ...@@ -258,25 +259,42 @@ class test_RopLop(RopLop_checker):
def test_downsample(self): def test_downsample(self):
rng = numpy.random.RandomState(utt.fetch_seed()) rng = numpy.random.RandomState(utt.fetch_seed())
test_ws = ((1, 1), (3, 2), (2, 3)) # ws, shp
vx = (rng.rand(2, 3, 3, 4) * 2.0).astype(theano.config.floatX) examples = (
vv = (rng.rand(2, 3, 3, 4) * 2.0).astype(theano.config.floatX) ((2,), (16,)),
input = theano.shared(vx) ((2,), (4, 16,)),
eval_p = theano.shared(vv) ((2,), (4, 2, 16,)),
for ws in test_ws: ((1, 1), (4, 2, 16, 16)),
for ignore_border in [False, True]: ((2, 2), (4, 2, 16, 16)),
out = Pool(ignore_border)(input, ws).flatten() ((3, 3), (4, 2, 16, 16)),
yv = tensor.Rop(out, input, eval_p) ((3, 2), (4, 2, 16, 16)),
rop_f = function([], yv, on_unused_input='ignore') ((3, 2, 2), (3, 2, 16, 16, 16)),
sy, _ = theano.scan(lambda i, y, x, v: ((2, 3, 2), (3, 2, 16, 16, 16)),
(tensor.grad(y[i], x) * v).sum(), ((2, 2, 3), (3, 2, 16, 16, 16)),
sequences=tensor.arange(out.shape[0]), ((2, 2, 3, 2), (3, 2, 6, 6, 6, 5)),
non_sequences=[out, input, eval_p]) )
scan_f = function([], sy, on_unused_input='ignore')
v1 = rop_f() for example, ignore_border in itertools.product(examples, [True, False]):
v2 = scan_f() (ws, shp) = example
assert numpy.allclose(v1, v2), ("Rop mismatch: %s %s" % vx = rng.rand(*shp)
(v1, v2)) vex = rng.rand(*shp)
x = theano.shared(vx)
ex = theano.shared(vex)
maxpool_op = Pool(ignore_border, ndim=len(ws))
a_pooled = maxpool_op(x, ws).flatten()
yv = tensor.Rop(a_pooled, x, ex)
rop_f = function([], yv, on_unused_input='ignore')
sy, _ = theano.scan(lambda i, y, x, v:
(tensor.grad(y[i], x) * v).sum(),
sequences=tensor.arange(a_pooled.shape[0]),
non_sequences=[a_pooled, x, ex])
scan_f = function([], sy, on_unused_input='ignore')
v1 = rop_f()
v2 = scan_f()
assert numpy.allclose(v1, v2), ("Rop mismatch: %s %s" %
(v1, v2))
def test_conv(self): def test_conv(self):
for conv_op in [conv.conv2d, conv2d]: for conv_op in [conv.conv2d, conv2d]:
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论