提交 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,7 +253,12 @@ class test_CAReduce(unittest.TestCase): ...@@ -253,7 +253,12 @@ 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():
class test_Prod(unittest.TestCase):
def setUp(self):
unittest_tools.seed_rng()
def test_prod_grad(self):
x_val = numpy.asarray([[1,2,3],[4,5,6],[7,8,9]], dtype='float32') x_val = numpy.asarray([[1,2,3],[4,5,6],[7,8,9]], dtype='float32')
x = theano.tensor.dmatrix() x = theano.tensor.dmatrix()
p = Prod(axis=0)(x) p = Prod(axis=0)(x)
...@@ -280,7 +285,21 @@ def test_prod_grad(): ...@@ -280,7 +285,21 @@ def test_prod_grad():
assert numpy.allclose(g_bc_ret, assert numpy.allclose(g_bc_ret,
numpy.asarray([[6.,3.,2.],[30.,24.,20.],[72.,63.,56.]])) 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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论