Example #1
0
	def _compute(self):
		start = datetime.datetime.now()
		
		C = self.C
		gamma = self.gamma
		Kcount = len( gamma )
		(N,d) = self.data.shape
		X = self.data
		
		# CMF of observations X
		Xcmf = ( (X.reshape(N,1,d) > transpose(X.reshape(N,1,d),[1,0,2])).prod(2).sum(1,dtype=float) / N ).reshape([N,1])
		
		# epsilon of observations X
		e = sqrt( (1./N) * ( Xcmf ) * (1.-Xcmf) ).reshape([N,1])
		
		K = self._K( Xcmf.reshape(N,1,d), transpose(Xcmf.reshape(N,1,d), [1,0,2]), gamma )

		xipos = cvxmod.optvar( 'xi+', N,1)
		xipos.pos = True
		xineg = cvxmod.optvar( 'xi-', N,1)
		xineg.pos = True
			
		alphas = list()
		expr = ( C*cvxmod.sum(xipos) ) + ( C*cvxmod.sum(xineg) )
		ineq = 0
		eq = 0
		
		for i in range( Kcount ):
			alpha = cvxmod.optvar( 'alpha(%s)' % i, N,1)
			alpha.pos = True
			
			alphas.append( alpha )
			expr += ( float(1./gamma[i]) * cvxmod.sum( alpha ) )
			ineq += ( cvxopt.matrix( K[i], (N,N) ) * alpha )
			eq += cvxmod.sum( alpha )
			
		objective = cvxmod.minimize( expr )
		
		ineq1 = ineq <= cvxopt.matrix( Xcmf + e ) + xineg
		ineq2 = ineq >= cvxopt.matrix( Xcmf - e ) - xipos
		eq1 = eq == cvxopt.matrix( 1.0 )
		

		# Solve!
		p = cvxmod.problem( objective = objective, constr = [ineq1,ineq2,eq1] )
		
		start = datetime.datetime.now()
		p.solve()
		duration = datetime.datetime.now() - start
		print "optimized in %ss" % (float(duration.microseconds)/1000000)
		
		self.Fl = Xcmf
		self.betas = [ ma.masked_less( alpha.value, 1e-4) for alpha in alphas ]
		
		print "SV's found: %s" % [ len( beta.compressed()) for beta in self.betas ]
Example #2
0
    def _compute(self):
        C = self.C
        gamma = self.gamma
        (N, d) = self.data.shape
        X = self.data

        Xcmf = (
            (X.reshape(N, 1, d) > transpose(X.reshape(N, 1, d), [1, 0, 2])).prod(2).sum(1, dtype=float) / N
        ).reshape([N, 1])
        sigma = 0.75 / sqrt(N)

        K = self._K(X.reshape(N, 1, d), transpose(X.reshape(N, 1, d), [1, 0, 2]), gamma).reshape([N, N])
        # NOTE: this integral depends on K being the gaussian kernel
        Kint = (1.0 / gamma) * scipy.special.ndtr((X - X.T) / gamma)

        alpha = cvxmod.optvar("alpha", N, 1)
        alpha.pos = True
        xi = cvxmod.optvar("xi", N, 1)
        xi.pos = True
        pXcmf = cvxmod.param("Xcmf", N, 1)
        pXcmf.pos = True
        pXcmf.value = cvxopt.matrix(Xcmf, (N, 1))
        pKint = cvxmod.param("Kint", N, N)
        pKint.value = cvxopt.matrix(Kint, (N, N))

        objective = cvxmod.minimize(cvxmod.sum(cvxmod.atoms.power(alpha, 2)) + (C * cvxmod.sum(xi)))
        eq1 = cvxmod.abs((pKint * alpha) - pXcmf) <= sigma + xi
        eq2 = cvxmod.sum(alpha) == 1.0

        # Solve!
        p = cvxmod.problem(objective=objective, constr=[eq1, eq2])
        p.solve()

        beta = ma.masked_less(alpha.value, 1e-7)
        mask = ma.getmask(beta)
        data = ma.array(X, mask=mask)

        self.beta = beta.compressed().reshape([1, len(beta.compressed())])
        self.SV = data.compressed().reshape([len(beta.compressed()), 1])
        print "%s SV's found" % len(self.SV)
