提交 61944a99 authored 作者: Kumar Krishna Agrawal's avatar Kumar Krishna Agrawal

Uses corrected two pass algorithm in theano.tensor.var

上级 a668c6c5
......@@ -3126,7 +3126,7 @@ def mean(input, axis=None, dtype=None, op=False, keepdims=False,
@constructor
def var(input, axis=None, keepdims=False):
def var(input, axis=None, keepdims=False, corrected=False):
"""
Computes the variance along the given axis(es) of a tensor `input`.
......@@ -3139,6 +3139,9 @@ def var(input, axis=None, keepdims=False):
If this is set to True, the axes which are reduced are
left in the result as dimensions with size one. With this option,
the result will broadcast correctly against the original tensor.
corrected : bool
If this is set to True, the 'corrected_two_pass' algorithm is
used to compute the variance.
Notes
-----
......@@ -3167,6 +3170,12 @@ def var(input, axis=None, keepdims=False):
# return the mean sqr
v = mean((centered_input ** 2), axis, keepdims=keepdims)
# use 'corrected_two_pass' algorithm
if corrected :
error = mean(centered_input, axis, keepdims=keepdims)**2
v = v - error
v.name = 'var'
return v
......
......@@ -6332,6 +6332,8 @@ def test_var():
f = function([a], var(a, axis=2))
assert numpy.allclose(numpy.var(a_val, axis=2), f(a_val))
f = function([a], var(a, corrected=True))
assert numpy.allclose(numpy.var(a_val), f(a_val))
class T_sum(unittest.TestCase):
def test_sum_overflow(self):
......
......@@ -625,9 +625,10 @@ class _tensor_py_operators(object):
dtype=dtype, keepdims=keepdims,
acc_dtype=acc_dtype)
def var(self, axis=None, keepdims=False):
def var(self, axis=None, keepdims=False, corrected=False):
"""See `theano.tensor.var`."""
return theano.tensor.basic.var(self, axis, keepdims=keepdims)
return theano.tensor.basic.var(self, axis, keepdims=keepdims,
corrected=corrected)
def std(self, axis=None, keepdims=False):
"""See `theano.tensor.std`."""
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论