def test_basicSS(self): for mtype in _mtypes: x = as_sparse_variable(mtype((500, 3))) x.data[(10, 1)] = 1 x.data[(20, 2)] = 2 self.assertTrue(_is_sparse_variable(x)) xT = x.T self.assertTrue(_is_sparse_variable(xT)) zop = true_dot(x, xT) self.assertTrue(_is_sparse_variable(zop)) z = eval_outputs([zop]) self.assertTrue(_is_sparse(z)) self.assertTrue(z.shape == (500, 500)) self.assertTrue(type(z) is mtype) w = mtype((500, 500)) w[(10, 10)] = 1 w[(20, 20)] = 4 self.assertTrue(z.shape == w.shape) self.assertTrue(type(z) == type(w)) self.assertTrue(z.dtype == w.dtype) #self.assertTrue(z == w) self.assertTrue(abs(z - w).nnz == 0) z = z.todense() w = w.todense() self.assertTrue((z == w).all() == True)
def test_basicSD(self): for mtype in _mtypes: x = as_sparse_variable(mtype((500, 3))) x.data[(10, 1)] = 1 x.data[(20, 2)] = 2 self.assertTrue(_is_sparse_variable(x)) y = tensor.as_tensor_variable([[1., 2], [3, 4], [2, 1]]) self.assertTrue(_is_dense_variable(y)) zop = true_dot(x, y) self.assertTrue(_is_sparse_variable(zop)) z = eval_outputs([zop]) self.assertTrue(_is_sparse(z)) self.assertTrue(z.shape == (500, 2)) self.assertTrue(type(z) is mtype) w = mtype((500, 2)) w[(10, 0)] = 3. w[(20, 0)] = 4 w[(10, 1)] = 4 w[(20, 1)] = 2 self.assertTrue(z.shape == w.shape) self.assertTrue(type(z) == type(w)) self.assertTrue(z.dtype == w.dtype) #self.assertTrue(z == w) self.assertTrue(abs(z - w).nnz == 0) z = z.todense() w = w.todense() self.assertTrue((z == w).all() == True)
def test_basicSD(self): for mtype in _mtypes: x = as_sparse_variable(mtype((500,3))) x.data[(10, 1)] = 1 x.data[(20, 2)] = 2 self.assertTrue(_is_sparse_variable(x)) y = tensor.as_tensor_variable([[1., 2], [3, 4], [2, 1]]) self.assertTrue(_is_dense_variable(y)) zop = true_dot(x,y) self.assertTrue(_is_sparse_variable(zop)) z = eval_outputs([zop]) self.assertTrue(_is_sparse(z)) self.assertTrue(z.shape == (500,2)) self.assertTrue(type(z) is mtype) w = mtype((500,2)) w[(10, 0)] = 3. w[(20, 0)] = 4 w[(10, 1)] = 4 w[(20, 1)] = 2 self.assertTrue(z.shape == w.shape) self.assertTrue(type(z) == type(w)) self.assertTrue(z.dtype == w.dtype) #self.assertTrue(z == w) self.assertTrue(abs(z-w).nnz == 0) z = z.todense() w = w.todense() self.assertTrue((z == w).all() == True)
def test_basicSS(self): for mtype in _mtypes: x = as_sparse_variable(mtype((500,3))) x.data[(10, 1)] = 1 x.data[(20, 2)] = 2 self.assertTrue(_is_sparse_variable(x)) xT = x.T self.assertTrue(_is_sparse_variable(xT)) zop = true_dot(x,xT) self.assertTrue(_is_sparse_variable(zop)) z = eval_outputs([zop]) self.assertTrue(_is_sparse(z)) self.assertTrue(z.shape == (500,500)) self.assertTrue(type(z) is mtype) w = mtype((500,500)) w[(10, 10)] = 1 w[(20, 20)] = 4 self.assertTrue(z.shape == w.shape) self.assertTrue(type(z) == type(w)) self.assertTrue(z.dtype == w.dtype) #self.assertTrue(z == w) self.assertTrue(abs(z-w).nnz == 0) z = z.todense() w = w.todense() self.assertTrue((z == w).all() == True)
def true_dot(x, y, grad_preserves_dense=True): """ @todo: Maybe the triple-transposition formulation (when x is dense) is slow. See if there is a direct way to do this. """ if hasattr(x, 'getnnz'): x = as_sparse_variable(x) if hasattr(y, 'getnnz'): y = as_sparse_variable(y) x_is_sparse_variable = _is_sparse_variable(x) y_is_sparse_variable = _is_sparse_variable(y) if not x_is_sparse_variable and not y_is_sparse_variable: raise TypeError() if x_is_sparse_variable: return TrueDot(grad_preserves_dense)(x, y) else: assert y_is_sparse_variable return transpose(TrueDot(grad_preserves_dense)(y.T, x.T))
def wrapper(*args): x = as_sparse_variable(args[0]) xs = [scalar.as_scalar(arg) for arg in args[1:]] data, ind, ptr, shape = csm_properties(x) data = tensor_op(data, *xs) return CSM(x.format)(data, ind, ptr, shape)
def make_node(self, A, b): assert imported_scipy, ( "Scipy not available. Scipy is needed for the Solve op") A = as_sparse_variable(A) b = as_tensor_variable(b) assert A.ndim == 2 assert b.ndim in [1, 2] otype = tensor.tensor(broadcastable=b.broadcastable, dtype=(A * b).dtype) return Apply(self, [A, b], [otype])
def structured_exp(x): """ Element-wise exponential function to the non-zero elements. """ x = as_sparse_variable(x) x_data, x_ind, x_ptr, x_shape = csm_properties(x) x_data = tensor.exp(x_data) return CSR(x_data, x_ind, x_ptr, x_shape)
def structured_sigmoid(x): """ Element-wise sigmoid function only to the non-zero elements. """ x = as_sparse_variable(x) x_data, x_ind, x_ptr, x_shape = csm_properties(x) x_data = tensor.nnet.sigmoid(x_data) return CSR(x_data, x_ind, x_ptr, x_shape)
def make_node(self, A, b): assert imported_scipy, ( "Scipy not available. Scipy is needed for the Solve op") A = as_sparse_variable(A) b = as_tensor_variable(b) assert A.ndim == 2 assert b.ndim in [1, 2] otype = tensor.tensor( broadcastable=b.broadcastable, dtype=(A * b).dtype) return Apply(self, [A, b], [otype])
def make_node(self, x, y): x = as_sparse_variable(x) y = tensor.as_tensor_variable(y) assert y.type.ndim == 1 if x.type.dtype != y.type.dtype: raise NotImplementedError() return gof.Apply(self, [x, y], [ SparseType(dtype=x.type.dtype, format=x.type.format).make_variable() ])
def make_node(self, x, y): x = as_sparse_variable(x) y = tensor.as_tensor_variable(y) assert y.type.ndim == 1 if x.type.dtype != y.type.dtype: raise NotImplementedError() return gof.Apply(self, [x, y], [SparseType(dtype=x.type.dtype, format=x.type.format).make_variable()])
def structured_minimum(x, y): """ Element-wise minimum function only to non-zero elements. """ x = as_sparse_variable(x) y = tensor.as_tensor_variable(y) x_data, x_ind, x_ptr, x_shape = csm_properties(x) x_data = tensor.minimum(x_data, y) return CSR(x_data, x_ind, x_ptr, x_shape)
def test_basicDS(self): for mtype in _mtypes: x = as_sparse_variable(mtype((500,3))) x.data[(10, 1)] = 1 x.data[(20, 2)] = 2 self.assertTrue(_is_sparse_variable(x)) y = tensor.as_tensor_variable([[1., 2], [3, 4], [2, 1]]) self.assertTrue(_is_dense_variable(y)) x.data = x.data.T y.data = y.data.T zop = true_dot(y, x) zop = transpose(true_dot(y, x)) self.assertTrue(_is_sparse_variable(zop)) z = eval_outputs([zop]) self.assertTrue(_is_sparse(z)) self.assertTrue(z.shape == (500,2)) # self.assertTrue(type(z) is mtype) w = mtype((500,2)) w[(10, 0)] = 3. w[(20, 0)] = 4 w[(10, 1)] = 4 w[(20, 1)] = 2 self.assertTrue(z.shape == w.shape) # Type should switch from csr to csc and vice-versa, so don't perform this test #self.assertTrue(type(z) == type(w)) self.assertTrue(z.dtype == w.dtype) # Type should switch from csr to csc and vice-versa, so don't perform this test #self.assertTrue(z == w) self.assertTrue(abs(z-w).nnz == 0) z = z.todense() w = w.todense() self.assertTrue((z == w).all() == True)
def test_basicDS(self): for mtype in _mtypes: x = as_sparse_variable(mtype((500, 3))) x.data[(10, 1)] = 1 x.data[(20, 2)] = 2 self.assertTrue(_is_sparse_variable(x)) y = tensor.as_tensor_variable([[1., 2], [3, 4], [2, 1]]) self.assertTrue(_is_dense_variable(y)) x.data = x.data.T y.data = y.data.T zop = true_dot(y, x) zop = transpose(true_dot(y, x)) self.assertTrue(_is_sparse_variable(zop)) z = eval_outputs([zop]) self.assertTrue(_is_sparse(z)) self.assertTrue(z.shape == (500, 2)) # self.assertTrue(type(z) is mtype) w = mtype((500, 2)) w[(10, 0)] = 3. w[(20, 0)] = 4 w[(10, 1)] = 4 w[(20, 1)] = 2 self.assertTrue(z.shape == w.shape) # Type should switch from csr to csc and vice-versa, so don't perform this test #self.assertTrue(type(z) == type(w)) self.assertTrue(z.dtype == w.dtype) # Type should switch from csr to csc and vice-versa, so don't perform this test #self.assertTrue(z == w) self.assertTrue(abs(z - w).nnz == 0) z = z.todense() w = w.todense() self.assertTrue((z == w).all() == True)
def make_node(self, n, p): n = tensor.as_tensor_variable(n) p = as_sparse_variable(p) return gof.Apply(self, [n, p], [p.type()])
def make_node(self, x): x = as_sparse_variable(x) return gof.Apply(self, [x], [SparseType(dtype=self.out_type, format=x.format).make_variable()])
def make_node(self, n, p): n = tensor.as_tensor_variable(n) p = as_sparse_variable(p) assert p.format in ["csr", "csc"] return gof.Apply(self, [n, p], [p.type()])
def make_node(self, x, a): x = as_sparse_variable(x) a = tensor.as_tensor_variable(a) return gof.Apply(self, [x, a], [tensor.TensorType(dtype=x.type.dtype, broadcastable=(False,)).make_variable()])
def make_node(self, x): x = as_sparse_variable(x) return gof.Apply(self, [x], [x.type()])
def make_node(self, x): x = as_sparse_variable(x) return gof.Apply( self, [x], [SparseType(dtype=self.out_type, format=x.format).make_variable()])