提交 401a00a4 authored 作者: Hengjean's avatar Hengjean

added ptp() and associated case test

上级 5d221f46
......@@ -5000,3 +5000,27 @@ 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, unless out was specified, in which case a reference to out is returned.
"""
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,62 @@ 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)
maxLessMin=[numpy.amax(y)-numpy.amin(y)]
self.assertTrue(result==maxLessMin)
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)
maxLessMin=[numpy.amax(i)-numpy.amin(i) for i in y]
self.assertTrue(numpy.array_equal(result,maxLessMin))
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)
y = numpy.swapaxes(y, 0,1)
maxLessMin=[numpy.amax(i)-numpy.amin(i) for i in y]
self.assertTrue(numpy.array_equal(result,maxLessMin))
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)
maxLessMin=[numpy.amax(i)-numpy.amin(i) for i in y]
if __name__ == '__main__':
t = TestInferShape('setUp')
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论