提交 e916e90e authored 作者: Hengjean's avatar Hengjean

formatted to pep8 and added doc

上级 401a00a4
......@@ -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
========
......
......@@ -5001,26 +5001,23 @@ def 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.
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.
: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.
:return : A new array holding the result.
"""
a = as_tensor_variable(a)
out = max (a, axis)-min(a, axis)
return out
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,ptp)
stacklists, DimShuffle, hessian, ptp)
from theano.tests import unittest_tools as utt
......@@ -6777,6 +6777,7 @@ 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):
"""
......@@ -6785,54 +6786,55 @@ class test_ptp(unittest.TestCase):
x = scalar('x')
p = ptp(x)
f = theano.function([x], p)
self.assertTrue(f(rand()*20000-10000)==0)
self.assertTrue(f(rand() * 20000 - 10000) == 0)
def test_vector(self):
x = vector('x')
p = ptp(x,0)
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)
maxLessMin = [numpy.amax(y) - numpy.amin(y)]
self.assertTrue(result == maxLessMin)
def test_matrix_first_axis(self):
x = matrix('x')
p = ptp(x,1)
p = ptp(x, 1)
f = theano.function([x], p)
y = rand_ranged(-1000, 1000, [100,100])
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))
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)
p = ptp(x, 0)
f = theano.function([x], p)
y = rand_ranged(-1000, 1000, [100,100])
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))
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)
p = ptp(x, -1)
f = theano.function([x], p)
y = rand_ranged(-1000, 1000, [100,100])
y = rand_ranged(-1000, 1000, [100, 100])
result = f(y)
maxLessMin=[numpy.amax(i)-numpy.amin(i) for i in y]
maxLessMin = [numpy.amax(i) - numpy.amin(i) for i in y]
self.assertTrue(numpy.array_equal(result, maxLessMin))
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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论