def update_before_paramupdate(self, model):
        for i in xrange(len(model.grad_w)):
            if sp.isspmatrix(model.grad_w[i]):
                nonzero = model.grad_w[i].nonzero()
                #t1,t2 = model.grad_w[i].shape
                #print "grad_w sparity",len(nonzero[0]) * 1.0 / t1 /t2
                self.nonzero["%d"%i] = nonzero
                grad_w  = np.asarray(model.grad_w[i].data) + 2 * (model.l2_lambda) * model.w[i][nonzero]
                self.ada_w[i][nonzero] = self.ada_w[i][nonzero] + grad_w * grad_w            
            else:
                grad_w = model.grad_w[i] + 2 * (model.l2_lambda) * model.w[i]
                self.ada_w[i] = self.ada_w[i] + grad_w * grad_w
            
            grad_b = model.grad_b[i]
            self.ada_b[i] = self.ada_b[i] + grad_b * grad_b            


        #print type(model.grad_lw)        
        if sp.isspmatrix(model.grad_lw):
            nonzero = model.grad_lw.nonzero()
            #t1,t2 = model.grad_lw.shape
            #print "grad_lw sparity",len(nonzero[0])*1.0/t1 /t2
            self.nonzero['l'] = nonzero
            grad_lw = np.asarray(model.grad_lw.data) + 2 * model.l2_lambda * model.lw[nonzero]
            self.ada_lw[nonzero] = self.ada_lw[nonzero] + grad_lw * grad_lw      
        else:
            grad_lw = model.grad_lw + 2 * model.l2_lambda * model.lw
            self.ada_lw = self.ada_lw + grad_lw * grad_lw
        
        grad_lb = model.grad_lb
        self.ada_lb = self.ada_lb + grad_lb * grad_lb  
Example #2
0
def assert_sparse_matrix_equal(m1, m2, decimal=6):
    """Assert two scipy.sparse matrices are equal."""

    # delay the import to speed up stuff if this method is unused
    from scipy.sparse import isspmatrix
    from numpy.linalg import norm

    # both are sparse matricies
    assert isspmatrix(m1)
    assert isspmatrix(m2)

    # make sure they have the same format
    eq_(m1.format, m2.format)

    #make sure they have the same shape
    eq_(m1.shape, m2.shape)

    # even though its called assert_array_almost_equal, it will
    # work for scalars
    m1 = m1.tocsr()
    m2 = m2.tocsr()
    m1.eliminate_zeros()
    m2.eliminate_zeros()

    xi1, yi1 = m1.nonzero()
    xi2, yi2 = m2.nonzero()

    assert_array_equal(xi1, xi2)
    assert_array_equal(yi1, yi2)
    assert_array_almost_equal(m1.data, m2.data, decimal=decimal)
    def apply(self):
        #import cProfile, pstats, StringIO
        #pr =  cProfile.Profile()
        #pr.enable()
        learn_rate   = self.learnrate
        l2_lambda       = self.l2_lambda
        n_layer      = len(self.w)
        for i in xrange(n_layer):
        #    print type(self.grad_w[i])
        #    print "sp.issmatrix(self.grad_w[i]",sp.isspmatrix(self.grad_w[i])
            if sp.isspmatrix(self.grad_w[i]):
                nonzero             = self.rater.nonzero['%d'%i]
        #        print "nonzero_grad",self.grad_w[i].nonzero()
                self.w[i][nonzero] -= self.rater.rate_w[i][nonzero] * ( np.asarray(self.grad_w[i].data) \
                                      + 2 * l2_lambda * self.w[i][nonzero])
                #self.w[i]  -= self.rater.rate_w[0] * (np.asarray(self.grad_w[i].todense()) + 2 * l2_lambda * self.w[i])
               # print "w[0]"
               # m,n = self.w[0].shape
                #for k in xrange(m):
                #    for j in xrange(n):
                #       print self.w[0][k][j],"\t",
                 #   print ""

            else:
                self.w[i] -= self.rater.rate_w[i] * (self.grad_w[i] + 2 * l2_lambda * self.w[i])
            self.b[i] -= self.rater.rate_b[i] * ( self.grad_b[i] + 2 * l2_lambda * self.b[i])
       
       
        if sp.isspmatrix(self.grad_lw):
            nonzero           = self.rater.nonzero['l']
            self.lw[nonzero] -= self.rater.rate_lw[nonzero] * (np.asarray(self.grad_lw.data) \
                                + 2 * l2_lambda * self.lw[nonzero])
        else:
            self.lw -= self.rater.rate_lw * (self.grad_lw + 2 * l2_lambda * self.lw) 
        self.lb -= self.rater.rate_lb * (self.grad_lb + 2 * l2_lambda * self.lb)
Example #4
0
File: linalg.py Project: bjzu/MF
def elop(X, Y, op):
    """
    Compute element-wise operation of matrix :param:`X` and matrix :param:`Y`.
    
    :param X: First input matrix.
    :type X: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil, dia or :class:`numpy.matrix`
    :param Y: Second input matrix.
    :type Y: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil, dia or :class:`numpy.matrix`
    :param op: Operation to be performed. 
    :type op: `func` 
    """
    try:
        zp1 = op(0, 1) if sp.isspmatrix(X) else op(1, 0)
        zp2 = op(0, 0) 
        zp = zp1 != 0 or zp2 != 0
    except:
        zp = 0
    if sp.isspmatrix(X) or sp.isspmatrix(Y):
        return _op_spmatrix(X, Y, op) if not zp else _op_matrix(X, Y, op)
    else:
        try:
            X[X == 0] = np.finfo(X.dtype).eps
            Y[Y == 0] = np.finfo(Y.dtype).eps
        except ValueError:
            return op(np.asmatrix(X), np.asmatrix(Y))
        return op(np.asmatrix(X), np.asmatrix(Y))
Example #5
0
def check_svm_model_equal(dense_svm, sparse_svm, X_train, y_train, X_test):
    dense_svm.fit(X_train.toarray(), y_train)
    if sparse.isspmatrix(X_test):
        X_test_dense = X_test.toarray()
    else:
        X_test_dense = X_test
    sparse_svm.fit(X_train, y_train)
    assert sparse.issparse(sparse_svm.support_vectors_)
    assert sparse.issparse(sparse_svm.dual_coef_)
    assert_array_almost_equal(dense_svm.support_vectors_,
                              sparse_svm.support_vectors_.toarray())
    assert_array_almost_equal(dense_svm.dual_coef_,
                              sparse_svm.dual_coef_.toarray())
    if dense_svm.kernel == "linear":
        assert sparse.issparse(sparse_svm.coef_)
        assert_array_almost_equal(dense_svm.coef_, sparse_svm.coef_.toarray())
    assert_array_almost_equal(dense_svm.support_, sparse_svm.support_)
    assert_array_almost_equal(dense_svm.predict(X_test_dense),
                              sparse_svm.predict(X_test))
    assert_array_almost_equal(dense_svm.decision_function(X_test_dense),
                              sparse_svm.decision_function(X_test))
    assert_array_almost_equal(dense_svm.decision_function(X_test_dense),
                              sparse_svm.decision_function(X_test_dense))
    if isinstance(dense_svm, svm.OneClassSVM):
        msg = "cannot use sparse input in 'OneClassSVM' trained on dense data"
    else:
        assert_array_almost_equal(dense_svm.predict_proba(X_test_dense),
                                  sparse_svm.predict_proba(X_test), 4)
        msg = "cannot use sparse input in 'SVC' trained on dense data"
    if sparse.isspmatrix(X_test):
        assert_raise_message(ValueError, msg, dense_svm.predict, X_test)
Example #6
0
 def _validate_for_predict(self, X):
     X = atleast2d_or_csr(X, dtype=np.float64, order="C")
     if self._sparse and not sp.isspmatrix(X):
         X = sp.csr_matrix(X)
     elif sp.isspmatrix(X) and not self._sparse:
         raise ValueError("cannot use sparse input in %r trained on dense data" % type(self).__name__)
     return X
def mm_dnssps(A, v):
    """compute A*v for sparse or dense A"""
    try:
        return A.matvec(v)
    except AttributeError:
        pass
    if sps.isspmatrix(A) or sps.isspmatrix(v):
        return A*v
    else:
        return np.dot(A, v)
Example #8
0
    def test_shape_compatibility(self):
        use_solver(useUmfpack=True)
        A = csc_matrix([[1., 0], [0, 2]])
        bs = [
            [1, 6],
            array([1, 6]),
            [[1], [6]],
            array([[1], [6]]),
            csc_matrix([[1], [6]]),
            csr_matrix([[1], [6]]),
            dok_matrix([[1], [6]]),
            bsr_matrix([[1], [6]]),
            array([[1., 2., 3.], [6., 8., 10.]]),
            csc_matrix([[1., 2., 3.], [6., 8., 10.]]),
            csr_matrix([[1., 2., 3.], [6., 8., 10.]]),
            dok_matrix([[1., 2., 3.], [6., 8., 10.]]),
            bsr_matrix([[1., 2., 3.], [6., 8., 10.]]),
            ]

        for b in bs:
            x = np.linalg.solve(A.toarray(), toarray(b))
            for spmattype in [csc_matrix, csr_matrix, dok_matrix, lil_matrix]:
                x1 = spsolve(spmattype(A), b, use_umfpack=True)
                x2 = spsolve(spmattype(A), b, use_umfpack=False)

                # check solution
                if x.ndim == 2 and x.shape[1] == 1:
                    # interprets also these as "vectors"
                    x = x.ravel()

                assert_array_almost_equal(toarray(x1), x, err_msg=repr((b, spmattype, 1)))
                assert_array_almost_equal(toarray(x2), x, err_msg=repr((b, spmattype, 2)))

                # dense vs. sparse output  ("vectors" are always dense)
                if isspmatrix(b) and x.ndim > 1:
                    assert_(isspmatrix(x1), repr((b, spmattype, 1)))
                    assert_(isspmatrix(x2), repr((b, spmattype, 2)))
                else:
                    assert_(isinstance(x1, np.ndarray), repr((b, spmattype, 1)))
                    assert_(isinstance(x2, np.ndarray), repr((b, spmattype, 2)))

                # check output shape
                if x.ndim == 1:
                    # "vector"
                    assert_equal(x1.shape, (A.shape[1],))
                    assert_equal(x2.shape, (A.shape[1],))
                else:
                    # "matrix"
                    assert_equal(x1.shape, x.shape)
                    assert_equal(x2.shape, x.shape)

        A = csc_matrix((3, 3))
        b = csc_matrix((1, 3))
        assert_raises(ValueError, spsolve, A, b)
Example #9
0
 def _validate_for_predict(self, X):
     X = atleast2d_or_csr(X, dtype=np.float64, order="C")
     if self._sparse and not sp.isspmatrix(X):
         X = sp.csr_matrix(X)
     elif sp.isspmatrix(X) and not self._sparse:
         raise ValueError("cannot use sparse input in %r trained on dense data" % type(self).__name__)
     if not self.raw_coef_.flags["F_CONTIGUOUS"]:
         warnings.warn("Coefficients are the fortran-contiguous. " "Copying them.", RuntimeWarning, stacklevel=3)
         self.raw_coef_ = np.asfortranarray(self.raw_coef_)
     self._check_n_features(X)
     return X
Example #10
0
def comp_uvz_spdns(umat, vmat, zmat):
    """comp u*v*z for sparse or dense u or v"""

    if sps.isspmatrix(vmat):
        vz = vmat * zmat
    else:
        vz = np.dot(vmat, zmat)
    if sps.isspmatrix(umat):
        return umat * vz
    else:
        return np.dot(umat, vz)
Example #11
0
def assert_sparse_matrix_equal(m1, m2, decimal=6):
    """Assert two scipy.sparse matrices are equal."""
    # both are sparse matricies
    assert isspmatrix(m1)
    assert isspmatrix(m1)

    # make sure they have the same format
    eq_(m1.format, m2.format)

    # even though its called assert_array_almost_equal, it will
    # work for scalars
    assert_array_almost_equal((m1 - m2).sum(), 0, decimal=decimal)
    def update_before_paramupdate(self, model):
        for i in xrange(len(model.grad_w)):
            if sp.isspmatrix(model.grad_w[i]):
                nonzero = model.grad_w[i].nonzero()
                self.nonzero["%d"%i] = nonzero

        #print type(model.grad_lw)        
        if sp.isspmatrix(model.grad_lw):
            nonzero = model.grad_lw.nonzero()
            #t1,t2 = model.grad_lw.shape
            #print "grad_lw sparity",len(nonzero[0])*1.0/t1 /t2
            self.nonzero['l'] = nonzero
    def compute_rate(self, model): 
        for i in xrange(len(model.grad_w)):
            if sp.isspmatrix(model.grad_w[i]):
                nonzero = self.nonzero['%d'%i]
                self.rate_w[i][nonzero] = self.initial_rate / np.sqrt(self.ada_w[i][nonzero])
            else:
                self.rate_w[i] = self.initial_rate / np.sqrt(self.ada_w[i])            
            self.rate_b[i] = self.initial_rate / np.sqrt(self.ada_b[i])

        if sp.isspmatrix(model.grad_lw):
            nonzero = self.nonzero['l']
            self.rate_lw[nonzero] = self.initial_rate / np.sqrt(self.ada_lw[nonzero])
        else:
            self.rate_lw = self.initial_rate / np.sqrt(self.ada_lw)
        self.rate_lb = self.initial_rate / np.sqrt(self.ada_lb)
Example #14
0
def _pre_fit(X, y, Xy, precompute, normalize, fit_intercept, copy):
    """Aux function used at beginning of fit in linear models"""
    n_samples, n_features = X.shape
    if sparse.isspmatrix(X):
        precompute = False
        X, y, X_mean, y_mean, X_std = sparse_center_data(
            X, y, fit_intercept, normalize)
    else:
        # copy was done in fit if necessary
        X, y, X_mean, y_mean, X_std = center_data(
            X, y, fit_intercept, normalize, copy=copy)

    if hasattr(precompute, '__array__') \
            and not np.allclose(X_mean, np.zeros(n_features)) \
            and not np.allclose(X_std, np.ones(n_features)):
        # recompute Gram
        precompute = 'auto'
        Xy = None

    # precompute if n_samples > n_features
    if precompute == 'auto':
        precompute = (n_samples > n_features)

    if precompute is True:
        precompute = np.dot(X.T, X)

    if not hasattr(precompute, '__array__'):
        Xy = None  # cannot use Xy if precompute is not Gram

    if hasattr(precompute, '__array__') and Xy is None:
        Xy = np.dot(X.T, y)

    return X, y, X_mean, y_mean, X_std, precompute, Xy
Example #15
0
    def _validate_for_predict(self, X):
        check_is_fitted(self, 'support_')

        X = check_array(X, accept_sparse='csr', dtype=np.float64, order="C")
        if self._sparse and not sp.isspmatrix(X):
            X = sp.csr_matrix(X)
        if self._sparse:
            X.sort_indices()

        if sp.issparse(X) and not self._sparse and not callable(self.kernel):
            raise ValueError(
                "cannot use sparse input in %r trained on dense data"
                % type(self).__name__)
        n_samples, n_features = X.shape

        if self.kernel == "precomputed":
            if X.shape[1] != self.shape_fit_[0]:
                raise ValueError("X.shape[1] = %d should be equal to %d, "
                                 "the number of samples at training time" %
                                 (X.shape[1], self.shape_fit_[0]))
        elif n_features != self.shape_fit_[1]:
            raise ValueError("X.shape[1] = %d should be equal to %d, "
                             "the number of features at training time" %
                             (n_features, self.shape_fit_[1]))
        return X
Example #16
0
File: linalg.py Project: bjzu/MF
def all(X, axis = None):
    """
    Test whether all elements along a given axis of sparse or dense matrix :param:`X` are nonzero.
    
    :param X: Target matrix.
    :type X: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil, dia or :class:`numpy.matrix`
    :param axis: Specified axis along which nonzero test is performed. If :param:`axis` not specified, whole matrix is considered. 
    :type axis: `int`
    """
    if sp.isspmatrix(X):
        X = X.tocsr()
        assert axis == 0 or axis == 1 or axis == None, "Incorrect axis number."
        if axis is None:
            return len(X.data) == X.shape[0] * X.shape[1]
        res = [0 for _ in xrange(X.shape[1 - axis])] 
        def _caxis(now, row, col):
            res[col] += 1
        def _raxis(now, row, col):
            res[row] += 1
        check = _caxis if axis == 0 else _raxis
        now = 0
        for row in range(X.shape[0]):
            upto = X.indptr[row+1]
            while now < upto:
                col = X.indices[now]
                check(now, row, col)
                now += 1
        sol = [x == X.shape[0] if axis == 0 else x == X.shape[1] for x in res]
        return np.mat(sol) if axis == 0 else np.mat(sol).T
    else:
        return X.all(axis)
Example #17
0
File: linalg.py Project: bjzu/MF
def norm(X, p = "fro"):
    """
    Compute entry-wise norms (! not induced/operator norms).
    
    :param X: The input matrix.
    :type X: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil, dia or :class:`numpy.matrix`
    :param p: Order of the norm.
    :type p: `str` or `float`
    """
    assert 1 in X.shape or p != 2, "Computing entry-wise norms only."
    if sp.isspmatrix(X):
        fro = lambda X: sum(abs(x)**2 for x in X.data)**(1. / 2)
        inf = lambda X: abs(X).sum(axis = 1).max() if 1 not in X.shape else abs(X).max()
        m_inf = lambda X: abs(X).sum(axis = 1).min() if 1 not in X.shape else abs(X).min()
        one = lambda X: abs(X).sum(axis = 0).max() if 1 not in X.shape else sum(abs(x)**p for x in X.data)**(1. / p)
        m_one = lambda X: abs(X).sum(axis = 0).min() if 1 not in X.shape else sum(abs(x)**p for x in X.data)**(1. / p)
        v = {
         "fro": fro,
         "inf": inf,
        "-inf": m_inf,
             1: one,
            -1: m_one,
            }.get(p)
        return v(X) if v != None else sum(abs(x)**p for x in X.data)**(1. / p)
    else:
        return nla.norm(np.mat(X), p)
