Esempio n. 1
0
def ecis_fit(feature_matrix, normalized_E, mu, weights = None):

    A = feature_matrix.copy()
    f = normalized_E.copy()

#     A = A[0:150,:]
#     f = f[0:150]

    if weights is None:
        weights = np.ones(len(f))


    A_w = A * weights[:, None] ** 0.5
    f_w = f * weights ** 0.5

    from l1regls import l1regls, solvers
    solvers.options['show_progress'] = False
    from cvxopt import matrix
    A1 = matrix(A)
    b = matrix(f * mu)
    ecis = (np.array(l1regls(A1, b)) / mu).flatten()

    ce_energies = np.dot(A, ecis)

    rmse = np.average((ce_energies - f)**2)**0.5
#     print('RMSE = (in meV)', rmse*1000/2)
    return ecis, rmse
Esempio n. 2
0
def Bayes_l1_opt(A, f, mu, cov):
    Abar = np.dot(np.linalg.pinv(np.transpose(A)),
                  (np.dot(np.transpose(A), A) + cov))

    solvers.options['show_progress'] = False
    A1 = matrix(Abar)
    b = matrix(f * mu)
    ecis = (np.array(l1regls(A1, b)) / mu).flatten()
    return ecis
Esempio n. 3
0
    def test_l1regls(self):
        from cvxopt import normal, setseed
        import l1regls
        setseed(100)
        m,n = 250,500
        A = normal(m,n)
        b = normal(m,1)

        x = l1regls.l1regls(A,b)
        # Check optimality conditions (list should be empty, e.g., False)
        self.assertFalse([t for t in zip(A.T*(A*x-b),x) if abs(t[1])>1e-6 and abs(t[0]) > 1.0])
Esempio n. 4
0
def l1_opt(A, f, mu):

    #     A = A[0:150,:]

    A_w = A
    f_w = f

    solvers.options['show_progress'] = False
    A1 = matrix(A)
    b = matrix(f * mu)
    ecis = (np.array(l1regls(A1, b)) / mu).flatten()
    return ecis
Esempio n. 5
0
def solve_lasso(A_val, y_val, hparams):
    if hparams.lasso_solver == 'sklearn':
        lasso_est = Lasso(alpha=hparams.lmbd)
        lasso_est.fit(A_val.T, y_val.reshape(hparams.num_measurements))
        x_hat = lasso_est.coef_
        x_hat = np.reshape(x_hat, [-1])
    if hparams.lasso_solver == 'cvxopt':
        A_mat = matrix(A_val.T)
        y_mat = matrix(y_val)
        x_hat_mat = l1regls(A_mat, y_mat)
        x_hat = np.asarray(x_hat_mat)
        x_hat = np.reshape(x_hat, [-1])
    return x_hat
Esempio n. 6
0
    def test_l1regls(self):
        from cvxopt import normal, setseed
        import l1regls
        setseed(100)
        m, n = 250, 500
        A = normal(m, n)
        b = normal(m, 1)

        x = l1regls.l1regls(A, b)
        # Check optimality conditions (list should be empty, e.g., False)
        self.assertFalse([
            t for t in zip(A.T * (A * x - b), x)
            if abs(t[1]) > 1e-6 and abs(t[0]) > 1.0
        ])