Example #3
0
def interior_point(X, y, lam):
    """
    solve lasso using an interior point method
    requires cvxmod (Jacob Mattingley and Stephen Boyd)
    http://cvxmod.net/
    """
    import cvxmod as cvx
    n, m = X.shape
    X_cvx = cvx.matrix(np.array(X))
    y_cvx = cvx.matrix(np.array(y))
    theta = cvx.optvar('theta', m)
    p = cvx.problem(cvx.minimize(cvx.sum(cvx.atoms.power(X_cvx*theta - y_cvx, 2)) + 
                    (2*lam)*cvx.norm1(theta)))
    p.solve() 
    return np.array(cvx.value(theta))
Example #4
0
	def _compute(self):
		start = datetime.datetime.now()

		gamma = self.gamma
		(N,d) = self.data.shape
		X = self.data

		Xcmf = ( (X.reshape(N,1,d) > transpose(X.reshape(N,1,d),[1,0,2])).prod(2).sum(1,dtype=float) / N ).reshape([N,1])
		sigma = .75 / sqrt(N)
		
		K = self._K( X.reshape(N,1,d), transpose(X.reshape(N,1,d), [1,0,2]), gamma ).reshape([N,N])
		#NOTE: this integral depends on K being the gaussian kernel
		Kint =  ( (1.0/gamma)*scipy.special.ndtr( (X-X.T)/gamma ) )
		
		alpha = cvxmod.optvar( 'alpha',N,1)
		alpha.pos = True
		pK = cvxmod.param( 'K',N,N )
		pK.psd = True
		pK.value = cvxopt.matrix(K,(N,N) )
		pKint = cvxmod.param( 'Kint',N,N )
		pKint.value = cvxopt.matrix(Kint,(N,N))
		#pKint.pos = True
		pXcmf = cvxmod.param( 'Xcmf',N,1)
		pXcmf.value = cvxopt.matrix(Xcmf, (N,1))
		#pXcmf.pos = True
		
		objective = cvxmod.minimize( cvxmod.atoms.quadform(alpha, pK) )
		eq1 = cvxmod.abs( pXcmf - ( pKint * alpha ) ) <= sigma
		eq2 = cvxmod.sum( alpha ) == 1.0
		
		# Solve!
		p = cvxmod.problem( objective = objective, constr = [eq1, eq2] )
		
		start = datetime.datetime.now()
		p.solve()
		duration = datetime.datetime.now() - start
		print "optimized in %ss" % (float(duration.microseconds)/1000000)
		
		beta = ma.masked_less( alpha.value, 1e-7 )
		mask = ma.getmask( beta )
		data = ma.array(X,mask=mask)
		
		self.Fl = Xcmf
		self.beta = beta.compressed().reshape([ 1, len(beta.compressed()) ])
		self.SV = data.compressed().reshape([len(beta.compressed()),1])
		print "%s SV's found" % len(self.SV)
def run_opt(feature_lists, reference_indices, alpha):
    """
    run_opt( feature_lists ) -> weights
    feature_lists is a list of I image_feature_sets
        image_feature_sets are a list of P stacked_features
        stacked_features are a list of N different feature types
    reference_indices is a set of indices which will be held out as "reference"
    performs the opt:
        min sum_{i_r in ref_idx} sum_{i < I not in ref_idx} sum_{p < P}
            w' * ||f_{i_r,p,n} - f_{i,p,n}||^2 - alpha/(P-1) * sum_{p'<p} || f_{i_r,p,n} - f_{i,p',n} ||^2
    """

    I = len(feature_lists)
    P = len(feature_lists[0])
    N = len(feature_lists[0][0])

    non_reference_indices = [i for i in range(I) if not i in reference_indices]
    closeness_reward = np.zeros(N)
    uniqueness_penalty = np.zeros(N)
    f = feature_lists
    for i_r in reference_indices:
        for i in non_reference_indices:
            for p in range(P):
                for n in range(N):
                    closeness_reward[n] += feature_distance(
                        f[i_r][p][n], f[i][p][n])
                    for p_false in range(p):
                        uniqueness_penalty[n] += feature_distance(
                            f[i_r][p][n], f[i][p_false][n])
    c = cvx.param('c',
                  value=cvx.matrix(closeness_reward -
                                   alpha / float(P - 1) * uniqueness_penalty))
    print c.value
    w = cvx.optvar('w', N)
    w.pos = True
    w | cvx.In | cvx.norm1ball(N)
    p = cvx.problem()
    p.objective = cvx.minimize(cvx.tp(c) * w)
    p.constr = [cvx.sum(w) == 1]
    print "Running solver"
    p.solve()
    print "Ran!"
    return np.array(w.value)