Example #18
0
File: linalg.py Project: bjzu/MF
def std(X, axis = None, ddof = 0):
    """
    Compute the standard deviation along the specified :param:`axis` of matrix :param:`X`.
    
    :param X: Target matrix. 
    :type X: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil, dia or :class:`numpy.matrix`
    :param axis: Axis along which deviation is computed. If not specified, whole matrix :param:`X` is considered.
    :type axis: `int`
    :param ddof: Means delta degrees of freedom. The divisor used in computation is N - :param:`ddof`, where N represents the 
                 number of elements. Default is 0. 
    :type ddof: `float`
    """
    assert len(X.shape) == 2, "Input matrix X should be 2-D."
    assert axis == 0 or axis == 1 or axis == None, "Incorrect axis number."
    if sp.isspmatrix(X):
        if axis == None:
            mean = X.mean()
            no = X.shape[0] * X.shape[1]
            return sqrt(1. / (no - ddof) * sum((x - mean)**2 for x in X.data) + (no - len(X.data) * mean**2))
        if axis == 0:
            return np.mat([np.std(X[:, i].toarray(), axis, ddof) for i in xrange(X.shape[1])])
        if axis == 1:
            return np.mat([np.std(X[i, :].toarray(), axis, ddof) for i in xrange(X.shape[0])]).T
    else:
        return np.std(X, axis = axis, ddof = ddof)
Example #19
0
File: linalg.py Project: bjzu/MF
def dot(X, Y):
    """
    Compute dot product of matrices :param:`X` and :param:`Y`.
    
    :param X: First input matrix.
    :type X: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil, dia or :class:`numpy.matrix`
    :param Y: Second input matrix. 
    :type Y: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil, dia or :class:`numpy.matrix` 
    """
    if sp.isspmatrix(X) and sp.isspmatrix(Y):
        return X * Y
    elif sp.isspmatrix(X) or sp.isspmatrix(Y):
        # avoid dense dot product with mixed factors
        return sp.csr_matrix(X) * sp.csr_matrix(Y)
    else:
        return np.asmatrix(X) * np.asmatrix(Y)
Example #20
0
def sparse_center_data(X, y, fit_intercept, normalize=False):
    """
    Compute information needed to center data to have mean zero along
    axis 0. Be aware that X will not be centered since it would break
    the sparsity, but will be normalized if asked so.
    """
    if fit_intercept:
        # we might require not to change the csr matrix sometimes
        # store a copy if normalize is True.
        # Change dtype to float64 since mean_variance_axis accepts
        # it that way.
        if sp.isspmatrix(X) and X.getformat() == 'csr':
            X = sp.csr_matrix(X, copy=normalize, dtype=np.float64)
        else:
            X = sp.csc_matrix(X, copy=normalize, dtype=np.float64)

        X_offset, X_var = mean_variance_axis(X, axis=0)
        if normalize:
            # transform variance to std in-place
            X_var *= X.shape[0]
            X_std = np.sqrt(X_var, X_var)
            del X_var
            X_std[X_std == 0] = 1
            inplace_column_scale(X, 1. / X_std)
        else:
            X_std = np.ones(X.shape[1])
        y_offset = y.mean(axis=0)
        y = y - y_offset
    else:
        X_offset = np.zeros(X.shape[1])
        X_std = np.ones(X.shape[1])
        y_offset = 0. if y.ndim == 1 else np.zeros(y.shape[1], dtype=X.dtype)

    return X, y, X_offset, y_offset, X_std
Example #21
0
def extractData(features, examples=None, scaler=None, featureOrder=None, scaling=False):
    vec = DictVectorizer()
    samples = vec.fit_transform(features)
    featureNames = vec.get_feature_names()
    
    if (featureOrder != None):
        indices = [featureNames.index(feature) for feature in featureOrder]
        samples = samples[:, indices]
    imp = pp.Imputer(missing_values='NaN', strategy='mean')
    if (examples == None):
        imp.fit(samples)
    else :
        imp.fit(examples)
    impSamples = imp.transform(samples)
    if (impSamples.shape == samples.shape):
        samples = impSamples
    else:
        print("too few samples to replace missing values, using 0's")
        samples[shouldReplace(samples)]=0
    
#     if (scaler == None):
#         scaler = pp.StandardScaler(with_mean=False)
#         scaler.fit(samples)
#     samples = scaler.transform(samples)
    if (scaling):
        samples = pp.scale(samples,with_mean=False)
    if (sprs.isspmatrix(samples)):
        samples = samples.todense()
    
    return [samples, featureNames,imp,scaler]
    def fit(self, X, y, Xy=None, coef_init=None):
        """Fit Elastic Net model with coordinate descent

        Parameters
        -----------
        X: ndarray or scipy.sparse matrix, (n_samples, n_features)
            Data
        y: ndarray, (n_samples)
            Target
        Xy : array-like, optional
            Xy = np.dot(X.T, y) that can be precomputed. It is useful
            only when the Gram matrix is precomputed.
        coef_init: ndarray of shape n_features
            The initial coeffients to warm-start the optimization

        Notes
        -----

        Coordinate descent is an algorithm that considers each column of
        data at a time hence it will automatically convert the X input
        as a fortran contiguous numpy array if necessary.

        To avoid memory re-allocation it is advised to allocate the
        initial data in memory directly using that format.
        """

        fit = self._sparse_fit if sp.isspmatrix(X) else self._dense_fit
        fit(X, y, Xy, coef_init)
        return self
Example #23
0
    def predict_proba(self, X):
        """Predict probability for each possible outcome.

        Compute the probability estimates for each single sample in X
        and each possible outcome seen during training (categorical
        distribution).

        Parameters
        ----------
        X : array_like, shape = [n_samples, n_features]

        Returns
        -------
        probabilities : array, shape = [n_samples, n_classes]
            Normalized probability distributions across
            class labels
        """
        if sparse.isspmatrix(X):
            X_2d = X
        else:
            X_2d = np.atleast_2d(X)
        weight_matrices = self._get_kernel(self.X_, X_2d)
        if self.kernel == 'knn':
            probabilities = []
            for weight_matrix in weight_matrices:
                ine = np.sum(self.label_distributions_[weight_matrix], axis=0)
                probabilities.append(ine)
            probabilities = np.array(probabilities)
        else:
            weight_matrices = weight_matrices.T
            probabilities = np.dot(weight_matrices, self.label_distributions_)
        normalizer = np.atleast_2d(np.sum(probabilities, axis=1)).T
        probabilities /= normalizer
        return probabilities
    def solve(self, b):
        """
        Solve linear equation A x = b for x

        Parameters
        ----------
        b : ndarray
            Right-hand side of the matrix equation. Can be vector or a matrix.

        Returns
        -------
        x : ndarray
            Solution to the matrix equation

        """
        if isspmatrix(b):
            b = b.toarray()

        if b.shape[0] != self._A.shape[1]:
            raise ValueError("Shape of b is not compatible with that of A")

        b_arr = asarray(b, dtype=self._A.dtype).reshape(b.shape[0], -1)
        x = np.zeros((self._A.shape[0], b_arr.shape[1]), dtype=self._A.dtype)
        for j in range(b_arr.shape[1]):
            x[:,j] = self.umf.solve(UMFPACK_A, self._A, b_arr[:,j], autoTranspose=True)
        return x.reshape((self._A.shape[0],) + b.shape[1:])
Example #25
0
    def fit(self, X, y, Xy=None, coef_init=None):
        """Fit Elastic Net model with coordinate descent

        Parameters
        -----------
        X: ndarray or scipy.sparse matrix, (n_samples, n_features)
            Data
        y: ndarray, shape = (n_samples,) or (n_samples, n_targets)
            Target
        Xy : array-like, optional
            Xy = np.dot(X.T, y) that can be precomputed. It is useful
            only when the Gram matrix is precomputed.
        coef_init: ndarray of shape n_features or (n_targets, n_features)
            The initial coeffients to warm-start the optimization

        Notes
        -----

        Coordinate descent is an algorithm that considers each column of
        data at a time hence it will automatically convert the X input
        as a fortran contiguous numpy array if necessary.

        To avoid memory re-allocation it is advised to allocate the
        initial data in memory directly using that format.
        """
        X = atleast2d_or_csc(X, dtype=np.float64, order='F',
                             copy=self.copy_X and self.fit_intercept)
        # From now on X can be touched inplace
        y = np.asarray(y, dtype=np.float64)
        # now all computation with X can be done inplace
        fit = self._sparse_fit if sparse.isspmatrix(X) else self._dense_fit
        fit(X, y, Xy, coef_init)
        return self
Example #26
0
def affinity_matrix(distances, neighbors_radius, symmetrize = True):
    if neighbors_radius <= 0.:
        raise ValueError('neighbors_radius must be >0.')
    A = distances.copy()
    if sparse.isspmatrix( A ):
        A.data = A.data**2
        A.data = A.data/(-neighbors_radius**2)
        np.exp( A.data, A.data )
        if symmetrize:
            A = symmetrize_sparse( A )  # converts to CSR; deletes 0's
        else:
            pass
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            # sparse will complain that this is faster with lil_matrix
            A.setdiag(1) # the 0 on the diagonal is a true zero
    else:
        A **= 2
        A /= (-neighbors_radius**2)
        np.exp(A, A)
        if symmetrize:
            A = (A+A.T)/2
            A = np.asarray( A, order="C" )  # is this necessary??
        else:
            pass
    return A
def test_sparse_coef():
    """ Check that the sparse_coef propery works """
    clf = ElasticNet()
    clf.coef_ = [1, 2, 3]

    assert_true(sp.isspmatrix(clf.sparse_coef_))
    assert_equal(clf.sparse_coef_.todense().tolist()[0], clf.coef_)
Example #28
0
 def Vstack(Tuple):
     ind = where([isscalar(elem) or prod(elem.shape)!=0 for elem in Tuple])[0].tolist()
     elems = [Tuple[i] for i in ind]
     if PythonAny(isspmatrix(elem) for elem in elems):
         return VstackSP(elems)
     else:
         return vstack(elems)
Example #29
0
def plot(W, idx2term):
    """
    Plot the interpretation of NMF basis vectors on Medlars data set. 
    
    :param W: Basis matrix of the fitted factorization model.
    :type W: `scipy.sparse.csr_matrix`
    :param idx2term: Index-to-term translator.
    :type idx2term: `dict`
    """
    print "Plotting highest weighted terms in basis vectors ..."
    for c in xrange(W.shape[1]):
        if sp.isspmatrix(W):
            top10 = sorted(enumerate(W[:, c].todense().ravel().tolist()[0]), key = itemgetter(1), reverse = True)[:10]
        else:
            top10 = sorted(enumerate(W[:, c].ravel().tolist()[0]), key = itemgetter(1), reverse = True)[:10]
        pos = np.arange(10) + .5
        val = zip(*top10)[1][::-1]
        plb.figure(c + 1)
        plb.barh(pos, val, color = "yellow", align = "center")
        plb.yticks(pos, [idx2term[idx] for idx in zip(*top10)[0]][::-1])
        plb.xlabel("Weight")
        plb.ylabel("Term")
        plb.title("Highest Weighted Terms in Basis Vector W%d" % (c + 1))
        plb.grid(True)
        plb.savefig("documents_basisW%d.png" % (c + 1), bbox_inches = "tight")
    print "... Finished."
Example #30
0
def object_diff(a, b, pre=''):
    """Compute all differences between two python variables

    Parameters
    ----------
    a : object
        Currently supported: dict, list, tuple, ndarray, int, str, bytes,
        float.
    b : object
        Must be same type as x1.
    pre : str
        String to prepend to each line.

    Returns
    -------
    diffs : str
        A string representation of the differences.
    """
    out = ''
    if type(a) != type(b):
        out += pre + ' type mismatch (%s, %s)\n' % (type(a), type(b))
    elif isinstance(a, dict):
        k1s = _sort_keys(a)
        k2s = _sort_keys(b)
        m1 = set(k2s) - set(k1s)
        if len(m1):
            out += pre + ' x1 missing keys %s\n' % (m1)
        for key in k1s:
            if key not in k2s:
                out += pre + ' x2 missing key %s\n' % key
            else:
                out += object_diff(a[key], b[key], pre + 'd1[%s]' % repr(key))
    elif isinstance(a, (list, tuple)):
        if len(a) != len(b):
            out += pre + ' length mismatch (%s, %s)\n' % (len(a), len(b))
        else:
            for xx1, xx2 in zip(a, b):
                out += object_diff(xx1, xx2, pre='')
    elif isinstance(a, (string_types, int, float, bytes)):
        if a != b:
            out += pre + ' value mismatch (%s, %s)\n' % (a, b)
    elif a is None:
        pass  # b must be None due to our type checking
    elif isinstance(a, np.ndarray):
        if not np.array_equal(a, b):
            out += pre + ' array mismatch\n'
    elif sparse is not None and sparse.isspmatrix(a):
        # sparsity and sparse type of b vs a already checked above by type()
        if b.shape != a.shape:
            out += pre + (' sparse matrix a and b shape mismatch'
                          '(%s vs %s)' % (a.shape, b.shape))
        else:
            c = a - b
            c.eliminate_zeros()
            if c.nnz > 0:
                out += pre + (' sparse matrix a and b differ on %s '
                              'elements' % c.nnz)
    else:
        raise RuntimeError(pre + ': unsupported type %s (%s)' % (type(a), a))
    return out
Example #31
0
    def __init__(self, adj_matrix, attr_matrix=None, labels=None,
                 node_names=None, attr_names=None, class_names=None, metadata=None):
        """Create an attributed graph.

        Parameters
        ----------
        adj_matrix : sp.csr_matrix, shape [num_nodes, num_nodes]
            Adjacency matrix in CSR format.
        attr_matrix : sp.csr_matrix or np.ndarray, shape [num_nodes, num_attr], optional
            Attribute matrix in CSR or numpy format.
        labels : np.ndarray, shape [num_nodes], optional
            Array, where each entry represents respective node's label(s).
        node_names : np.ndarray, shape [num_nodes], optional
            Names of nodes (as strings).
        attr_names : np.ndarray, shape [num_attr]
            Names of the attributes (as strings).
        class_names : np.ndarray, shape [num_classes], optional
            Names of the class labels (as strings).
        metadata : object
            Additional metadata such as text.

        """
        # Make sure that the dimensions of matrices / arrays all agree
        if sp.isspmatrix(adj_matrix):
            adj_matrix = adj_matrix.tocsr().astype(np.float32)
        else:
            raise ValueError("Adjacency matrix must be in sparse format (got {0} instead)"
                             .format(type(adj_matrix)))

        if adj_matrix.shape[0] != adj_matrix.shape[1]:
            raise ValueError("Dimensions of the adjacency matrix don't agree")

        if attr_matrix is not None:
            if sp.isspmatrix(attr_matrix):
                attr_matrix = attr_matrix.tocsr().astype(np.float32)
            elif isinstance(attr_matrix, np.ndarray):
                attr_matrix = attr_matrix.astype(np.float32)
            else:
                raise ValueError("Attribute matrix must be a sp.spmatrix or a np.ndarray (got {0} instead)"
                                 .format(type(attr_matrix)))

            if attr_matrix.shape[0] != adj_matrix.shape[0]:
                raise ValueError("Dimensions of the adjacency and attribute matrices don't agree")

        if labels is not None:
            if labels.shape[0] != adj_matrix.shape[0]:
                raise ValueError("Dimensions of the adjacency matrix and the label vector don't agree")

        if node_names is not None:
            if len(node_names) != adj_matrix.shape[0]:
                raise ValueError("Dimensions of the adjacency matrix and the node names don't agree")

        if attr_names is not None:
            if len(attr_names) != attr_matrix.shape[1]:
                raise ValueError("Dimensions of the attribute matrix and the attribute names don't agree")

        self.adj_matrix = adj_matrix
        self.attr_matrix = attr_matrix
        self.labels = labels
        self.node_names = node_names
        self.attr_names = attr_names
        self.class_names = class_names
        self.metadata = metadata
