提交 7a5a89af authored 作者: Arnaud Bergeron's avatar Arnaud Bergeron

Fix perform() of SoftmaxWithBias when faced with multiple dtypes.

上级 3f734f94
......@@ -80,10 +80,20 @@ class SoftmaxWithBias(gof.Op):
# sm[i] *= 1.0 / numpy.sum(sm[i])
# output_storage[0][0] = sm
if x.size == 0:
# Numpy doesn't like the max of a zero-sized object.
output_storage[0][0] = numpy.zeros(x.shape, dtype=x.dtype)
return
x_dtype = x.dtype
# Perform computations in float32 otherwise the result is too imprecise
if x.dtype == 'float16':
x = x.astype('float32')
x_plus_b = x + b[None, :]
e_x = numpy.exp(x_plus_b - x_plus_b.max(axis=1)[:, None])
e_x *= 1.0 / e_x.sum(axis=1)[:, None]
output_storage[0][0] = e_x
output_storage[0][0] = e_x.astype(x_dtype)
def grad(self, inp, grads):
x, b = inp
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论