提交 4b28212d authored 作者: Eric Larsen's avatar Eric Larsen 提交者: Frederic

testing infer_shape: ops Prepend_scalar_constant_to_each_row and Prepend_scalar_to_each_row

上级 91879477
......@@ -1648,6 +1648,11 @@ class Prepend_scalar_constant_to_each_row(gof.Op):
out[:, 0].fill(self.val.data)
out[:, 1:] = mat
def infer_shape(self, node, in_shapes):
shp = (in_shapes[0][0], in_shapes[0][1] + 1)
return [shp]
def grad(self, inp, grads):
mat, = inp
goutput, = grads
......@@ -1697,6 +1702,10 @@ class Prepend_scalar_to_each_row(gof.Op):
out[:, 0].fill(val)
out[:, 1:] = mat
def infer_shape(self, node, in_shapes):
shp = (in_shapes[1][0], in_shapes[1][1] + 1)
return [shp]
def grad(self, inp, grads):
val, mat = inp
goutput, = grads
......
......@@ -24,7 +24,7 @@ from theano.tensor.nnet import (categorical_crossentropy,
softmax_with_bias, SoftmaxGrad,
Prepend_scalar_constant_to_each_row,
Prepend_scalar_to_each_row)
from theano.tensor import dmatrix, dvector, lvector
from theano.tensor import dmatrix, dvector, lvector, dscalar
class T_sigmoid(unittest.TestCase):
def setUp(self):
......@@ -237,11 +237,9 @@ class T_CrossentropySoftmaxArgmax1HotWithBias(utt.InferShapeTester):
CrossentropySoftmaxArgmax1HotWithBias)
class T_prepend(unittest.TestCase):
def setUp(self):
utt.seed_rng()
class T_prepend(utt.InferShapeTester):
def test0(self):
"""basic functionality"""
x=tensor.matrix('x')
y=Prepend_scalar_constant_to_each_row(4.)(x)
f=theano.function([x],[y])
......@@ -250,8 +248,18 @@ class T_prepend(unittest.TestCase):
self.assertTrue(my.shape == (3, 6), my.shape)
self.assertTrue(numpy.all( my[:,0] == 4.0))
def test_infer_shape(self):
admat = dmatrix()
rng = numpy.random.RandomState(utt.fetch_seed())
admat_val = rng.rand(3, 5)
adscal_val = rng.rand()
self._compile_and_check([admat],
[Prepend_scalar_constant_to_each_row(adscal_val)(admat)],
[admat_val],
Prepend_scalar_constant_to_each_row)
class T_prepend(unittest.TestCase):
class T_prepend(utt.InferShapeTester):
def test0(self):
"""basic functionality"""
x=tensor.matrix('x')
......@@ -262,6 +270,17 @@ class T_prepend(unittest.TestCase):
self.assertTrue(my.shape == (3, 6))
self.assertTrue(numpy.all(my[:,0] == 5.0))
def test_infer_shape(self):
admat = dmatrix()
adscal = dscalar()
rng = numpy.random.RandomState(utt.fetch_seed())
admat_val = rng.rand(3, 5)
adscal_val = rng.rand()
self._compile_and_check([adscal, admat],
[Prepend_scalar_to_each_row()(adscal, admat)],
[adscal_val, admat_val],
Prepend_scalar_to_each_row)
class T_CrossentropyCategorical1Hot(unittest.TestCase):
def setUp(self):
utt.seed_rng()
......@@ -1131,7 +1150,7 @@ class Test_softmax_opt:
if __name__ == '__main__':
t = T_CrossentropySoftmaxArgmax1HotWithBias('setUp')
t = T_prepend('setUp')
t.setUp()
t.test_infer_shape()
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论