Example #32
0
def spsolve(A, b, permc_spec=None, use_umfpack=True):
    """Solve the sparse linear system Ax=b, where b may be a vector or a matrix.

    Parameters
    ----------
    A : ndarray or sparse matrix
        The square matrix A will be converted into CSC or CSR form
    b : ndarray or sparse matrix
        The matrix or vector representing the right hand side of the equation.
        If a vector, b.size must be (n,) or (n, 1)
    permc_spec : str, optional
        How to permute the columns of the matrix for sparsity preservation.
        (default: 'COLAMD')

        - ``NATURAL``: natural ordering.
        - ``MMD_ATA``: minimum degree ordering on the structure of A^T A.
        - ``MMD_AT_PLUS_A``: minimum degree ordering on the structure of A^T+A.
        - ``COLAMD``: approximate minimum degree column ordering
    use_umfpack : bool, optional
        if True (default) then use umfpack for the solution.  This is
        only referenced if b is a vector and ``scikit-umfpack`` is installed.

    Returns
    -------
    x : ndarray or sparse matrix
        the solution of the sparse linear equation.
        If b is a vector, then x is a vector of size A.shape[1]
        If b is a matrix, then x is a matrix of size (A.shape[1], b.shape[1])

    Notes
    -----
    For solving the matrix expression AX = B, this solver assumes the resulting
    matrix X is sparse, as is often the case for very sparse inputs.  If the
    resulting X is dense, the construction of this sparse result will be
    relatively expensive.  In that case, consider converting A to a dense
    matrix and using scipy.linalg.solve or its variants.
    """
    if not (isspmatrix_csc(A) or isspmatrix_csr(A)):
        A = csc_matrix(A)
        warn('spsolve requires A be CSC or CSR matrix format',
                SparseEfficiencyWarning)

    # b is a vector only if b have shape (n,) or (n, 1)
    b_is_sparse = isspmatrix(b)
    if not b_is_sparse:
        b = asarray(b)
    b_is_vector = ((b.ndim == 1) or (b.ndim == 2 and b.shape[1] == 1))

    A.sort_indices()
    A = A.asfptype()  # upcast to a floating point format

    # validate input shapes
    M, N = A.shape
    if (M != N):
        raise ValueError("matrix must be square (has shape %s)" % ((M, N),))

    if M != b.shape[0]:
        raise ValueError("matrix - rhs dimension mismatch (%s - %s)"
                         % (A.shape, b.shape[0]))

    use_umfpack = use_umfpack and useUmfpack

    if b_is_vector and use_umfpack:
        if b_is_sparse:
            b_vec = b.toarray()
        else:
            b_vec = b
        b_vec = asarray(b_vec, dtype=A.dtype).ravel()

        if noScikit:
            raise RuntimeError('Scikits.umfpack not installed.')

        if A.dtype.char not in 'dD':
            raise ValueError("convert matrix data to double, please, using"
                  " .astype(), or set linsolve.useUmfpack = False")

        family = {'d': 'di', 'D': 'zi'}
        umf = umfpack.UmfpackContext(family[A.dtype.char])
        x = umf.linsolve(umfpack.UMFPACK_A, A, b_vec,
                         autoTranspose=True)
    else:
        if b_is_vector and b_is_sparse:
            b = b.toarray()
            b_is_sparse = False

        if not b_is_sparse:
            if isspmatrix_csc(A):
                flag = 1  # CSC format
            else:
                flag = 0  # CSR format

            options = dict(ColPerm=permc_spec)
            x, info = _superlu.gssv(N, A.nnz, A.data, A.indices, A.indptr,
                                    b, flag, options=options)
            if info != 0:
                warn("Matrix is exactly singular", MatrixRankWarning)
                x.fill(np.nan)
            if b_is_vector:
                x = x.ravel()
        else:
            # b is sparse
            Afactsolve = factorized(A)

            if not isspmatrix_csc(b):
                warn('spsolve is more efficient when sparse b '
                     'is in the CSC matrix format', SparseEfficiencyWarning)
                b = csc_matrix(b)

            # Create a sparse output matrix by repeatedly applying
            # the sparse factorization to solve columns of b.
            data_segs = []
            row_segs = []
            col_segs = []
            for j in range(b.shape[1]):
                bj = b[:, j].A.ravel()
                xj = Afactsolve(bj)
                w = np.flatnonzero(xj)
                segment_length = w.shape[0]
                row_segs.append(w)
                col_segs.append(np.ones(segment_length, dtype=int)*j)
                data_segs.append(np.asarray(xj[w], dtype=A.dtype))
            sparse_data = np.concatenate(data_segs)
            sparse_row = np.concatenate(row_segs)
            sparse_col = np.concatenate(col_segs)
            x = A.__class__((sparse_data, (sparse_row, sparse_col)),
                           shape=b.shape, dtype=A.dtype)

    return x
Example #33
0
def fit(self, X, y, sample_weight=None):
    """Fit the SVM model according to the given training data.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape (n_samples, n_features)
            Training vectors, where n_samples is the number of samples
            and n_features is the number of features.
            For kernel="precomputed", the expected shape of X is
            (n_samples, n_samples).

        y : array-like, shape (n_samples,)
            Target values (class labels in classification, real numbers in
            regression)

        sample_weight : array-like, shape (n_samples,)
            Per-sample weights. Rescale C per sample. Higher weights
            force the classifier to put more emphasis on these points.

        Returns
        -------
        self : object

        Notes
        ------
        If X and y are not C-ordered and contiguous arrays of np.float64 and
        X is not a scipy.sparse.csr_matrix, X and/or y may be copied.

        If X is a dense array, then the other methods will not support sparse
        matrices as input.
        """
    rnd = check_random_state(self.random_state)

    sparse = sp.isspmatrix(X)
    if sparse and self.kernel == "precomputed":
        raise TypeError("Sparse precomputed kernels are not supported.")
    self._sparse = sparse and not callable(self.kernel)

    X, y = check_X_y(X,
                     y,
                     dtype=np.float64,
                     order='C',
                     accept_sparse='csr',
                     accept_large_sparse=False)
    y = self._validate_targets(y)

    sample_weight = np.asarray([] if sample_weight is None else sample_weight,
                               dtype=np.float64)
    solver_type = _get_libsvm_impl().index(self._impl)

    # input validation
    if solver_type != 2 and X.shape[0] != y.shape[0]:
        raise ValueError("X and y have incompatible shapes.\n" +
                         "X has %s samples, but y has %s." %
                         (X.shape[0], y.shape[0]))

    if self.kernel == "precomputed" and X.shape[0] != X.shape[1]:
        raise ValueError("X.shape[0] should be equal to X.shape[1]")

    if sample_weight.shape[0] > 0 and sample_weight.shape[0] != X.shape[0]:
        raise ValueError("sample_weight and X have incompatible shapes: "
                         "%r vs %r\n"
                         "Note: Sparse matrices cannot be indexed w/"
                         "boolean masks (use `indices=True` in CV)." %
                         (sample_weight.shape, X.shape))

    self._gamma = _compute_gamma(self.gamma, self.kernel, X, sparse)

    kernel = self.kernel
    if callable(kernel):
        kernel = 'precomputed'

    fit = self._sparse_fit if self._sparse else self._dense_fit
    if self.verbose:  # pragma: no cover
        print('[LibSVM]', end='')

    # see comment on the other call to np.iinfo in this file
    seed = rnd.randint(np.iinfo('i').max)

    is_support_weights = sample_weight.size == 0 and self.class_weight is None
    if ( not sparse and not self.probability and not getattr(self, 'break_ties', False) and \
         kernel in ['linear', 'rbf']) and is_support_weights:
        logging.info("sklearn.svm.SVC.fit: " + get_patch_message("daal"))
        self._daal_fit = True
        _daal4py_fit(self, X, y, kernel)
        self.fit_status_ = 0
    else:
        logging.info("sklearn.svm.SVC.fit: " + get_patch_message("sklearn"))
        self._daal_fit = False
        fit(X, y, sample_weight, solver_type, kernel, random_seed=seed)

    self.shape_fit_ = X.shape

    # In binary case, we need to flip the sign of coef, intercept and
    # decision function. Use self._intercept_ and self._dual_coef_ internally.
    if not self._daal_fit:
        self._internal_intercept_ = self.intercept_.copy()
        self._internal_dual_coef_ = self.dual_coef_.copy()
    else:
        self._internal_intercept_ = self.intercept_.copy()
        self._internal_dual_coef_ = self.dual_coef_.copy()
        if len(self.classes_) == 2:
            self._internal_dual_coef_ *= -1
            self._internal_intercept_ *= -1

    if not self._daal_fit and len(
            self.classes_) == 2 and self._impl in ['c_svc', 'nu_svc']:
        self.intercept_ *= -1
        self.dual_coef_ *= -1

    return self
Example #34
0
    def predict(self, X):
        """
        Predict regression target for X.

        The predicted regression target of an input sample is computed.

        Parameters
        ----------
        X : array-like or sparse matrix of shape = [n_samples, n_features]
            The input samples.

        Returns
        -------
        y : array of shape = [n_samples]
            The predicted values.
        """
        if self._fitted is None:
            raise NotFittedError(_NOT_FITTED_ERROR_DESC)

        X = check_array(X, accept_sparse=True)
        n_features = X.shape[1]
        if self._n_features != n_features:
            raise ValueError("Number of features of the model must "
                             "match the input. Model n_features is %s and "
                             "input n_features is %s "
                             % (self._n_features, n_features))

        test_x_loc = os.path.join(_TEMP_PATH, self._file_prefix + ".test.data.x")
        if sp.isspmatrix(X):
            _sparse_savetxt(test_x_loc, X)
        else:
            np.savetxt(test_x_loc, X, delimiter=' ', fmt="%s")

        # Find latest model location
        model_glob = os.path.join(_TEMP_PATH, self._file_prefix + ".model*")
        model_files = glob(model_glob)
        if not model_files:
            raise Exception('Model learning result is not found in {0}. '
                            'This is rgf_python error.'.format(_TEMP_PATH))
        latest_model_loc = sorted(model_files, reverse=True)[0]

        # Format test command
        pred_loc = os.path.join(_TEMP_PATH, self._file_prefix + ".predictions.txt")
        params = []
        params.append("test_x_fn=%s" % test_x_loc)
        params.append("prediction_fn=%s" % pred_loc)
        params.append("model_fn=%s" % latest_model_loc)

        cmd = (_EXE_PATH, "predict", ",".join(params))

        output = subprocess.Popen(cmd,
                                  stdout=subprocess.PIPE,
                                  stderr=subprocess.STDOUT,
                                  universal_newlines=True).communicate()

        if self.verbose:
            for k in output:
                print(k)

        y_pred = np.loadtxt(pred_loc)
        return y_pred
Example #35
0
    def fit(self, X, y, sample_weight=None):
        """Fit the SVM model according to the given training data.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape = [n_samples, n_features]
            Training vectors, where n_samples is the number of samples
            and n_features is the number of features.

        y : array-like, shape = [n_samples]
            Target values (class labels in classification, real numbers in
            regression)

        sample_weight : array-like, shape = [n_samples], optional
            Weights applied to individual samples (1. for unweighted).

        Returns
        -------
        self : object
            Returns self.

        Notes
        ------
        If X and y are not C-ordered and contiguous arrays of np.float64 and
        X is not a scipy.sparse.csr_matrix, X and/or y may be copied.

        If X is a dense array, then the other methods will not support sparse
        matrices as input.
        """

        self._sparse = sp.isspmatrix(X) and not self._pairwise

        if self._sparse and self._pairwise:
            raise ValueError("Sparse precomputed kernels are not supported. "
                             "Using sparse data and dense kernels is possible "
                             "by not using the ``sparse`` parameter")

        X = atleast2d_or_csr(X, dtype=np.float64, order='C')
        y = self._validate_targets(y)

        sample_weight = np.asarray(
            [] if sample_weight is None else sample_weight, dtype=np.float64)
        solver_type = LIBSVM_IMPL.index(self._impl)

        # input validation
        if solver_type != 2 and X.shape[0] != y.shape[0]:
            raise ValueError("X and y have incompatible shapes.\n" +
                             "X has %s samples, but y has %s." %
                             (X.shape[0], y.shape[0]))

        if self.kernel == "precomputed" and X.shape[0] != X.shape[1]:
            raise ValueError("X.shape[0] should be equal to X.shape[1]")

        if sample_weight.shape[0] > 0 and sample_weight.shape[0] != X.shape[0]:
            raise ValueError("sample_weight and X have incompatible shapes:"
                             "%r vs %r\n"
                             "Note: Sparse matrices cannot be indexed w/"
                             "boolean masks (use `indices=True` in CV)." %
                             (sample_weight.shape, X.shape))

        if (self.kernel in ['poly', 'rbf']) and (self.gamma == 0):
            # if custom gamma is not provided ...
            self._gamma = 1.0 / X.shape[1]
        else:
            self._gamma = self.gamma

        kernel = self.kernel
        if callable(kernel):
            kernel = 'precomputed'

        fit = self._sparse_fit if self._sparse else self._dense_fit
        if self.verbose:  # pragma: no cover
            print('[LibSVM]', end='')
        fit(X, y, sample_weight, solver_type, kernel)

        self.shape_fit_ = X.shape

        # In binary case, we need to flip the sign of coef, intercept and
        # decision function. Use self._intercept_ internally.
        self._intercept_ = self.intercept_.copy()
        if self._impl in ['c_svc', 'nu_svc'] and len(self.classes_) == 2:
            self.intercept_ *= -1
        return self
Example #36
0
def mkl_spsolve(A, b, perm=None, verbose=False, **kwargs):
    """
    Solves a sparse linear system of equations using the 
    Intel MKL Pardiso solver.
    
    Parameters
    ----------
    A : csr_matrix
        Sparse matrix.
    b : ndarray or sparse matrix
        The vector or matrix representing the right hand side of the equation.
        If a vector, b.shape must be (n,) or (n, 1).
    perm : ndarray (optional)
        User defined matrix factorization permutation.
    
    Returns
    -------
    x : ndarray or csr_matrix
        The solution of the sparse linear equation.
        If b is a vector, then x is a vector of size A.shape[1]
        If b is a matrix, then x is a matrix of size (A.shape[1], b.shape[1])
    
    """
    lu = mkl_splu(A, perm=perm, verbose=verbose, **kwargs)
    b_is_sparse = sp.isspmatrix(b)
    b_shp = b.shape
    if b_is_sparse and b.shape[1] == 1:
        b = b.toarray()
        b_is_sparse = False
    elif b_is_sparse and b.shape[1] != 1:
        nrhs = b.shape[1]
        if lu._is_complex:
            b = sp.csc_matrix(b, dtype=np.complex128, copy=False)
        else:
            b = sp.csc_matrix(b, dtype=np.float64, copy=False)

    # Do dense RHS solving
    if not b_is_sparse:
        x = lu.solve(b, verbose=verbose)
    # Solve each RHS vec individually and convert to sparse
    else:
        data_segs = []
        row_segs = []
        col_segs = []
        for j in range(nrhs):
            bj = b[:, j].A.ravel()
            xj = lu.solve(bj)
            w = np.flatnonzero(xj)
            segment_length = w.shape[0]
            row_segs.append(w)
            col_segs.append(np.ones(segment_length, dtype=np.int32) * j)
            data_segs.append(np.asarray(xj[w], dtype=xj.dtype))
        sp_data = np.concatenate(data_segs)
        sp_row = np.concatenate(row_segs)
        sp_col = np.concatenate(col_segs)
        x = sp.coo_matrix((sp_data, (sp_row, sp_col)), shape=b_shp).tocsr()

    info = lu.info()
    lu.delete()
    if 'return_info' in kwargs.keys() and kwargs['return_info'] == True:
        return x, info
    else:
        return x
Example #37
0
def approximate_spectral_radius(A,
                                tol=0.01,
                                maxiter=15,
                                restart=5,
                                symmetric=None,
                                initial_guess=None,
                                return_vector=False):
    """Approximate the spectral radius of a matrix.

    Parameters
    ----------
    A : {dense or sparse matrix}
        E.g. csr_matrix, csc_matrix, ndarray, etc.
    tol : {scalar}
        Relative tolerance of approximation, i.e., the error divided
        by the approximate spectral radius is compared to tol.
    maxiter : {integer}
        Maximum number of iterations to perform
    restart : {integer}
        Number of restarted Arnoldi processes.  For example, a value of 0 will
        run Arnoldi once, for maxiter iterations, and a value of 1 will restart
        Arnoldi once, using the maximal eigenvector from the first Arnoldi
        process as the initial guess.
    symmetric : {boolean}
        True  - if A is symmetric Lanczos iteration is used (more efficient)
        False - if A is non-symmetric Arnoldi iteration is used (less efficient)
    initial_guess : {array|None}
        If n x 1 array, then use as initial guess for Arnoldi/Lanczos.
        If None, then use a random initial guess.
    return_vector : {boolean}
        True - return an approximate dominant eigenvector, in addition to the spectral radius.
        False - Do not return the approximate dominant eigenvector

    Returns
    -------
    An approximation to the spectral radius of A, and
    if return_vector=True, then also return the approximate dominant
    eigenvector

    Notes
    -----
    The spectral radius is approximated by looking at the Ritz eigenvalues.
    Arnoldi iteration (or Lanczos) is used to project the matrix A onto a
    Krylov subspace: H = Q* A Q.  The eigenvalues of H (i.e. the Ritz
    eigenvalues) should represent the eigenvalues of A in the sense that the
    minimum and maximum values are usually well matched (for the symmetric case
    it is true since the eigenvalues are real).

    References
    ----------
    .. [1] Z. Bai, J. Demmel, J. Dongarra, A. Ruhe, and H. van der Vorst,
       editors.  "Templates for the Solution of Algebraic Eigenvalue Problems:
       A Practical Guide", SIAM, Philadelphia, 2000.

    Examples
    --------
    >>> from pyamg.util.linalg import approximate_spectral_radius
    >>> import numpy as np
    >>> from scipy.linalg import eigvals, norm
    >>> A = np.array([[1.,0.],[0.,1.]])
    >>> print approximate_spectral_radius(A,maxiter=3)
    1.0
    >>> print max([norm(x) for x in eigvals(A)])
    1.0

    """
    if not hasattr(A, 'rho') or return_vector:
        # somehow more restart causes a nonsymmetric case to fail...look at
        # this what about A.dtype=int?  convert somehow?

        # The use of the restart vector v0 requires that the full Krylov
        # subspace V be stored.  So, set symmetric to False.
        symmetric = False

        if maxiter < 1:
            raise ValueError('expected maxiter > 0')
        if restart < 0:
            raise ValueError('expected restart >= 0')
        if A.dtype == int:
            raise ValueError('expected A to be float (complex or real)')
        if A.shape[0] != A.shape[1]:
            raise ValueError('expected square A')

        if initial_guess is None:
            v0 = sp.rand(A.shape[1], 1)
            if A.dtype == complex:
                v0 = v0 + 1.0j * sp.rand(A.shape[1], 1)
        else:
            if initial_guess.shape[0] != A.shape[0]:
                raise ValueError('initial_guess and A must have same shape')
            if (len(initial_guess.shape) > 1) and (initial_guess.shape[1] > 1):
                raise ValueError('initial_guess must be an (n,1) or\
                                  (n,) vector')
            v0 = initial_guess.reshape(-1, 1)
            v0 = np.array(v0, dtype=A.dtype)

        for j in range(restart + 1):
            [evect, ev, H, V, breakdown_flag] =\
                _approximate_eigenvalues(A, tol, maxiter,
                                         symmetric, initial_guess=v0)
            # Calculate error in dominant eigenvector
            nvecs = ev.shape[0]
            max_index = np.abs(ev).argmax()
            error = H[nvecs, nvecs - 1] * evect[-1, max_index]

            # error is a fast way of calculating the following line
            # error2 = ( A - ev[max_index]*sp.mat(
            #           sp.eye(A.shape[0],A.shape[1])) )*\
            #           ( sp.mat(sp.hstack(V[:-1]))*\
            #           evect[:,max_index].reshape(-1,1) )
            # print str(error) + "    " + str(sp.linalg.norm(e2))

            if (np.abs(error)/np.abs(ev[max_index]) < tol) or\
               breakdown_flag:
                # halt if below relative tolerance
                v0 = np.dot(np.hstack(V[:-1]), evect[:,
                                                     max_index].reshape(-1, 1))
                break
            else:
                v0 = np.dot(np.hstack(V[:-1]), evect[:,
                                                     max_index].reshape(-1, 1))
        # end j-loop

        rho = np.abs(ev[max_index])
        if sparse.isspmatrix(A):
            A.rho = rho

        if return_vector:
            return (rho, v0)
        else:
            return rho

    else:
        return A.rho
