提交 09eadfb9 authored 作者: Samira Shabanian's avatar Samira Shabanian

kmap removed and tests fixed

上级 ea493492
...@@ -93,20 +93,6 @@ def _is_dense(x): ...@@ -93,20 +93,6 @@ def _is_dense(x):
return isinstance(x, numpy.ndarray) return isinstance(x, numpy.ndarray)
def _kmap_eq(a, b):
if a is None and b is None:
return True
if a is None or b is None:
return False
return numpy.all(a == b)
def _kmap_hash(a):
if a is None:
return 12345
return hash(numpy.str(a))
# Wrapper type # Wrapper type
def as_sparse_variable(x, name=None): def as_sparse_variable(x, name=None):
""" """
...@@ -517,9 +503,9 @@ class CSMProperties(gof.Op): ...@@ -517,9 +503,9 @@ class CSMProperties(gof.Op):
# we don't return a view of the shape, we create a new ndarray from the # we don't return a view of the shape, we create a new ndarray from the
# shape tuple. # shape tuple.
__props__ = ()
view_map = {0: [0], 1: [0], 2: [0]} view_map = {0: [0], 1: [0], 2: [0]}
kmap = None
""" """
Indexing to speficied what part of the data parameter Indexing to speficied what part of the data parameter
should be use to construct the sparse matrix. should be use to construct the sparse matrix.
...@@ -527,22 +513,9 @@ class CSMProperties(gof.Op): ...@@ -527,22 +513,9 @@ class CSMProperties(gof.Op):
""" """
def __init__(self, kmap=None): def __init__(self, kmap=None):
if kmap == None: if kmap is not None:
self.kmap = kmap
else:
raise Exception("Do not use kmap, it is removed") raise Exception("Do not use kmap, it is removed")
def __eq__(self, other):
return type(self) == type(other) and _kmap_eq(self.kmap, other.kmap)
def __hash__(self):
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)
assert csm.format in ["csr", "csc"] assert csm.format in ["csr", "csc"]
...@@ -554,12 +527,10 @@ class CSMProperties(gof.Op): ...@@ -554,12 +527,10 @@ class CSMProperties(gof.Op):
def perform(self, node, inputs, out): def perform(self, node, inputs, out):
(csm,) = inputs (csm,) = inputs
if self.kmap is None: out[0][0] = csm.data
out[0][0] = csm.data
if str(csm.data.dtype) == 'int32': if str(csm.data.dtype) == 'int32':
out[0][0] = theano._asarray(out[0][0], dtype='int32') out[0][0] = theano._asarray(out[0][0], dtype='int32')
# backport # backport
# out[0][0] = csm.data if self.kmap is None else csm.data[self.kmap]
out[1][0] = theano._asarray(csm.indices, dtype='int32') out[1][0] = theano._asarray(csm.indices, dtype='int32')
out[2][0] = theano._asarray(csm.indptr, dtype='int32') out[2][0] = theano._asarray(csm.indptr, dtype='int32')
out[3][0] = theano._asarray(csm.shape, dtype='int32') out[3][0] = theano._asarray(csm.shape, dtype='int32')
...@@ -639,7 +610,6 @@ def csm_shape(csm): ...@@ -639,7 +610,6 @@ def csm_shape(csm):
class CSM(gof.Op): class CSM(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.
kmap = None
""" """
Indexing to speficied what part of the data parameter Indexing to speficied what part of the data parameter
should be used to construct the sparse matrix. should be used to construct the sparse matrix.
...@@ -656,24 +626,17 @@ class CSM(gof.Op): ...@@ -656,24 +626,17 @@ class CSM(gof.Op):
if format not in ('csr', 'csc'): if format not in ('csr', 'csc'):
raise ValueError("format must be one of: 'csr', 'csc'", format) raise ValueError("format must be one of: 'csr', 'csc'", format)
self.format = format self.format = format
if kmap is not None:
# for efficiency, if remap does nothing, then do not apply it
if kmap is not None and all(kmap == numpy.arange(numpy.size(kmap))):
kmap = None
self.kmap = kmap
if self.kmap is not None:
raise Exception("Do not use kmap, it is removed") raise Exception("Do not use kmap, it is removed")
if not isinstance(self.kmap, numpy.ndarray): self.kmap = kmap
# should view the other inputs too, but viewing multiple # should view the other inputs too, but viewing multiple
# inputs is not currently supported by the destroyhandler # inputs is not currently supported by the destroyhandler
self.view_map = {0: [0]} self.view_map = {0: [0]}
self._hashval = (hash(type(self)) ^ hash(self.format) ^ self._hashval = (hash(type(self)) ^ hash(self.format))
_kmap_hash(self.kmap))
def __eq__(self, other): def __eq__(self, other):
return (type(other) is CSM and other.format == self.format and return (type(other) is CSM and other.format == self.format)
_kmap_eq(self.kmap, other.kmap))
def __hash__(self): def __hash__(self):
return self._hashval return self._hashval
...@@ -728,13 +691,11 @@ class CSM(gof.Op): ...@@ -728,13 +691,11 @@ class CSM(gof.Op):
if len(shape) != 2: if len(shape) != 2:
raise ValueError('Shape should be an array of length 2') raise ValueError('Shape should be an array of length 2')
if (data.shape != indices.shape and numpy.size(data) != if data.shape != indices.shape:
numpy.size(self.kmap)):
errmsg = ('Data (shape ' + repr(data.shape) + errmsg = ('Data (shape ' + repr(data.shape) +
' must have the same number of elements ' + ' must have the same number of elements ' +
'as indices (shape' + repr(indices.shape) + 'as indices (shape' + repr(indices.shape) +
') or elements as kmap (' + ')')
repr(numpy.size(self.kmap)) + ')')
raise ValueError(errmsg) raise ValueError(errmsg)
if self.format == 'csc': if self.format == 'csc':
out[0] = scipy.sparse.csc_matrix((data, indices.copy(), out[0] = scipy.sparse.csc_matrix((data, indices.copy(),
...@@ -759,9 +720,8 @@ class CSM(gof.Op): ...@@ -759,9 +720,8 @@ class CSM(gof.Op):
return [g_data, DisconnectedType()(), DisconnectedType()(), DisconnectedType()()] return [g_data, DisconnectedType()(), DisconnectedType()(), DisconnectedType()()]
def infer_shape(self, node, shapes): def infer_shape(self, node, shapes):
if self.kmap is None: # node.inputs[3] is of lenght as we only support sparse matrix.
# node.inputs[3] is of lenght as we only support sparse matrix. return [(node.inputs[3][0], node.inputs[3][1])]
return [(node.inputs[3][0], node.inputs[3][1])]
CSC = CSM('csc') CSC = CSM('csc')
""" """
...@@ -838,28 +798,16 @@ class CSMGrad(gof.op.Op): ...@@ -838,28 +798,16 @@ 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__ = ()
def __init__(self, kmap=None): def __init__(self, kmap=None):
if kmap == None: if kmap is not None:
self.kmap = kmap
else:
raise Exception("Do not use kmap, it is removed") raise Exception("Do not use kmap, it is removed")
# This class always allocate a new output. # This class always allocate a new output.
# I keep this here to help GD understand what this kmap think is. # I keep this here to help GD understand what this kmap think is.
# 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):
return "%s{%s}" % (
self.__class__.__name__,
self.kmap)
def make_node(self, x_data, x_indices, x_indptr, x_shape, def make_node(self, x_data, x_indices, x_indptr, x_shape,
g_data, g_indices, g_indptr, g_shape): g_data, g_indices, g_indptr, g_shape):
gout_data = g_data.type() gout_data = g_data.type()
...@@ -888,12 +836,10 @@ class CSMGrad(gof.op.Op): ...@@ -888,12 +836,10 @@ class CSMGrad(gof.op.Op):
for j_ptr in range(g_indptr[i], g_indptr[i + 1]): for j_ptr in range(g_indptr[i], g_indptr[i + 1]):
g_row[g_indices[j_ptr]] = 0 g_row[g_indices[j_ptr]] = 0
if self.kmap is None: g_out[0] = gout_data
g_out[0] = gout_data
def infer_shape(self, node, shapes): def infer_shape(self, node, shapes):
if self.kmap is None: return [shapes[1]]
return [shapes[1]]
csm_grad = CSMGrad csm_grad = CSMGrad
......
...@@ -43,12 +43,6 @@ class ConvolutionIndices(Op): ...@@ -43,12 +43,6 @@ class ConvolutionIndices(Op):
""" """
__props__ = () __props__ = ()
@staticmethod
def sparse_eval(inshp, kshp, nkern, strides=(1, 1), mode='valid'):
(dx, dy) = strides
return convolution_indices.evaluate(inshp, kshp, (dx, dy),
nkern, mode=mode, ws=False)
@staticmethod @staticmethod
def conv_eval(inshp, kshp, strides=(1, 1), mode='valid'): def conv_eval(inshp, kshp, strides=(1, 1), mode='valid'):
(dx, dy) = strides (dx, dy) = strides
...@@ -73,7 +67,7 @@ class ConvolutionIndices(Op): ...@@ -73,7 +67,7 @@ class ConvolutionIndices(Op):
:param mode: 'valid' generates output only when kernel and :param mode: 'valid' generates output only when kernel and
image overlap overlap fully. Convolution obtained image overlap overlap fully. Convolution obtained
by zero-padding the input by zero-padding the input
:param ws: True if weight sharing, false otherwise :param ws: must be always True
:param (dx,dy): offset parameter. In the case of no weight sharing, :param (dx,dy): offset parameter. In the case of no weight sharing,
gives the pixel offset between two receptive fields. gives the pixel offset between two receptive fields.
With weight sharing gives the offset between the With weight sharing gives the offset between the
...@@ -83,6 +77,9 @@ class ConvolutionIndices(Op): ...@@ -83,6 +77,9 @@ class ConvolutionIndices(Op):
:returns: the structure of a sparse matrix, and the logical dimensions :returns: the structure of a sparse matrix, and the logical dimensions
of the image which will be the result of filtering. of the image which will be the result of filtering.
""" """
if not ws:
raise Exception("ws is obsolete and it must be always True")
(dx, dy) = strides (dx, dy) = strides
N = numpy N = numpy
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论