def fitCoefficients(self,xName,T=[],xData=[]): if (len(T)!=len(xData)): raise (ValueError("Error: There has to be the same number of temperature and data points.")) if len(T)<self._minPoints: raise (ValueError("Error: You should use at least "+str(self._minPoints)+" points.")) def fun(coefficients,xName,T,xData): # Values for conductivity are very small, # algorithms prefer larger values if xName=='L': calculated = numpy.array([self._PropsFit(coefficients,xName,T=Ti) for Ti in T])*1e6 data = numpy.array(xData)*1e6 # Fit logarithms for viscosity and saturation pressure elif xName=='V' or xName=='Psat': calculated = numpy.log([self._PropsFit(coefficients,xName,T=Ti) for Ti in T]) data = numpy.log(xData) else: calculated = numpy.array([self._PropsFit(coefficients,xName,T=Ti) for Ti in T]) data = numpy.array(xData) res = numpy.sum((calculated-data)**2.) return res initValues = self.getCoefficients(xName)[:] arguments = (xName,T,xData) options = {'maxiter': 1e4, 'maxfev': 1e6} res = minimize(fun, initValues, method='Powell', args=arguments, tol=1e-15, options=options) if res.success: return res.x else: print res return False
def train(date=0, theta=0): if date == 0: filename = MERGEDTRAININGFILE filename = DIR + r'\trainingset_3_8_5.merged.csv' filename = ur'F:\AliRecommendHomeworkData\1000.csv' else: filename = TRAININGFILE % date raw_data = load(filename) #raw_data = expendX(raw_data) #加上除法特征 print 'data loaded,len=%d' % len(raw_data) posi_dup = POSI_DUP raw_data = np.array(raw_data) if NEGA_RATE > 0 or posi_dup > 1: posi = raw_data[raw_data[:, 2] == 1, :] nega = raw_data[raw_data[:, 2] == 0, :] np.random.shuffle(nega) raw_data = nega[:NEGA_RATE * posi_dup * len(posi), :] for i in range(posi_dup): raw_data = np.append(raw_data, posi, axis=0) #np.random.shuffle(raw_data) x, y, online, result = getXY(raw_data) print ur'扩充完毕' print ur'正例:%d, 负例:%d' % (sum(y), sum(y == 0)) print ur'在线商品:%d, 在线商品中正例%d' % (sum(online), sum(y[online == 1])) x = mapFeature(x) print ur'正例扩充%d倍,训练集总大小:%d,维度:%d' % (posi_dup, len(x), x.shape[1]) if theta == 0: theta = np.zeros(x.shape[1]) else: theta = readTheta() args = (x, y, pLambda) res = minimize(costFunctionReg, theta, jac=gradFunctionReg, args=args, method='BFGS') theta = res.x p = predict(theta, x) writeTheta(theta) with open(MAINDIR + "\\thetas\\%d.txt" % date, 'w') as f: f.write(','.join([str(item) for item in theta])) accuracy = np.mean(p == y) * 100 print 'Train Accuracy:%f' % accuracy print_analyse(sum(p[y == 1]), sum(p), sum(y)) pass
def train(date=0,theta=0): if date == 0: filename = MERGEDTRAININGFILE filename = DIR + r'\trainingset_3_8_5.merged.csv' filename = ur'F:\AliRecommendHomeworkData\1000.csv' else: filename = TRAININGFILE % date raw_data = load(filename) #raw_data = expendX(raw_data) #加上除法特征 print 'data loaded,len=%d' % len(raw_data) posi_dup = POSI_DUP raw_data = np.array(raw_data) if NEGA_RATE > 0 or posi_dup > 1: posi = raw_data[raw_data[:,2] == 1,:] nega = raw_data[raw_data[:,2] == 0,:] np.random.shuffle(nega) raw_data = nega[:NEGA_RATE * posi_dup * len(posi),:] for i in range(posi_dup): raw_data = np.append(raw_data,posi,axis=0) #np.random.shuffle(raw_data) x,y,online,result = getXY(raw_data) print ur'扩充完毕' print ur'正例:%d, 负例:%d' % (sum(y),sum(y == 0)) print ur'在线商品:%d, 在线商品中正例%d' % (sum(online),sum(y[online == 1])) x = mapFeature(x) print ur'正例扩充%d倍,训练集总大小:%d,维度:%d' % (posi_dup,len(x),x.shape[1]) if theta == 0: theta = np.zeros(x.shape[1]) else: theta = readTheta() args = (x,y,pLambda) res = minimize(costFunctionReg,theta,jac=gradFunctionReg,args=args,method='BFGS') theta = res.x p = predict(theta,x) writeTheta(theta) with open(MAINDIR + "\\thetas\\%d.txt" % date,'w') as f: f.write(','.join([str(item) for item in theta])) accuracy = np.mean(p == y) * 100 print 'Train Accuracy:%f' % accuracy print_analyse(sum(p[y == 1]),sum(p),sum(y)) pass
def train(): print ur'正在载入文件:', filename raw_data = load(filename) #raw_data = expendX(raw_data) #加上除法特征 print ur'数据载入成功,len=%d' % len(raw_data) posi_dup = POSI_DUP raw_data = np.array(raw_data) if NEGA_RATE > 0 or posi_dup > 1: posi = raw_data[raw_data[:, 2] == 1, :] nega = raw_data[raw_data[:, 2] == 0, :] np.random.shuffle(nega) raw_data = nega[:NEGA_RATE * posi_dup * len(posi), :] for i in range(posi_dup): raw_data = np.append(raw_data, posi, axis=0) np.random.shuffle(raw_data) x, y, online, result = getXY(raw_data) print ur'扩充完毕' print ur'正例:%d, 负例:%d' % (sum(y), sum(y == 0)) print ur'在线商品:%d, 在线商品中正例%d' % (sum(online), sum(y[online == 1])) x = mapFeature(x) print ur'正例扩充%d倍,训练集总大小:%d,维度:%d' % (posi_dup, len(x), x.shape[1]) theta = np.zeros(x.shape[1]) args = (x, y, pLambda) res = minimize(costFunctionReg, theta, jac=gradFunctionReg, args=args, method='BFGS') theta = res.x writeTheta(theta) p = predict(theta, x) accuracy = np.mean(p == y) * 100 print 'Train Accuracy:%f' % accuracy print_analyse(sum(p[y == 1]), sum(p), sum(y)) test(theta) pass
def fit(self, X, y): # For loss function = l2 print("Yugal") if self.lf == 'l2': Xnew = numpy.vstack((X,numpy.ones(X.shape[0]))).T self.params = numpy.linalg.pinv(Xnew).dot(y) return None # For loss function = huber a = self.lfp p1 = numpy.random.uniform(0.001,0.0008) p2 = numpy.random.uniform(0.001,0.0008) p = [p1,p2] self.params = p self.params = optimize.minimize(lambda x: self.huber_objfunc(X, y, x, a), self.params, method = 'BFGS', jac = lambda x: self.huber_objfunc_derivative(X, y, x, a)).x return None
def train(): print ur'正在载入文件:',filename raw_data = load(filename) #raw_data = expendX(raw_data) #加上除法特征 print ur'数据载入成功,len=%d' % len(raw_data) posi_dup = POSI_DUP raw_data = np.array(raw_data) if NEGA_RATE > 0 or posi_dup > 1: posi = raw_data[raw_data[:,2] == 1,:] nega = raw_data[raw_data[:,2] == 0,:] np.random.shuffle(nega) raw_data = nega[:NEGA_RATE * posi_dup * len(posi),:] for i in range(posi_dup): raw_data = np.append(raw_data,posi,axis=0) np.random.shuffle(raw_data) x,y,online,result = getXY(raw_data) print ur'扩充完毕' print ur'正例:%d, 负例:%d' % (sum(y),sum(y == 0)) print ur'在线商品:%d, 在线商品中正例%d' % (sum(online),sum(y[online == 1])) x = mapFeature(x) print ur'正例扩充%d倍,训练集总大小:%d,维度:%d' % (posi_dup,len(x),x.shape[1]) theta = np.zeros(x.shape[1]) args = (x,y,pLambda) res = minimize(costFunctionReg,theta,jac=gradFunctionReg,args=args,method='BFGS') theta = res.x writeTheta(theta) p = predict(theta,x) accuracy = np.mean(p == y) * 100 print 'Train Accuracy:%f' % accuracy print_analyse(sum(p[y == 1]),sum(p),sum(y)) test(theta) pass
def fitCoefficients(self, xName, T=[], xData=[]): if (len(T) != len(xData)): raise (ValueError( "Error: There has to be the same number of temperature and data points." )) if len(T) < self._minPoints: raise (ValueError("Error: You should use at least " + str(self._minPoints) + " points.")) def fun(coefficients, xName, T, xData): # Values for conductivity are very small, # algorithms prefer larger values if xName == 'L': calculated = numpy.array( [self._PropsFit(coefficients, xName, T=Ti) for Ti in T]) * 1e6 data = numpy.array(xData) * 1e6 # Fit logarithms for viscosity and saturation pressure elif xName == 'V' or xName == 'Psat': calculated = numpy.log( [self._PropsFit(coefficients, xName, T=Ti) for Ti in T]) data = numpy.log(xData) else: calculated = numpy.array( [self._PropsFit(coefficients, xName, T=Ti) for Ti in T]) data = numpy.array(xData) res = numpy.sum((calculated - data)**2.) return res initValues = self.getCoefficients(xName)[:] arguments = (xName, T, xData) options = {'maxiter': 1e4, 'maxfev': 1e6} res = minimize(fun, initValues, method='Powell', args=arguments, tol=1e-15, options=options) if res.success: return res.x else: print res return False
def fit_perspective_transform(X,A,guess): X_=np.zeros((len(X),3)) for i in range(len(X)): X_[i][0]=X[i][0] X_[i][1]=X[i][1] X_[i][2]=1.0 X=np.array(X_) A=np.array(A) def y(M): re=0 for n in range(X.shape[0]): u=M.dot(X[n]) u=[u[0]/u[2],u[1]/u[2]] re+=(u[0]-A[n][0])**2+(u[1]-A[n][1])**2 return re def y_multivar(i): return y(np.array([[i[0],i[1],i[2]],[i[3],i[4],i[5]],[i[6],i[7],i[8]]])) def p_y_p_Mij(M,i,j): delta=.001 delMij=np.zeros(M.shape) delMij[i,j]=delta return (y(M+delMij)-y(M))/delta def grad_y_by_M(M): re=np.zeros(M.shape) for i in range(M.shape[0]): for j in range(M.shape[1]): re[i,j]=p_y_p_Mij(M,i,j) return re M=minimize(y_multivar, [guess[0,0],guess[0,1],guess[0,2] ,guess[1,0],guess[1,1],guess[1,2], 0,0,1], args=())['x'] M=np.array([[M[0],M[1],M[2]],[M[3],M[4],M[5]],[M[6],M[7],M[8]]]) M/=M[2,2] return M
def getCoeffsIterative1D(x_in, z_in, eqnType, coeffs, DEBUG=False): if x_in is None: raise ValueError("You did not provide data for the x-values.") if z_in is None: raise ValueError("You did not provide data for the z-values.") if eqnType is None: raise ValueError("You did not provide data for eqnType.") if coeffs is None: raise ValueError("You did not provide data for the coefficients.") if DEBUG is None: raise ValueError("You did not provide data for DEBUG.") sErr = None #fit = "Powell" # use Powell's algorithm #fit = "BFGS" # use Broyden-Fletcher-Goldfarb-Shanno #fit = "LMA" # use the Levenberg-Marquardt algorithm from curve_fit fit = ["LMA","Powell","BFGS"] # First try LMA, use others as fall-back # make sure that we use other routines for polynomials if (eqnType==IncompressibleData.INCOMPRESSIBLE_POLYNOMIAL) or \ (eqnType==IncompressibleData.INCOMPRESSIBLE_EXPPOLYNOMIAL) : raise ValueError("Please use the specific polynomial functions, they are much better.") expLog = False # Fitting the logarithm of z_in? if (eqnType==IncompressibleData.INCOMPRESSIBLE_EXPONENTIAL or eqnType==IncompressibleData.INCOMPRESSIBLE_LOGEXPONENTIAL): expLog = True xData = np.array(x_in.flat) if expLog: zData = np.log(np.clip(z_in.flat,1e-10,IncompressibleData.maxLin)) else: zData = np.array(z_in.flat) # Remove np.nan elements mask = np.isfinite(zData) xData = xData[mask] zData = zData[mask] # The residual function def fun(coefficients,xArray,yArray): """ This function takes the coefficient array and x and y data. It evaluates the function and returns the sum of the squared residuals if yArray is not equal to None """ # No offset and no Tbase etc for 1D functions! calculated = IncompressibleData.baseFunc(xArray, y=0.0, xbase=0.0, ybase=0.0, eqnType=eqnType, c=coefficients) if expLog: calculated = np.log(calculated) if yArray is None: return calculated data = yArray res = np.sum(np.square(calculated-data)) return res # Loop through the list of algorithms with basic settings to keep track of our efforts success = False counter = 0 tolerance = 1e-16 while (not success): algorithm = fit[counter] #fit = "LMA" # use the Levenberg-Marquardt algorithm from curve_fit if algorithm=="LMA": def func(xVector, *coefficients): return fun(np.array(coefficients), xVector, None) #return self.baseFunction(xVector, 0.0, 0.0, 0.0, np.array(coefficients)) #np.array([self._PropsFit(coefficients,xName,T=Ti) for Ti in T]) try: #print func(xData, coeffs_start) # Do the actual fitting popt, pcov = curve_fit(func, xData, zData, p0=coeffs, ftol=tolerance) if np.any(popt!=coeffs): success = True if DEBUG: print("Fit succeeded with: {0}".format(algorithm)) sErr = zData - func(xData, popt) #print "Fit succeeded for "+fit[counter]+": " #print "data: {0}, func: {1}".format(yData[ 2],func(xData[ 2], popt)) #print "data: {0}, func: {1}".format(yData[ 6],func(xData[ 6], popt)) #print "data: {0}, func: {1}".format(yData[-1],func(xData[-1], popt)) #if DEBUG: print("Estimated covariance of parameters: {0}".format(pcov)) #ssErr = np.sqrt(np.diag(pcov)).sum() #ssTot = ((zData-zData.mean())**2).sum() #r2 = 1-(ssErr/ssTot ) #print("\n r2 FMA: ",r2.shape,r2,"\n") return popt,sErr else: if DEBUG: print("Fit failed for {0}.".format(algorithm)) if DEBUG: sys.stdout.flush() success = False except RuntimeError as e: if DEBUG: print("Exception using "+algorithm+": "+str(e)) if DEBUG: sys.stdout.flush() success = False #fit = "MIN" # use a home-made minimisation with Powell and Broyden-Fletcher-Goldfarb-Shanno elif algorithm=="Powell" or algorithm=="BFGS": arguments = (xData,zData) #options = {'maxiter': 1e2, 'maxfev': 1e5} try: res = minimize(fun, coeffs, method=algorithm, args=arguments, tol=tolerance) if res.success: success = True if DEBUG: print("Fit succeeded with: {0}".format(algorithm)) sErr = zData - fun(np.array(res.x), xData, None) #if res.has_key('fvec'): #ssErr = (res['fvec']**2).sum() #ssTot = ((zData-zData.mean())**2).sum() #r2 = 1-(ssErr/ssTot ) #print("\n r2 : ",r2.shape,r2,algorithm,"\n") return res.x,sErr else: if DEBUG: print("Fit failed for {0}.".format(algorithm)) if DEBUG: sys.stdout.flush() success = False except RuntimeError as e: if DEBUG: print("Exception using "+algorithm+": "+str(e)) if DEBUG: sys.stdout.flush() success = False # Something went wrong, probably a typo in the algorithm selector else: raise (ValueError("Error: You used an unknown fit method.")) if counter<len(fit)-1: #print("Fit did not succeed with {0}, reducing tolerance to {1}.".format(algorithm,tol)) success = False counter += 1 elif tolerance<1e-3: tolerance *= 1e2 if DEBUG: print("Fit did not succeed, reducing tolerance to {0}.".format(tolerance)) success = False counter = 0 else: if DEBUG: print("--------------------------------------------------------------") if DEBUG: print("Fit failed for {0}. ".format(fit)) if DEBUG: print("--------------------------------------------------------------") return coeffs,1
def opt_hyper(gpr, hyperparams, Ifilter=None, maxiter=1000, gradcheck=False, bounds=None, callback=None, # optimizer=OPT.fmin_tnc, gradient_tolerance=1e-8, messages=True, *args, **kw_args): """ Optimize hyperparemters of :py:class:`pygp.gp.basic_gp.GP` ``gpr`` starting from given hyperparameters ``hyperparams``. **Parameters:** gpr : :py:class:`pygp.gp.basic_gp` GP regression class hyperparams : {'covar':logtheta, ...} Dictionary filled with starting hyperparameters for optimization. logtheta are the CF hyperparameters. Ifilter : [boolean] Index vector, indicating which hyperparameters shall be optimized. For instance:: logtheta = [1,2,3] Ifilter = [0,1,0] means that only the second entry (which equals 2 in this example) of logtheta will be optimized and the others remain untouched. bounds : [[min,max]] Array with min and max value that can be attained for any hyperparameter maxiter: int maximum number of function evaluations gradcheck: boolean check gradients comparing the analytical gradients to their approximations ** argument passed onto LML** priors : [:py:class:`pygp.priors`] non-default prior, otherwise assume first index amplitude, last noise, rest:lengthscales """ # optimizer: :py:class:`scipy.optimize` # which scipy optimizer to use? (standard lbfgsb) global __last_lml__ __last_lml__ = 0 # i=0 # mill = {0:'-',1:'\\',2:"|",4:'-',3:'/'} # def callback(x): # i = (i+1)%5 # import sys; # sys.stdout.flush() # sys.stdout.write("optimizing...{}\r".format(mill[i])) def f(x): x_ = X0 x_[Ifilter_x] = x global __last_lml__ __last_lml__ = gpr.LML(param_list_to_dict(x_, param_struct, skeys), *args, **kw_args) # LG.debug("L("+str(x_)+")=="+str(rv)) if SP.isnan(__last_lml__): return 1E6 return __last_lml__ def df(x): x_ = X0 x_[Ifilter_x] = x rv = gpr.LMLgrad(param_list_to_dict(x_, param_struct, skeys), *args, **kw_args) rv = param_dict_to_list(rv, skeys) # LG.debug("dL("+str(x_)+")=="+str(rv)) if not SP.isfinite(rv).all(): # SP.isnan(rv).any(): In = ~SP.isfinite(rv)#SP.isnan(rv) rv[In] = 1E6 return rv[Ifilter_x] # 0. store parameter structure skeys = SP.sort(hyperparams.keys()) param_struct = dict([(name, hyperparams[name].shape) for name in skeys]) # 1. convert the dictionaries to parameter lists X0 = param_dict_to_list(hyperparams, skeys) if Ifilter is not None: Ifilter_x = SP.array(param_dict_to_list(Ifilter, skeys), dtype='bool') else: Ifilter_x = slice(None)#SP.ones(len(X0), dtype='bool') # 2. bounds if bounds is not None: # go through all hyperparams and build bound array (flattened) _b = [] for key in skeys: if key in bounds.keys(): _b.extend(bounds[key]) else: _b.extend([(-SP.inf, +SP.inf)] * hyperparams[key].size) bounds = SP.array(_b) bounds = bounds[Ifilter_x] pass # 2. set starting point of optimization, truncate the non-used dimensions x = X0.copy()[Ifilter_x] LG.debug("startparameters for opt:" + str(x)) if gradcheck: checkgrad(f, df, x, hyper_names=None) LG.info("check_grad (pre) (Enter to continue):" + str(OPT.check_grad(f, df, x))) raw_input() # # LG.debug("start optimization") # general optimizer interface # note: x is a subset of X, indexing the parameters that are optimized over # Ifilter_x pickes the subest of X, yielding x try: #======================================================================= # TNC: # opt_RV, opt_lml = optimizer(f, x, fprime=df, maxfun=int(maxiter), pgtol=gradient_tolerance, messages=messages, bounds=bounds)[:2] #======================================================================= #======================================================================= # L BFGS: # opt_RV, opt_lml = OPT.fmin_l_bfgs_b(f, x, fprime=df, maxfun=int(maxiter), approx_grad=False, iprint=0, bounds=bounds, factr=10.0 , pgtol=gradient_tolerance)[:2] #======================================================================= #======================================================================= # L_BFGS_B minimize iprint = -1 if messages: iprint = 1 res = minimize(f, x, method="L-BFGS-B", jac=df, bounds=bounds, callback=callback, tol=gradient_tolerance, options=dict(maxiter=maxiter, iprint=iprint)) opt_RV, opt_lml = res.x, res.fun #======================================================================= opt_x = opt_RV except LinAlgError as error: print error opt_x = X0 opt_lml = __last_lml__ # opt_RV = OPT.fmin_bfgs(f, x, fprime=df, maxiter=int(maxiter), disp=1) # opt_x = opt_RV # relate back to X Xopt = X0.copy() Xopt[Ifilter_x] = opt_x # convert into dictionary opt_hyperparams = param_list_to_dict(Xopt, param_struct, skeys) # get the log marginal likelihood at the optimum: # opt_lml = gpr.LML(opt_hyperparams,**kw_args) if gradcheck: checkgrad(f, df, Xopt, hyper_names=None) LG.info("check_grad (post) (Enter to continue):" + str(OPT.check_grad(f, df, opt_x))) pdb.set_trace() # # raw_input() # LG.debug("old parameters:") # LG.debug(str(hyperparams)) # LG.debug("optimized parameters:") # LG.debug(str(opt_hyperparams)) # LG.debug("grad:"+str(df(opt_x))) return [opt_hyperparams, opt_lml]
def optimize(self): frequency_grape = self.D0 + self.AN #-C_known[0] #---------- 作成したいSTATE ----------# n_goal = len(self.target_list) n_tslot = int(np.round(self.pulse_time / self.t_div)) permin = -self.permax rate_hem = 2 #触らない freq_cut = 10 #触らない class_pulse = pulsegen.create_pulse_gen(pulse_type='RNDFOURIER', dyn=None, pulse_params=None) class_pulse.num_tslots = n_tslot #class_pulse.num_ctrl = 1 init_pulse_prescale = class_pulse.gen_pulse() init_pulse = init_pulse_prescale * self.permax / max( init_pulse_prescale) #init_pulse = 1/pulse_time/2.0*(sin(2*pi*(AN)*linspace(t_div,pulse_time,n_tslot))) t_hem = self.pulse_time * rate_hem n_hem = int(round(t_hem / self.t_div)) f_list = linspace(0, 1.0 / self.t_div, n_tslot + n_hem * 2) Φ1_j = np.zeros(n_tslot) Φ2_j = np.zeros(n_tslot) """ Create and return a pulse generator object matching the given type. The pulse generators each produce a different type of pulse, see the gen_pulse function description for details. These are the random pulse options: RND - Independent random value in each timeslot RNDFOURIER - Fourier series with random coefficients RNDWAVES - Summation of random waves RNDWALK1 - Random change in amplitude each timeslot RNDWALK2 - Random change in amp gradient each timeslot These are the other non-periodic options: LIN - Linear, i.e. contant gradient over the time ZERO - special case of the LIN pulse, where the gradient is 0 These are the periodic options SINE - Sine wave SQUARE - Square wave SAW - Saw tooth wave TRIANGLE - Triangular wave If a Dynamics object is passed in then this is used in instantiate the PulseGen, meaning that some timeslot and amplitude properties are copied over. """ def callback(self, pulse, f_list, t_hem, n_hem, n_tslot, freq_cut, permin): #print("========== Before FFT ==========") 描画する時はコメントアウトしない """ plt.plot(pulse) plt.grid(b="on") plt.show() """ pulse_hem_fft = hstack((zeros(n_hem), pulse, zeros(n_hem))) t_list = linspace(self.t_div, self.pulse_time + t_hem * 2, n_tslot + n_hem * 2) FFT = np.fft.fft(pulse_hem_fft) / (n_tslot + n_hem * 2) FFT_cut = hstack( (FFT[0:int((n_tslot + n_hem * 2) / (1 / self.t_div / freq_cut))], zeros( len(FFT) - int((n_tslot + n_hem * 2) / (1 / self.t_div / freq_cut))))) * 2 pulse_hem = np.fft.fft(FFT_cut)[::-1].real pulse = pulse_hem[n_hem:len(pulse_hem) - n_hem] """描画したいときはコメントアウトしない plt.plot(f_list,FFT) plt.plot(f_list,FFT_cut) plt.xlim(0,freq_cut) plt.grid(b="on") plt.show() pulse = np.clip(pulse,permin,self.permax) print("========== After FFT ==========") plt.plot(pulse) plt.grid(b="on") plt.show() """ return pulse #=============================================== GRAPE!!! ===============================================# number_C_known = len(self.C_known) number_H = len(self.C_inhomo) II_C_known = Qobj(qeye(2**number_C_known), dims=[ list(2 * ones(number_C_known)), list(2 * ones(number_C_known)) ]) HhfCz_inhomo = 0 if number_C_known == 0: for i in range(number_H): HhfCz_inhomo = HhfCz_inhomo - self.C_inhomo[i] * tensor( ket2dm(basis(number_H, i)), Sz, III, sigz) else: for i in range(number_H): HhfCz_inhomo = HhfCz_inhomo - self.C_inhomo[i] * tensor( ket2dm(basis(number_H, i)), Sz, III, II_C_known, sigz) if number_H == 0: II_inhomo = Qobj([[1]]) else: II_inhomo = qeye(number_H) H0 = defH0(self.D0, self.QN, self.AN, self.C_known, self.Bz) Hdrift = matrix((tensor(II_inhomo, H0, II) + HhfCz_inhomo).full()) Hdrive1 = matrix( tensor(II_inhomo, Hxdrive(1, 0, 0, 0, self.theta), III, II_C_known, II).full()) Hdrive2 = matrix( tensor(II_inhomo, Hxdrive(0, 1, 0, 0, self.theta), III, II_C_known, II).full()) Hdetuning = matrix(tensor(II_inhomo, Szz, III, II_C_known, II).full()) U0 = matrix(tensor(II_inhomo, III, III, II_C_known, II).full()) for i in range(n_goal): if number_H == 0: self.state_init[i] = matrix( tensor(II_inhomo, self.state_init[i], II / 2.0).full()) self.state_goal[i] = matrix( tensor(II_inhomo, self.state_goal[i], II / 2.0).full()) else: self.state_init[i] = matrix( tensor(II_inhomo / number_H, self.state_init[i], II / 2.0).full()) self.state_goal[i] = matrix( tensor(II_inhomo / number_H, self.state_goal[i], II / 2.0).full()) """ weighting_value_targ = [] for i in range(n_goal): weighting_value_targ = weighting_value_targ+[weighting_value[target_list[i]]] """ bounds = [(permin, self.permax)] * n_tslot pulse = init_pulse state_init = self.state_init t_div = self.t_div state_goal = self.state_goal weighting_value = self.weighting_value def grape_fid_err(pulse): U_b = U0 fid_sum = [] for i in range(n_goal): for j in range(n_tslot): U_b = matrix( sclin.expm(-2 * pi * 1j * (Hdrift - (frequency_grape) * Hdetuning + pulse[j] * Hdrive1) * self.t_div)) * U_b state_final = U_b * state_init[i] * conj(U_b.T) state_final_qobj = Qobj(state_final) state_goal_qobj = Qobj(state_goal[i]) fid_sum = fid_sum + [ fidelity(state_final_qobj, state_goal_qobj) * weighting_value[i] ] fid_ave = sum(fid_sum) / sum(weighting_value) #print("fid = "+str(fid_ave)) 描画する時はコメントアウトしない return 1 - fid_ave def grape_fid_err_grad(pulse): U_rho_j = [] U_lamda_j = [] fid_err_grad_ave = [] U_rho = U0 U_lamda = U0 for j in range(n_tslot): U_rho = matrix( sclin.expm(-2 * pi * 1j * (Hdrift - (frequency_grape) * Hdetuning + pulse[j] * Hdrive1) * t_div)) * U_rho U_lamda = conj( matrix( sclin.expm(-2 * pi * 1j * (Hdrift - (frequency_grape) * Hdetuning + pulse[n_tslot - j - 1] * Hdrive1) * t_div)).T) * U_lamda U_rho_j = U_rho_j + [U_rho] U_lamda_j = U_lamda_j + [U_lamda] for j in range(n_tslot): fid_err_grad_j_sum = [] for k in range(n_goal): lamda = (U_lamda_j[n_tslot - j - 2]) * state_goal[k] * conj( U_lamda_j[n_tslot - j - 2].T) state_j = U_rho_j[j] * state_init[k] * conj(U_rho_j[j].T) fid_err_grad_j = np.real( 1j * 2 * pi * t_div * trace(lamda * (Hdrive1 * state_j - state_j * Hdrive1))) fid_err_grad_j_sum = fid_err_grad_j_sum + [ fid_err_grad_j * weighting_value[k] ] fid_err_grad_ave = fid_err_grad_ave + [ sum(fid_err_grad_j_sum) / sum(weighting_value) ] return array(fid_err_grad_ave) for i in range(2): pulse = callback(self, pulse, f_list, t_hem, n_hem, n_tslot, freq_cut, permin) result = sp_min.minimize(grape_fid_err,pulse,method="L-BFGS-B",jac=grape_fid_err_grad,bounds=bounds,\ options={'disp': None, 'maxcor': 10, 'ftol': 2.220446049250313e-09, 'gtol': 1e-05, 'eps': 1e-9, 'maxfun': 15000, 'maxiter': 1500, 'iprint': -1, 'maxls': 20})#,tol=1e-2) pulse = result.x Ω1_j = pulse #ラビ周波数の配列 Ω2_j = np.zeros(n_tslot) return Φ1_j, Ω1_j """
def getCoeffsIterative1D(x_in, z_in, eqnType, coeffs, DEBUG=False): if x_in is None: raise ValueError("You did not provide data for the x-values.") if z_in is None: raise ValueError("You did not provide data for the z-values.") if eqnType is None: raise ValueError("You did not provide data for eqnType.") if coeffs is None: raise ValueError("You did not provide data for the coefficients.") if DEBUG is None: raise ValueError("You did not provide data for DEBUG.") sErr = None #fit = "Powell" # use Powell's algorithm #fit = "BFGS" # use Broyden-Fletcher-Goldfarb-Shanno #fit = "LMA" # use the Levenberg-Marquardt algorithm from curve_fit fit = ["LMA", "Powell", "BFGS"] # First try LMA, use others as fall-back # make sure that we use other routines for polynomials if (eqnType==IncompressibleData.INCOMPRESSIBLE_POLYNOMIAL) or \ (eqnType==IncompressibleData.INCOMPRESSIBLE_EXPPOLYNOMIAL) : raise ValueError( "Please use the specific polynomial functions, they are much better." ) expLog = False # Fitting the logarithm of z_in? if (eqnType == IncompressibleData.INCOMPRESSIBLE_EXPONENTIAL or eqnType == IncompressibleData.INCOMPRESSIBLE_LOGEXPONENTIAL): expLog = True xData = np.array(x_in.flat) if expLog: zData = np.log(np.clip(z_in.flat, 1e-10, IncompressibleData.maxLin)) else: zData = np.array(z_in.flat) # Remove np.nan elements mask = np.isfinite(zData) xData = xData[mask] zData = zData[mask] # The residual function def fun(coefficients, xArray, yArray): """ This function takes the coefficient array and x and y data. It evaluates the function and returns the sum of the squared residuals if yArray is not equal to None """ # No offset and no Tbase etc for 1D functions! calculated = IncompressibleData.baseFunc(xArray, y=0.0, xbase=0.0, ybase=0.0, eqnType=eqnType, c=coefficients) if expLog: calculated = np.log(calculated) if yArray is None: return calculated data = yArray res = np.sum(np.square(calculated - data)) return res # Loop through the list of algorithms with basic settings to keep track of our efforts success = False counter = 0 tolerance = 1e-16 while (not success): algorithm = fit[counter] #fit = "LMA" # use the Levenberg-Marquardt algorithm from curve_fit if algorithm == "LMA": def func(xVector, *coefficients): return fun(np.array(coefficients), xVector, None) #return self.baseFunction(xVector, 0.0, 0.0, 0.0, np.array(coefficients)) #np.array([self._PropsFit(coefficients,xName,T=Ti) for Ti in T]) try: #print func(xData, coeffs_start) # Do the actual fitting popt, pcov = curve_fit(func, xData, zData, p0=coeffs, ftol=tolerance) if np.any(popt != coeffs): success = True if DEBUG: print("Fit succeeded with: {0}".format(algorithm)) sErr = zData - func(xData, popt) #print "Fit succeeded for "+fit[counter]+": " #print "data: {0}, func: {1}".format(yData[ 2],func(xData[ 2], popt)) #print "data: {0}, func: {1}".format(yData[ 6],func(xData[ 6], popt)) #print "data: {0}, func: {1}".format(yData[-1],func(xData[-1], popt)) #if DEBUG: print("Estimated covariance of parameters: {0}".format(pcov)) #ssErr = np.sqrt(np.diag(pcov)).sum() #ssTot = ((zData-zData.mean())**2).sum() #r2 = 1-(ssErr/ssTot ) #print("\n r2 FMA: ",r2.shape,r2,"\n") return popt, sErr else: if DEBUG: print("Fit failed for {0}.".format(algorithm)) if DEBUG: sys.stdout.flush() success = False except RuntimeError as e: if DEBUG: print("Exception using " + algorithm + ": " + str(e)) if DEBUG: sys.stdout.flush() success = False #fit = "MIN" # use a home-made minimisation with Powell and Broyden-Fletcher-Goldfarb-Shanno elif algorithm == "Powell" or algorithm == "BFGS": arguments = (xData, zData) #options = {'maxiter': 1e2, 'maxfev': 1e5} try: res = minimize(fun, coeffs, method=algorithm, args=arguments, tol=tolerance) if res.success: success = True if DEBUG: print("Fit succeeded with: {0}".format(algorithm)) sErr = zData - fun(np.array(res.x), xData, None) #if res.has_key('fvec'): #ssErr = (res['fvec']**2).sum() #ssTot = ((zData-zData.mean())**2).sum() #r2 = 1-(ssErr/ssTot ) #print("\n r2 : ",r2.shape,r2,algorithm,"\n") return res.x, sErr else: if DEBUG: print("Fit failed for {0}.".format(algorithm)) if DEBUG: sys.stdout.flush() success = False except RuntimeError as e: if DEBUG: print("Exception using " + algorithm + ": " + str(e)) if DEBUG: sys.stdout.flush() success = False # Something went wrong, probably a typo in the algorithm selector else: raise (ValueError("Error: You used an unknown fit method.")) if counter < len(fit) - 1: #print("Fit did not succeed with {0}, reducing tolerance to {1}.".format(algorithm,tol)) success = False counter += 1 elif tolerance < 1e-3: tolerance *= 1e2 if DEBUG: print("Fit did not succeed, reducing tolerance to {0}.". format(tolerance)) success = False counter = 0 else: if DEBUG: print( "--------------------------------------------------------------" ) if DEBUG: print("Fit failed for {0}. ".format(fit)) if DEBUG: print( "--------------------------------------------------------------" ) return coeffs, 1
m= X.shape[0] theta = np.matrix(np.zeros(X.shape[1])) ## using multiple one vs all logistic regression models to build a multi class classifier. There are 10 different numbers contained in the images 0 - 9, ## thus 10 classes and 10 logistic regression classifiers thetaList = [] yList = np.unique(Y) for i in yList: ## We train the data by looping each multi variate variable, and set y to 1 for the variable we wish to train, and zero from the rest. ## Once trained, we then evaluate the probability that a training example belongs to a particular output based on the sigmoid function yhat = np.zeros(Y.shape[0]) yhat[np.where(Y == i)[0]] = 1 m = X.shape[0] thetaOut = minimize(fun = logisticCost, x0 = np.asarray(theta)[0], args = (m,X,yhat,True,1), method = 'SLSQP' ,options = {'maxiter':500})['x'] hyp = sigmoid(X,thetaOut) ## see the distribution of probabilities of the training examples belonging to Y set i #plt.plot(np.array(hyp)[0]) thetaList.append(thetaOut) ## Thus we are learning what for example a "4" looks like, by training the regression parameters on the pixels of different images of 4s where we set y = 1, and others were we set y = 0 when clssifying a 4, that will yield ## the highest probability from the sigmoid hypothesis. i.e. this combination of pixels when applied to the regression parameters for a 4 will predict a 4. This could then work for anything ## really, just training a set of regression parameters so that when applied to a group of pixels it will be a able to tell with a certain confidence if we have ## the image that was trained on or not. testHyp = sigmoid(X,np.matrix(thetaList)) prediction = [np.where(np.array(testHyp[x,:] == testHyp[x,:].max())[0])[0][0]+1 for x in range(testHyp.shape[0])] ## Precision of predictions based on learned parameters per digit
fig = plt.figure() plt.scatter(data1.exam1[data1.yesNo == 1 ], data1.exam2[data1.yesNo == 1 ], marker = '+', color = 'black', label = 'admitted') plt.scatter(data1.exam1[data1.yesNo == 0 ], data1.exam2[data1.yesNo == 0 ], marker = 'o', color = 'yellow', label = 'not admitted') legend = plt.legend(loc = 'upper right') ## Hypothesis for logistic regression uses sigmoid funtion, so hypothesis is either 1 or zero for binary logistic regression X = np.concatenate((np.matrix(np.ones(data1.shape[0])).T,data1.iloc[:,:2].as_matrix()),axis =1) m= X.shape[0] Y = np.array(data1.iloc[:,2]) theta = np.matrix(np.zeros(X.shape[1])) sigVals = sigmoid(X, theta) ## now try to find minimum with a built in minimizer theta = minimize(fun = logisticCost, x0 = np.asarray(theta)[0], args = (m,X,Y), method = 'SLSQP' ) ## thus we use the sigmoid function using a particular training set and the calculated thetas to get a # 1 or a zero i.e. a yes or a no ## plot the decision boundary, y = 1: theta0 + theta1X1 + theta2X2 >= 0, => X2 = (-theta0 - theta1X1)/ theta2 plt.plot(np.array(data1.exam1), (np.array(X[:,:2].dot(-theta.x[:2]))[0])/theta.x[2]) #### Regularized logistic regression data2 = pd.read_csv('C:\\Users\\Nicholas.R_adm\\Documents\\workspace\\Python\\Home Scripts\\ML exercises\\ex2\\ex2Data2.txt',header=None ) data2.columns = ['test1','test2','yesNo'] fig = plt.figure() plt.scatter(data2.test1[data2.yesNo == 1 ], data2.test2[data2.yesNo == 1 ], marker = '+', color = 'black', label = 'admitted')
Y = data['y'].flatten().astype('float64') Ytest = data['ytest'].flatten().astype('float64') Yval = data['yval'].flatten().astype('float64') m= X.shape[0] imgplot = plt.plot(X,Y, 'x') X = np.concatenate((np.matrix(np.ones(X.shape[0])).T,np.matrix(X).T),axis =1).astype('float64') Xval = np.concatenate((np.matrix(np.ones(Xval.shape[0])).T,np.matrix(Xval).T),axis =1).astype('float64') theta = np.matrix([1,1]) regLinCost(theta,X,np.matrix(Y).T,m,lamb = 1) thetaSolve = minimize(fun = regLinCost, x0 = np.array(theta)[0], args = (X,np.matrix(Y).T,m,0), method = 'TNC', jac = True ,options = {'maxiter':100000})['x'] plt.plot(X[:,1],X.dot(np.matrix(thetaSolve).T)) ''' NOTE: when calculating the trsining or cross validation error, ALWAYS set lamb to 0! ''' train = np.zeros(X.shape[0]) cv = np.zeros(Xval.shape[0]) for i in range(1,X.shape[0]): thetaSolve = minimize(fun = regLinCost, x0 = np.array(theta)[0], args = (X[:i],np.matrix(Y[:i]).T,m,0), method = 'TNC', jac = True ,options = {'maxiter':100000})['x'] train[i] = regLinCost(thetaSolve,X[:i],np.matrix(Y[:i]).T,m,lamb = 0)[0] cv[i] = regLinCost(thetaSolve,Xval,np.matrix(Yval).T,m,lamb = 0)[0] ## This reflects high bias, as increasing the number of observations imcreases the amount of training error (the lower line). The model is too simple to fit all of the data, and as we increase, ## the number of training examples we get no further decrease in the cross validation error. THus more training examples will not improve performance, but a more complicated model might.
def fitCoefficients(self,xName,T=[],xData=[]): if (len(T)!=len(xData)): raise (ValueError("Error: There has to be the same number of temperature and data points.")) if len(T)<self._minPoints: raise (ValueError("Error: You should use at least "+str(self._minPoints)+" points.")) def fun(coefficients,xName,T,xData): # Values for conductivity are very small, # algorithms prefer larger values if xName=='L': calculated = numpy.array([self._PropsFit(coefficients,xName,T=Ti) for Ti in T]) data = numpy.array(xData) # Fit logarithms for viscosity and saturation pressure elif xName=='V' or xName=='Psat': calculated = numpy.log(numpy.array([self._PropsFit(coefficients,xName,T=Ti) for Ti in T])) data = numpy.log(numpy.array(xData)) else: calculated = numpy.array([self._PropsFit(coefficients,xName,T=Ti) for Ti in T]) data = numpy.array(xData) res = numpy.sum((calculated-data)**2.) return res initValues = self.getCoefficients(xName)[:] # Fit logarithms for viscosity and saturation pressure if xName=='V' or xName=='Psat': #fit = "MIN" # use a home-made minimisation with Powell and Broyden-Fletcher-Goldfarb-Shanno #fit = "LMA" # use the Levenberg-Marquardt algorithm from curve_fit #fit = "POL" # use a polynomial in an exponential function fit = ["LMA","MIN"] # First try LMA, use MIN as a fall-back solver success = False counter = -1 while (not success): counter += 1 if fit[counter]=="LMA": xData = numpy.array(xData) fit_log = True def func(T, *coefficients): result = numpy.array([self._PropsFit(coefficients,xName,T=Ti) for Ti in T]) if fit_log: return numpy.log(result) else: return result if fit_log: xData = numpy.log(xData) try: # Do the actual fitting popt, pcov = curve_fit(func, T, xData, p0=initValues, maxfev=1000) #print popt #print pcov success = True return popt except RuntimeError as e: print "Exception: "+str(e) print "Using: "+str(fit[counter+1])+" as a fall-back." success = False elif fit[counter]=="MIN": print "Fitting exponential with "+str(len(initValues))+" coefficients." arguments = (xName,T,numpy.exp(xData)) #options = {'maxiter': 1e2, 'maxfev': 1e5} if xName=='V': method = "Powell" elif xName=='Psat': method = "BFGS" tolStart = 1e-13 tol = tolStart res = minimize(fun, initValues, method=method, args=arguments, tol=tol) while ((not res.success) and tol<1e-2): tol *= 1e2 print "Fit did not succeed, reducing tolerance to "+str(tol) res = minimize(fun, initValues, method=method, args=arguments, tol=tol) # Include these lines for an additional fit with new guess values. #if res.success and tol>tolStart: # print "Refitting with new guesses and original tolerance of "+str(tolStart) # res = minimize(fun, res.x, method=method, args=arguments, tol=tolStart) if res.success: success = True return res.x else: print "Fit failed: " print res success = False elif fit[counter]=="POL": print "Fitting exponential polynomial with "+str(len(initValues))+" coefficients." z = numpy.polyfit(T, numpy.log(xData)[:], len(initValues)-1) return z[::-1] else: raise (ValueError("Error: You used an unknown fit method.")) else: # just a polynomial print "Fitting polynomial with "+str(len(initValues))+" coefficients." z = numpy.polyfit(T, xData, len(initValues)-1) return z[::-1]
def fitCoefficients(self, xName, T=[], xData=[]): if (len(T) != len(xData)): raise (ValueError( "Error: There has to be the same number of temperature and data points." )) if len(T) < self._minPoints: raise (ValueError("Error: You should use at least " + str(self._minPoints) + " points.")) def fun(coefficients, xName, T, xData): # Values for conductivity are very small, # algorithms prefer larger values if xName == 'L': calculated = numpy.array( [self._PropsFit(coefficients, xName, T=Ti) for Ti in T]) data = numpy.array(xData) # Fit logarithms for viscosity and saturation pressure elif xName == 'V' or xName == 'Psat': calculated = numpy.log( numpy.array([ self._PropsFit(coefficients, xName, T=Ti) for Ti in T ])) data = numpy.log(numpy.array(xData)) else: calculated = numpy.array( [self._PropsFit(coefficients, xName, T=Ti) for Ti in T]) data = numpy.array(xData) res = numpy.sum((calculated - data)**2.) return res initValues = self.getCoefficients(xName)[:] # Fit logarithms for viscosity and saturation pressure if xName == 'V' or xName == 'Psat': #fit = "MIN" # use a home-made minimisation with Powell and Broyden-Fletcher-Goldfarb-Shanno #fit = "LMA" # use the Levenberg-Marquardt algorithm from curve_fit #fit = "POL" # use a polynomial in an exponential function fit = ["LMA", "MIN"] # First try LMA, use MIN as a fall-back solver if self._expPoly: fit = ["POL"] # Overwrite preferences for polynomial success = False counter = -1 while (not success): counter += 1 if fit[counter] == "LMA": xData = numpy.array(xData) fit_log = True def func(T, *coefficients): result = numpy.array([ self._PropsFit(coefficients, xName, T=Ti) for Ti in T ]) if fit_log: return numpy.log(result) else: return result if fit_log: xData = numpy.log(xData) try: # Do the actual fitting popt, pcov = curve_fit(func, T, xData, p0=initValues, maxfev=1000) #print popt #print pcov success = True return popt except RuntimeError as e: print("Exception: " + str(e)) print("Using: " + str(fit[counter + 1]) + " as a fall-back.") success = False elif fit[counter] == "MIN": print("Fitting exponential with " + str(len(initValues)) + " coefficients.") arguments = (xName, T, numpy.exp(xData)) #options = {'maxiter': 1e2, 'maxfev': 1e5} if xName == 'V': method = "Powell" elif xName == 'Psat': method = "BFGS" tolStart = 1e-13 tol = tolStart res = minimize(fun, initValues, method=method, args=arguments, tol=tol) while ((not res.success) and tol < 1e-2): tol *= 1e2 print("Fit did not succeed, reducing tolerance to " + str(tol)) res = minimize(fun, initValues, method=method, args=arguments, tol=tol) # Include these lines for an additional fit with new guess values. #if res.success and tol>tolStart: # print "Refitting with new guesses and original tolerance of "+str(tolStart) # res = minimize(fun, res.x, method=method, args=arguments, tol=tolStart) if res.success: success = True return res.x else: print("Fit failed: ") print(res) success = False elif fit[counter] == "POL": print("Fitting exponential polynomial with " + str(len(initValues)) + " coefficients.") z = numpy.polyfit(T - self._Tbase, numpy.log(xData)[:], len(initValues) - 1) return z[::-1] else: raise ( ValueError("Error: You used an unknown fit method.")) else: # just a polynomial print("Fitting polynomial with " + str(len(initValues)) + " coefficients.") z = numpy.polyfit(T - self._Tbase, xData, len(initValues) - 1) return z[::-1]
''' solve ''' layerNum = len(weights2) layerdim = [] for i in range(len(weights2)): layerdim.append(weights2[i].shape) inputWeights = np.concatenate((weights2[0].flatten(), weights2[1].flatten())) ''' need to provide gradients to solver to improve performance, as solver uses finite differences, but if you can calculate the jacobian instead then will be much more efficiant. Setting jacobian = True tells the solver that the "fun" will return the objective function along with the gradients''' weightsOut = minimize(fun = NNlogisticCost, x0 = inputWeights, args = (m,X,YhatMat,True,1, layerNum, layerdim), method = 'TNC', jac = True ,options = {'maxiter':100000})['x'] weights = [] if (layerNum is not None) & (layerdim is not None): strt = 0 for i in range(layerNum): weights.append(weightsOut[strt:strt+np.array(layerdim[i]).prod()].reshape(layerdim[i])) strt += np.array(layerdim[i]).prod() nLayer = X for i in range(len(weights)): nLayer = sigmoid(nLayer,np.matrix(weights[i])) if i != len(weights)-1: nLayer = np.concatenate((np.matrix(np.ones(nLayer.shape[0])).T,nLayer), axis = 1) ##which output has the highest probability per training example. We add 1 as the examples are from 1 -> 10, where 10 represents a 0.