Example #38
0
    def _write(self,
               stream,
               a,
               comment='',
               field=None,
               precision=None,
               symmetry=None):
        if isinstance(a, list) or isinstance(a, ndarray) or \
           isinstance(a, tuple) or hasattr(a, '__array__'):
            rep = self.FORMAT_ARRAY
            a = asarray(a)
            if len(a.shape) != 2:
                raise ValueError('Expected 2 dimensional array')
            rows, cols = a.shape

            if field is not None:

                if field == self.FIELD_INTEGER:
                    if not can_cast(a.dtype, 'intp'):
                        raise OverflowError(
                            "mmwrite does not support integer "
                            "dtypes larger than native 'intp'.")
                    a = a.astype('intp')
                elif field == self.FIELD_REAL:
                    if a.dtype.char not in 'fd':
                        a = a.astype('d')
                elif field == self.FIELD_COMPLEX:
                    if a.dtype.char not in 'FD':
                        a = a.astype('D')

        else:
            if not isspmatrix(a):
                raise ValueError('unknown matrix type: %s' % type(a))

            rep = 'coordinate'
            rows, cols = a.shape

        typecode = a.dtype.char

        if precision is None:
            if typecode in 'fF':
                precision = 8
            else:
                precision = 16
        if field is None:
            kind = a.dtype.kind
            if kind == 'i':
                if not can_cast(a.dtype, 'intp'):
                    raise OverflowError("mmwrite does not support integer "
                                        "dtypes larger than native 'intp'.")
                field = 'integer'
            elif kind == 'f':
                field = 'real'
            elif kind == 'c':
                field = 'complex'
            elif kind == 'u':
                field = 'unsigned-integer'
            else:
                raise TypeError('unexpected dtype kind ' + kind)

        if symmetry is None:
            symmetry = self._get_symmetry(a)

        # validate rep, field, and symmetry
        self.__class__._validate_format(rep)
        self.__class__._validate_field(field)
        self.__class__._validate_symmetry(symmetry)

        # write initial header line
        stream.write(
            asbytes('%%MatrixMarket matrix {0} {1} {2}\n'.format(
                rep, field, symmetry)))

        # write comments
        for line in comment.split('\n'):
            stream.write(asbytes('%%%s\n' % (line)))

        template = self._field_template(field, precision)
        # write dense format
        if rep == self.FORMAT_ARRAY:
            # write shape spec
            stream.write(asbytes('%i %i\n' % (rows, cols)))

            if field in (self.FIELD_INTEGER, self.FIELD_REAL,
                         self.FIELD_UNSIGNED):
                if symmetry == self.SYMMETRY_GENERAL:
                    for j in range(cols):
                        for i in range(rows):
                            stream.write(asbytes(template % a[i, j]))

                elif symmetry == self.SYMMETRY_SKEW_SYMMETRIC:
                    for j in range(cols):
                        for i in range(j + 1, rows):
                            stream.write(asbytes(template % a[i, j]))

                else:
                    for j in range(cols):
                        for i in range(j, rows):
                            stream.write(asbytes(template % a[i, j]))

            elif field == self.FIELD_COMPLEX:

                if symmetry == self.SYMMETRY_GENERAL:
                    for j in range(cols):
                        for i in range(rows):
                            aij = a[i, j]
                            stream.write(
                                asbytes(template % (real(aij), imag(aij))))
                else:
                    for j in range(cols):
                        for i in range(j, rows):
                            aij = a[i, j]
                            stream.write(
                                asbytes(template % (real(aij), imag(aij))))

            elif field == self.FIELD_PATTERN:
                raise ValueError('pattern type inconsisted with dense format')

            else:
                raise TypeError('Unknown field type %s' % field)

        # write sparse format
        else:
            coo = a.tocoo()  # convert to COOrdinate format

            # if symmetry format used, remove values above main diagonal
            if symmetry != self.SYMMETRY_GENERAL:
                lower_triangle_mask = coo.row >= coo.col
                coo = coo_matrix((coo.data[lower_triangle_mask],
                                  (coo.row[lower_triangle_mask],
                                   coo.col[lower_triangle_mask])),
                                 shape=coo.shape)

            # write shape spec
            stream.write(asbytes('%i %i %i\n' % (rows, cols, coo.nnz)))

            template = self._field_template(field, precision - 1)

            if field == self.FIELD_PATTERN:
                for r, c in zip(coo.row + 1, coo.col + 1):
                    stream.write(asbytes("%i %i\n" % (r, c)))
            elif field in (self.FIELD_INTEGER, self.FIELD_REAL,
                           self.FIELD_UNSIGNED):
                for r, c, d in zip(coo.row + 1, coo.col + 1, coo.data):
                    stream.write(asbytes(("%i %i " % (r, c)) + (template % d)))
            elif field == self.FIELD_COMPLEX:
                for r, c, d in zip(coo.row + 1, coo.col + 1, coo.data):
                    stream.write(
                        asbytes(("%i %i " % (r, c)) + (template %
                                                       (d.real, d.imag))))
            else:
                raise TypeError('Unknown field type %s' % field)
Example #39
0
def train_GAN(h0_size, h1_size, h2_size, h3_size, NUM_EPOCH, NUM_SUB_EPOCHS,
              BATCH_SIZE, DISPLAY_ITER, LEARNING_RATE, to_restore, model_name,
              dataset, GANLAMBDA):

    DATA_DIR = dataset + '/'

    show2id_path = DATA_DIR + "item2id.txt"
    niche_tags_path = DATA_DIR + "niche_items.txt"

    user_tag_matrix_path = DATA_DIR + "item_counts.csv"

    dataset_name = dataset.split('/')[-1].strip()

    if dataset_name == '':
        dataset_name = dataset.split('/')[-2].strip()

    output_path = "chkpt/" + dataset_name + "_" + model_name + "_" + str(
        GANLAMBDA) + "/"

    if not os.path.exists(output_path):
        os.makedirs(output_path)

    item_list_path = DATA_DIR + 'item_list.txt'

    pro_dir = DATA_DIR  # os.path.join(DATA_DIR, 'pro_sg_tags_1k')

    unique_sid = list()
    with open(os.path.join(pro_dir, 'unique_item_id.txt'), 'r') as f:
        for line in f:
            unique_sid.append(line.strip())

    n_items = len(unique_sid)

    print('Loading Items...', end='')
    SHOW2ID, IDs_present, NICHE_TAGS, ALL_TAGS, OTHER_TAGS = load_pop_niche_tags(
        show2id_path, item_list_path, niche_tags_path, n_items)
    print('Done.')

    # One Hot Vectors for Items
    print('Loading Item Features...', end='')
    ITEM_FEATURE_DICT, FEATURE_LEN, ITEM_FEATURE_ARR = load_item_features(
        item_list_path, SHOW2ID, n_items)
    print('Done.')

    # Load Binary Interaction Matrix X
    print('Loading Training Interaction Matrix...', end='')
    train_data, uid_start_idx = load_train_data(
        os.path.join(pro_dir, 'train_GAN.csv'), n_items)
    print('Done.')

    # Load Data for Validation
    print('Loading Validation Matrix...', end='')
    vad_data_tr, vad_data_te, uid_start_idx_vad = load_tr_te_data(
        os.path.join(pro_dir, 'validation_tr.csv'),
        os.path.join(pro_dir, 'validation_te.csv'), n_items)
    print('Done.')

    # Load User's Popular and Niche Items
    print("Loading User's Popular and Niche Items...", end='')
    user_popular_data = load_user_items(
        os.path.join(pro_dir, 'train_GAN_popular.csv'))
    user_niche_data = load_user_items(
        os.path.join(pro_dir, 'train_GAN_niche.csv'))
    print("Done.")

    print('Loading item overlap coefficients....', end='')
    OVERLAP_COEFFS = load_overlap_coeff(show2id_path, user_tag_matrix_path)
    print('Done.')

    N = train_data.shape[0]
    idxlist = range(N)

    user_x_niche_vectors, user_x_popular_n_vectors = load_vectors(
        user_popular_data, user_niche_data, OVERLAP_COEFFS, ITEM_FEATURE_DICT,
        N)
    print('Vectors Loaded')

    print('Loading Items to Sample....', end='')
    USER_TAGS_TO_SAMPLE = load_items_to_sample(user_popular_data,
                                               user_niche_data, NICHE_TAGS,
                                               OVERLAP_COEFFS, N)
    print("Done")

    N_vad = vad_data_tr.shape[0]
    idxlist_vad = range(N_vad)

    print('Number of Users: ', N)

    batches_per_epoch = int(np.ceil(float(N) / BATCH_SIZE))

    print('Batches Per Epoch: ', batches_per_epoch)

    global_step = tf.Variable(0, name="global_step", trainable=False)

    tf.reset_default_graph()

    # Generator
    generator_network, generator_out, g_vae_loss, g_params, p_dims, total_anneal_steps, anneal_cap = generator(
        pro_dir)

    generated_tags = tf.placeholder(tf.float32, [None, n_items],
                                    name="generated_tags")

    # Discriminator
    y_data, y_generated, d_params, x_generated_id, x_popular_n_id, x_popular_g_id, x_niche_id, item_feature_arr, keep_prob = discriminator(
        n_items, FEATURE_LEN, h0_size, h1_size, h2_size, h3_size)

    zero = tf.constant(0, dtype=tf.float32)

    # Loss Function

    d_loss = -tf.reduce_sum(tf.log(y_data)) - tf.reduce_sum(
        tf.log(1 - y_generated))
    d_loss_mean = tf.reduce_mean(d_loss)

    sampled_generator_out = tf.multiply(generator_out, generated_tags)

    sampled_generator_out = tf.reshape(sampled_generator_out, [-1])

    sampled_generator_out_non_zero = tf.gather_nd(
        sampled_generator_out,
        tf.where(tf.not_equal(sampled_generator_out, zero)))

    sampled_cnt = tf.placeholder_with_default(1., shape=None)
    gen_lambda = tf.placeholder_with_default(1.0, shape=None)

    g_loss = g_vae_loss - (1.0 * gen_lambda / sampled_cnt) * tf.reduce_sum(
        tf.multiply(sampled_generator_out_non_zero, y_generated))
    g_loss_mean = tf.reduce_mean(g_loss)
    gan_loss = -(1.0 * gen_lambda / sampled_cnt) * tf.reduce_sum(
        tf.multiply(sampled_generator_out_non_zero, y_generated))

    # optimizer : AdamOptimizer
    optimizer = tf.train.AdamOptimizer(LEARNING_RATE)

    # discriminator and generator loss
    d_trainer = optimizer.minimize(d_loss, var_list=d_params)
    g_trainer = optimizer.minimize(g_loss, var_list=g_params)

    init = tf.global_variables_initializer()

    saver = tf.train.Saver()

    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.333,
                                allow_growth=True)
    sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))

    sess.run(init)

    curr_gen_lamda = GANLAMBDA

    update_count = 0.0

    for i in range(NUM_EPOCH):

        batch_total_sampled_tags = []
        batch_curr_x_generated = []
        batch_curr_x_popular_g = []
        batch_curr_x_popular_n = []
        batch_curr_x_niche = []
        batch_X = []
        batch_total_sampled_cnt = []

        # train for each epoch
        user_err_cnt = 0
        for bnum, st_idx in enumerate(range(0, N, BATCH_SIZE)):
            end_idx = min(st_idx + BATCH_SIZE, N)
            X = train_data[idxlist[st_idx:end_idx]]

            if sparse.isspmatrix(X):
                X = X.toarray()
            X = X.astype('float32')

            curr_generator_out = sess.run(
                generator_out, feed_dict={generator_network.input_ph: X})

            curr_x_popular_n = []
            curr_x_niche = []

            curr_x_popular_g = []
            curr_x_generated = []

            total_sampled_cnt = 0
            total_sampled_tags = []

            for ii, user_idx in enumerate(idxlist[st_idx:end_idx]):
                if user_idx + uid_start_idx not in user_popular_data or user_idx + uid_start_idx not in user_niche_data:
                    # Invalid User: user_idx + uid_start_idx
                    user_err_cnt += 1
                    total_sampled_tags.append([0] * n_items)
                    continue

                curr_pop_vectors = user_popular_data[user_idx + uid_start_idx]
                curr_niche_vectors = user_niche_data[user_idx + uid_start_idx]

                curr_x_niche += user_x_niche_vectors[user_idx + uid_start_idx]
                curr_x_popular_n += user_x_popular_n_vectors[user_idx +
                                                             uid_start_idx]

                curr_sampled_tags_bin, curr_sampled_tags = sample_from_generator_new(
                    USER_TAGS_TO_SAMPLE[user_idx + uid_start_idx],
                    np.asarray(curr_generator_out)[
                        ii, USER_TAGS_TO_SAMPLE[user_idx + uid_start_idx]],
                    len(curr_niche_vectors), n_items)

                curr_cnt = 0
                curr_sampled_tags.sort()

                for generated_tag_idx in curr_sampled_tags:

                    max_coeff = -1.0

                    max_pop_tag_idx = np.random.choice(
                        range(len(curr_pop_vectors)))

                    max_pop_tag_idx = curr_pop_vectors[max_pop_tag_idx]

                    if generated_tag_idx not in ITEM_FEATURE_DICT or max_pop_tag_idx not in ITEM_FEATURE_DICT:
                        # Invalid Generated Tag Pair: generated_tag_idx, max_pop_tag_idx
                        curr_sampled_tags_bin[generated_tag_idx] = 0
                        continue

                    curr_x_generated.append(generated_tag_idx)
                    curr_x_popular_g.append(max_pop_tag_idx)

                    curr_cnt += 1

                total_sampled_tags.append(curr_sampled_tags_bin)
                total_sampled_cnt += curr_cnt

            if curr_x_generated == []:
                continue

            total_sampled_tags = np.asarray(total_sampled_tags)
            curr_x_generated = np.asarray(curr_x_generated)
            curr_x_popular_g = np.asarray(curr_x_popular_g)
            curr_x_popular_n = np.asarray(curr_x_popular_n)
            curr_x_niche = np.asarray(curr_x_niche)

            batch_total_sampled_tags.append(total_sampled_tags)
            batch_curr_x_generated.append(curr_x_generated)
            batch_curr_x_popular_g.append(curr_x_popular_g)
            batch_curr_x_popular_n.append(curr_x_popular_n)
            batch_curr_x_niche.append(curr_x_niche)
            batch_X.append(X)
            batch_total_sampled_cnt.append(total_sampled_cnt)

        batch_total_sampled_tags = np.asarray(batch_total_sampled_tags)
        batch_curr_x_generated = np.asarray(batch_curr_x_generated)
        batch_curr_x_popular_g = np.asarray(batch_curr_x_popular_g)
        batch_curr_x_popular_n = np.asarray(batch_curr_x_popular_n)
        batch_curr_x_niche = np.asarray(batch_curr_x_niche)
        batch_X = np.asarray(batch_X)
        batch_total_sampled_cnt = np.asarray(batch_total_sampled_cnt)

        print("global-epoch:", i, "Data Creation Finished", "user_err_cnt:",
              user_err_cnt)

        # print(batch_total_sampled_cnt.tolist())

        indices = np.arange(batch_total_sampled_tags.shape[0])
        np.random.shuffle(indices)

        for j_disc in range(NUM_SUB_EPOCHS):

            for disc_batch_idx in indices:

                X = batch_X[disc_batch_idx]
                curr_x_popular_id_n = batch_curr_x_popular_n[disc_batch_idx]
                curr_x_popular_id_g = batch_curr_x_popular_g[disc_batch_idx]
                curr_x_niche_id = batch_curr_x_niche[disc_batch_idx]
                curr_x_generated_id = batch_curr_x_generated[disc_batch_idx]
                total_sampled_tags = batch_total_sampled_tags[disc_batch_idx]
                total_sampled_cnt = batch_total_sampled_cnt[disc_batch_idx]

                _, curr_d_loss = sess.run(
                    [d_trainer, d_loss_mean],
                    feed_dict={
                        generator_network.input_ph: X,
                        x_popular_n_id: curr_x_popular_id_n,
                        x_popular_g_id: curr_x_popular_id_g,
                        x_niche_id: curr_x_niche_id,
                        x_generated_id: curr_x_generated_id,
                        generated_tags: total_sampled_tags,
                        sampled_cnt: total_sampled_cnt,
                        keep_prob: np.sum(0.7).astype(np.float32),
                        item_feature_arr: ITEM_FEATURE_ARR
                    })

            print("global-epoch:%s, discr-epoch:%s, d_loss:%.5f" %
                  (i, j_disc, curr_d_loss))

        print('')

        for j_gen in range(NUM_SUB_EPOCHS):

            for gen_batch_idx in indices:
                X = batch_X[gen_batch_idx]
                curr_x_popular_id_n = batch_curr_x_popular_n[gen_batch_idx]
                curr_x_popular_id_g = batch_curr_x_popular_g[gen_batch_idx]
                curr_x_niche_id = batch_curr_x_niche[gen_batch_idx]
                curr_x_generated_id = batch_curr_x_generated[gen_batch_idx]
                total_sampled_tags = batch_total_sampled_tags[gen_batch_idx]
                total_sampled_cnt = batch_total_sampled_cnt[gen_batch_idx]

                if total_anneal_steps > 0:
                    anneal = min(anneal_cap,
                                 1. * ((update_count) / total_anneal_steps))
                else:
                    anneal = anneal_cap

                update_count += 1

                _, curr_g_loss, curr_g_loss_term_1, curr_g_loss_term_2 = sess.run(
                    [g_trainer, g_loss_mean, g_vae_loss, gan_loss],
                    feed_dict={
                        generator_network.input_ph: X,
                        x_popular_n_id: curr_x_popular_id_n,
                        x_popular_g_id: curr_x_popular_id_g,
                        x_niche_id: curr_x_niche_id,
                        x_generated_id: curr_x_generated_id,
                        generated_tags: total_sampled_tags,
                        sampled_cnt: total_sampled_cnt,
                        generator_network.keep_prob_ph: 0.75,
                        generator_network.is_training_ph: 1,
                        generator_network.anneal_ph: anneal,
                        gen_lambda: curr_gen_lamda,
                        keep_prob: np.sum(0.7).astype(np.float32)
                    })

            print(
                "global-epoch:%s, generator-epoch:%s, g_loss:%.5f (vae_loss: %.5f + gan_loss: %.5f, anneal: %.5f)"
                % (i, j_gen, curr_g_loss, curr_g_loss_term_1,
                   curr_g_loss_term_2, anneal))

        print('')

        X_vad = vad_data_tr[idxlist_vad[0:N_vad]]

        if sparse.isspmatrix(X_vad):
            X_vad = X_vad.toarray()
        X_vad = X_vad.astype('float32')

        pred_vad = sess.run(generator_out,
                            feed_dict={generator_network.input_ph: X_vad})
        # exclude examples from training and validation (if any)
        pred_vad[X_vad.nonzero()] = -np.inf
        ndcg_vad = NDCG_binary_at_k_batch(pred_vad,
                                          vad_data_te[idxlist_vad[0:N_vad]])

        recall_at_20, not_found_20 = Recall_at_k_batch(
            pred_vad, vad_data_te[idxlist_vad[0:N_vad]], k=20)

        recall_at_50, not_found_50 = Recall_at_k_batch(
            pred_vad, vad_data_te[idxlist_vad[0:N_vad]], k=50)

        print('global-epoch:', i, 'gen-epoch:', j_gen, 'Vad: NDCG:',
              np.mean(ndcg_vad), 'Recall@20:', np.mean(recall_at_20),
              'Recall@50:', np.mean(recall_at_50), 'Num_users:', len(ndcg_vad),
              len(recall_at_20), len(recall_at_50))

        print('')

        saver.save(sess, os.path.join(output_path, "model_" + str(i)))

        print('Model saved at global-epoch', i)