Esempio n. 7
0
 def solve_lasso(A_val, y_val, hparams):  #(n,m), (1,m)
     if hparams.lasso_solver == 'sklearn':
         lasso_est = Lasso(alpha=hparams.lmbd)
         lasso_est.fit(A_val.T, y_val.reshape(hparams.num_measurements))
         x_hat = lasso_est.coef_
         x_hat = np.reshape(x_hat, [-1])
     elif hparams.lasso_solver == 'cvxopt':
         A_mat = matrix(A_val.T)  #[m,n]
         y_mat = matrix(y_val.T)  ###
         x_hat_mat = l1regls(A_mat, y_mat)
         x_hat = np.asarray(x_hat_mat)
         x_hat = np.reshape(x_hat, [-1])  #[n, ]
     elif hparams.lasso_solver == 'pyunlocbox':
         tau = hparams.tau
         f1 = functions.norm_l1(lambda_=tau)
         f2 = functions.norm_l2(y=y_val.T, A=A_val.T)
         if hparams.model_type == 'compressing':
             if hparams.image_mode == '3D':
                 A_real = np.random.normal(
                     size=(int(hparams.num_measurements / 3),
                           sig_shape))
             else:
                 A_real = np.random.normal(
                     size=(hparams.num_measurements, sig_shape))
             step = 0.5 / np.linalg.norm(A_real, ord=2)**2
         else:
             step = 0.5 / np.linalg.norm(A_val, ord=2)**2
         solver = solvers.forward_backward(step=step)
         x0 = np.zeros((sig_shape, 1))
         ret = solvers.solve([f1, f2],
                             x0,
                             solver,
                             rtol=1e-4,
                             maxit=3000)
         x_hat_mat = ret['sol']
         x_hat = np.asarray(x_hat_mat)
         x_hat = np.reshape(x_hat, [-1])  #[n, ]
     return x_hat
Esempio n. 8
0
# If you have
# https://github.com/cvxopt/cvxopt/blob/master/examples/doc/chap8/l1regls.py
# and cvxopt installed, try the following code to make sure that
# FISTA is about ten times faster than the l1regls with CVXOPT for the same LASSO regression problem.
l1regls_cvxopt = False

