def convert_to_sparse(self): # This converts full layers to sparse layers, I don't have conv layers yet so not going to worry about that yet. if self._layer_type == 'Sparse': print('Model is already sparse') return if self._comp_type == 'CPU': for i in range(self._depth): self._layers[i]._weights = csr_matrix(self._layers[i]._weights) return if self._comp_type == 'GPU': for i in range(self._depth): rows = cp.repeat(cp.arange(self._layers[i]._weights.shape[0]), self._layers[i]._weights.shape[1]) columns = cp.tile(cp.arange(self._layers[i]._weights.shape[1]), self._layers[i]._weights.shape[0]) if self._layers[i]._activation_type == 'Relu': self._layers[i] = sparse_relu_layer( size=self._layers[i]._size, weights=csr_matrix( self._layers[i]._weights.transpose()), biases=cp.array(self._layers[i]._biases)) if self._layers[i]._activation_type == 'Linear': self._layers[i] = sparse_linear_layer( size=self._layers[i]._size, weights=csr_matrix( self._layers[i]._weights.transpose()), biases=cp.array(self._layers[i]._biases)) self._layer_type = 'Sparse' print('Model is now sparse') for layer in self.layers: layer.get_coordinates() return
def initialize_sparse_weights(self, density, init_method='Xavier', bias_constant=None): """ This initializes all weights, I might add a module to initialize based on a list of values(normal SDs), one for each layer, but for now I am using Xavier initialization for everything.""" print('Initalizing sparse weights') for i in range(len(self._layers)): if i == 0: shape = (self._layers[i]._size, self._input_size) else: shape = (self._layers[i]._size, self._layers[i - 1]._size) if init_method == 'Xavier': self._layers[i]._weights = csr_matrix( scipy.sparse.random(shape[0], shape[1], density=density, format='csr', dtype=np.float32, data_rvs=np.random.randn) * np.sqrt(3. / sum(shape))) self._layers[i]._biases = cp.full(self._layers[i]._size, bias_constant) # This is just rescaling the initialization by the density. if init_method == 'Xavier_2': self._layers[i]._weights = csr_matrix( scipy.sparse.random(shape[0], shape[1], density=density, format='csr', dtype=np.float32, data_rvs=np.random.randn) * np.sqrt(3. / sum(shape)) / density) self._layers[i]._biases = cp.full(self._layers[i]._size, bias_constant) if type(init_method) == float: self._layers[i]._weights = csr_matrix( scipy.sparse.random(shape[0], shape[1], density=density, format='csr', dtype=np.float32, data_rvs=np.random.randn) * init_method) self._layers[i]._biases = cp.full(self._layers[i]._size, bias_constant) for layer in self.layers: layer.get_coordinates() return
def row_norms(X, squared=False): """Row-wise (squared) Euclidean norm of X. Equivalent to np.sqrt((X * X).sum(axis=1)), but also supports sparse matrices and does not create an X.shape-sized temporary. Performs no input validation. Parameters ---------- X : array_like The input array squared : bool, optional (default = False) If True, return squared norms. Returns ------- array_like The row-wise (squared) Euclidean norm of X. """ if sparse.issparse(X): if not isinstance(X, sparse.csr_matrix): X = sparse.csr_matrix(X) # norms = csr_row_norms(X) else: norms = np.einsum('ij,ij->i', X, X) if not squared: np.sqrt(norms, norms) return norms
def _get_arrayXslice(self, row: Sequence[int], col: slice) -> ss.csr_matrix: idxs = np.asarray(row) if idxs.dtype == bool: idxs = np.where(idxs) return ss.csr_matrix(get_compressed_vectors(self, idxs), shape=(len(idxs), self.shape[1]))[:, col]
def kneighbors_graph(idx, n_neighbors, n_fit): """ Returns k-neighbors graph built using k-nearest neighbors indices. Parameters ---------- idx : cudf.DataFrame The indices of the kNN for each cell in X. n_neighbors : int Number of neighbors for kNN. n_fit: int Distance metric to use for kNN. Currently, only 'euclidean' is supported. Returns ------- G : cugraph.Graph k-neighbors graph. """ n_nonzero = n_neighbors * n_fit weight = cp.ones(n_nonzero) indptr = cp.arange(0, n_nonzero + 1, n_neighbors) graph = csr_matrix((weight, cp.array(idx.ravel()), indptr), shape=(n_fit, n_fit)) offsets = cudf.Series(graph.indptr) indices = cudf.Series(graph.indices) G = cugraph.Graph() G.from_cudf_adjlist(offsets, indices, None) return G
def sparse_matrix_from_args(type, arg1, *args, **kwargs): if type == "coo": return _sp.coo_matrix(arg1, *args, **kwargs) elif type == "csr": return _sp.csr_matrix(arg1, *args, **kwargs) elif type == "csc": return _sp.csc_matrix(arg1, *args, **kwargs) elif type == "dia": return _sp.dia_matrix(arg1, *args, **kwargs)
def test_csrmatrix(self): A = sp.csr_matrix(self.A, dtype=self.dtype) b = cp.array(self.b, dtype=self.dtype) x = cupyx.linalg.sparse.lschol(A, b) testing.assert_array_almost_equal(x, self.x, decimal=self.decimal)
def test_shape(self): A = sp.csr_matrix(self.A, dtype=self.dtype) b = cp.array(numpy.tile(self.b, (2, 1)), dtype=self.dtype) cupyx.linalg.sparse.lschol(A, b)
def test_size(self): A = sp.csr_matrix(self.A, dtype=self.dtype) b = cp.array(numpy.append(self.b, [1]), dtype=self.dtype) cupyx.linalg.sparse.lschol(A, b)
def test_shape(self): A = sp.csr_matrix(self.A, dtype=self.dtype) b = cp.array(numpy.tile(self.b, (2, 1)), dtype=self.dtype) with pytest.raises(ValueError): cupyx.linalg.sparse.lschol(A, b)
def test_size(self): A = sp.csr_matrix(self.A, dtype=self.dtype) b = cp.array(numpy.append(self.b, [1]), dtype=self.dtype) with pytest.raises(ValueError): cupyx.linalg.sparse.lschol(A, b)
def _get_intXslice(self, row: int, col: slice) -> ss.csr_matrix: return ss.csr_matrix(get_compressed_vector(self, row), shape=(1, self.shape[1]))[:, col]