Example #40
0
def run_mcl(matrix,
            expansion=2,
            inflation=2,
            loop_value=1,
            iterations=100,
            pruning_threshold=0.001,
            pruning_frequency=1,
            convergence_check_frequency=1,
            verbose=False):
    """
    Perform MCL on the given similarity matrix
    
    :param matrix: The similarity matrix to cluster
    :param expansion: The cluster expansion factor
    :param inflation: The cluster inflation factor
    :param loop_value: Initialization value for self-loops
    :param iterations: Maximum number of iterations
           (actual number of iterations will be less if convergence is reached)
    :param pruning_threshold: Threshold below which matrix elements will be set
           set to 0
    :param pruning_frequency: Perform pruning every 'pruning_frequency'
           iterations. 
    :param convergence_check_frequency: Perform the check for convergence
           every convergence_check_frequency iterations
    :param verbose: Print extra information to the console
    :returns: The final matrix
    """
    assert expansion > 1, "Invalid expansion parameter"
    assert inflation > 1, "Invalid inflation parameter"
    assert loop_value >= 0, "Invalid loop_value"
    assert iterations > 0, "Invalid number of iterations"
    assert pruning_threshold >= 0, "Invalid pruning_threshold"
    assert pruning_frequency > 0, "Invalid pruning_frequency"
    assert convergence_check_frequency > 0, "Invalid convergence_check_frequency"

    printer = MessagePrinter(verbose)

    printer.print("-" * 50)
    printer.print("MCL Parameters")
    printer.print("Expansion: {}".format(expansion))
    printer.print("Inflation: {}".format(inflation))
    if pruning_threshold > 0:
        printer.print(
            "Pruning threshold: {}, frequency: {} iteration{}".format(
                pruning_threshold, pruning_frequency,
                "s" if pruning_frequency > 1 else ""))
    else:
        printer.print("No pruning")
    printer.print("Convergence check: {} iteration{}".format(
        convergence_check_frequency,
        "s" if convergence_check_frequency > 1 else ""))
    printer.print("Maximum iterations: {}".format(iterations))
    printer.print(
        "{} matrix mode".format("Sparse" if isspmatrix(matrix) else "Dense"))
    printer.print("-" * 50)

    # Initialize self-loops
    if loop_value > 0:
        matrix = add_self_loops(matrix, loop_value)

    # Normalize
    matrix = normalize(matrix)

    # iterations
    for i in range(iterations):
        printer.print("Iteration {}".format(i + 1))

        # store current matrix for convergence checking
        last_mat = matrix.copy()

        # perform MCL expansion and inflation
        matrix = iterate(matrix, expansion, inflation)

        # prune
        if pruning_threshold > 0 and i % pruning_frequency == pruning_frequency - 1:
            printer.print("Pruning")
            matrix = prune(matrix, pruning_threshold)

        # Check for convergence
        if i % convergence_check_frequency == convergence_check_frequency - 1:
            printer.print("Checking for convergence")
            if converged(matrix, last_mat):
                printer.print("Converged after {} iteration{}".format(
                    i + 1, "s" if i > 0 else ""))
                break

    printer.print("-" * 50)

    return matrix
Example #41
0
    def add_sparse_convolution(self,
                               name,
                               sp_pattern,
                               use_sp_data=True,
                               kernelsize=1,
                               stride=1,
                               pad=0,
                               biasterm=False,
                               weight_filler={},
                               bias_filler={},
                               blobs_lr=[1.0, 2.0],
                               weight_decays=[1.0, 1.0],
                               weights=None):
        from scipy import sparse
        assert sparse.isspmatrix(sp_pattern)
        num_output = sp_pattern.shape[0]  # sparsity pattern is given in input

        cp = gpudm.ConvolutionParameter()
        cp.set_kernel_h(kernelsize)
        cp.set_kernel_w(kernelsize)
        cp.set_num_output(num_output)
        assert stride > 0, 'error: stride is 0 for ' + name
        cp.set_stride_h(stride)
        cp.set_stride_w(stride)
        cp.set_pad_h(pad)
        cp.set_pad_w(pad)
        cp.set_bias_term(biasterm)
        self.set_filler_params(cp.mutable_weight_filler(), weight_filler)
        self.set_filler_params(cp.mutable_bias_filler(), bias_filler)
        lp = gpudm.LayerParameter()
        lp.set_allocated_convolution_param(cp)
        cp.this.disown()  # otherwise it will be freed 2 times

        def arrToBlob(arr):  # dirty function but simpler for now
            bb = gpudm.BlobFloat(1, 1, 1, arr.size)
            bb.mutable_to_numpy_ref().view(arr.dtype)[:] = arr.ravel()
            return bb

        if sparse.isspmatrix_csr(sp_pattern):
            sparsity_args = (sp_pattern.nnz, arrToBlob(sp_pattern.indptr),
                             arrToBlob(sp_pattern.indices),
                             arrToBlob(sp_pattern.data)
                             if use_sp_data else None)
            self.add_layer(name, gpudm.CSR_SparseConvolutionLayerFloat, lp)
        elif sparse.isspmatrix_bsr(sp_pattern):
            br, bc = sp_pattern.blocksize
            assert br == bc, "error: not implemented for non-square blocks"
            sparsity_args = (sp_pattern.nnz / (br * bc), br,
                             arrToBlob(sp_pattern.indptr),
                             arrToBlob(sp_pattern.indices),
                             arrToBlob(sp_pattern.data)
                             if use_sp_data else None)
            self.add_layer(name, gpudm.BSR_SparseConvolutionLayerFloat, lp)
        else:
            assert False, "This sparse matrix type is not implemented"

        # define sparsity pattern now
        self.layers[-1][1].SetSparsityPattern(*sparsity_args)

        self.layers[-1][1].blobs_lr = blobs_lr
        self.layers[-1][1].weight_decays = weight_decays
        if weights: self.set_parameters({name: weights}, verbose=0)
Example #42
0
def energy_based_strength_of_connection(A, theta=0.0, k=2):
    """
    Compute a strength of connection matrix using an energy-based measure.

    Parameters
    ----------
    A : {sparse-matrix}
        matrix from which to generate strength of connection information
    theta : {float}
        Threshold parameter in [0,1]
    k : {int}
        Number of relaxation steps used to generate strength information

    Returns
    -------
    S : {csr_matrix}
        Matrix graph defining strong connections.  The sparsity pattern
        of S matches that of A.  For BSR matrices, S is a reduced strength
        of connection matrix that describes connections between supernodes.

    Notes
    -----
    This method relaxes with weighted-Jacobi in order to approximate the
    matrix inverse.  A normalized change of energy is then used to define
    point-wise strength of connection values.  Specifically, let v be the
    approximation to the i-th column of the inverse, then

    (S_ij)^2 = <v_j, v_j>_A / <v, v>_A,

    where v_j = v, such that entry j in v has been zeroed out.  As is common,
    larger values imply a stronger connection.

    Current implementation is a very slow pure-python implementation for
    experimental purposes, only.

    References
    ----------
    .. [1] Brannick, Brezina, MacLachlan, Manteuffel, McCormick.
       "An Energy-Based AMG Coarsening Strategy",
       Numerical Linear Algebra with Applications,
       vol. 13, pp. 133-148, 2006.

    Examples
    --------
    >>> import numpy as np
    >>> from pyamg.gallery import stencil_grid
    >>> from pyamg.strength import energy_based_strength_of_connection
    >>> n=3
    >>> stencil =  np.array([[-1.0,-1.0,-1.0],
    ...                        [-1.0, 8.0,-1.0],
    ...                        [-1.0,-1.0,-1.0]])
    >>> A = stencil_grid(stencil, (n,n), format='csr')
    >>> S = energy_based_strength_of_connection(A, 0.0)
    """

    if (theta < 0):
        raise ValueError('expected a positive theta')
    if not sparse.isspmatrix(A):
        raise ValueError('expected sparse matrix')
    if (k < 0):
        raise ValueError('expected positive number of steps')
    if not isinstance(k, int):
        raise ValueError('expected integer')

    if sparse.isspmatrix_bsr(A):
        bsr_flag = True
        numPDEs = A.blocksize[0]
        if A.blocksize[0] != A.blocksize[1]:
            raise ValueError('expected square blocks in BSR matrix A')
    else:
        bsr_flag = False

    # Convert A to csc and Atilde to csr
    if sparse.isspmatrix_csr(A):
        Atilde = A.copy()
        A = A.tocsc()
    else:
        A = A.tocsc()
        Atilde = A.copy()
        Atilde = Atilde.tocsr()

    # Calculate the weighted-Jacobi parameter
    from pyamg.util.linalg import approximate_spectral_radius
    D = A.diagonal()
    Dinv = 1.0 / D
    Dinv[D == 0] = 0.0
    Dinv = sparse.csc_matrix(
        (Dinv, (np.arange(A.shape[0]), np.arange(A.shape[1]))), shape=A.shape)
    DinvA = Dinv * A
    omega = 1.0 / approximate_spectral_radius(DinvA)
    del DinvA

    # Approximate A-inverse with k steps of w-Jacobi and a zero initial guess
    S = sparse.csc_matrix(A.shape, dtype=A.dtype)  # empty matrix
    I = sparse.eye(A.shape[0], A.shape[1], format='csc')
    for i in range(k + 1):
        S = S + omega * (Dinv * (I - A * S))

    # Calculate the strength entries in S column-wise, but only strength
    # values at the sparsity pattern of A
    for i in range(Atilde.shape[0]):
        v = np.mat(S[:, i].todense())
        Av = np.mat(A * v)
        denom = np.sqrt(np.conjugate(v).T * Av)
        # replace entries in row i with strength values
        for j in range(Atilde.indptr[i], Atilde.indptr[i + 1]):
            col = Atilde.indices[j]
            vj = v[col].copy()
            v[col] = 0.0
            #   =  (||v_j||_A - ||v||_A) / ||v||_A
            val = np.sqrt(np.conjugate(v).T * A * v) / denom - 1.0

            # Negative values generally imply a weak connection
            if val > -0.01:
                Atilde.data[j] = abs(val)
            else:
                Atilde.data[j] = 0.0

            v[col] = vj

    # Apply drop tolerance
    Atilde = classical_strength_of_connection(Atilde, theta=theta)
    Atilde.eliminate_zeros()

    # Put ones on the diagonal
    Atilde = Atilde + I.tocsr()
    Atilde.sort_indices()

    # Amalgamate Atilde for the BSR case, using ones for all strong connections
    if bsr_flag:
        Atilde = Atilde.tobsr(blocksize=(numPDEs, numPDEs))
        nblocks = Atilde.indices.shape[0]
        uone = np.ones((nblocks, ))
        Atilde = sparse.csr_matrix((uone, Atilde.indices, Atilde.indptr),
                                   shape=(int(Atilde.shape[0] / numPDEs),
                                          int(Atilde.shape[1] / numPDEs)))

    # Scale C by the largest magnitude entry in each row
    Atilde = scale_rows_by_largest_entry(Atilde)

    return Atilde
def laplacian(csgraph, normed=False, return_diag=False, use_out_degree=False):
    """
    Return the Laplacian matrix of a directed graph.

    Parameters
    ----------
    csgraph : array_like or sparse matrix, 2 dimensions
        compressed-sparse graph, with shape (N, N).
    normed : bool, optional
        If True, then compute normalized Laplacian.
    return_diag : bool, optional
        If True, then also return an array related to vertex degrees.
    use_out_degree : bool, optional
        If True, then use out-degree instead of in-degree.
        This distinction matters only if the graph is asymmetric.
        Default: False.

    Returns
    -------
    lap : ndarray or sparse matrix
        The N x N laplacian matrix of csgraph. It will be a numpy array (dense)
        if the input was dense, or a sparse matrix otherwise.
    diag : ndarray, optional
        The length-N diagonal of the Laplacian matrix.
        For the normalized Laplacian, this is the array of square roots
        of vertex degrees or 1 if the degree is zero.

    Notes
    -----
    The Laplacian matrix of a graph is sometimes referred to as the
    "Kirchoff matrix" or the "admittance matrix", and is useful in many
    parts of spectral graph theory.  In particular, the eigen-decomposition
    of the laplacian matrix can give insight into many properties of the graph.

    Examples
    --------
    >>> from scipy.sparse import csgraph
    >>> G = np.arange(5) * np.arange(5)[:, np.newaxis]
    >>> G
    array([[ 0,  0,  0,  0,  0],
           [ 0,  1,  2,  3,  4],
           [ 0,  2,  4,  6,  8],
           [ 0,  3,  6,  9, 12],
           [ 0,  4,  8, 12, 16]])
    >>> csgraph.laplacian(G, normed=False)
    array([[  0,   0,   0,   0,   0],
           [  0,   9,  -2,  -3,  -4],
           [  0,  -2,  16,  -6,  -8],
           [  0,  -3,  -6,  21, -12],
           [  0,  -4,  -8, -12,  24]])
    """
    if csgraph.ndim != 2 or csgraph.shape[0] != csgraph.shape[1]:
        raise ValueError('csgraph must be a square matrix or array')

    if normed and (np.issubdtype(csgraph.dtype, np.signedinteger)
                   or np.issubdtype(csgraph.dtype, np.uint)):
        csgraph = csgraph.astype(float)

    create_lap = _laplacian_sparse if isspmatrix(csgraph) else _laplacian_dense
    degree_axis = 1 if use_out_degree else 0
    lap, d = create_lap(csgraph, normed=normed, axis=degree_axis)
    if return_diag:
        return lap, d
    return lap
