提交 6317960c authored 作者: Frédéric Bastien's avatar Frédéric Bastien

Merge pull request #1847 from Hengjean/ptp

added ptp() and associated case test
......@@ -966,6 +966,18 @@ Reductions
* an *int* - computed along this axis
* a *list of ints* - computed along these axes
.. function:: ptp(x, axis = None)
Range of values (maximum - minimum) along an axis.
The name of the function comes from the acronym for peak to peak.
:Parameter: *x* Input tensor.
:Parameter: *axis* Axis along which to find the peaks. By default,
flatten the array.
:Returns: A new array holding the result.
Indexing
========
......
......@@ -5000,3 +5000,24 @@ def stacklists(arg):
return stack(*map(stacklists, arg))
else:
return arg
def ptp(a, axis=None):
"""
Range of values (maximum - minimum) along an axis.
The name of the function comes from the acronym for peak to peak.
:param a : Input tensor.
:param axis : Axis along which to find the peaks. By default,
flatten the array.
:return : A new array holding the result.
"""
a = as_tensor_variable(a)
out = max(a, axis) - min(a, axis)
return out
......@@ -45,7 +45,7 @@ from theano.tensor import (_shared, wvector, bvector, autocast_float_as,
dtensor3, SpecifyShape, Mean,
itensor3, Tile, switch, Diagonal, Diag,
nonzero, flatnonzero, nonzero_values,
stacklists, DimShuffle, hessian)
stacklists, DimShuffle, hessian, ptp)
from theano.tests import unittest_tools as utt
......@@ -6777,6 +6777,74 @@ def test_norm():
f = theano.function([x], n)
assert numpy.allclose(f([1, 1]), numpy.sqrt(2))
class test_ptp(unittest.TestCase):
def test_scalar(self):
"""
Should return 0 for all scalar
"""
x = scalar('x')
p = ptp(x)
f = theano.function([x], p)
self.assertTrue(f(rand() * 20000 - 10000) == 0)
def test_vector(self):
x = vector('x')
p = ptp(x, 0)
f = theano.function([x], p)
y = rand_ranged(-1000, 1000, [100])
result = f(y)
numpyResult = numpy.ptp(y, 0)
self.assertTrue(numpy.array_equal(result, numpyResult))
def test_matrix_first_axis(self):
x = matrix('x')
p = ptp(x, 1)
f = theano.function([x], p)
y = rand_ranged(-1000, 1000, [100, 100])
result = f(y)
numpyResult = numpy.ptp(y, 1)
self.assertTrue(numpy.array_equal(result, numpyResult))
def test_matrix_second_axis(self):
x = matrix('x')
p = ptp(x, 0)
f = theano.function([x], p)
y = rand_ranged(-1000, 1000, [100, 100])
result = f(y)
numpyResult = numpy.ptp(y, 0)
self.assertTrue(numpy.array_equal(result, numpyResult))
def test_matrix_neg_axis(self):
x = matrix('x')
p = ptp(x, -1)
f = theano.function([x], p)
y = rand_ranged(-1000, 1000, [100, 100])
result = f(y)
numpyResult = numpy.ptp(y, -1)
self.assertTrue(numpy.array_equal(result, numpyResult))
def test_matrix_no_axis(self):
x = matrix('x')
p = ptp(x)
f = theano.function([x], p)
y = rand_ranged(-1000, 1000, [100, 100])
result = f(y)
numpyResult = numpy.ptp(y)
self.assertTrue(numpy.array_equal(result, numpyResult))
if __name__ == '__main__':
t = TestInferShape('setUp')
......
......@@ -552,6 +552,11 @@ class _tensor_py_operators:
def cumprod(self, axis=None):
return theano.tensor.extra_ops.cumprod(self, axis)
def ptp(self, axis=None):
"""see 'theano.tensor.ptp'"""
return theano.tensor.ptp(self, axis)
class TensorVariable(_tensor_py_operators, Variable):
"""Subclass to add the tensor operators to the basic `Variable` class."""
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论