提交 227be8bf authored 作者: Iban Harlouchet's avatar Iban Harlouchet

Added multiple corrections to basic.py

上级 72798782
...@@ -578,7 +578,6 @@ class CSM(gof.Op): ...@@ -578,7 +578,6 @@ class CSM(gof.Op):
_hashval = None _hashval = None
"""Pre-computed hash value, defined by __init__""" """Pre-computed hash value, defined by __init__"""
__props__ = ()
def __init__(self, format, kmap=None): def __init__(self, format, kmap=None):
if format not in ('csr', 'csc'): if format not in ('csr', 'csc'):
...@@ -599,6 +598,13 @@ class CSM(gof.Op): ...@@ -599,6 +598,13 @@ class CSM(gof.Op):
self._hashval = (hash(type(self)) ^ hash(self.format) ^ self._hashval = (hash(type(self)) ^ hash(self.format) ^
_kmap_hash(self.kmap)) _kmap_hash(self.kmap))
def __eq__(self, other):
return (type(other) is CSM and other.format == self.format and
_kmap_eq(self.kmap, other.kmap))
def __hash__(self):
return self._hashval
def __str__(self): def __str__(self):
if self.kmap is not None: if self.kmap is not None:
return "%s{%s}" % (self.__class__.__name__, str(self.kmap)) return "%s{%s}" % (self.__class__.__name__, str(self.kmap))
...@@ -752,7 +758,6 @@ class CSMGrad(gof.op.Op): ...@@ -752,7 +758,6 @@ class CSMGrad(gof.op.Op):
# 2. The elements in the sparse dimension are not guaranteed to be sorted. # 2. The elements in the sparse dimension are not guaranteed to be sorted.
# Therefore, the input data vector may have a different order than the # Therefore, the input data vector may have a different order than the
# gradient data vector. # gradient data vector.
__props__ = ("kmap",)
def __init__(self, kmap=None): def __init__(self, kmap=None):
self.kmap = kmap self.kmap = kmap
...@@ -761,6 +766,12 @@ class CSMGrad(gof.op.Op): ...@@ -761,6 +766,12 @@ class CSMGrad(gof.op.Op):
# if self.kmap is None: # if self.kmap is None:
# self.view_map = {0: [1]} # self.view_map = {0: [1]}
def __eq__(self, other):
return type(self) == type(other) and _kmap_eq(self.kmap, other.kmap)
def __hash__(self):
return 82345 ^ hash(type(self)) ^ _kmap_hash(self.kmap)
def __str__(self): def __str__(self):
return "%s{%s}" % ( return "%s{%s}" % (
self.__class__.__name__, self.__class__.__name__,
...@@ -885,7 +896,7 @@ def cast(variable, dtype): ...@@ -885,7 +896,7 @@ def cast(variable, dtype):
class DenseFromSparse(gof.op.Op): class DenseFromSparse(gof.op.Op):
# See doc in instance of this Op or function after this class definition. # See doc in instance of this Op or function after this class definition.
__props__ = () __props__ = () # We don't put sparse_grad in the props.
def __init__(self, structured=True): def __init__(self, structured=True):
self.sparse_grad = structured self.sparse_grad = structured
...@@ -1041,9 +1052,6 @@ class GetItemList(gof.op.Op): ...@@ -1041,9 +1052,6 @@ class GetItemList(gof.op.Op):
return [GetItemListGrad(self)(x, indices, gout), return [GetItemListGrad(self)(x, indices, gout),
grad_undefined(self, 1, indices, "No gradient for this input")] grad_undefined(self, 1, indices, "No gradient for this input")]
def __str__(self):
return self.__class__.__name__
get_item_list = GetItemList() get_item_list = GetItemList()
"""Select row of sparse matrix, """Select row of sparse matrix,
returning them as a new sparse matrix. returning them as a new sparse matrix.
...@@ -1095,9 +1103,6 @@ class GetItemListGrad(gof.op.Op): ...@@ -1095,9 +1103,6 @@ class GetItemListGrad(gof.op.Op):
out[0] = y out[0] = y
def __str__(self):
return self.__class__.__name__
get_item_list_grad = GetItemListGrad() get_item_list_grad = GetItemListGrad()
...@@ -1132,9 +1137,6 @@ class GetItem2Lists(gof.op.Op): ...@@ -1132,9 +1137,6 @@ class GetItem2Lists(gof.op.Op):
grad_undefined(self, 1, ind1, "No gradient for this input"), grad_undefined(self, 1, ind1, "No gradient for this input"),
grad_undefined(self, 1, ind2, "No gradient for this input")] grad_undefined(self, 1, ind2, "No gradient for this input")]
def __str__(self):
return self.__class__.__name__
get_item_2lists = GetItem2Lists() get_item_2lists = GetItem2Lists()
"""Select elements of sparse matrix, returning them in a vector. """Select elements of sparse matrix, returning them in a vector.
...@@ -1185,9 +1187,6 @@ class GetItem2ListsGrad(gof.op.Op): ...@@ -1185,9 +1187,6 @@ class GetItem2ListsGrad(gof.op.Op):
out[0] = y out[0] = y
def __str__(self):
return self.__class__.__name__
get_item_2lists_grad = GetItem2ListsGrad() get_item_2lists_grad = GetItem2ListsGrad()
...@@ -1280,9 +1279,6 @@ class GetItem2d(gof.op.Op): ...@@ -1280,9 +1279,6 @@ class GetItem2d(gof.op.Op):
assert _is_sparse(x) assert _is_sparse(x)
out[0] = x[start1:stop1:step1, start2:stop2:step2] out[0] = x[start1:stop1:step1, start2:stop2:step2]
def __str__(self):
return self.__class__.__name__
get_item_2d = GetItem2d() get_item_2d = GetItem2d()
"""Implement a subtensor of sparse variable, returning a """Implement a subtensor of sparse variable, returning a
sparse matrix. sparse matrix.
...@@ -1350,9 +1346,6 @@ class GetItemScalar(gof.op.Op): ...@@ -1350,9 +1346,6 @@ class GetItemScalar(gof.op.Op):
assert _is_sparse(x) assert _is_sparse(x)
out[0] = theano._asarray(x[ind1, ind2], x.dtype) out[0] = theano._asarray(x[ind1, ind2], x.dtype)
def __str__(self):
return self.__class__.__name__
get_item_scalar = GetItemScalar() get_item_scalar = GetItemScalar()
"""Implement a subtensor of a sparse variable that takes """Implement a subtensor of a sparse variable that takes
two scalars as index and returns a scalar. two scalars as index and returns a scalar.
...@@ -1499,9 +1492,6 @@ class ColScaleCSC(gof.op.Op): ...@@ -1499,9 +1492,6 @@ class ColScaleCSC(gof.op.Op):
def infer_shape(self, node, ins_shapes): def infer_shape(self, node, ins_shapes):
return [ins_shapes[0]] return [ins_shapes[0]]
def __str__(self):
return self.__class__.__name__
class RowScaleCSC(gof.op.Op): class RowScaleCSC(gof.op.Op):
# Scale each row of a sparse matrix by the corresponding element of # Scale each row of a sparse matrix by the corresponding element of
...@@ -1551,9 +1541,6 @@ class RowScaleCSC(gof.op.Op): ...@@ -1551,9 +1541,6 @@ class RowScaleCSC(gof.op.Op):
def infer_shape(self, node, ins_shapes): def infer_shape(self, node, ins_shapes):
return [ins_shapes[0]] return [ins_shapes[0]]
def __str__(self):
return self.__class__.__name__
def col_scale(x, s): def col_scale(x, s):
"""Scale each columns of a sparse matrix by the corresponding """Scale each columns of a sparse matrix by the corresponding
...@@ -1598,7 +1585,12 @@ def row_scale(x, s): ...@@ -1598,7 +1585,12 @@ def row_scale(x, s):
class SpSum(gof.op.Op): class SpSum(gof.op.Op):
# See doc in instance of this Op or function after this class definition. # See doc in instance of this Op or function after this class definition.
__props__ = ("axis", "sparse_grad") __props__ = ("axis",)
# WARNING: judgement call...
# We are not using the structured in the comparison or hashing
# because it doesn't change the perform method therefore, we
# *do* want Sums with different structured values to be merged
# by the merge optimization and this requires them to compare equal.
def __init__(self, axis=None, sparse_grad=True): def __init__(self, axis=None, sparse_grad=True):
super(SpSum, self).__init__() super(SpSum, self).__init__()
...@@ -1721,8 +1713,6 @@ class Diag(gof.op.Op): ...@@ -1721,8 +1713,6 @@ class Diag(gof.op.Op):
def infer_shape(self, nodes, shapes): def infer_shape(self, nodes, shapes):
return [(tensor.minimum(*shapes[0]), )] return [(tensor.minimum(*shapes[0]), )]
def __str__(self):
return self.__class__.__name__
diag = Diag() diag = Diag()
"""Extract the diagonal of a square sparse matrix as a dense vector. """Extract the diagonal of a square sparse matrix as a dense vector.
...@@ -1769,8 +1759,6 @@ class SquareDiagonal(gof.op.Op): ...@@ -1769,8 +1759,6 @@ class SquareDiagonal(gof.op.Op):
def infer_shape(self, nodes, shapes): def infer_shape(self, nodes, shapes):
return [(shapes[0][0], shapes[0][0])] return [(shapes[0][0], shapes[0][0])]
def __str__(self):
return self.__class__.__name__
square_diagonal = SquareDiagonal() square_diagonal = SquareDiagonal()
"""Return a square sparse (csc) matrix whose diagonal """Return a square sparse (csc) matrix whose diagonal
is given by the dense vector argument. is given by the dense vector argument.
...@@ -1856,9 +1844,6 @@ class AddSS(gof.op.Op): ...@@ -1856,9 +1844,6 @@ class AddSS(gof.op.Op):
# see the doc of add() for more detail. # see the doc of add() for more detail.
__props__ = () __props__ = ()
def __str__(self):
return self.__class__.__name__
def make_node(self, x, y): def make_node(self, x, y):
x, y = map(as_sparse_variable, [x, y]) x, y = map(as_sparse_variable, [x, y])
assert x.format in ["csr", "csc"] assert x.format in ["csr", "csc"]
...@@ -1925,8 +1910,6 @@ class AddSSData(gof.op.Op): ...@@ -1925,8 +1910,6 @@ class AddSSData(gof.op.Op):
def infer_shape(self, node, ins_shapes): def infer_shape(self, node, ins_shapes):
return [ins_shapes[0]] return [ins_shapes[0]]
def __str__(self):
return self.__class__.__name__
add_s_s_data = AddSSData() add_s_s_data = AddSSData()
"""Add two sparse matrices assuming they have the same sparsity """Add two sparse matrices assuming they have the same sparsity
pattern. pattern.
...@@ -1948,9 +1931,6 @@ class AddSD(gof.op.Op): ...@@ -1948,9 +1931,6 @@ class AddSD(gof.op.Op):
# see the doc of add() for more detail. # see the doc of add() for more detail.
__props__ = () __props__ = ()
def __str__(self):
return self.__class__.__name__
def make_node(self, x, y): def make_node(self, x, y):
x, y = as_sparse_variable(x), tensor.as_tensor_variable(y) x, y = as_sparse_variable(x), tensor.as_tensor_variable(y)
assert x.format in ["csr", "csc"] assert x.format in ["csr", "csc"]
...@@ -2022,8 +2002,6 @@ class StructuredAddSV(gof.op.Op): ...@@ -2022,8 +2002,6 @@ class StructuredAddSV(gof.op.Op):
def infer_shape(self, node, ins_shapes): def infer_shape(self, node, ins_shapes):
return [ins_shapes[0]] return [ins_shapes[0]]
def __str__(self):
return self.__class__.__name__
structured_add_s_v = StructuredAddSV() structured_add_s_v = StructuredAddSV()
"""Structured addition of a sparse matrix and a dense vector. """Structured addition of a sparse matrix and a dense vector.
The elements of the vector are only added to the corresponding The elements of the vector are only added to the corresponding
...@@ -2103,9 +2081,6 @@ class MulSS(gof.op.Op): ...@@ -2103,9 +2081,6 @@ class MulSS(gof.op.Op):
# See the doc of mul() for more detail # See the doc of mul() for more detail
__props__ = () __props__ = ()
def __str__(self):
return self.__class__.__name__
def make_node(self, x, y): def make_node(self, x, y):
x, y = as_sparse_variable(x), as_sparse_variable(y) x, y = as_sparse_variable(x), as_sparse_variable(y)
assert x.format in ["csr", "csc"] assert x.format in ["csr", "csc"]
...@@ -2142,9 +2117,6 @@ class MulSD(gof.op.Op): ...@@ -2142,9 +2117,6 @@ class MulSD(gof.op.Op):
# See the doc of mul() for more detail # See the doc of mul() for more detail
__props__ = () __props__ = ()
def __str__(self):
return self.__class__.__name__
def make_node(self, x, y): def make_node(self, x, y):
x, y = as_sparse_variable(x), tensor.as_tensor_variable(y) x, y = as_sparse_variable(x), tensor.as_tensor_variable(y)
...@@ -2276,8 +2248,6 @@ class MulSV(gof.op.Op): ...@@ -2276,8 +2248,6 @@ class MulSV(gof.op.Op):
def infer_shape(self, node, ins_shapes): def infer_shape(self, node, ins_shapes):
return [ins_shapes[0]] return [ins_shapes[0]]
def __str__(self):
return self.__class__.__name__
mul_s_v = MulSV() mul_s_v = MulSV()
"""Multiplication of sparse matrix by a broadcasted dense vector element wise. """Multiplication of sparse matrix by a broadcasted dense vector element wise.
...@@ -2370,9 +2340,6 @@ class __ComparisonOpSS(gof.op.Op): ...@@ -2370,9 +2340,6 @@ class __ComparisonOpSS(gof.op.Op):
def infer_shape(self, node, ins_shapes): def infer_shape(self, node, ins_shapes):
return [ins_shapes[0]] return [ins_shapes[0]]
def __str__(self):
return self.__class__.__name__
class __ComparisonOpSD(gof.op.Op): class __ComparisonOpSD(gof.op.Op):
""" """
...@@ -2410,10 +2377,6 @@ class __ComparisonOpSD(gof.op.Op): ...@@ -2410,10 +2377,6 @@ class __ComparisonOpSD(gof.op.Op):
def infer_shape(self, node, ins_shapes): def infer_shape(self, node, ins_shapes):
return [ins_shapes[0]] return [ins_shapes[0]]
def __str__(self):
return self.__class__.__name__
def __ComparisonSwitch(SS, SD, DS): def __ComparisonSwitch(SS, SD, DS):
""" """
:param SS: function to apply between two sparses matrices. :param SS: function to apply between two sparses matrices.
...@@ -2769,17 +2732,13 @@ def vstack(blocks, format=None, dtype=None): ...@@ -2769,17 +2732,13 @@ def vstack(blocks, format=None, dtype=None):
class Remove0(gof.Op): class Remove0(gof.Op):
# See doc in instance of this Op or a function after the class definition. # See doc in instance of this Op or a function after the class definition.
__props__ = ("inplace",)
def __init__(self, inplace=False): def __init__(self, inplace=False):
self.inplace = inplace self.inplace = inplace
if self.inplace: if self.inplace:
self.destroy_map = {0: [0]} self.destroy_map = {0: [0]}
def __eq__(self, other):
return type(self) == type(other) and self.inplace == other.inplace
def __hash__(self):
return 64153 ^ hash(type(self)) ^ hash(self.inplace)
def __str__(self): def __str__(self):
l = [] l = []
if self.inplace: if self.inplace:
...@@ -3056,9 +3015,6 @@ class TrueDot(gof.op.Op): ...@@ -3056,9 +3015,6 @@ class TrueDot(gof.op.Op):
def __init__(self, grad_preserves_dense=True): def __init__(self, grad_preserves_dense=True):
self.grad_preserves_dense = grad_preserves_dense self.grad_preserves_dense = grad_preserves_dense
def __ne__(self, other):
return not (self == other)
def make_node(self, x, y): def make_node(self, x, y):
# NOTE # NOTE
# Because of trickiness of implementing, # Because of trickiness of implementing,
...@@ -3133,10 +3089,6 @@ class TrueDot(gof.op.Op): ...@@ -3133,10 +3089,6 @@ class TrueDot(gof.op.Op):
def infer_shape(self, node, shapes): def infer_shape(self, node, shapes):
return [(shapes[0][0], shapes[1][1])] return [(shapes[0][0], shapes[1][1])]
def __str__(self):
return self.__class__.__name__
def true_dot(x, y, grad_preserves_dense=True): def true_dot(x, y, grad_preserves_dense=True):
""" """
Operation for efficiently calculating the dot product when Operation for efficiently calculating the dot product when
...@@ -3182,9 +3134,6 @@ class StructuredDot(gof.Op): ...@@ -3182,9 +3134,6 @@ class StructuredDot(gof.Op):
# See doc in instance of this Op or function after this class definition. # See doc in instance of this Op or function after this class definition.
__props__ = () __props__ = ()
def __str__(self):
return self.__class__.__name__
def make_node(self, a, b): def make_node(self, a, b):
a = as_sparse_variable(a) a = as_sparse_variable(a)
...@@ -3318,9 +3267,6 @@ class StructuredDotGradCSC(gof.Op): ...@@ -3318,9 +3267,6 @@ class StructuredDotGradCSC(gof.Op):
# matrix in csc format. # matrix in csc format.
__props__ = () __props__ = ()
def __str__(self):
return self.__class__.__name__
def make_node(self, a_indices, a_indptr, b, g_ab): def make_node(self, a_indices, a_indptr, b, g_ab):
return gof.Apply(self, [a_indices, a_indptr, b, g_ab], return gof.Apply(self, [a_indices, a_indptr, b, g_ab],
[tensor.tensor(g_ab.dtype, (False,))]) [tensor.tensor(g_ab.dtype, (False,))])
...@@ -3451,9 +3397,6 @@ class StructuredDotGradCSR(gof.Op): ...@@ -3451,9 +3397,6 @@ class StructuredDotGradCSR(gof.Op):
# matrix in csr format. # matrix in csr format.
__props__ = () __props__ = ()
def __str__(self):
return self.__class__.__name__
def make_node(self, a_indices, a_indptr, b, g_ab): def make_node(self, a_indices, a_indptr, b, g_ab):
return gof.Apply(self, [a_indices, a_indptr, b, g_ab], return gof.Apply(self, [a_indices, a_indptr, b, g_ab],
[tensor.tensor(b.dtype, (False,))]) [tensor.tensor(b.dtype, (False,))])
...@@ -3635,8 +3578,6 @@ class SamplingDot(gof.op.Op): ...@@ -3635,8 +3578,6 @@ class SamplingDot(gof.op.Op):
def infer_shape(self, node, ins_shapes): def infer_shape(self, node, ins_shapes):
return [ins_shapes[2]] return [ins_shapes[2]]
def __str__(self):
return self.__class__.__name__
sampling_dot = SamplingDot() sampling_dot = SamplingDot()
"""Operand for calculating the dot product dot(`x`, `y`.T) = `z` when you """Operand for calculating the dot product dot(`x`, `y`.T) = `z` when you
only want to calculate a subset of `z`. only want to calculate a subset of `z`.
...@@ -3871,9 +3812,6 @@ class ConstructSparseFromList(gof.Op): ...@@ -3871,9 +3812,6 @@ class ConstructSparseFromList(gof.Op):
# See doc in instance of this Op or function after this class definition. # See doc in instance of this Op or function after this class definition.
__props__ = () __props__ = ()
def __str__(self):
return self.__class__.__name__
def make_node(self, x, values, ilist): def make_node(self, x, values, ilist):
""" """
:param x: a dense matrix that specify the output shape. :param x: a dense matrix that specify the output shape.
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论