Example #44
0
def cgnr(A, b, x0=None, tol=1e-5, maxiter=None, xtype=None, M=None,
         callback=None, residuals=None):
    """Conjugate Gradient, Normal Residual algorithm.

    Applies CG to the normal equations, A.H A x = b. Left preconditioning
    is supported.  Note that unless A is well-conditioned, the use of
    CGNR is inadvisable

    Parameters
    ----------
    A : array, matrix, sparse matrix, LinearOperator
        n x n, linear system to solve
    b : array, matrix
        right hand side, shape is (n,) or (n,1)
    x0 : array, matrix
        initial guess, default is a vector of zeros
    tol : float
        relative convergence tolerance, i.e. tol is scaled by ||r_0||_2
    maxiter : int
        maximum number of allowed iterations
    xtype : type
        dtype for the solution, default is automatic type detection
    M : array, matrix, sparse matrix, LinearOperator
        n x n, inverted preconditioner, i.e. solve M A.H A x = b.
    callback : function
        User-supplied function is called after each iteration as
        callback(xk), where xk is the current solution vector
    residuals : list
        residuals has the residual norm history,
        including the initial residual, appended to it

    Returns
    -------
    (xNew, info)
    xNew : an updated guess to the solution of Ax = b
    info : halting status of cgnr

            ==  =======================================
            0   successful exit
            >0  convergence to tolerance not achieved,
                return iteration count instead.
            <0  numerical breakdown, or illegal input
            ==  =======================================


    Notes
    -----
    The LinearOperator class is in scipy.sparse.linalg.interface.
    Use this class if you prefer to define A or M as a mat-vec routine
    as opposed to explicitly constructing the matrix.  A.psolve(..) is
    still supported as a legacy.

    Examples
    --------
    >>> from pyamg.krylov.cgnr import cgnr
    >>> from pyamg.util.linalg import norm
    >>> import numpy as np
    >>> from pyamg.gallery import poisson
    >>> A = poisson((10,10))
    >>> b = np.ones((A.shape[0],))
    >>> (x,flag) = cgnr(A,b, maxiter=2, tol=1e-8)
    >>> print norm(b - A*x)
    9.3910201849

    References
    ----------
    .. [1] Yousef Saad, "Iterative Methods for Sparse Linear Systems,
       Second Edition", SIAM, pp. 276-7, 2003
       http://www-users.cs.umn.edu/~saad/books.html

    """
    # Store the conjugate transpose explicitly as it will be used much later on
    if isspmatrix(A):
        AH = A.H
    else:
        # TODO avoid doing this since A may be a different sparse type
        AH = aslinearoperator(np.asarray(A).conj().T)

    # Convert inputs to linear system, with error checking
    A, M, x, b, postprocess = make_system(A, M, x0, b)
    dimen = A.shape[0]

    # Ensure that warnings are always reissued from this function
    import warnings
    warnings.filterwarnings('always', module='pyamg\.krylov\._cgnr')

    # Choose type
    if not hasattr(A, 'dtype'):
        Atype = upcast(x.dtype, b.dtype)
    else:
        Atype = A.dtype
    if not hasattr(M, 'dtype'):
        Mtype = upcast(x.dtype, b.dtype)
    else:
        Mtype = M.dtype
    xtype = upcast(Atype, x.dtype, b.dtype, Mtype)

    # Should norm(r) be kept
    if residuals == []:
        keep_r = True
    else:
        keep_r = False

    # How often should r be recomputed
    recompute_r = 8

    # Check iteration numbers. CGNR suffers from loss of orthogonality quite
    # easily, so we arbitrarily let the method go up to 130% over the
    # theoretically necessary limit of maxiter=dimen
    if maxiter is None:
        maxiter = int(np.ceil(1.3*dimen)) + 2
    elif maxiter < 1:
        raise ValueError('Number of iterations must be positive')
    elif maxiter > (1.3*dimen):
        warn('maximum allowed inner iterations (maxiter) are the 130% times \
              the number of dofs')
        maxiter = int(np.ceil(1.3*dimen)) + 2

    # Prep for method
    r = b - A*x
    rhat = AH*r
    normr = norm(r)
    if keep_r:
        residuals.append(normr)

    # Check initial guess ( scaling by b, if b != 0,
    #   must account for case when norm(b) is very small)
    normb = norm(b)
    if normb == 0.0:
        normb = 1.0
    if normr < tol*normb:
        if callback is not None:
            callback(x)
        return (postprocess(x), 0)

    # Scale tol by ||r_0||_2
    if normr != 0.0:
        tol = tol*normr

    # Begin CGNR

    # Apply preconditioner and calculate initial search direction
    z = M*rhat
    p = z.copy()
    old_zr = np.inner(z.conjugate(), rhat)

    for iter in range(maxiter):

        # w_j = A p_j
        w = A*p

        # alpha = (z_j, rhat_j) / (w_j, w_j)
        alpha = old_zr / np.inner(w.conjugate(), w)

        # x_{j+1} = x_j + alpha*p_j
        x += alpha*p

        # r_{j+1} = r_j - alpha*w_j
        if np.mod(iter, recompute_r) and iter > 0:
            r -= alpha*w
        else:
            r = b - A*x

        # rhat_{j+1} = A.H*r_{j+1}
        rhat = AH*r

        # z_{j+1} = M*r_{j+1}
        z = M*rhat

        # beta = (z_{j+1}, rhat_{j+1}) / (z_j, rhat_j)
        new_zr = np.inner(z.conjugate(), rhat)
        beta = new_zr / old_zr
        old_zr = new_zr

        # p_{j+1} = A.H*z_{j+1} + beta*p_j
        p *= beta
        p += z

        # Allow user access to residual
        if callback is not None:
            callback(x)

        # test for convergence
        normr = norm(r)
        if keep_r:
            residuals.append(normr)
        if normr < tol:
            return (postprocess(x), 0)

    # end loop

    return (postprocess(x), iter+1)
Example #45
0
 def wrapper(*args, **kwargs):
     if 'accept_sparse' in kwargs and not sparse.isspmatrix(args[0]):
         raise TypeError('A dense matrix was passed in, but sparse'
                         'data is required.')
     result = func(*args, **kwargs)
     return result
Example #46
0
    def fit(self, X, y):
        """Fit the model according to the given training data.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape = [n_samples, n_features]
            Training vector, where n_samples in the number of samples and
            n_features is the number of features.

        y : array-like, shape = [n_samples]
            Target vector relative to X

        class_weight : {dict, 'auto'}, optional
            Weights associated with classes. If not given, all classes
            are supposed to have weight one.

        Returns
        -------
        self : object
            Returns self.
        """
        self._enc = LabelEncoder()
        y = self._enc.fit_transform(y)
        if len(self.classes_) < 2:
            raise ValueError("The number of classes has to be greater than"
                             " one.")

        X = atleast2d_or_csr(X, dtype=np.float64, order="C")

        self.class_weight_ = compute_class_weight(self.class_weight,
                                                  self.classes_, y)

        if X.shape[0] != y.shape[0]:
            raise ValueError("X and y have incompatible shapes.\n"
                             "X has %s samples, but y has %s." %
                             (X.shape[0], y.shape[0]))

        liblinear.set_verbosity_wrap(self.verbose)

        if sp.isspmatrix(X):
            train = liblinear.csr_train_wrap
        else:
            train = liblinear.train_wrap

        rnd = check_random_state(self.random_state)
        if self.verbose:
            print('[LibLinear]', end='')

        # LibLinear wants targets as doubles, even for classification
        y = np.asarray(y, dtype=np.float64).ravel()
        self.raw_coef_ = train(
            X,
            y,
            self._get_solver_type(),
            self.tol,
            self._get_bias(),
            self.C,
            self.class_weight_,
            # seed for srand in range [0..INT_MAX);
            # due to limitations in Numpy on 32-bit
            # platforms, we can't get to the UINT_MAX
            # limit that srand supports
            rnd.randint(np.iinfo('i').max))

        if self.fit_intercept:
            self.coef_ = self.raw_coef_[:, :-1]
            self.intercept_ = self.intercept_scaling * self.raw_coef_[:, -1]
        else:
            self.coef_ = self.raw_coef_
            self.intercept_ = 0.

        return self
Example #47
0
    def fit(self, X, y):
        """Fit a semi-supervised label propagation model based

        All the input area_data is provided matrix X (labeled and unlabeled)
        and corresponding label matrix y with a dedicated marker value for
        unlabeled samples.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            A matrix of shape (n_samples, n_samples) will be created from this.

        y : array-like of shape (n_samples,)
            `n_labeled_samples` (unlabeled points are marked as -1)
            All unlabeled samples will be transductively assigned labels.

        Returns
        -------
        self : object
        """
        X, y = self._validate_data(X, y)
        self.X_ = X
        check_classification_targets(y)

        # actual graph construction (implementations should override this)
        graph_matrix = self._build_graph()

        # label construction
        # construct a categorical distribution for classification only
        classes = np.unique(y)
        classes = (classes[classes != -1])
        self.classes_ = classes

        n_samples, n_classes = len(y), len(classes)

        alpha = self.alpha
        if self._variant == 'spreading' and \
                (alpha is None or alpha <= 0.0 or alpha >= 1.0):
            raise ValueError('alpha=%s is invalid: it must be inside '
                             'the open interval (0, 1)' % alpha)
        y = np.asarray(y)
        unlabeled = y == -1

        # initialize distributions
        self.label_distributions_ = np.zeros((n_samples, n_classes))
        for label in classes:
            self.label_distributions_[y == label, classes == label] = 1

        y_static = np.copy(self.label_distributions_)
        if self._variant == 'propagation':
            # LabelPropagation
            y_static[unlabeled] = 0
        else:
            # LabelSpreading
            y_static *= 1 - alpha

        l_previous = np.zeros((self.X_.shape[0], n_classes))

        unlabeled = unlabeled[:, np.newaxis]
        if sparse.isspmatrix(graph_matrix):
            graph_matrix = graph_matrix.tocsr()

        for self.n_iter_ in range(self.max_iter):
            if np.abs(self.label_distributions_ - l_previous).sum() < self.tol:
                break

            l_previous = self.label_distributions_
            self.label_distributions_ = safe_sparse_dot(
                graph_matrix, self.label_distributions_)

            if self._variant == 'propagation':
                normalizer = np.sum(self.label_distributions_,
                                    axis=1)[:, np.newaxis]
                self.label_distributions_ /= normalizer
                self.label_distributions_ = np.where(unlabeled,
                                                     self.label_distributions_,
                                                     y_static)
            else:
                # clamp
                self.label_distributions_ = np.multiply(
                    alpha, self.label_distributions_) + y_static
        else:
            warnings.warn('max_iter=%d was reached without convergence.' %
                          self.max_iter,
                          category=ConvergenceWarning)
            self.n_iter_ += 1

        normalizer = np.sum(self.label_distributions_, axis=1)[:, np.newaxis]
        normalizer[normalizer == 0] = 1
        self.label_distributions_ /= normalizer

        # set the transduction item
        transduction = self.classes_[np.argmax(self.label_distributions_,
                                               axis=1)]
        self.transduction_ = transduction.ravel()
        return self
Example #48
0
def evaluate(n_users, n_items, train_data, vad_data_tr, vad_data_te,
             test_data_tr, test_data_te):
    # layer node num
    p_dims = [200, 600, n_items]

    tf.reset_default_graph()
    vae = model.MultiVAE(p_dims, lam=0.0)
    saver, logits_var, _, _, _ = vae.build_graph()

    arch_str = "I-%s-I" % ('-'.join([str(d) for d in vae.dims[1:-1]]))
    chkpt_dir = './chkpt/VAE_anneal{}K_cap{:1.1E}/{}'.format(
        total_anneal_steps / 1000, anneal_cap, arch_str)

    N_test = test_data_tr.shape[0]
    idxlist_test = range(N_test)

    # validation batch size (since the entire validation set might not fit into GPU memory)
    batch_size_test = 2000

    print("chkpt directory: %s" % chkpt_dir)

    n100_list, r20_list, r50_list = [], [], []

    with tf.Session() as sess:
        saver.restore(sess, '{}/model'.format(chkpt_dir))

        for bnum, st_idx in enumerate(range(0, N_test, batch_size_test)):
            end_idx = min(st_idx + batch_size_test, N_test)
            X = test_data_tr[idxlist_test[st_idx:end_idx]]

            if sparse.isspmatrix(X):
                X = X.toarray()
            X = X.astype('float32')

            pred_val = sess.run(logits_var,
                                feed_dict={
                                    vae.input_ph: X,
                                    vae.is_training_ph: 0
                                })
            # exclude examples from training and validation (if any)
            pred_val[X.nonzero()] = -np.inf
            n100_list.append(
                metric.NDCG_binary_at_k_batch(
                    pred_val,
                    test_data_te[idxlist_test[st_idx:end_idx]],
                    k=100))
            r20_list.append(
                metric.Recall_at_k_batch(
                    pred_val, test_data_te[idxlist_test[st_idx:end_idx]],
                    k=20))
            r50_list.append(
                metric.Recall_at_k_batch(
                    pred_val, test_data_te[idxlist_test[st_idx:end_idx]],
                    k=50))

    n100_list = np.concatenate(n100_list)
    r20_list = np.concatenate(r20_list)
    r50_list = np.concatenate(r50_list)

    print("Test NDCG@100=%.5f (%.5f)" %
          (np.mean(n100_list), np.std(n100_list) / np.sqrt(len(n100_list))))
    print("Test Recall@20=%.5f (%.5f)" %
          (np.mean(r20_list), np.std(r20_list) / np.sqrt(len(r20_list))))
    print("Test Recall@50=%.5f (%.5f)" %
          (np.mean(r50_list), np.std(r50_list) / np.sqrt(len(r50_list))))
Example #49
0
    def transform(self, X):
        """Transform data to polynomial features.

        Parameters
        ----------
        X : {array-like, sparse matrix} of shape (n_samples, n_features)
            The data to transform, row by row.

            Prefer CSR over CSC for sparse input (for speed), but CSC is
            required if the degree is 4 or higher. If the degree is less than
            4 and the input format is CSC, it will be converted to CSR, have
            its polynomial features generated, then converted back to CSC.

            If the degree is 2 or 3, the method described in "Leveraging
            Sparsity to Speed Up Polynomial Feature Expansions of CSR Matrices
            Using K-Simplex Numbers" by Andrew Nystrom and John Hughes is
            used, which is much faster than the method used on CSC input. For
            this reason, a CSC input will be converted to CSR, and the output
            will be converted back to CSC prior to being returned, hence the
            preference of CSR.

        Returns
        -------
        XP : {ndarray, sparse matrix} of shape (n_samples, NP)
            The matrix of features, where NP is the number of polynomial
            features generated from the combination of inputs. If a sparse
            matrix is provided, it will be converted into a sparse
            ``csr_matrix``.
        """
        check_is_fitted(self)

        X = self._validate_data(X,
                                order="F",
                                dtype=FLOAT_DTYPES,
                                reset=False,
                                accept_sparse=("csr", "csc"))

        n_samples, n_features = X.shape

        if sparse.isspmatrix_csr(X):
            if self.degree > 3:
                return self.transform(X.tocsc()).tocsr()
            to_stack = []
            if self.include_bias:
                to_stack.append(np.ones(shape=(n_samples, 1), dtype=X.dtype))
            to_stack.append(X)
            for deg in range(2, self.degree + 1):
                Xp_next = _csr_polynomial_expansion(X.data, X.indices,
                                                    X.indptr, X.shape[1],
                                                    self.interaction_only, deg)
                if Xp_next is None:
                    break
                to_stack.append(Xp_next)
            XP = sparse.hstack(to_stack, format="csr")
        elif sparse.isspmatrix_csc(X) and self.degree < 4:
            return self.transform(X.tocsr()).tocsc()
        else:
            if sparse.isspmatrix(X):
                combinations = self._combinations(n_features, self.degree,
                                                  self.interaction_only,
                                                  self.include_bias)
                columns = []
                for combination in combinations:
                    if combination:
                        out_col = 1
                        for col_idx in combination:
                            out_col = X[:, col_idx].multiply(out_col)
                        columns.append(out_col)
                    else:
                        bias = sparse.csc_matrix(np.ones((X.shape[0], 1)))
                        columns.append(bias)
                XP = sparse.hstack(columns, dtype=X.dtype).tocsc()
            else:
                XP = np.empty(
                    (n_samples, self.n_output_features_),
                    dtype=X.dtype,
                    order=self.order,
                )

                # What follows is a faster implementation of:
                # for i, comb in enumerate(combinations):
                #     XP[:, i] = X[:, comb].prod(1)
                # This implementation uses two optimisations.
                # First one is broadcasting,
                # multiply ([X1, ..., Xn], X1) -> [X1 X1, ..., Xn X1]
                # multiply ([X2, ..., Xn], X2) -> [X2 X2, ..., Xn X2]
                # ...
                # multiply ([X[:, start:end], X[:, start]) -> ...
                # Second optimisation happens for degrees >= 3.
                # Xi^3 is computed reusing previous computation:
                # Xi^3 = Xi^2 * Xi.

                if self.include_bias:
                    XP[:, 0] = 1
                    current_col = 1
                else:
                    current_col = 0

                # d = 0
                XP[:, current_col:current_col + n_features] = X
                index = list(range(current_col, current_col + n_features))
                current_col += n_features
                index.append(current_col)

                # d >= 1
                for _ in range(1, self.degree):
                    new_index = []
                    end = index[-1]
                    for feature_idx in range(n_features):
                        start = index[feature_idx]
                        new_index.append(current_col)
                        if self.interaction_only:
                            start += index[feature_idx +
                                           1] - index[feature_idx]
                        next_col = current_col + end - start
                        if next_col <= current_col:
                            break
                        # XP[:, start:end] are terms of degree d - 1
                        # that exclude feature #feature_idx.
                        np.multiply(
                            XP[:, start:end],
                            X[:, feature_idx:feature_idx + 1],
                            out=XP[:, current_col:next_col],
                            casting="no",
                        )
                        current_col = next_col

                    new_index.append(current_col)
                    index = new_index

        return XP