def run_opt( feature_lists, reference_indices, alpha ):
    """
    run_opt( feature_lists ) -> weights
    feature_lists is a list of I image_feature_sets
        image_feature_sets are a list of P stacked_features
        stacked_features are a list of N different feature types
    reference_indices is a set of indices which will be held out as "reference"
    performs the opt:
        min sum_{i_r in ref_idx} sum_{i < I not in ref_idx} sum_{p < P}
            w' * ||f_{i_r,p,n} - f_{i,p,n}||^2 - alpha/(P-1) * sum_{p'<p} || f_{i_r,p,n} - f_{i,p',n} ||^2
    """

    I = len( feature_lists )
    P = len( feature_lists[0] )
    N = len( feature_lists[0][0] )

    non_reference_indices = [i for i in range(I) if not i in reference_indices]
    closeness_reward = np.zeros( N )
    uniqueness_penalty = np.zeros( N )
    f = feature_lists
    for i_r in reference_indices:
        for i in non_reference_indices:
            for p in range(P):
                for n in range(N):
                    closeness_reward[ n ] += feature_distance( f[i_r][p][n], f[i][p][n] )
                    for p_false in range(p):
                        uniqueness_penalty[ n ] += feature_distance( f[i_r][p][n], f[i][p_false][n] )
    c = cvx.param('c', value = cvx.matrix( closeness_reward - alpha / float(P-1) * uniqueness_penalty ) )
    print c.value
    w = cvx.optvar('w', N )
    w.pos = True
    w | cvx.In | cvx.norm1ball(N)
    p = cvx.problem()
    p.objective = cvx.minimize( cvx.tp(c) * w  )
    p.constr = [ cvx.sum(w) == 1]
    print "Running solver"
    p.solve()
    print "Ran!"
    return np.array( w.value )
