提交 76ef9ebc authored 作者: Eric Larsen's avatar Eric Larsen 提交者: Frederic

testing infer_shape: op CrossentropyCategorical1Hot

上级 4b28212d
...@@ -1036,6 +1036,7 @@ class CrossentropyCategorical1HotGrad(gof.Op): ...@@ -1036,6 +1036,7 @@ class CrossentropyCategorical1HotGrad(gof.Op):
true_one_of_n[i]] true_one_of_n[i]]
g_coding_strg[0] = g_coding g_coding_strg[0] = g_coding
crossentropy_categorical_1hot_grad = CrossentropyCategorical1HotGrad() crossentropy_categorical_1hot_grad = CrossentropyCategorical1HotGrad()
...@@ -1094,6 +1095,9 @@ class CrossentropyCategorical1Hot(gof.Op): ...@@ -1094,6 +1095,9 @@ class CrossentropyCategorical1Hot(gof.Op):
y[i] = -numpy.log(coding[i, one_of_n[i]]) y[i] = -numpy.log(coding[i, one_of_n[i]])
y_out[0] = y y_out[0] = y
def infer_shape(self, node, in_shapes):
return [(in_shapes[0][0],)]
def grad(self, inp, grads): def grad(self, inp, grads):
coding, one_of_n = inp coding, one_of_n = inp
g_y, = grads g_y, = grads
......
...@@ -19,6 +19,7 @@ from theano.tensor.nnet import (categorical_crossentropy, ...@@ -19,6 +19,7 @@ from theano.tensor.nnet import (categorical_crossentropy,
crossentropy_softmax_argmax_1hot_with_bias, crossentropy_softmax_argmax_1hot_with_bias,
CrossentropySoftmax1HotWithBiasDx, CrossentropySoftmax1HotWithBiasDx,
CrossentropySoftmaxArgmax1HotWithBias, CrossentropySoftmaxArgmax1HotWithBias,
CrossentropyCategorical1Hot,
sigmoid, softplus, sigmoid, softplus,
Softmax, softmax, SoftmaxWithBias, softmax_grad, Softmax, softmax, SoftmaxWithBias, softmax_grad,
softmax_with_bias, SoftmaxGrad, softmax_with_bias, SoftmaxGrad,
...@@ -281,10 +282,8 @@ class T_prepend(utt.InferShapeTester): ...@@ -281,10 +282,8 @@ class T_prepend(utt.InferShapeTester):
[adscal_val, admat_val], [adscal_val, admat_val],
Prepend_scalar_to_each_row) Prepend_scalar_to_each_row)
class T_CrossentropyCategorical1Hot(unittest.TestCase):
def setUp(self): class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
utt.seed_rng()
def test_grad(self): def test_grad(self):
x = tensor.matrix('x') x = tensor.matrix('x')
one_of_n = tensor.lvector('one_of_n') one_of_n = tensor.lvector('one_of_n')
...@@ -306,6 +305,16 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase): ...@@ -306,6 +305,16 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
tensor.verify_grad(oplike, [x_val], rng=numpy.random) tensor.verify_grad(oplike, [x_val], rng=numpy.random)
def test_infer_shape(self):
admat = dmatrix()
alvec = lvector()
rng = numpy.random.RandomState(utt.fetch_seed())
admat_val = rng.rand(3, 2)
alvec_val = [0, 1, 0]
self._compile_and_check([admat, alvec],
[CrossentropyCategorical1Hot()(admat, alvec)],
[admat_val, alvec_val],
CrossentropyCategorical1Hot)
def test_softmax_optimizations(self): def test_softmax_optimizations(self):
x = tensor.matrix('x') x = tensor.matrix('x')
...@@ -1150,11 +1159,9 @@ class Test_softmax_opt: ...@@ -1150,11 +1159,9 @@ class Test_softmax_opt:
if __name__ == '__main__': if __name__ == '__main__':
t = T_prepend('setUp') t = T_CrossentropyCategorical1Hot('setUp')
t.setUp() t.setUp()
t.test_infer_shape() t.test_infer_shape()
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论