Example #50
0
def toarray(a):
    if isspmatrix(a):
        return a.toarray()
    else:
        return a
    def fit(self, X, y):
        """Fit a semi-supervised label propagation model based

        All the input data is provided matrix X (labeled and unlabeled)
        and corresponding label matrix y with a dedicated marker value for
        unlabeled samples.

        Parameters
        ----------
        X : array-like, shape = [n_samples, n_features]
            A {n_samples by n_samples} size matrix will be created from this

        y : array_like, shape = [n_samples]
            n_labeled_samples (unlabeled points are marked as -1)
            All unlabeled samples will be transductively assigned labels

        Returns
        -------
        self : returns an instance of self.
        """
        if sparse.isspmatrix(X):
            self.X_ = X
        else:
            self.X_ = np.asarray(X)

        # actual graph construction (implementations should override this)
        graph_matrix = self._build_graph()

        # label construction
        # construct a categorical distribution for classification only
        classes = np.unique(y)
        classes = (classes[classes != -1])
        self.classes_ = classes

        n_samples, n_classes = len(y), len(classes)

        y = np.asarray(y)
        unlabeled = y == -1
        clamp_weights = np.ones((n_samples, 1))
        clamp_weights[unlabeled, 0] = self.alpha

        # initialize distributions
        self.label_distributions_ = np.zeros((n_samples, n_classes))
        for label in classes:
            self.label_distributions_[y == label, classes == label] = 1

        y_static = np.copy(self.label_distributions_)
        if self.alpha > 0.:
            y_static *= 1 - self.alpha
        y_static[unlabeled] = 0

        l_previous = np.zeros((self.X_.shape[0], n_classes))

        remaining_iter = self.max_iter
        if sparse.isspmatrix(graph_matrix):
            graph_matrix = graph_matrix.tocsr()
        while (_not_converged(self.label_distributions_, l_previous, self.tol)
               and remaining_iter > 1):
            l_previous = self.label_distributions_
            self.label_distributions_ = safe_sparse_dot(
                graph_matrix, self.label_distributions_)
            # clamp
            self.label_distributions_ = np.multiply(
                clamp_weights, self.label_distributions_) + y_static
            remaining_iter -= 1

        normalizer = np.sum(self.label_distributions_, axis=1)[:, np.newaxis]
        self.label_distributions_ /= normalizer
        # set the transduction item
        transduction = self.classes_[np.argmax(self.label_distributions_,
                                               axis=1)]
        self.transduction_ = transduction.ravel()
        return self
def my_spectral_embedding(adjacency,
                          n_components=8,
                          eigen_solver=None,
                          random_state=None,
                          eigen_tol=0.0,
                          norm_laplacian=False,
                          drop_first=True):
    """Project the sample on the first eigenvectors of the graph Laplacian.
    The adjacency matrix is used to compute a normalized graph Laplacian
    whose spectrum (especially the eigenvectors associated to the
    smallest eigenvalues) has an interpretation in terms of minimal
    number of cuts necessary to split the graph into comparably sized
    components.
    This embedding can also 'work' even if the ``adjacency`` variable is
    not strictly the adjacency matrix of a graph but more generally
    an affinity or similarity matrix between samples (for instance the
    heat kernel of a euclidean distance matrix or a k-NN matrix).
    However care must taken to always make the affinity matrix symmetric
    so that the eigenvector decomposition works as expected.
    Note : Laplacian Eigenmaps is the actual algorithm implemented here.
    Read more in the :ref:`User Guide <spectral_embedding>`.
    Parameters
    ----------
    adjacency : array-like or sparse matrix, shape: (n_samples, n_samples)
        The adjacency matrix of the graph to embed.
    n_components : integer, optional, default 8
        The dimension of the projection subspace.
    eigen_solver : {None, 'arpack', 'lobpcg', or 'amg'}, default None
        The eigenvalue decomposition strategy to use. AMG requires pyamg
        to be installed. It can be faster on very large, sparse problems,
        but may also lead to instabilities.
    random_state : int, RandomState instance or None, optional, default: None
        A pseudo random number generator used for the initialization of the
        lobpcg eigenvectors decomposition.  If int, random_state is the seed
        used by the random number generator; If RandomState instance,
        random_state is the random number generator; If None, the random number
        generator is the RandomState instance used by `np.random`. Used when
        ``solver`` == 'amg'.
    eigen_tol : float, optional, default=0.0
        Stopping criterion for eigendecomposition of the Laplacian matrix
        when using arpack eigen_solver.
    norm_laplacian : bool, optional, default=True
        If True, then compute normalized Laplacian.
    drop_first : bool, optional, default=True
        Whether to drop the first eigenvector. For spectral embedding, this
        should be True as the first eigenvector should be constant vector for
        connected graph, but for spectral clustering, this should be kept as
        False to retain the first eigenvector.
    Returns
    -------
    embedding : array, shape=(n_samples, n_components)
        The reduced samples.
    Notes
    -----
    Spectral Embedding (Laplacian Eigenmaps) is most useful when the graph
    has one connected component. If there graph has many components, the first
    few eigenvectors will simply uncover the connected components of the graph.
    References
    ----------
    * https://en.wikipedia.org/wiki/LOBPCG
    * Toward the Optimal Preconditioned Eigensolver: Locally Optimal
      Block Preconditioned Conjugate Gradient Method
      Andrew V. Knyazev
      http://dx.doi.org/10.1137%2FS1064827500366124
    """
    import warnings

    import numpy as np
    from scipy import sparse
    from scipy.linalg import eigh
    from scipy.sparse.linalg import eigsh, lobpcg

    from sklearn.base import BaseEstimator
    from sklearn.externals import six
    from sklearn.utils import check_random_state, check_array, check_symmetric
    from sklearn.utils.extmath import _deterministic_vector_sign_flip
    from sklearn.metrics.pairwise import rbf_kernel
    from sklearn.neighbors import kneighbors_graph

    adjacency = check_symmetric(adjacency)
    try:
        from pyamg import smoothed_aggregation_solver
    except ImportError:
        if eigen_solver == "amg":
            raise ValueError("The eigen_solver was set to 'amg', but pyamg is "
                             "not available.")
    if eigen_solver is None:
        eigen_solver = 'arpack'
    elif eigen_solver not in ('arpack', 'lobpcg', 'amg'):
        raise ValueError("Unknown value for eigen_solver: '%s'."
                         "Should be 'amg', 'arpack', or 'lobpcg'" %
                         eigen_solver)
    random_state = check_random_state(random_state)
    n_nodes = adjacency.shape[0]
    # Whether to drop the first eigenvector
    if drop_first:
        n_components = n_components + 1
    if not _graph_is_connected(adjacency):
        warnings.warn("Graph is not fully connected, spectral embedding"
                      " may not work as expected.")
    laplacian, dd = sparse.csgraph.laplacian(adjacency,
                                             normed=norm_laplacian,
                                             return_diag=True)
    if (eigen_solver == 'arpack' or eigen_solver != 'lobpcg' and
        (not sparse.isspmatrix(laplacian) or n_nodes < 5 * n_components)):
        # lobpcg used with eigen_solver='amg' has bugs for low number of nodes
        # for details see the source code in scipy:
        # https://github.com/scipy/scipy/blob/v0.11.0/scipy/sparse/linalg/eigen
        # /lobpcg/lobpcg.py#L237
        # or matlab:
        # http://www.mathworks.com/matlabcentral/fileexchange/48-lobpcg-m
        laplacian = _set_diag(laplacian, 1, norm_laplacian)

        # Here we'll use shift-invert mode for fast eigenvalues
        # (see http://docs.scipy.org/doc/scipy/reference/tutorial/arpack.html
        #  for a short explanation of what this means)
        # Because the normalized Laplacian has eigenvalues between 0 and 2,
        # I - L has eigenvalues between -1 and 1.  ARPACK is most efficient
        # when finding eigenvalues of largest magnitude (keyword which='LM')
        # and when these eigenvalues are very large compared to the rest.
        # For very large, very sparse graphs, I - L can have many, many
        # eigenvalues very near 1.0.  This leads to slow convergence.  So
        # instead, we'll use ARPACK's shift-invert mode, asking for the
        # eigenvalues near 1.0.  This effectively spreads-out the spectrum
        # near 1.0 and leads to much faster convergence: potentially an
        # orders-of-magnitude speedup over simply using keyword which='LA'
        # in standard mode.
        try:
            # We are computing the opposite of the laplacian inplace so as
            # to spare a memory allocation of a possibly very large array
            laplacian *= -1
            v0 = random_state.uniform(-1, 1, laplacian.shape[0])
            lambdas, diffusion_map = eigsh(laplacian,
                                           k=n_components,
                                           sigma=1.0,
                                           which='LM',
                                           tol=eigen_tol,
                                           v0=v0)
            embedding = diffusion_map.T[n_components::-1] * dd
        except RuntimeError:
            # When submatrices are exactly singular, an LU decomposition
            # in arpack fails. We fallback to lobpcg
            eigen_solver = "lobpcg"
            # Revert the laplacian to its opposite to have lobpcg work
            laplacian *= -1
    if eigen_solver == 'amg':
        # Use AMG to get a preconditioner and speed up the eigenvalue
        # problem.
        if not sparse.issparse(laplacian):
            warnings.warn("AMG works better for sparse matrices")
        # lobpcg needs double precision floats
        laplacian = check_array(laplacian,
                                dtype=np.float64,
                                accept_sparse=True)
        laplacian = _set_diag(laplacian, 1, norm_laplacian)
        ml = smoothed_aggregation_solver(check_array(laplacian, 'csr'))
        M = ml.aspreconditioner()
        X = random_state.rand(laplacian.shape[0], n_components + 1)
        X[:, 0] = dd.ravel()
        lambdas, diffusion_map = lobpcg(laplacian,
                                        X,
                                        M=M,
                                        tol=1.e-12,
                                        largest=False)
        embedding = diffusion_map.T * dd
        if embedding.shape[0] == 1:
            raise ValueError

    elif eigen_solver == "lobpcg":
        # lobpcg needs double precision floats
        laplacian = check_array(laplacian,
                                dtype=np.float64,
                                accept_sparse=True)
        if n_nodes < 5 * n_components + 1:
            # see note above under arpack why lobpcg has problems with small
            # number of nodes
            # lobpcg will fallback to eigh, so we short circuit it
            if sparse.isspmatrix(laplacian):
                laplacian = laplacian.toarray()
            lambdas, diffusion_map = eigh(laplacian)
            embedding = diffusion_map.T[:n_components] * dd
        else:
            laplacian = _set_diag(laplacian, 1, norm_laplacian)
            # We increase the number of eigenvectors requested, as lobpcg
            # doesn't behave well in low dimension
            X = random_state.rand(laplacian.shape[0], n_components + 1)
            X[:, 0] = dd.ravel()
            lambdas, diffusion_map = lobpcg(laplacian,
                                            X,
                                            tol=1e-15,
                                            largest=False,
                                            maxiter=2000)
            embedding = diffusion_map.T[:n_components] * dd
            if embedding.shape[0] == 1:
                raise ValueError
    embedding = _deterministic_vector_sign_flip(embedding)
    if drop_first:
        vectors = embedding[1:n_components].T
    else:
        vectors = embedding[:n_components].T

    return (lambdas, vectors)
Example #53
0
def _fit_liblinear(X, y, C, fit_intercept, intercept_scaling, class_weight,
                   penalty, dual, verbose, max_iter, tol,
                   random_state=None, multi_class='ovr',
                   loss='logistic_regression', epsilon=0.1,
                   sample_weight=None):
    """Used by Logistic Regression (and CV) and LinearSVC/LinearSVR.

    Preprocessing is done in this function before supplying it to liblinear.

    Parameters
    ----------
    X : {array-like, sparse matrix}, shape (n_samples, n_features)
        Training vector, where n_samples in the number of samples and
        n_features is the number of features.

    y : array-like, shape (n_samples,)
        Target vector relative to X

    C : float
        Inverse of cross-validation parameter. Lower the C, the more
        the penalization.

    fit_intercept : bool
        Whether or not to fit the intercept, that is to add a intercept
        term to the decision function.

    intercept_scaling : float
        LibLinear internally penalizes the intercept and this term is subject
        to regularization just like the other terms of the feature vector.
        In order to avoid this, one should increase the intercept_scaling.
        such that the feature vector becomes [x, intercept_scaling].

    class_weight : {dict, 'balanced'}, optional
        Weights associated with classes in the form ``{class_label: weight}``.
        If not given, all classes are supposed to have weight one. For
        multi-output problems, a list of dicts can be provided in the same
        order as the columns of y.

        The "balanced" mode uses the values of y to automatically adjust
        weights inversely proportional to class frequencies in the input data
        as ``n_samples / (n_classes * np.bincount(y))``

    penalty : str, {'l1', 'l2'}
        The norm of the penalty used in regularization.

    dual : bool
        Dual or primal formulation,

    verbose : int
        Set verbose to any positive number for verbosity.

    max_iter : int
        Number of iterations.

    tol : float
        Stopping condition.

    random_state : int, RandomState instance or None, optional (default=None)
        The seed of the pseudo random number generator to use when shuffling
        the data.  If int, random_state is the seed used by the random number
        generator; If RandomState instance, random_state is the random number
        generator; If None, the random number generator is the RandomState
        instance used by `np.random`.

    multi_class : str, {'ovr', 'crammer_singer'}
        `ovr` trains n_classes one-vs-rest classifiers, while `crammer_singer`
        optimizes a joint objective over all classes.
        While `crammer_singer` is interesting from an theoretical perspective
        as it is consistent it is seldom used in practice and rarely leads to
        better accuracy and is more expensive to compute.
        If `crammer_singer` is chosen, the options loss, penalty and dual will
        be ignored.

    loss : str, {'logistic_regression', 'hinge', 'squared_hinge',
                 'epsilon_insensitive', 'squared_epsilon_insensitive}
        The loss function used to fit the model.

    epsilon : float, optional (default=0.1)
        Epsilon parameter in the epsilon-insensitive loss function. Note
        that the value of this parameter depends on the scale of the target
        variable y. If unsure, set epsilon=0.

    sample_weight : array-like, optional
        Weights assigned to each sample.

    Returns
    -------
    coef_ : ndarray, shape (n_features, n_features + 1)
        The coefficient vector got by minimizing the objective function.

    intercept_ : float
        The intercept term added to the vector.

    n_iter_ : int
        Maximum number of iterations run across all classes.
    """
    if loss not in ['epsilon_insensitive', 'squared_epsilon_insensitive']:
        enc = LabelEncoder()
        y_ind = enc.fit_transform(y)
        classes_ = enc.classes_
        if len(classes_) < 2:
            raise ValueError("This solver needs samples of at least 2 classes"
                             " in the data, but the data contains only one"
                             " class: %r" % classes_[0])

        class_weight_ = compute_class_weight(class_weight, classes_, y)
    else:
        class_weight_ = np.empty(0, dtype=np.float64)
        y_ind = y
    liblinear.set_verbosity_wrap(verbose)
    rnd = check_random_state(random_state)
    if verbose:
        print('[LibLinear]', end='')

    # LinearSVC breaks when intercept_scaling is <= 0
    bias = -1.0
    if fit_intercept:
        if intercept_scaling <= 0:
            raise ValueError("Intercept scaling is %r but needs to be greater than 0."
                             " To disable fitting an intercept,"
                             " set fit_intercept=False." % intercept_scaling)
        else:
            bias = intercept_scaling

    libsvm.set_verbosity_wrap(verbose)
    libsvm_sparse.set_verbosity_wrap(verbose)
    liblinear.set_verbosity_wrap(verbose)

    # Liblinear doesn't support 64bit sparse matrix indices yet
    if sp.issparse(X):
        _check_large_sparse(X)

    # LibLinear wants targets as doubles, even for classification
    y_ind = np.asarray(y_ind, dtype=np.float64).ravel()
    y_ind = np.require(y_ind, requirements="W")
    if sample_weight is None:
        sample_weight = np.ones(X.shape[0])
    else:
        sample_weight = np.array(sample_weight, dtype=np.float64, order='C')
        check_consistent_length(sample_weight, X)

    solver_type = _get_liblinear_solver_type(multi_class, penalty, loss, dual)
    raw_coef_, n_iter_ = liblinear.train_wrap(
        X, y_ind, sp.isspmatrix(X), solver_type, tol, bias, C,
        class_weight_, max_iter, rnd.randint(np.iinfo('i').max),
        epsilon, sample_weight)
    # Regarding rnd.randint(..) in the above signature:
    # seed for srand in range [0..INT_MAX); due to limitations in Numpy
    # on 32-bit platforms, we can't get to the UINT_MAX limit that
    # srand supports
    n_iter_ = max(n_iter_)
    if n_iter_ >= max_iter:
        warnings.warn("Liblinear failed to converge, increase "
                      "the number of iterations.", ConvergenceWarning)

    if fit_intercept:
        coef_ = raw_coef_[:, :-1]
        intercept_ = intercept_scaling * raw_coef_[:, -1]
    else:
        coef_ = raw_coef_
        intercept_ = 0.

    return coef_, intercept_, n_iter_
Example #54
0
def _pre_fit(
    X,
    y,
    Xy,
    precompute,
    normalize,
    fit_intercept,
    copy,
    check_input=True,
    sample_weight=None,
):
    """Aux function used at beginning of fit in linear models

    Parameters
    ----------
    order : 'F', 'C' or None, default=None
        Whether X and y will be forced to be fortran or c-style. Only relevant
        if sample_weight is not None.
    """
    n_samples, n_features = X.shape

    if sparse.isspmatrix(X):
        # copy is not needed here as X is not modified inplace when X is sparse
        precompute = False
        X, y, X_offset, y_offset, X_scale = _preprocess_data(
            X,
            y,
            fit_intercept=fit_intercept,
            normalize=normalize,
            copy=False,
            return_mean=True,
            check_input=check_input,
        )
    else:
        # copy was done in fit if necessary
        X, y, X_offset, y_offset, X_scale = _preprocess_data(
            X,
            y,
            fit_intercept=fit_intercept,
            normalize=normalize,
            copy=copy,
            check_input=check_input,
            sample_weight=sample_weight,
        )
    if sample_weight is not None:
        X, y = _rescale_data(X, y, sample_weight=sample_weight)

    # FIXME: 'normalize' to be removed in 1.2
    if hasattr(precompute, "__array__"):
        if (fit_intercept and not np.allclose(X_offset, np.zeros(n_features))
                or normalize
                and not np.allclose(X_scale, np.ones(n_features))):
            warnings.warn(
                "Gram matrix was provided but X was centered to fit "
                "intercept, or X was normalized : recomputing Gram matrix.",
                UserWarning,
            )
            # recompute Gram
            precompute = "auto"
            Xy = None
        elif check_input:
            # If we're going to use the user's precomputed gram matrix, we
            # do a quick check to make sure its not totally bogus.
            _check_precomputed_gram_matrix(X, precompute, X_offset, X_scale)

    # precompute if n_samples > n_features
    if isinstance(precompute, str) and precompute == "auto":
        precompute = n_samples > n_features

    if precompute is True:
        # make sure that the 'precompute' array is contiguous.
        precompute = np.empty(shape=(n_features, n_features),
                              dtype=X.dtype,
                              order="C")
        np.dot(X.T, X, out=precompute)

    if not hasattr(precompute, "__array__"):
        Xy = None  # cannot use Xy if precompute is not Gram

    if hasattr(precompute, "__array__") and Xy is None:
        common_dtype = np.find_common_type([X.dtype, y.dtype], [])
        if y.ndim == 1:
            # Xy is 1d, make sure it is contiguous.
            Xy = np.empty(shape=n_features, dtype=common_dtype, order="C")
            np.dot(X.T, y, out=Xy)
        else:
            # Make sure that Xy is always F contiguous even if X or y are not
            # contiguous: the goal is to make it fast to extract the data for a
            # specific target.
            n_targets = y.shape[1]
            Xy = np.empty(shape=(n_features, n_targets),
                          dtype=common_dtype,
                          order="F")
            np.dot(y.T, X, out=Xy.T)

    return X, y, X_offset, y_offset, X_scale, precompute, Xy
