提交 9af9ba54 authored 作者: nouiz's avatar nouiz

Merge pull request #1082 from abalkin/issue-1080

Issue 1080: Make TensorVariable interface more similar to that of numpy.ndarray
......@@ -1634,6 +1634,9 @@ class _tensor_py_operators:
def flatten(self, ndim=1):
return flatten(self, ndim)
def ravel(self):
return flatten(self)
# CASTING
def astype(self, dtype):
return cast(self, dtype)
......@@ -1732,6 +1735,8 @@ class _tensor_py_operators:
def __rdot__(right, left):
return dot(left, right)
dot = __dot__
def sum(self, axis=None, dtype=None, keepdims=False):
"""See `theano.tensor.sum`"""
return sum(self, axis=axis, dtype=dtype, keepdims=keepdims)
......@@ -1756,6 +1761,10 @@ class _tensor_py_operators:
"""See `theano.tensor.var`"""
return var(self, axis, keepdims=keepdims)
def std(self, axis=None, keepdims=False):
"""See `theano.tensor.std`"""
return std(self, axis, keepdims=keepdims)
def min(self, axis=None, keepdims=False):
"""See `theano.tensor.min`"""
return min(self, axis, keepdims=keepdims)
......@@ -1764,6 +1773,40 @@ class _tensor_py_operators:
"""See `theano.tensor.max`"""
return max(self, axis, keepdims=keepdims)
def argmin(self, axis=None, keepdims=False):
"""See `theano.tensor.argmin`"""
return argmin(self, axis, keepdims=keepdims)
def argmax(self, axis=None, keepdims=False):
"""See `theano.tensor.argmax`"""
return argmax(self, axis, keepdims=keepdims)
def argsort(self, axis=-1, kind='quicksort', order=None):
"""See `theano.tensor.sort.argsort`"""
from theano.tensor.sort import argsort
return argsort(self, axis, kind, order)
def clip(self, a_min, a_max):
"Clip (limit) the values in an array."
return clip(self, a_min, a_max)
def conj(self):
"""See `theano.tensor.conj`"""
return conj(self)
def repeat(self, repeats, axis=None):
"""See `theano.tensor.repeat`"""
from theano.tensor.extra_ops import repeat
return repeat(self, repeats, axis)
def round(self, mode="half_away_from_zero"):
"""See `theano.tensor.round`"""
return round(self, mode)
def trace(self):
from theano.sandbox.linalg import trace
return trace(self)
# TO TRUMP NUMPY OPERATORS
__array_priority__ = 1000
......@@ -2969,12 +3012,12 @@ def psi(a):
@_scal_elemwise_with_nfunc('real', 1, -1)
def real(z):
"""Return real component of complex-valued tensor `z`"""
_tensor_py_operators.real = property(real)
@_scal_elemwise_with_nfunc('imag', 1, -1)
def imag(z):
"""Return imaginary component of complex-valued tensor `z`"""
_tensor_py_operators.imag = property(imag)
@_scal_elemwise_with_nfunc('angle', 1, -1)
def angle(z):
......
......@@ -14,7 +14,7 @@ builtin_min = __builtin__.min
from nose.plugins.skip import SkipTest
import numpy
from numpy.testing import dec
from numpy.testing import dec, assert_array_equal, assert_allclose
from numpy.testing.noseclasses import KnownFailureTest
import theano
......@@ -7001,6 +7001,85 @@ class TestInferShape(utt.InferShapeTester):
[tile(adtens4, aivec_val, ndim)],
[adtens4_val], Tile)
class TestTensorInstanceMethods(unittest.TestCase):
def setUp(self):
self.vars = matrices('X', 'Y')
self.vals = [rand(2,2),rand(2,2)]
def test_argmin(self):
X, _ = self.vars
x, _ = self.vals
assert_array_equal(X.argmin().eval({X: x}), x.argmin())
def test_argmax(self):
X, _ = self.vars
x, _ = self.vals
assert_array_equal(X.argmax().eval({X: x}), x.argmax())
def test_argsort(self):
X, _ = self.vars
x, _ = self.vals
assert_array_equal(X.argsort().eval({X: x}), x.argsort())
assert_array_equal(X.argsort(1).eval({X: x}), x.argsort(1))
def test_dot(self):
X, Y = self.vars
x, y = self.vals
Z = X.clip(0.5 - Y, 0.5 + Y)
z = x.clip(0.5 - y, 0.5 + y)
assert_array_equal(Z.eval({X: x, Y: y}), z)
def test_dot(self):
X, Y = self.vars
x, y = self.vals
assert_array_equal(x.dot(y), X.dot(Y).eval({X: x, Y: y}))
Z = X.dot(Y)
z = x.dot(y)
assert_array_equal(x.dot(z), X.dot(Z).eval({X: x, Z: z}))
def test_real_imag(self):
X, Y = self.vars
x, y = self.vals
Z = X + Y * 1j
z = x + y * 1j
assert_array_equal(Z.real.eval({Z: z}), x)
assert_array_equal(Z.imag.eval({Z: z}), y)
def test_conj(self):
X, Y = self.vars
x, y = self.vals
Z = X + Y * 1j
z = x + y * 1j
assert_array_equal(Z.conj().eval({Z: z}), z.conj())
def test_round(self):
X, _ = self.vars
x, _ = self.vals
assert_array_equal(X.round().eval({X: x}), x.round())
def test_std(self):
X, _ = self.vars
x, _ = self.vals
# std() is implemented as theano tree and does not pass its
# args directly to numpy. This sometimes results in small
# difference, so we use allclose test.
assert_allclose(X.std().eval({X: x}), x.std())
def test_repeat(self):
X, _ = self.vars
x, _ = self.vals
assert_array_equal(X.repeat(2).eval({X: x}), x.repeat(2))
def test_trace(self):
X, _ = self.vars
x, _ = self.vals
assert_array_equal(X.trace().eval({X: x}), x.trace())
def test_ravel(self):
X, _ = self.vars
x, _ = self.vals
assert_array_equal(X.ravel().eval({X: x}), x.ravel())
if __name__ == '__main__':
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论