Example #7
0
def fit_ellipse_stack_squared(dx, dy, dz, di):
    """
    fit ellipoid using squared loss

    idea to learn all stacks together including smoothness
    """

    # sanity check
    assert len(dx) == len(dy)
    assert len(dx) == len(dz)
    assert len(dx) == len(di)

    # unique zs
    dat = defaultdict(list)

    # resort data
    for idx in range(len(dx)):
        dat[dz[idx]].append( [dx[idx], dy[idx], di[idx]] )

    # init ret
    ellipse_stack = []
    for idx in range(max(dz)):
        ellipse_stack.append(Ellipse(0, 0, idx, 1, 1, 0))
    

    total_N = len(dx)
    M = len(dat.keys())
    D = 5

    X_matrix = []
    thetas = []


    for z in dat.keys():

        x = numpy.array(dat[z])[:,0]
        y = numpy.array(dat[z])[:,1]

        # intensities
        i = numpy.array(dat[z])[:,2]

        # log intensities
        i = numpy.log(i)

        # create matrix
        ity = numpy.diag(i)

        # dimensionality
        N = len(x)
        d = numpy.zeros((N, D))

        d[:,0] = x*x
        d[:,1] = y*y
        #d[:,2] = x*y
        d[:,2] = x
        d[:,3] = y
        d[:,4] = numpy.ones(N)

        #d[:,0] = x*x
        #d[:,1] = y*y
        #d[:,2] = x*y
        #d[:,3] = x
        #d[:,4] = y
        #d[:,5] = numpy.ones(N)
    
        # consider intensities
        old_shape = d.shape

        d = numpy.dot(ity, d)
        assert d.shape == old_shape
    
        print d.shape   
        d = cvxmod.matrix(d)
        #### parameters

        # da
        X = cvxmod.param("X" + str(z), N, D)
        X.value = d
        X_matrix.append(X)


        #### varibales
    
        # parameter vector
        theta = cvxmod.optvar("theta" + str(z), D)
        thetas.append(theta)


    # contruct objective
    objective = 0
    for (i,X) in enumerate(X_matrix):
        #TODO try abs loss here!
        objective += cvxmod.sum(cvxmod.atoms.square(X*thetas[i]))
        #objective += cvxmod.sum(cvxmod.atoms.abs(X*thetas[i]))

    # add smoothness regularization
    reg_const = float(total_N) / float(M-1)
    for i in xrange(M-1):
        objective += reg_const * cvxmod.sum(cvxmod.atoms.square(thetas[i] - thetas[i+1]))

    print objective

    # create problem                                    
    p = cvxmod.problem(cvxmod.minimize(objective))

    # add constraints
    for i in xrange(M):
        p.constr.append(thetas[i][0] + thetas[i][1] == 1)
    
    
    ###### set values
    p.solve()
    

    # wrap up result
    ellipse_stack = {}

    active_layers = dat.keys()
    assert len(active_layers) == M

    for i in xrange(M):

        theta_ = numpy.array(cvxmod.value(thetas[i]))
        z_layer = active_layers[i]
        ellipse_stack[z_layer] = conic_to_ellipse(theta_)
        ellipse_stack[z_layer].cz = z_layer

    return ellipse_stack
Example #8
0
def fit_ellipse_stack_abs(dx, dy, dz, di):
    """
    fit ellipoid using squared loss

    idea to learn all stacks together including smoothness
    """

    # sanity check
    assert len(dx) == len(dy)
    assert len(dx) == len(dz)
    assert len(dx) == len(di)

    # unique zs
    dat = defaultdict(list)

    # resort data
    for idx in range(len(dx)):
        dat[dz[idx]].append( [dx[idx], dy[idx], di[idx]] )

    # init ret
    ellipse_stack = []
    for idx in range(max(dz)):
        ellipse_stack.append(Ellipse(0, 0, idx, 1, 1, 0))
    

    total_N = len(dx)
    M = len(dat.keys())
    D = 5

    X_matrix = []
    thetas = []
    slacks = []
    eps_slacks = []

    mean_di = float(numpy.mean(di))

    for z in dat.keys():

        x = numpy.array(dat[z])[:,0]
        y = numpy.array(dat[z])[:,1]

        # intensities
        i = numpy.array(dat[z])[:,2]

        # log intensities
        i = numpy.log(i)

        # create matrix
        ity = numpy.diag(i)# / mean_di

        # dimensionality
        N = len(x)
        d = numpy.zeros((N, D))

        d[:,0] = x*x
        d[:,1] = y*y
        #d[:,2] = x*y
        d[:,2] = x
        d[:,3] = y
        d[:,4] = numpy.ones(N)

        #d[:,0] = x*x
        #d[:,1] = y*y
        #d[:,2] = x*y
        #d[:,3] = x
        #d[:,4] = y
        #d[:,5] = numpy.ones(N)

        print "old", d
        # consider intensities
        old_shape = d.shape
        d = numpy.dot(ity, d)
        print "new", d
        assert d.shape == old_shape
    
        print d.shape   
        d = cvxmod.matrix(d)
        #### parameters

        # da
        X = cvxmod.param("X" + str(z), N, D)
        X.value = d
        X_matrix.append(X)


        #### varibales
    
        # parameter vector
        theta = cvxmod.optvar("theta" + str(z), D)
        thetas.append(theta)


    # construct obj
    objective = 0

    # loss term
    for i in xrange(M):
        objective += cvxmod.atoms.norm1(X_matrix[i] * thetas[i])

    # add smoothness regularization
    reg_const = 5 * float(total_N) / float(M-1)

    for i in xrange(M-1):
        objective += reg_const * cvxmod.norm1(thetas[i] - thetas[i+1])


    # create problem                                    
    prob = cvxmod.problem(cvxmod.minimize(objective))

    # add constraints
    """
    for (i,X) in enumerate(X_matrix):
        p.constr.append(X*thetas[i] <= slacks[i])
        p.constr.append(-X*thetas[i] <= slacks[i])

        #eps = 0.5
        #p.constr.append(slacks[i] - eps <= eps_slacks[i])
        #p.constr.append(0 <= eps_slacks[i])
    """

    # add non-degeneracy constraints
    for i in xrange(1, M-1):
        prob.constr.append(thetas[i][0] + thetas[i][1] == 1.0) # A + C = 1

    # pinch ends
    prob.constr.append(cvxmod.sum(thetas[0]) >= -0.01)
    prob.constr.append(cvxmod.sum(thetas[-1]) >= -0.01)

    print prob

    ###### set values
    from cvxopt import solvers
    solvers.options['reltol'] = 1e-1
    solvers.options['abstol'] = 1e-1
    print solvers.options

    prob.solve()
    

    # wrap up result
    ellipse_stack = {}

    active_layers = dat.keys()
    assert len(active_layers) == M


    # reconstruct original parameterization
    for i in xrange(M):

        theta_ = numpy.array(cvxmod.value(thetas[i]))
        z_layer = active_layers[i]
        ellipse_stack[z_layer] = conic_to_ellipse(theta_)
        ellipse_stack[z_layer].cz = z_layer

    return ellipse_stack