Example #55
0
def ishermitian(A, fast_check=True, tol=1e-6, verbose=False):
    r"""Return True if A is Hermitian to within tol.

    Parameters
    ----------
    A   : {dense or sparse matrix}
        e.g. array, matrix, csr_matrix, ...
    fast_check : {bool}
        If True, use the heuristic < Ax, y> = < x, Ay>
        for random vectors x and y to check for conjugate symmetry.
        If False, compute A - A.conj().T.
    tol : {float}
        Symmetry tolerance

    verbose: {bool}
        prints
        max( \|A - A.conj().T\| ) if nonhermitian and fast_check=False..
        abs( <Ax, y> - <x, Ay> )  if nonhermitian and fast_check=False

    Returns
    -------
    True                        if hermitian
    False                       if nonhermitian

    Notes
    -----
    This function applies a simple test of conjugate symmetry

    Examples
    --------
    >>> import numpy as np
    >>> from pyamg.util.linalg import ishermitian
    >>> ishermitian(np.array([[1,2],[1,1]]))
    False

    >>> from pyamg.gallery import poisson
    >>> ishermitian(poisson((10,10)))
    True

    """
    # convert to array type
    if not sparse.isspmatrix(A):
        A = np.asarray(A)

    if fast_check:
        x = sp.rand(A.shape[0], 1)
        y = sp.rand(A.shape[0], 1)
        if A.dtype == complex:
            x = x + 1.0j * sp.rand(A.shape[0], 1)
            y = y + 1.0j * sp.rand(A.shape[0], 1)
        xAy = np.dot((A.dot(x)).conjugate().T, y)
        xAty = np.dot(x.conjugate().T, A.dot(y))
        diff = float(np.abs(xAy - xAty) / np.sqrt(np.abs(xAy * xAty)))

    else:
        # compute the difference, A - A.conj().T
        if sparse.isspmatrix(A):
            diff = np.ravel((A - A.conj().T).data)
        else:
            diff = np.ravel(A - A.conj().T)

        if np.max(diff.shape) == 0:
            diff = 0
        else:
            diff = np.max(np.abs(diff))

    if diff < tol:
        diff = 0
        return True
    else:
        if verbose:
            print(diff)
        return False

    return diff
Example #56
0
    def fit(self, X, y, sample_weight=None):
        """
        Build a RGF Regressor from the training set (X, y).

        Parameters
        ----------
        X : array-like or sparse matrix of shape = [n_samples, n_features]
            The training input samples.

        y : array-like, shape = [n_samples]
            The target values (real numbers in regression).

        sample_weight : array-like, shape = [n_samples] or None
            Individual weights for each sample.

        Returns
        -------
        self : object
            Returns self.
        """
        _validate_params(**self.get_params())

        X, y = check_X_y(X, y, accept_sparse=True, multi_output=False, y_numeric=True)
        n_samples, self._n_features = X.shape

        if self.sl2 is None:
            self._sl2 = self.l2
        else:
            self._sl2 = self.sl2

        if isinstance(self.min_samples_leaf, _FLOATS):
            self._min_samples_leaf = ceil(self.min_samples_leaf * n_samples)
        else:
            self._min_samples_leaf = self.min_samples_leaf

        if self.n_iter is None:
            if self.loss == "LS":
                self._n_iter = 10
            else:
                self._n_iter = 5
        else:
            self._n_iter = self.n_iter

        if sample_weight is None:
            sample_weight = np.ones(n_samples, dtype=np.float32)
        else:
            sample_weight = column_or_1d(sample_weight, warn=True)
            if (sample_weight <= 0).any():
                raise ValueError("Sample weights must be positive.")
        check_consistent_length(X, y, sample_weight)

        train_x_loc = os.path.join(_TEMP_PATH, self._file_prefix + ".train.data.x")
        train_y_loc = os.path.join(_TEMP_PATH, self._file_prefix + ".train.data.y")
        train_weight_loc = os.path.join(_TEMP_PATH, self._file_prefix + ".train.data.weight")
        if sp.isspmatrix(X):
            _sparse_savetxt(train_x_loc, X)
        else:
            np.savetxt(train_x_loc, X, delimiter=' ', fmt="%s")
        np.savetxt(train_y_loc, y, delimiter=' ', fmt="%s")
        np.savetxt(train_weight_loc, sample_weight, delimiter=' ', fmt="%s")

        # Format train command
        params = []
        if self.verbose > 0:
            params.append("Verbose")
        if self.verbose > 5:
            params.append("Verbose_opt")  # Add some info on weight optimization
        if self.normalize:
            params.append("NormalizeTarget")
        params.append("train_x_fn=%s" % train_x_loc)
        params.append("train_y_fn=%s" % train_y_loc)
        params.append("algorithm=%s" % self.algorithm)
        params.append("loss=%s" % self.loss)
        params.append("max_leaf_forest=%s" % self.max_leaf)
        params.append("test_interval=%s" % self.test_interval)
        params.append("reg_L2=%s" % self.l2)
        params.append("reg_sL2=%s" % self._sl2)
        params.append("reg_depth=%s" % self.reg_depth)
        params.append("min_pop=%s" % self._min_samples_leaf)
        params.append("num_iteration_opt=%s" % self._n_iter)
        params.append("num_tree_search=%s" % self.n_tree_search)
        params.append("opt_interval=%s" % self.opt_interval)
        params.append("opt_stepsize=%s" % self.learning_rate)
        params.append("memory_policy=%s" % self.memory_policy.title())
        params.append("model_fn_prefix=%s" % os.path.join(_TEMP_PATH, self._file_prefix + ".model"))
        params.append("train_w_fn=%s" % train_weight_loc)

        cmd = (_EXE_PATH, "train", ",".join(params))

        # Train
        output = subprocess.Popen(cmd,
                                  stdout=subprocess.PIPE,
                                  stderr=subprocess.STDOUT,
                                  universal_newlines=True).communicate()

        if self.verbose:
            for k in output:
                print(k)

        self._fitted = True
        return self
Example #57
0
    init = tf.global_variables_initializer()
    sess.run(init)

    best_ndcg = -np.inf

    update_count = 0.0

    for epoch in range(n_epochs):
        np.random.shuffle(idxlist)
        # train for one epoch
        for bnum, st_idx in enumerate(range(0, N, batch_size)):
            end_idx = min(st_idx + batch_size, N)
            X = train_data[idxlist[st_idx:end_idx]]

            if sparse.isspmatrix(X):
                X = X.toarray()
            X = X.astype('float32')

            if total_anneal_steps > 0:
                anneal = min(anneal_cap,
                             1. * update_count / total_anneal_steps)
            else:
                anneal = anneal_cap

            feed_dict = {
                vae.input_ph: X,
                vae.K: JK_train,
                #vae.keep_prob_ph: 0.5,
                vae.K_u: JK_u,
                vae.anneal_ph: anneal,
Example #58
0
def invBlkTriDiag(M, nn):
    '''
    
    Efficiently inverts a block tridiagonal (a block diagonal matrix 
    with off-diagonal blocks) matrix. Blocks are (nn,nn) with nblocks
    equal to the # of blocks.
    
    Args:
        M : *sparse* square 2D array, the block tridiagonal matrix
        nn : size of each block

    Returns:
        MinvDiag : values on the diagonal of inverted M
        MinvBlocks : values on the main diagonal blocks of invM
        MinvBelowDiagBlocks : values on the off diagonal blocks of invM
    '''
    if not isspmatrix(M):
        print('Casting M to sparse format')
        M = csr_matrix(M)

    nblocks = int(M.shape[0] / nn)  # number of total blocks

    # Matrices to store during recursions
    A = np.zeros((nn, nn, nblocks))  # for below-diagonal blocks
    B = np.zeros((nn, nn, nblocks))  # for diagonal blocks
    C = np.zeros((nn, nn, nblocks))  # for above-diagonal blocks
    D = np.zeros((nn, nn, nblocks))  # quantity to compute
    E = np.zeros((nn, nn, nblocks))  # quantity to compute

    # Initialize first D block
    inds = np.arange(nn)  # indices for 1st block
    B[:, :, 0] = M[np.ix_(inds, inds)].todense()
    C[:, :, 0] = M[np.ix_(inds, inds + nn)].todense()
    D[:, :, 0] = np.linalg.solve(B[:, :, 0], C[:, :, 0])

    # Initialize last E block
    inds = (nblocks - 1) * nn + inds  # indices for last block
    A[:, :, -1] = M[np.ix_(inds, inds - nn)].todense()
    B[:, :, -1] = M[np.ix_(inds, inds)].todense()
    E[:, :, -1] = np.linalg.solve(B[:, :, -1], A[:, :, -1])

    # Extract blocks A, B, and C
    for ii in np.arange(1, nblocks - 1):
        inds = np.arange(nn) + ii * nn  # indices for center block
        A[:, :, ii] = M[np.ix_(inds,
                               inds - nn)].todense()  # below-diagonal block
        B[:, :, ii] = M[np.ix_(inds, inds)].todense()  # middle diagonal block
        C[:, :, ii] = M[np.ix_(inds,
                               inds + nn)].todense()  # above diagonal block

    # Make a pass through data to compute D and E
    for ii in np.arange(1, nblocks - 1):
        # Forward recursion
        D[:, :, ii] = np.linalg.solve(
            B[:, :, ii] - A[:, :, ii] @ D[:, :, ii - 1], C[:, :, ii])

        # Backward recursion
        jj = nblocks - ii - 1
        E[:, :, jj] = np.linalg.solve(
            B[:, :, jj] - C[:, :, jj] @ E[:, :, jj + 1], A[:, :, jj])

    # Now form blocks of inverse covariance
    I = np.eye(nn)
    MinvBlocks = np.zeros((nn, nn, nblocks))
    MinvBelowDiagBlocks = np.zeros((nn, nn, nblocks - 1))
    MinvBlocks[:, :, 0] = np.linalg.inv(
        B[:, :, 0] @ (I - D[:, :, 0] @ E[:, :, 1]))
    MinvBlocks[:, :, -1] = np.linalg.inv(B[:, :, -1] -
                                         A[:, :, -1] @ D[:, :, -2])
    for ii in np.arange(1, nblocks - 1):
        # Compute diagonal blocks of inverse
        MinvBlocks[:, :, ii] = np.linalg.inv(
            (B[:, :, ii] - A[:, :, ii] @ D[:, :, ii - 1])
            @ (I - D[:, :, ii] @ E[:, :, ii + 1]))
        # Compute below-diagonal blocks
        MinvBelowDiagBlocks[:, :, ii -
                            1] = -D[:, :, ii - 1] @ MinvBlocks[:, :, ii]

    MinvBelowDiagBlocks[:, :, -1] = -D[:, :, -2] @ MinvBlocks[:, :, -1]

    # Extract just the diagonal elements
    MinvDiag = np.zeros(nn * nblocks)
    for ii in np.arange(nblocks):
        MinvDiag[ii * nn:(ii + 1) * nn] = np.diag(MinvBlocks[:, :, ii])

    return MinvDiag, MinvBlocks, MinvBelowDiagBlocks
Example #59
0
nx.draw_networkx_edge_labels(G,
                             pos=nx.spring_layout(G, seed=my_seed),
                             edge_labels=labels)
nx.draw_networkx_edges(G, pos=nx.spring_layout(G, seed=my_seed),
                       width=list(labels.values()))
plt.axis('off')
plt.show()

# Exercise: edges
# 1) show edges weights on the graph
# 2) change the edge width to be proportional to the edge weights

# %%

A = nx.adjacency_matrix(G)
print(isspmatrix(A))
print(A.todense())


# %%

nx.shortest_path(G, 'C', 'B', weight='weight')

# ## Definition : *incidence matrice*
# Let $G = (V,E)$ be a (non-oriented) graph with $n$ vertices,
# $V = [1,\dots,n]$, and $p$ edges, $E = [1,\dots,p]$.
# The graph can be represented by its vertex-edge incidence matrix
# $D^\top \in \mathbb{R}^{p \times n}$ defined by
# \begin{equation}
#   (D^\top)_{e,v} =
#   \begin{cases}
Example #60
0
    def fit(self, X, y, sample_weight=None):
        """Fit the SVM model according to the given training data.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape (n_samples, n_features)
            Training vectors, where n_samples is the number of samples
            and n_features is the number of features.
            For kernel="precomputed", the expected shape of X is
            (n_samples, n_samples).

        y : array-like, shape (n_samples,)
            Target values (class labels in classification, real numbers in
            regression)

        sample_weight : array-like, shape (n_samples,)
            Per-sample weights. Rescale C per sample. Higher weights
            force the classifier to put more emphasis on these points.

        Returns
        -------
        self : object

        Notes
        ------
        If X and y are not C-ordered and contiguous arrays of np.float64 and
        X is not a scipy.sparse.csr_matrix, X and/or y may be copied.

        If X is a dense array, then the other methods will not support sparse
        matrices as input.
        """

        rnd = check_random_state(self.random_state)

        sparse = sp.isspmatrix(X)
        if sparse and self.kernel == "precomputed":
            raise TypeError("Sparse precomputed kernels are not supported.")
        self._sparse = sparse and not callable(self.kernel)

        X, y = check_X_y(X, y, dtype=np.float64,
                         order='C', accept_sparse='csr',
                         accept_large_sparse=False)
        y = self._validate_targets(y)

        sample_weight = np.asarray([]
                                   if sample_weight is None
                                   else sample_weight, dtype=np.float64)
        solver_type = LIBSVM_IMPL.index(self._impl)

        # input validation
        if solver_type != 2 and X.shape[0] != y.shape[0]:
            raise ValueError("X and y have incompatible shapes.\n" +
                             "X has %s samples, but y has %s." %
                             (X.shape[0], y.shape[0]))

        if self.kernel == "precomputed" and X.shape[0] != X.shape[1]:
            raise ValueError("X.shape[0] should be equal to X.shape[1]")

        if sample_weight.shape[0] > 0 and sample_weight.shape[0] != X.shape[0]:
            raise ValueError("sample_weight and X have incompatible shapes: "
                             "%r vs %r\n"
                             "Note: Sparse matrices cannot be indexed w/"
                             "boolean masks (use `indices=True` in CV)."
                             % (sample_weight.shape, X.shape))

        if self.gamma in ('scale', 'auto_deprecated'):
            if sparse:
                # var = E[X^2] - E[X]^2
                X_var = (X.multiply(X)).mean() - (X.mean()) ** 2
            else:
                X_var = X.var()
            if self.gamma == 'scale':
                if X_var != 0:
                    self._gamma = 1.0 / (X.shape[1] * X_var)
                else:
                    self._gamma = 1.0
            else:
                kernel_uses_gamma = (not callable(self.kernel) and self.kernel
                                     not in ('linear', 'precomputed'))
                if kernel_uses_gamma and not np.isclose(X_var, 1.0):
                    # NOTE: when deprecation ends we need to remove explicitly
                    # setting `gamma` in examples (also in tests). See
                    # https://github.com/scikit-learn/scikit-learn/pull/10331
                    # for the examples/tests that need to be reverted.
                    warnings.warn("The default value of gamma will change "
                                  "from 'auto' to 'scale' in version 0.22 to "
                                  "account better for unscaled features. Set "
                                  "gamma explicitly to 'auto' or 'scale' to "
                                  "avoid this warning.", FutureWarning)
                self._gamma = 1.0 / X.shape[1]
        elif self.gamma == 'auto':
            self._gamma = 1.0 / X.shape[1]
        else:
            self._gamma = self.gamma

        kernel = self.kernel
        if callable(kernel):
            kernel = 'precomputed'

        fit = self._sparse_fit if self._sparse else self._dense_fit
        if self.verbose:  # pragma: no cover
            print('[LibSVM]', end='')

        seed = rnd.randint(np.iinfo('i').max)
        fit(X, y, sample_weight, solver_type, kernel, random_seed=seed)
        # see comment on the other call to np.iinfo in this file

        self.shape_fit_ = X.shape

        # In binary case, we need to flip the sign of coef, intercept and
        # decision function. Use self._intercept_ and self._dual_coef_
        # internally.
        self._intercept_ = self.intercept_.copy()
        self._dual_coef_ = self.dual_coef_
        if self._impl in ['c_svc', 'nu_svc'] and len(self.classes_) == 2:
            self.intercept_ *= -1
            self.dual_coef_ = -self.dual_coef_

        return self