提交 0fdd1c3d authored 作者: Frederic's avatar Frederic

Added __str__ to many sparse Op. Removed __ne__ as they do the same as the default implementation.

上级 b8ecaae2
...@@ -525,12 +525,14 @@ class CSMProperties(gof.Op): ...@@ -525,12 +525,14 @@ class CSMProperties(gof.Op):
def __eq__(self, other): def __eq__(self, other):
return type(self) == type(other) and _kmap_eq(self.kmap, other.kmap) return type(self) == type(other) and _kmap_eq(self.kmap, other.kmap)
def __ne__(self, other):
return not (self == other)
def __hash__(self): def __hash__(self):
return 8234 ^ hash(type(self)) ^ _kmap_hash(self.kmap) return 8234 ^ hash(type(self)) ^ _kmap_hash(self.kmap)
def __str__(self):
return "%s{%s}" % (
self.__class__.__name__,
self.kmap)
def make_node(self, csm): def make_node(self, csm):
csm = as_sparse_variable(csm) csm = as_sparse_variable(csm)
data = tensor.TensorType(dtype=csm.type.dtype, data = tensor.TensorType(dtype=csm.type.dtype,
...@@ -700,12 +702,14 @@ class CSMGrad(gof.op.Op): ...@@ -700,12 +702,14 @@ class CSMGrad(gof.op.Op):
def __eq__(self, other): def __eq__(self, other):
return type(self) == type(other) and _kmap_eq(self.kmap, other.kmap) return type(self) == type(other) and _kmap_eq(self.kmap, other.kmap)
def __ne__(self, other):
return not (self == other)
def __hash__(self): def __hash__(self):
return 82345 ^ hash(type(self)) ^ _kmap_hash(self.kmap) return 82345 ^ hash(type(self)) ^ _kmap_hash(self.kmap)
def __str__(self):
return "%s{%s}" % (
self.__class__.__name__,
self.kmap)
def make_node(self, data, gout_data, gout_indices): def make_node(self, data, gout_data, gout_indices):
g_data = gout_data.type() g_data = gout_data.type()
return gof.Apply(self, [data, gout_data, gout_indices], [g_data]) return gof.Apply(self, [data, gout_data, gout_indices], [g_data])
...@@ -761,6 +765,11 @@ class DenseFromSparse(gof.op.Op): ...@@ -761,6 +765,11 @@ class DenseFromSparse(gof.op.Op):
def __hash__(self): def __hash__(self):
return hash(type(self))^hash(self.sparse_grad) return hash(type(self))^hash(self.sparse_grad)
def __str__(self):
return "%s{structured_grad=%s}" % (
self.__class__.__name__,
self.sparse_grad)
def make_node(self, x): def make_node(self, x):
x = as_sparse_variable(x) x = as_sparse_variable(x)
return gof.Apply(self, return gof.Apply(self,
...@@ -801,12 +810,14 @@ class SparseFromDense(gof.op.Op): ...@@ -801,12 +810,14 @@ class SparseFromDense(gof.op.Op):
def __eq__(self, other): def __eq__(self, other):
return type(self) == type(other) and self.format == other.format return type(self) == type(other) and self.format == other.format
def __ne__(self, other):
return not (self == other)
def __hash__(self): def __hash__(self):
return 982374 ^ hash(self.format) ^ hash(DenseFromSparse) return 982374 ^ hash(self.format) ^ hash(DenseFromSparse)
def __str__(self):
return "%s{%s}" % (
self.__class__.__name__,
self.format)
def make_node(self, x): def make_node(self, x):
x = tensor.as_tensor_variable(x) x = tensor.as_tensor_variable(x)
if x.ndim > 2: if x.ndim > 2:
...@@ -1005,6 +1016,9 @@ class Transpose(gof.op.Op): ...@@ -1005,6 +1016,9 @@ class Transpose(gof.op.Op):
def __hash__(self): def __hash__(self):
return hash(type(self)) return hash(type(self))
def __str__(self):
return "Sparse" + self.__class__.__name__
def make_node(self, x): def make_node(self, x):
x = as_sparse_variable(x) x = as_sparse_variable(x)
return gof.Apply(self, return gof.Apply(self,
...@@ -1034,6 +1048,9 @@ class Neg(gof.op.Op): ...@@ -1034,6 +1048,9 @@ class Neg(gof.op.Op):
def __hash__(self): def __hash__(self):
return hash(type(self)) return hash(type(self))
def __str__(self):
return "Sparse" + self.__class__.__name__
def make_node(self, x): def make_node(self, x):
x = as_sparse_variable(x) x = as_sparse_variable(x)
return gof.Apply(self, [x], [x.type()]) return gof.Apply(self, [x], [x.type()])
...@@ -1060,6 +1077,9 @@ class AddSS(gof.op.Op): ...@@ -1060,6 +1077,9 @@ class AddSS(gof.op.Op):
def __hash__(self): def __hash__(self):
return hash(type(self)) return hash(type(self))
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])
if x.type.dtype != y.type.dtype: if x.type.dtype != y.type.dtype:
...@@ -1096,6 +1116,9 @@ class AddSD(gof.op.Op): ...@@ -1096,6 +1116,9 @@ class AddSD(gof.op.Op):
def __hash__(self): def __hash__(self):
return hash(type(self)) return hash(type(self))
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)
if x.type.dtype != y.type.dtype: if x.type.dtype != y.type.dtype:
...@@ -1161,6 +1184,9 @@ class MulSS(gof.op.Op): ...@@ -1161,6 +1184,9 @@ class MulSS(gof.op.Op):
def __hash__(self): def __hash__(self):
return hash(type(self)) return hash(type(self))
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)
if x.type != y.type: if x.type != y.type:
...@@ -1195,6 +1221,9 @@ class MulSD(gof.op.Op): ...@@ -1195,6 +1221,9 @@ class MulSD(gof.op.Op):
def __hash__(self): def __hash__(self):
return hash(type(self)) return hash(type(self))
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)
...@@ -1304,6 +1333,9 @@ class StructuredDot(gof.Op): ...@@ -1304,6 +1333,9 @@ class StructuredDot(gof.Op):
def __hash__(self): def __hash__(self):
return hash(type(self)) return hash(type(self))
def __str__(self):
return self.__class__.__name__
def make_node(self, a, b): def make_node(self, a, b):
if not _is_sparse_variable(a): if not _is_sparse_variable(a):
raise TypeError('First argument must be of type SparseVariable ' raise TypeError('First argument must be of type SparseVariable '
...@@ -1405,6 +1437,9 @@ class StructuredDotCSC(gof.Op): ...@@ -1405,6 +1437,9 @@ class StructuredDotCSC(gof.Op):
def __hash__(self): def __hash__(self):
return hash(type(self)) return hash(type(self))
def __str__(self):
return self.__class__.__name__
def make_node(self, a_val, a_ind, a_ptr, a_nrows, b): def make_node(self, a_val, a_ind, a_ptr, a_nrows, b):
dtype_out = scalar.upcast(a_val.type.dtype, b.type.dtype) dtype_out = scalar.upcast(a_val.type.dtype, b.type.dtype)
r = gof.Apply(self, [a_val, a_ind, a_ptr, a_nrows, b], r = gof.Apply(self, [a_val, a_ind, a_ptr, a_nrows, b],
...@@ -1577,6 +1612,9 @@ class StructuredDotCSR(gof.Op): ...@@ -1577,6 +1612,9 @@ class StructuredDotCSR(gof.Op):
def __hash__(self): def __hash__(self):
return hash(type(self)) return hash(type(self))
def __str__(self):
return self.__class__.__name__
def make_node(self, a_val, a_ind, a_ptr, b): def make_node(self, a_val, a_ind, a_ptr, b):
self.dtype_out = scalar.upcast(a_val.type.dtype, b.type.dtype) self.dtype_out = scalar.upcast(a_val.type.dtype, b.type.dtype)
r = gof.Apply(self, [a_val, a_ind, a_ptr, b], r = gof.Apply(self, [a_val, a_ind, a_ptr, b],
...@@ -1759,6 +1797,9 @@ class StructuredDotGradCSC(gof.Op): ...@@ -1759,6 +1797,9 @@ class StructuredDotGradCSC(gof.Op):
def __hash__(self): def __hash__(self):
return hash(type(self)) return hash(type(self))
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,))])
...@@ -1878,6 +1919,9 @@ class StructuredDotGradCSR(gof.Op): ...@@ -1878,6 +1919,9 @@ class StructuredDotGradCSR(gof.Op):
def __hash__(self): def __hash__(self):
return hash(type(self)) return hash(type(self))
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,))])
...@@ -2005,8 +2049,8 @@ class Dot(gof.op.Op): ...@@ -2005,8 +2049,8 @@ class Dot(gof.op.Op):
def __hash__(self): def __hash__(self):
return hash(type(self)) return hash(type(self))
def __ne__(self, other): def __str__(self):
return not (self == other) return "Sparse" + self.__class__.__name__
def infer_shape(self, node, shapes): def infer_shape(self, node, shapes):
xshp, yshp = shapes xshp, yshp = shapes
...@@ -2100,9 +2144,6 @@ class Usmm(gof.op.Op): ...@@ -2100,9 +2144,6 @@ class Usmm(gof.op.Op):
def __hash__(self): def __hash__(self):
return hash(type(self)) return hash(type(self))
def __ne__(self, other):
return not (self == other)
def __str__(self): def __str__(self):
return 'Usmm{no_inplace}' return 'Usmm{no_inplace}'
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论