Example #9
0
def solve_boosting(out, labels, nu, solver):
    '''
    solve boosting formulation used by gelher and novozin
    
    @param out: matrix (N,F) of predictions (for each f_i) for all examples
    @param y: vector (N,1) label for each example 
    @param p: regularization constant
    '''
    
    
    
    N = out.size[0]
    F = out.size[1]
    
    assert(N==len(labels))
    
    
    norm_fact = 1.0 / (nu * float(N))
    
    print norm_fact
    
    label_matrix = cvxmod.zeros((N,N))
    
    # avoid point-wise product
    for i in xrange(N):
        label_matrix[i,i] = labels[i] 
    
    
    #### parameters
    
    f = cvxmod.param("f", N, F)
    
    y = cvxmod.param("y", N, N, symm=True)
    
    norm = cvxmod.param("norm", 1) 
    
    #### varibales
    
    # rho
    rho = cvxmod.optvar("rho", 1)
    
    # dim = (N x 1)
    chi = cvxmod.optvar("chi", N)
    
    # dim = (F x 1)
    beta = cvxmod.optvar("beta", F)
    
    
    #objective = -rho + cvxmod.sum(chi) * norm_fact + square(norm2(beta)) 
    objective = -rho + cvxmod.sum(chi) * norm_fact
    
    print objective
    
    # create problem                                    
    p = cvxmod.problem(cvxmod.minimize(objective))
    
    
    # create contraint for probability simplex
    #p.constr.append(beta |cvxmod.In| probsimp(F))
    p.constr.append(cvxmod.sum(beta)==1.0)
    #p.constr.append(square(norm2(beta)) <= 1.0)
    p.constr.append(beta >= 0.0)
    
    
    #    y       f     beta          y    f*beta      y*f*beta
    # (N x N) (N x F) (F x 1) --> (N x N) (N x 1) --> (N x 1)
    p.constr.append(y * (f * beta) + chi >= rho)
    
    
    ###### set values
    f.value = out
    y.value = label_matrix
    norm.value = norm_fact 
    
    p.solve(lpsolver=solver)
    

    weights = numpy.array(cvxmod.value(beta))
    
    #print weights
    
    cvxmod.printval(chi)
    cvxmod.printval(beta)
    cvxmod.printval(rho)
    

    return p
