提交 3f0f9def authored 作者: Frederic Bastien's avatar Frederic Bastien

Make sure crossentropy op always raise error for negative index

上级 c75f7892
......@@ -863,7 +863,8 @@ class CrossentropySoftmaxArgmax1HotWithBias(gof.Op):
raise ValueError('b must have same number of columns as x')
if y_idx.shape[0] != x.shape[0]:
raise ValueError('y_idx must have same number of rows as x')
if any(y_idx < 0):
raise ValueError("y_i value out of bounds")
sm = numpy.zeros_like(x) # softmax
nll = numpy.zeros(x.shape[0], dtype=node.outputs[0].type.
dtype) # nll(y | softmax(x))
......@@ -1066,6 +1067,8 @@ class CrossentropySoftmax1HotWithBiasDx(gof.Op):
def perform(self, node, input_storage, output_storage):
dy, sm, y_idx = input_storage
if any(y_idx < 0):
raise ValueError("y_i value out of bounds")
dx = numpy.zeros_like(sm)
if dy.ndim == 0:
dy = dy[None]
......@@ -1096,7 +1099,7 @@ class CrossentropySoftmax1HotWithBiasDx(gof.Op):
return [g_dy, g_sm, g_y_idx]
def c_code_cache_version(self):
return (5,)
return (6,)
def c_code(self, node, name, inp, out, sub):
dnll, sm, y_idx = inp
......@@ -1193,9 +1196,9 @@ class CrossentropySoftmax1HotWithBiasDx(gof.Op):
{
dx_i[j * Sdx] = dnll_i * sm_i[j * Ssm];
}
if (y_i >= PyArray_DIMS(%(dx)s)[1])
if (y_i >= PyArray_DIMS(%(dx)s)[1] || (y_i < 0))
{
PyErr_SetString(PyExc_ValueError, "y_i >= dx dimensions[1]");
PyErr_SetString(PyExc_ValueError, "y_i >= dx dimensions[1] or y_i < 0.");
%(fail)s;
}
dx_i[y_i * Sdx] -= dnll_i;
......
......@@ -235,6 +235,20 @@ class T_CrossentropySoftmax1HotWithBiasDx(utt.InferShapeTester):
[advec_val, admat_val, alvec_val],
CrossentropySoftmax1HotWithBiasDx)
def test_neg_idx(self):
admat = matrix()
advec = vector()
alvec = lvector()
rng = numpy.random.RandomState(utt.fetch_seed())
admat_val = rng.rand(10, 5).astype(config.floatX)
admat_val /= admat_val.sum(axis=1).reshape(10, 1)
advec_val = rng.rand(10).astype(config.floatX)
alvec_val = rng.randint(low=0, high=5, size=10)
alvec_val[1] = -1
out = CrossentropySoftmax1HotWithBiasDx()(advec, admat, alvec)
f = theano.function([advec, admat, alvec], out)
self.assertRaises(ValueError, f, advec_val, admat_val, alvec_val)
class T_CrossentropySoftmaxArgmax1HotWithBias(utt.InferShapeTester):
......@@ -278,6 +292,19 @@ class T_CrossentropySoftmaxArgmax1HotWithBias(utt.InferShapeTester):
[admat_val, advec_val, alvec_val],
CrossentropySoftmaxArgmax1HotWithBias)
def test_neg_idx(self):
admat = matrix()
advec = vector()
alvec = lvector()
rng = numpy.random.RandomState(utt.fetch_seed())
admat_val = rng.rand(3, 5).astype(config.floatX)
advec_val = rng.rand(5).astype(config.floatX)
alvec_val = rng.randint(low=0, high=5, size=3)
alvec_val[1] = -1
out = CrossentropySoftmaxArgmax1HotWithBias()(admat, advec, alvec)
f = theano.function([admat, advec, alvec], out)
self.assertRaises(ValueError, f, admat_val, advec_val, alvec_val)
class T_prepend(utt.InferShapeTester):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论