提交 3294825e authored 作者: fsavard's avatar fsavard

Added tests based on verify_grad for both elemise:Prod and sandbox.neighbours:grad

上级 9b57d59d
...@@ -7,6 +7,8 @@ from neighbours import images2neibs, neibs2images, Images2Neibs, GpuImages2Neibs ...@@ -7,6 +7,8 @@ from neighbours import images2neibs, neibs2images, Images2Neibs, GpuImages2Neibs
from nose.plugins.skip import SkipTest from nose.plugins.skip import SkipTest
import theano.sandbox.cuda as cuda import theano.sandbox.cuda as cuda
from theano.tests import unittest_tools
if theano.config.mode=='FAST_COMPILE': if theano.config.mode=='FAST_COMPILE':
mode_with_gpu = theano.compile.mode.get_mode('FAST_RUN').including('gpu') mode_with_gpu = theano.compile.mode.get_mode('FAST_RUN').including('gpu')
mode_without_gpu = theano.compile.mode.get_mode('FAST_RUN').excluding('gpu') mode_without_gpu = theano.compile.mode.get_mode('FAST_RUN').excluding('gpu')
...@@ -375,9 +377,18 @@ def test_neibs_grad(): ...@@ -375,9 +377,18 @@ def test_neibs_grad():
assert numpy.allclose(got[0], should_get[0]) assert numpy.allclose(got[0], should_get[0])
assert numpy.allclose(got[1], should_get[1]) assert numpy.allclose(got[1], should_get[1])
def test_neibs_grad_verify_grad():
shape = (2,3,4,4)
images = T.dtensor4()
images_val = numpy.arange(numpy.prod(shape), dtype='float32').reshape(shape)
def fn(images):
return T.sum(T.sqr(images2neibs(images, (2,2))), axis=[0,1])
unittest_tools.verify_grad(fn, [images_val])
if __name__ == '__main__': if __name__ == '__main__':
#test_neibs_gpu() #test_neibs_gpu()
#test_neibs() #test_neibs()
test_neibs_grad() test_neibs_grad_verify_grad()
...@@ -253,34 +253,53 @@ class test_CAReduce(unittest.TestCase): ...@@ -253,34 +253,53 @@ class test_CAReduce(unittest.TestCase):
#self.with_linker(gof.CLinker(), or_) #self.with_linker(gof.CLinker(), or_)
#self.with_linker(gof.CLinker(), and_) #self.with_linker(gof.CLinker(), and_)
def test_prod_grad():
x_val = numpy.asarray([[1,2,3],[4,5,6],[7,8,9]], dtype='float32') class test_Prod(unittest.TestCase):
x = theano.tensor.dmatrix() def setUp(self):
p = Prod(axis=0)(x) unittest_tools.seed_rng()
# sanity check def test_prod_grad(self):
fn = theano.function([x], [p]) x_val = numpy.asarray([[1,2,3],[4,5,6],[7,8,9]], dtype='float32')
assert numpy.allclose(fn(x_val), numpy.array([ 28., 80., 162.])) x = theano.tensor.dmatrix()
p = Prod(axis=0)(x)
# very basic case for the product; no broadcasting in x
g = theano.tensor.grad(p.sum(), x) # sanity check
g_fn = theano.function([x], g) fn = theano.function([x], [p])
assert numpy.allclose(g_fn(x_val), assert numpy.allclose(fn(x_val), numpy.array([ 28., 80., 162.]))
numpy.asarray([[28.,40.,54.],[7.,16.,27.],[4.,10.,18.]]))
# very basic case for the product; no broadcasting in x
# now with some tranposition in input g = theano.tensor.grad(p.sum(), x)
x_bc = x.dimshuffle(1, 0) g_fn = theano.function([x], g)
p_bc = Prod(axis=0)(x_bc) assert numpy.allclose(g_fn(x_val),
p_bc_sum = p_bc.sum() numpy.asarray([[28.,40.,54.],[7.,16.,27.],[4.,10.,18.]]))
g_bc = theano.tensor.grad(p_bc_sum, x)
g_fn_bc = theano.function([x], [p_bc,g_bc]) # now with some tranposition in input
p_bc_ret, g_bc_ret = g_fn_bc(x_val) x_bc = x.dimshuffle(1, 0)
p_bc = Prod(axis=0)(x_bc)
assert numpy.allclose(p_bc_ret, numpy.array([ 6., 120., 504.])) p_bc_sum = p_bc.sum()
assert numpy.allclose(g_bc_ret, g_bc = theano.tensor.grad(p_bc_sum, x)
numpy.asarray([[6.,3.,2.],[30.,24.,20.],[72.,63.,56.]])) g_fn_bc = theano.function([x], [p_bc,g_bc])
p_bc_ret, g_bc_ret = g_fn_bc(x_val)
assert numpy.allclose(p_bc_ret, numpy.array([ 6., 120., 504.]))
assert numpy.allclose(g_bc_ret,
numpy.asarray([[6.,3.,2.],[30.,24.,20.],[72.,63.,56.]]))
def test_verify_grad(self):
x_val = numpy.asarray([[1,2,3],[4,5,6],[7,8,9]], dtype='float32')
x = theano.tensor.dmatrix()
# now with verify_grad
unittest_tools.verify_grad(Prod(axis=0), [x_val])
# second time, with some added complexity
# verify_grad takes the sum of the matrices anyway
def fn(x2):
return theano.tensor.sqr(Prod(axis=0)(x2))
unittest_tools.verify_grad(fn, [x_val])
if __name__ == '__main__': if __name__ == '__main__':
#unittest.main() unittest.main()
test_prod_grad() #suite = unittest.TestSuite([test_Prod('test_prod_grad')])
#unittest.TextTestRunner().run(suite)
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论