Example #10
0
def solve_svm(out, labels, nu, solver):
    '''
    solve boosting formulation used by gelher and nowozin
    
    @param out: matrix (N,F) of predictions (for each f_i) for all examples
    @param labels: vector (N,1) label for each example 
    @param nu: regularization constant
    @param solver: which solver to use. options: 'mosek', 'glpk'
    '''

    # get dimension
    N = out.size[0]
    F = out.size[1]

    assert N == len(labels), str(N) + " " + str(len(labels))

    norm_fact = 1.0 / (nu * float(N))
    print "normalization factor %f" % (norm_fact)

    # avoid point-wise product
    label_matrix = cvxmod.zeros((N, N))

    for i in xrange(N):
        label_matrix[i, i] = labels[i]

    #### parameters

    f = cvxmod.param("f", N, F)
    y = cvxmod.param("y", N, N, symm=True)
    norm = cvxmod.param("norm", 1)

    #### varibales

    # rho
    rho = cvxmod.optvar("rho", 1)

    # dim = (N x 1)
    chi = cvxmod.optvar("chi", N)

    # dim = (F x 1)
    beta = cvxmod.optvar("beta", F)

    #objective = -rho + cvxmod.sum(chi) * norm_fact + square(norm2(beta))
    objective = -rho + cvxmod.sum(chi) * norm_fact

    print objective

    # create problem
    p = cvxmod.problem(cvxmod.minimize(objective))

    # create contraints for probability simplex
    #p.constr.append(beta |cvxmod.In| probsimp(F))
    p.constr.append(cvxmod.sum(beta) == 1.0)
    p.constr.append(beta >= 0.0)
    p.constr.append(chi >= 0.0)

    # attempt to perform non-sparse boosting
    #p.constr.append(square(norm2(beta)) <= 1.0)

    #    y       f     beta          y    f*beta      y*f*beta
    # (N x N) (N x F) (F x 1) --> (N x N) (N x 1) --> (N x 1)
    p.constr.append(y * (f * beta) + chi >= rho)

    # set values for parameters
    f.value = out
    y.value = label_matrix
    norm.value = norm_fact

    print "solving problem"
    print "============================================="
    print p
    print "============================================="

    # start solver
    p.solve(lpsolver=solver)

    # print variables
    cvxmod.printval(chi)
    cvxmod.printval(beta)
    cvxmod.printval(rho)

    return numpy.array(cvxmod.value(beta))
Example #11
0
def fit_ellipse_linear(x, y):
    """
    fit ellipse stack using absolute loss
    """

    x = numpy.array(x)
    y = numpy.array(y)

    print "shapes", x.shape, y.shape

    assert len(x) == len(y)

    N = len(x)
    D = 6

    dat = numpy.zeros((N, D))
    dat[:,0] = x*x
    dat[:,1] = y*y
    dat[:,2] = y*x
    dat[:,3] = x
    dat[:,4] = y
    dat[:,5] = numpy.ones(N)


    print dat.shape
    dat = cvxmod.matrix(dat)


    # norm
    norm = numpy.zeros((N,N))
    for i in range(N):
        norm[i,i] = numpy.sqrt(numpy.dot(dat[i], numpy.transpose(dat[i])))
    norm = cvxmod.matrix(norm)

    #### parameters

    # data
    X = cvxmod.param("X", N, D)
    Q_grad = cvxmod.param("Q_grad", N, N)


    #### varibales
    
    # parameter vector
    theta = cvxmod.optvar("theta", D)
    
    # dim = (N x 1)
    s = cvxmod.optvar("s", N)
    
    # simple objective 
    objective = cvxmod.sum(s)
    
    # create problem                                    
    p = cvxmod.problem(cvxmod.minimize(objective))
    
    # add constraints 
    # (N x D) * (D X 1) = (N x N) * (N X 1)
    p.constr.append(X*theta <= Q_grad*s)
    p.constr.append(-X*theta <= Q_grad*s)
    
    #p.constr.append(theta[4] == 1)
    # trace constraint
    p.constr.append(theta[0] + theta[1] == 1)
    
    ###### set values
    X.value = dat
    Q_grad.value = norm
    #solver = "mosek" 
    #p.solve(lpsolver=solver)
    p.solve()
    
    cvxmod.printval(theta)
    theta_ = numpy.array(cvxmod.value(theta))
    ellipse = conic_to_ellipse(theta_)

    return ellipse