if l1regls_cvxopt:
    from l1regls import l1regls
    from cvxopt import matrix
    A = matrix(A) / l
    b = matrix(b)

    print("Running l1regls")
    t0 = time()
    x_est = l1regls(A, b) / l
    print('done in %.2fs.' % (time() - t0))
    A = A * l
    print('# nonzeros = %d' % (np.count_nonzero(x_est)))
    #print('supprt = ')
    #print(np.nonzero(x_est)[0])
    print(
        'rel. error of x = %.2e' %
        (linalg.norm(np.array(x_est).ravel() - x_true) / linalg.norm(x_true)))
    print('rel. reconst. error = %.2e' %
          (linalg.norm(np.array(A * x_est).ravel() - b_true) / normb))

    plt.figure()
    #plt.stem(x_true, markerfmt='g.')
    plt.plot(np.arange(n),
             x_true,
Esempio n. 9
0
def main():
    stopWords = loadStopWords(stopWordFile)
    numArticles = zeroes(len(signs))
    wordOccurence = zeroes(len(signs))
    allWords = []
    for idSign in range(len(signs)):
        sign = signs[idSign]
        archiveDir = inputDir + '/' + sign
        paths = []
        for df in map(lambda x: archiveDir + '/' + x, os.listdir(archiveDir)):
            if os.path.isdir(df):
                paths.append(df)
            else:
                print df, ': not a dir'
        
        wordOccurence[idSign] = {}
        for path in paths:
            for f in os.listdir(path):
                if f == '.DS_Store':
                    continue
                if fromTime and f[:8] < fromTime:
                    continue
                if toTime and f[:8] > toTime:
                    continue
                article = readFullDoc(path, f)
                numArticles[idSign] = numArticles[idSign] + 1
                seenWords = []
                for word in article.split():
                    word = word.strip()
                    if word in stopWords or len(word) <= 1 or word in seenWords:
                        continue
                    if not(word in allWords):
                        allWords.append(word)
                    seenWords.append(word) 
                    if not word in wordOccurence[idSign]:
                        wordOccurence[idSign][word] = 0
                    wordOccurence[idSign][word] = wordOccurence[idSign][word] + 1
    
    allWords = sorted(allWords)
    
    wordID = {}
    for i in range(len(allWords)):
        wordID[allWords[i]] = i
        
    #Init Big Matrix
    A = zeroes(len(signs))
    for i in range(len(A)):
        A[i] = zeroes(len(allWords))
        
    for signID in range(len(A)):
        for word in wordOccurence[signID]:
            A[signID][wordID[word]] = wordOccurence[signID][word]
        A[signID].append(1) #ONE for Bias
        
# Transpose A        
    AT = zeroes(len(A[0]))
    for i in range(len(AT)):
        AT[i] = zeroes(len(A))

    for i in range(len(AT)):
        for j in range(len(AT[i])):
            AT[i][j] = A[j][i]
    
#    using CVXOPT
    Am = matrix(AT)
    distinctive = zeroes(len(A))
    for signID in range(len(A)):
        m = len(A)
        n = len(A[signID])
        b = [zeroes(len(A))]
        for i in range(len(A)):
            if i == signID:
                b[0][i] = 1
            else:
                b[0][i] = 0
        bm = matrix(b)
        x = l1regls(Am,bm)
        
        distinctive[signID] = {}
        for i in range(len(allWords)):
            if (abs(x[i]) > 1e-6):
                distinctive[signID][allWords[i]] = x[i]
                
# Output
    for signID in range(len(signs)):
        dir = outputDir + '/' + signs[signID]
        if not os.path.exists(dir):
            os.makedirs(dir)
        with open(dir + '/' + signs[signID] + '.txt', 'w') as file:
            dict = sortedValueDict(distinctive[signID])
            for item in dict:
                file.write(item[0] + ' ' + str(item[1]) + '\n')
                
    with open(outputDir + '/all.txt', 'w') as file:
        for signID in range(len(signs)):
            dict = sortedValueDict(distinctive[signID])
            file.write('=======' + '\n' + signs[signID] + '\n')
            for item in dict:
                file.write(item[0] + ' ' + str(item[1]) + '\n')
Esempio n. 10
0
def run():
    for i in range(100):
        l1.l1regls(A, b)
def run():
    for i in range(100):
        l1.l1regls(A, b)
	def solve_SAT2(self,cnf,number_of_variables,number_of_clauses):
		#Solves CNFSAT by a Polynomial Time Approximation scheme:
		#	- Encode each clause as a linear equation in n variables: missing variables and negated variables are 0, others are 1
		#	- Solve previous system of equations by least squares algorithm to fit a line
		#	- Variable value above 0.5 is set to 1 and less than 0.5 is set to 0
		#	- Rounded of assignment array satisfies the CNFSAT with high probability
		#Returns: a tuple with set of satisfying assignments
		satass=[]
		x=[]
		self.solve_SAT(cnf,number_of_variables,number_of_clauses)
		for clause in self.cnfparsed:
			equation=[]
			for n in xrange(number_of_variables):
				equation.append(0)
			#print "clause:",clause
			for literal in clause:
				if literal[0] != "!":
					equation[int(literal[1:])-1]=1
				else:
					equation[int(literal[2:])-1]=0
			self.equationsA.append(equation)
		for n in xrange(number_of_clauses):
			self.equationsB.append(1)
		a = np.array(self.equationsA)
                b = np.array(self.equationsB)
		init_guess = []
		for n in xrange(number_of_variables):
			init_guess.append(0.00000000001)
		initial_guess = np.array(init_guess)
		self.A=a
		self.B=b
		#print "a:",a
		#print "b:",b
                #print "a.shape:",a.shape
                #print "b.shape:",b.shape
		matrixa=matrix(a,tc='d')
		matrixb=matrix(b,tc='d')

		x=None
		if number_of_variables == number_of_clauses:
			if self.Algorithm=="lsqr()":
				#x = np.dot(np.linalg.inv(a),b)
				#x = gmres(a,b) 
				#x = lgmres(a,b) 
				#x = minres(a,b) 
				#x = bicg(a,b) 
				#x = cg(a,b) 
				#x = cgs(a,b) 
				#x = bicgstab(a,b)
                		x = lsqr(a,b,atol=0,btol=0,conlim=0,show=True)
			if self.Algorithm=="lapack()":
				try:
					x = gesv(matrixa,matrixb)
					#x = gels(matrixa,matrixb)
					#x = sysv(matrixa,matrixb)
					#x = getrs(matrixa,matrixb)
					x = [matrixb]
				except:
					print "Exception:",sys.exc_info()
					return None 
			if self.Algorithm=="l1regls()":
				l1x = l1regls(matrixa,matrixb)
				ass=[]
				x=[]
				for n in l1x:
					ass.append(n)
				x.append(ass)
			if self.Algorithm=="lsmr()":
                		x = lsmr(a,b,atol=0,btol=0,conlim=0,show=True,x0=initial_guess)
		else:
			if self.Algorithm=="solve()":
                		x = solve(a,b)
			if self.Algorithm=="lstsq()":
                		x = lstsq(a,b,lapack_driver='gelsy')
			if self.Algorithm=="lsqr()":
                		x = lsqr(a,b,atol=0,btol=0,conlim=0,show=True)
			if self.Algorithm=="lsmr()":
                		#x = lsmr(a,b,atol=0.1,btol=0.1,maxiter=5,conlim=10,show=True)
                		x = lsmr(a,b,atol=0,btol=0,conlim=0,show=True,x0=initial_guess)
			if self.Algorithm=="spsolve()":
				x = dsolve.spsolve(csc_matrix(a),b)
			if self.Algorithm=="pinv2()":
				x=[]
				#pseudoinverse_a=pinv(a)
				pseudoinverse_a=pinv2(a,check_finite=False)
				x.append(matmul(pseudoinverse_a,b))
			if self.Algorithm=="lsq_linear()":
				x = lsq_linear(a,b,lsq_solver='exact')
			if self.Algorithm=="lapack()":
				try:
					#x = gesv(matrixa,matrixb)
					x = gels(matrixa,matrixb)
					#x = sysv(matrixa,matrixb)
					#x = getrs(matrixa,matrixb)
					x = [matrixb]
				except:
					print "Exception:",sys.exc_info()
					return None 
			if self.Algorithm=="l1regls()":
				l1x = l1regls(matrixa,matrixb)
				ass=[]
				x=[]
				for n in l1x:
					ass.append(n)
				x.append(ass)

		print "solve_SAT2(): ",self.Algorithm,": x:",x
		if x is None:
			return None
		cnt=0
		binary_parity=0
		real_parity=0.0
		if rounding_threshold == "Randomized":
			randomized_rounding_threshold=float(random.randint(1,100000))/100000.0
		else:
			min_assignment=min(x[0])
			max_assignment=max(x[0])
			randomized_rounding_threshold=(min_assignment + max_assignment)/2
		print "randomized_rounding_threshold = ", randomized_rounding_threshold
		print "approximate assignment :",x[0]
		for e in x[0]:
			if e > randomized_rounding_threshold:
				satass.append(1)
				binary_parity += 1
			else:
				satass.append(0)
				binary_parity += 0
			real_parity += e
			cnt+=1
		print "solve_SAT2(): real_parity = ",real_parity
		print "solve_SAT2(): binary_parity = ",binary_parity
		return (satass,real_parity,binary_parity,x[0])
Esempio n. 13
0
 def L1(self):
     A = cvxmatrix(self.A)
     Y = cvxmatrix(self.Y)
     X = cvxmatrix(self.X)
     self.L1XX = l1regls(A, Y)
     self.L1err = norm(self.L1XX-X, 2)/norm(self.X,2)
Esempio n. 14
0
    def solve_SAT2(self, cnf, number_of_variables, number_of_clauses):
        #Solves CNFSAT by a Polynomial Time Approximation scheme:
        #	- Encode each clause as a linear equation in n variables: missing variables and negated variables are 0, others are 1
        #	- Solve previous system of equations by least squares algorithm to fit a line
        #	- Variable value above 0.5 is set to 1 and less than 0.5 is set to 0
        #	- Rounded of assignment array satisfies the CNFSAT with high probability
        #Returns: a tuple with set of satisfying assignments
        satass = []
        x = []
        self.solve_SAT(cnf, number_of_variables, number_of_clauses)
        for clause in self.cnfparsed:
            equation = []
            for n in xrange(number_of_variables):
                equation.append(0)
            #print "clause:",clause
            for literal in clause:
                if literal[0] != "!":
                    equation[int(literal[1:]) - 1] = 1
                else:
                    equation[int(literal[2:]) - 1] = 0
            self.equationsA.append(equation)
        for n in xrange(number_of_clauses):
            self.equationsB.append(1)
        a = np.array(self.equationsA)
        b = np.array(self.equationsB)
        init_guess = []
        for n in xrange(number_of_variables):
            init_guess.append(0.00000000001)
        initial_guess = np.array(init_guess)
        self.A = a
        self.B = b
        #print "a:",a
        #print "b:",b
        #print "a.shape:",a.shape
        #print "b.shape:",b.shape
        matrixa = matrix(a, tc='d')
        matrixb = matrix(b, tc='d')

        x = None
        if number_of_variables == number_of_clauses:
            if self.Algorithm == "lsqr()":
                #x = np.dot(np.linalg.inv(a),b)
                #x = gmres(a,b)
                #x = lgmres(a,b)
                #x = minres(a,b)
                #x = bicg(a,b)
                #x = cg(a,b)
                #x = cgs(a,b)
                #x = bicgstab(a,b)
                x = lsqr(a, b, atol=0, btol=0, conlim=0, show=True)
            if self.Algorithm == "lapack()":
                try:
                    x = gesv(matrixa, matrixb)
                    #x = gels(matrixa,matrixb)
                    #x = sysv(matrixa,matrixb)
                    #x = getrs(matrixa,matrixb)
                    x = [matrixb]
                except:
                    print "Exception:", sys.exc_info()
                    return None
            if self.Algorithm == "l1regls()":
                l1x = l1regls(matrixa, matrixb)
                ass = []
                x = []
                for n in l1x:
                    ass.append(n)
                x.append(ass)
            if self.Algorithm == "lsmr()":
                x = lsmr(a,
                         b,
                         atol=0,
                         btol=0,
                         conlim=0,
                         show=True,
                         x0=initial_guess)
        else:
            if self.Algorithm == "solve()":
                x = solve(a, b)
            if self.Algorithm == "lstsq()":
                x = lstsq(a, b, lapack_driver='gelsy')
            if self.Algorithm == "lsqr()":
                x = lsqr(a, b, atol=0, btol=0, conlim=0, show=True)
            if self.Algorithm == "lsmr()":
                #x = lsmr(a,b,atol=0.1,btol=0.1,maxiter=5,conlim=10,show=True)
                x = lsmr(a,
                         b,
                         atol=0,
                         btol=0,
                         conlim=0,
                         show=True,
                         x0=initial_guess)
            if self.Algorithm == "spsolve()":
                x = dsolve.spsolve(csc_matrix(a), b)
            if self.Algorithm == "pinv2()":
                x = []
                #pseudoinverse_a=pinv(a)
                pseudoinverse_a = pinv2(a, check_finite=False)
                x.append(matmul(pseudoinverse_a, b))
            if self.Algorithm == "lsq_linear()":
                x = lsq_linear(a, b, lsq_solver='exact')
            if self.Algorithm == "lapack()":
                try:
                    #x = gesv(matrixa,matrixb)
                    x = gels(matrixa, matrixb)
                    #x = sysv(matrixa,matrixb)
                    #x = getrs(matrixa,matrixb)
                    x = [matrixb]
                except:
                    print "Exception:", sys.exc_info()
                    return None
            if self.Algorithm == "l1regls()":
                l1x = l1regls(matrixa, matrixb)
                ass = []
                x = []
                for n in l1x:
                    ass.append(n)
                x.append(ass)

        print "solve_SAT2(): ", self.Algorithm, ": x:", x
        if x is None:
            return None
        cnt = 0
        binary_parity = 0
        real_parity = 0.0
        if rounding_threshold == "Randomized":
            randomized_rounding_threshold = float(random.randint(
                1, 100000)) / 100000.0
        else:
            min_assignment = min(x[0])
            max_assignment = max(x[0])
            randomized_rounding_threshold = (min_assignment +
                                             max_assignment) / 2
        print "randomized_rounding_threshold = ", randomized_rounding_threshold
        print "approximate assignment :", x[0]
        for e in x[0]:
            if e > randomized_rounding_threshold:
                satass.append(1)
                binary_parity += 1
            else:
                satass.append(0)
                binary_parity += 0
            real_parity += e
            cnt += 1
        print "solve_SAT2(): real_parity = ", real_parity
        print "solve_SAT2(): binary_parity = ", binary_parity
        return (satass, real_parity, binary_parity, x[0])