提交 e404993d authored 作者: Razvan Pascanu's avatar Razvan Pascanu

added eye and identity_like ops + some documentation for them and for zeros_like, ones_like, fill

上级 7e4e3a42
...@@ -293,6 +293,7 @@ them perfectly, but a dscalar otherwise. ...@@ -293,6 +293,7 @@ them perfectly, but a dscalar otherwise.
:rtype: :class:`TensorVariable` or :class:`TensorConstant` :rtype: :class:`TensorVariable` or :class:`TensorConstant`
TensorType and TensorVariable TensorType and TensorVariable
============================= =============================
...@@ -581,6 +582,42 @@ dimensions, see :meth:`_tensor_py_operators.dimshuffle` ...@@ -581,6 +582,42 @@ dimensions, see :meth:`_tensor_py_operators.dimshuffle`
have shape (2, 60). have shape (2, 60).
.. function:: zeros_like(x)
:param x: tensor that has same shape as output
Returns a tensor filled with 0s that has same shape as `x`.
.. function:: ones_like(x)
:param x: tensor that has same shape as output
Returns a tensor filled with 1s that has same shape as `x`.
.. function:: fill(a,b)
:param a: tensor that has same shape as output
:param b: theano scalar or value with which you want to fill the output
Create a matrix by filling the shape of `a` with `b`
.. function:: eye(n, m = None, k = 0, dtype='float64')
:param n: number of rows in output (value or theano scalar)
:param m: number of columns in output (value or theano scalar)
:param k: Index of the diagonal: 0 refers to the main diagonal,
a positive value refers to an upper diagonal, and a
negative value to a lower diagonal. It can be a theano
scalar.
:returns: An array where all elements are equal to zero, except for the `k`-th
diagonal, whose values are equal to one.
.. function:: identity_like(x)
:param x: tensor
:returns: A tensor of same shape as `x` that is filled with 0s everywhere
except for the main diagonal, whose values are equal to one. The output
will have same dtype as `x`.
Reductions Reductions
========== ==========
...@@ -743,6 +780,7 @@ Casting ...@@ -743,6 +780,7 @@ Casting
Return the imaginary components of Tensor x. Return the imaginary components of Tensor x.
For non-complex `x` this function returns zeros_like(x). For non-complex `x` this function returns zeros_like(x).
Comparisons Comparisons
------------ ------------
......
...@@ -1702,6 +1702,37 @@ def zeros_like(model): ...@@ -1702,6 +1702,37 @@ def zeros_like(model):
#return Zeros(model.type.ndim)(shape(model)) #return Zeros(model.type.ndim)(shape(model))
return fill(model, constant(0.0, dtype=model.type.dtype)) return fill(model, constant(0.0, dtype=model.type.dtype))
class Eye(gof.Op):
def __init__(self, dtype='float64'):
self.dtype = dtype
def make_node(self,n,m,k):
n = as_tensor_variable(n)
m = as_tensor_variable(m)
k = as_tensor_variable(k)
return gof.Apply(self, [n,m,k], [TensorType(dtype = self.dtype, broadcastable = (False,False))()])
def perform(self, node, (n,m,k), (out,)):
out[0] = numpy.eye(n,m,k)
def grad(self, (n,m,k),(gout,)):
return [None, None, None]
def __eq__(self,other):
return type(self) == type(other) and self.dtype == other.dtype
def __hash__(self):
return hash(self.dtype) ^ hash(type(self))
def eye(n, m=None, k = 0, dtype = 'float64'):
if m == None:
m = n
localop = Eye(dtype)
return localop(n,m,k)
def identity_like(x):
return eye(x.shape[0], x.shape[1], k=0, dtype = x.dtype)
if 0: if 0:
## COMMENTED OUT FEB 17 2010 ## COMMENTED OUT FEB 17 2010
## TODO (DOCUMENT AND WRITE TESTS) OR DELETE ## TODO (DOCUMENT AND WRITE TESTS) OR DELETE
......
...@@ -1143,6 +1143,13 @@ class test_bitwise(unittest.TestCase): ...@@ -1143,6 +1143,13 @@ class test_bitwise(unittest.TestCase):
v = fn(l, r) v = fn(l, r)
self.failUnless(numpy.all(v == (~l)), (l, r, v)) self.failUnless(numpy.all(v == (~l)), (l, r, v))
def test_eye(self):
n = iscalar()
m = iscalar()
k = iscalar()
fn = theano.function([m,n,k],eye(m,n,k) )
self.failUnless(numpy.all(fn(5,6,1) == numpy.eye(5,6,1)))
class T_add(unittest.TestCase): class T_add(unittest.TestCase):
def setUp(self): def setUp(self):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论