Example #12
0
def fit_ellipse_eps_insensitive(x, y):
    """
    fit ellipse using epsilon-insensitive loss
    """

    x = numpy.array(x)
    y = numpy.array(y)

    print "shapes", x.shape, y.shape

    assert len(x) == len(y)

    N = len(x)
    D = 5

    dat = numpy.zeros((N, D))
    dat[:,0] = x*x
    dat[:,1] = y*y
    #dat[:,2] = y*x
    dat[:,2] = x
    dat[:,3] = y
    dat[:,4] = numpy.ones(N)


    print dat.shape
    dat = cvxmod.matrix(dat)
    #### parameters

    # data
    X = cvxmod.param("X", N, D)

    # parameter for eps-insensitive loss
    eps = cvxmod.param("eps", 1)


    #### varibales

    # parameter vector
    theta = cvxmod.optvar("theta", D)

    # dim = (N x 1)
    s = cvxmod.optvar("s", N)

    t = cvxmod.optvar("t", N)

    # simple objective 
    objective = cvxmod.sum(t)
    
    # create problem                                    
    p = cvxmod.problem(cvxmod.minimize(objective))
    
    # add constraints 
    # (N x D) * (D X 1) = (N X 1)
    p.constr.append(X*theta <= s)
    p.constr.append(-X*theta <= s)
    
    p.constr.append(s - eps <= t)
    p.constr.append(0 <= t)
    
    #p.constr.append(theta[4] == 1)
    # trace constraint
    p.constr.append(theta[0] + theta[1] == 1)
    
    ###### set values
    X.value = dat
    eps.value = 0.0
    #solver = "mosek" 
    #p.solve(lpsolver=solver)
    p.solve()
    
    cvxmod.printval(theta)
    theta_ = numpy.array(cvxmod.value(theta))
    ellipse = conic_to_ellipse(theta_)

    return ellipse
Example #13
0
def solve_boosting(out, labels, nu, solver):
    '''
    solve boosting formulation used by gelher and novozin
    
    @param out: matrix (N,F) of predictions (for each f_i) for all examples
    @param y: vector (N,1) label for each example 
    @param p: regularization constant
    '''
    
    
    
    N = out.size[0]
    F = out.size[1]
    
    assert(N==len(labels))
    
    
    norm_fact = 1.0 / (nu * float(N))
    
    print norm_fact
    
    label_matrix = cvxmod.zeros((N,N))
    
    # avoid point-wise product
    for i in xrange(N):
        label_matrix[i,i] = labels[i] 
    
    
    #### parameters
    
    f = cvxmod.param("f", N, F)
    
    y = cvxmod.param("y", N, N, symm=True)
    
    norm = cvxmod.param("norm", 1) 
    
    #### varibales
    
    # rho
    rho = cvxmod.optvar("rho", 1)
    
    # dim = (N x 1)
    chi = cvxmod.optvar("chi", N)
    
    # dim = (F x 1)
    beta = cvxmod.optvar("beta", F)
    
    
    #objective = -rho + cvxmod.sum(chi) * norm_fact + square(norm2(beta)) 
    objective = -rho + cvxmod.sum(chi) * norm_fact
    
    print objective
    
    # create problem                                    
    p = cvxmod.problem(cvxmod.minimize(objective))
    
    
    # create contraint for probability simplex
    #p.constr.append(beta |cvxmod.In| probsimp(F))
    p.constr.append(cvxmod.sum(beta)==1.0)
    #p.constr.append(square(norm2(beta)) <= 1.0)
    p.constr.append(beta >= 0.0)
    
    
    #    y       f     beta          y    f*beta      y*f*beta
    # (N x N) (N x F) (F x 1) --> (N x N) (N x 1) --> (N x 1)
    p.constr.append(y * (f * beta) + chi >= rho)
    
    
    ###### set values
    f.value = out
    y.value = label_matrix
    norm.value = norm_fact 
    
    p.solve(lpsolver=solver)
    

    weights = numpy.array(cvxmod.value(beta))
    
    #print weights
    
    cvxmod.printval(chi)
    cvxmod.printval(beta)
    cvxmod.printval(rho)
    

    return p