def setStructures(self): self._struct_p = apprentice.monomialStructure(self.dim, self.m) self._struct_q = apprentice.monomialStructure(self.dim, self.n) from apprentice import tools self._M = tools.numCoeffsPoly(self.dim, self.m) self._N = tools.numCoeffsPoly(self.dim, self.n) self._K = 1 + self._m + self._n
def mkFromDict(self, pdict): self._scaler = apprentice.Scaler(pdict["scaler"]) self._pcoeff = np.array(pdict["pcoeff"]) self._qcoeff = np.array(pdict["qcoeff"]) self._iterationinfo = pdict["iterationinfo"] self._dim = pdict["dim"] self._m = pdict["m"] self._n = pdict["n"] self._M = pdict["M"] self._N = pdict["N"] self._fittime = pdict["log"]["fittime"] self._strategy = pdict["strategy"] self._roboptstrategy = pdict["roboptstrategy"] self._localoptsolver = pdict["localoptsolver"] self._fitstrategy = pdict["fitstrategy"] self._trainingscale = pdict["trainingscale"] self._trainingsize = pdict["trainingsize"] self._penaltyparam = 0.0 if (self.strategy == 1 or self.strategy == 2): self._ppenaltybin = pdict['chosenppenalty'] self._qpenaltybin = pdict['chosenqpenalty'] if (self.strategy == 2): self._penaltyparam = pdict['lambda'] self._struct_p = apprentice.monomialStructure(self.dim, self.m) self._struct_q = apprentice.monomialStructure(self.dim, self.n)
def appgetonevardenom(x, y, app): import apprentice struct_q = apprentice.monomialStructure(datastore['dim'], datastore['n']) qcoeff = app.qcoeff a = 0 b = 0 c = 0 d = 0 for num, row in enumerate(struct_q): val = qcoeff[num] * (x**row[0] * y**row[1]) if (row[2] == 0): d += val elif (row[2] == 1): c += val elif (row[2] == 2): b += val elif (row[2] == 3): a += val # z = -0.2345 # fthis = a*z**3 + b*z**2 +c*z +d # fnorm = appevaldenom(x,y,z,app,0,0) # print(fthis,fnorm) return a, b, c, d
def setStructures(self): omax_p = np.max([r.m for r in self._RA]) omax_q = np.max([r.n if hasattr(r, "n") else 0 for r in self._RA]) omax = max(omax_p, omax_q) self._structure = np.array(apprentice.monomialStructure( self.dim, omax), dtype=np.int32) S = self._structure # Gradient helpers self._NNZ = [ np.where(self._structure[:, coord] != 0) for coord in range(self.dim) ] self._sred = np.array( [self._structure[nz][:, num] for num, nz in enumerate(self._NNZ)], dtype=np.int32) # Hessian helpers self._HH = np.ones((self.dim, self.dim, len(S)), dtype=np.float64) # Prefactors self._EE = np.full((self.dim, self.dim, len(S), self.dim), S, dtype=np.int32) # Initial structures for numx in range(self.dim): for numy in range(self.dim): if numx == numy: self._HH[numx][numy] = S[:, numx] * (S[:, numx] - 1) else: self._HH[numx][numy] = S[:, numx] * S[:, numy] self._EE[numx][numy][:, numx] -= 1 self._EE[numx][numy][:, numy] -= 1 self._HNONZ = np.empty((self.dim, self.dim), dtype=tuple) for numx in range(self.dim): for numy in range(self.dim): self._HNONZ[numx][numy] = np.where(self._HH[numx][numy] > 0) # Jacobians for Hessian JF = self._SCLR.jacfac for numx in range(self.dim): for numy in range(self.dim): self._HH[numx][numy][self._HNONZ[numx][numy]] *= (JF[numx] * JF[numy])
def prepareCache(self): import apprentice orders = [] for r in self._RA: orders.append(r.m) if hasattr(r, "n"): orders.append(r.n) omax = max(orders) self._structure = apprentice.monomialStructure(self.dim, omax) # Gradient helpers self._NNZ = [ np.where(self._structure[:, coord] != 0) for coord in range(self.dim) ] self._sred = np.array( [self._structure[nz][:, num] for num, nz in enumerate(self._NNZ)]) if self.dim == 1: self.recurrence = apprentice.monomial.recurrence1D else: self.recurrence = apprentice.monomial.recurrence
def setStructures(self): self._struct_p = apprentice.monomialStructure(self.dim, self.m) from apprentice import tools self._M = tools.numCoeffsPoly(self.dim, self.m) self._nnz = self._struct_p > 0
def plotmntesterrperfile(jsonfile,testfile, desc,folder): minp = np.inf minq = np.inf maxp = 0 maxq = 0 miny0 = 0 minl1 = np.inf minl2 = np.inf minlinf = np.inf pp = 0 qq =0 outfile1 = folder+"/"+desc+".299445.png" # outfile2 = folder+"/"+fno+"_index.299445.png" X_test, Y_test = readData(testfile) import json if jsonfile: with open(jsonfile, 'r') as fn: datastore = json.load(fn) from apprentice import monomial import apprentice for key in sorted(datastore.keys()): pdeg = datastore[key]['m'] qdeg = datastore[key]['n'] if(pdeg<minp): minp=pdeg if(qdeg<minq): minq=qdeg if pdeg > maxp: maxp = pdeg if qdeg > maxq: maxq = qdeg # print(minp,maxp,minq,maxq) error = np.zeros(shape = (maxp-minp+1,maxq-minq+1)) for key in sorted(datastore.keys()): pdeg = datastore[key]['m'] qdeg = datastore[key]['n'] Y_pred = np.array([],dtype=np.float64) if('scaler' in datastore[key]): rappsip = RationalApproximationSIP(datastore[key]) Y_pred = rappsip.predictOverArray(X_test) # print(Y_pred) else: structp = apprentice.monomialStructure(datastore[key]['dim'], pdeg) structq = apprentice.monomialStructure(datastore[key]['dim'], qdeg) for x in X_test: nnn = np.array(monomial.recurrence(x, structp)) p = np.array(datastore[key]['pcoeff']).dot(nnn) ddd = np.array(monomial.recurrence(x, structq)) q = np.array(datastore[key]['qcoeff']).dot(ddd) Y_pred = np.append(Y_pred,(p/q)) # print(np.c_[Y_pred,Y_test]) l1 = np.sum(np.absolute(Y_pred-Y_test)) # print(l1) l2 = np.sqrt(np.sum((Y_pred-Y_test)**2)) linf = np.max(np.absolute(Y_pred-Y_test)) x000 = np.zeros(datastore[key]['dim']) y000 = rappsip.predict(x000) print("p%d q%d %f"%(pdeg,qdeg,y000)) error[pdeg-minp][qdeg-minq] = l2 if(minl2>l2): minl2 = l2 minl1 = l1 minlinf = linf print(linf) pp = pdeg qq = qdeg miny0 = y000 # print(miiny0) import matplotlib as mpl import matplotlib.pyplot as plt mpl.rc('text', usetex = True) mpl.rc('font', family = 'serif', size=12) mpl.style.use("ggplot") cmapname = 'viridis' plt.clf() print(error) markersize = 1000 vmin = -4 vmax = 2 X,Y = np.meshgrid(range(minq,maxq+1),range(minp,maxp+1)) # plt.scatter(X,Y , marker = 's', s=markersize, c = np.ma.log10(error), cmap = cmapname, vmin=vmin, vmax=vmax, alpha = 1) plt.scatter(X,Y , marker = 's', s=markersize, c = error, cmap = cmapname, alpha = 1) plt.xlabel("$n$") plt.ylabel("$m$") plt.xlim((minq-1,maxq+1)) plt.ylim((minp-1,maxp+1)) b=plt.colorbar() # b.set_label("$\log_{10}\\left|\\left|f - \\frac{p^m}{q^n}\\right|\\right|_2$") b.set_label("$\\left|\\left|f - \\frac{p^m}{q^n}\\right|\\right|_2$") plt.title("l1=%f, l2=%f, linf=%f y0=%f found at (%d,%d)"%(minl1,minl2,minlinf,pp,qq,miny0)) plt.savefig(outfile1)
def tablesinc(m, n, ts, table_or_latex): from apprentice import monomial print(apprentice.monomialStructure(3, 3)) fname = "f20" larr = [10**-6, 10**-3] uarr = [2 * np.pi, 4 * np.pi] lbdesc = {0: "-6", 1: "-3"} ubdesc = {0: "2pi", 1: "4pi"} lblatex = {0: "$10^{-6}$", 1: "$10^{-3}$"} ublatex = {0: "$2\\pi$", 1: "$4\\pi$"} noisestr = "" folder = "%s%s_%s/sincrun" % (fname, noisestr, ts) if not os.path.exists(folder): print("folder %s not found") if not os.path.exists(folder + "/benchmarkdata"): os.mkdir(folder + '/benchmarkdata') data = {} for dim in range(2, 8): data[dim] = {} for numlb, lb in enumerate(larr): for numub, ub in enumerate(uarr): key = lbdesc[numlb] + ubdesc[numub] data[dim][key] = {} fndesc = "%s%s_%s_p%d_q%d_ts%s_d%d_lb%s_ub%s" % ( fname, noisestr, ts, m, n, ts, dim, lbdesc[numlb], ubdesc[numub]) file = folder + "/" + fndesc + '/out/' + fndesc + "_p" + str( m) + "_q" + str(n) + "_ts" + ts + ".json" if not os.path.exists(file): print("%s not found" % (file)) exit(1) if file: with open(file, 'r') as fn: datastore = json.load(fn) data[dim][key]['l2error'] = 0 testfile = "%s/benchmarkdata/%s%s_d%d_lb%s_ub%s_test.csv" % ( folder, fname, noisestr, dim, lbdesc[numlb], ubdesc[numub]) if not os.path.exists(testfile): print("%s not found" % (testfile)) exit(1) bottom_or_all = all try: X, Y = readData(testfile) except: DATA = tools.readH5(testfile, [0]) X, Y = DATA[0] if (bottom_or_all == "bottom"): testset = [i for i in range(trainingsize, len(X_test))] X_test = X[testset] Y_test = Y[testset] else: X_test = X Y_test = Y rappsip = RationalApproximationSIP(datastore) Y_pred_rappsip = rappsip.predictOverArray(X_test) Y_diff = (Y_pred_rappsip - Y_test)**2 print(dim, key) print(np.c_[Y_test[1:10], Y_pred_rappsip[1:10], Y_diff[1:10]]) l2allrappsip = np.sum((Y_pred_rappsip - Y_test)**2) l2allrappsip = np.sqrt(l2allrappsip) data[dim][key]['l2error'] = l2allrappsip rappsiptime = datastore['log']['fittime'] rdof = int(datastore['M'] + datastore['N']) rnoiters = len(datastore['iterationinfo']) rpnnl = datastore['M'] - (dim + 1) rqnnl = datastore['N'] - (dim + 1) data[dim][key]['rappsiptime'] = rappsiptime data[dim][key]['rdof'] = rdof data[dim][key]['rnoiters'] = rnoiters data[dim][key]['rpnnl'] = rappsiptime data[dim][key]['rqnnl'] = rqnnl # print(data) s = "" if (table_or_latex == "table"): print("TBD") elif (table_or_latex == "latex"): for dim in range(2, 8): for numlb, lb in enumerate(larr): for numub, ub in enumerate(uarr): key = lbdesc[numlb] + ubdesc[numub] s += "%d&%d&%d&%s&%s&%.3f&%d&%.3f" % ( dim, data[dim][key]['rdof'], data[dim][key]['rqnnl'], lblatex[numlb], ublatex[numub], data[dim][key]['rappsiptime'], data[dim][key]['rnoiters'], data[dim][key]['l2error']) s += "\\\\\hline\n" # print(s) import matplotlib.pyplot as plt X = range(2, 8) rangearr = [] labelarr = [] for numub, ub in enumerate(uarr): for numlb, lb in enumerate(larr): rangearr.append(lbdesc[numlb] + ubdesc[numub]) labelarr.append(lblatex[numlb] + " - " + ublatex[numub]) for r in rangearr: Y = [] for x in X: Y.append(data[x][r]['l2error']) plt.plot(X, np.log10(Y), linewidth=1) plt.legend(labelarr, loc='upper right') # plt.show() plt.savefig("/Users/mkrishnamoorthy/Desktop/sincerror.pdf") plt.clf() # ############################################## import matplotlib.pyplot as plt X = range(2, 8) rangearr = [] labelarr = [] for numub, ub in enumerate(uarr): for numlb, lb in enumerate(larr): rangearr.append(lbdesc[numlb] + ubdesc[numub]) labelarr.append(lblatex[numlb] + " - " + ublatex[numub]) for r in rangearr: Y = [] for x in X: Y.append(data[x][r]['rnoiters']) plt.plot(X, np.log10(Y), linewidth=1) plt.legend(labelarr, loc='upper left') # plt.show() plt.savefig("/Users/mkrishnamoorthy/Desktop/sinc.pdf") plt.clf() exit(1) # ############################################## dim = 3 fndesc = "%s%s_%s_p%d_q%d_ts%s_d%d_lb%s_ub%s" % ( fname, noisestr, ts, m, n, ts, dim, lbdesc[0], ubdesc[1]) file = folder + "/" + fndesc + '/out/' + fndesc + "_p" + str( m) + "_q" + str(n) + "_ts" + ts + ".json" if not os.path.exists(file): print("%s not found" % (file)) if file: with open(file, 'r') as fn: datastore = json.load(fn) iterinfo = datastore['iterationinfo'] print("#################") for iter in iterinfo: print(iter['robOptInfo']['robustArg']) print("#################") rappsip = RationalApproximationSIP(datastore) X1vals = np.arange(lb, ub, 0.1) X2vals = np.arange(lb, ub, 0.1) X3vals = np.arange(lb, ub, 0.1) print(len(X1vals) * len(X2vals) * len(X3vals)) Y_pred = [] Y_orig = [] for x1 in X1vals: for x2 in X2vals: for x3 in X3vals: Y_pred.append(rappsip([x1, x2, x3])) Y_orig.append(sinc([x1, x2, x3], 3)) l22 = np.sum((np.array(Y_pred) - np.array(Y_orig))**2) l22 = l22 / (len(X1vals) * len(X2vals) * len(X3vals)) print("\nUnscaled\n") print(datastore['scaler']) print("#################") for iter in iterinfo: x = rappsip._scaler.unscale(iter['robOptInfo']['robustArg']) print(x) print("#################") print("Min max for n=3 after final iteration") print(min(Y_pred), max(Y_pred)) print(min(Y_orig), max(Y_orig)) print("#################") print("#################") print("\nMean error after the final approximation = %f\n" % (l22)) print("#################") datastore['pcoeff'] = iterinfo[0]['pcoeff'] datastore['qcoeff'] = iterinfo[0]['qcoeff'] rappsip = RationalApproximationSIP(datastore) lb = larr[0] ub = uarr[1] Y_pred = [] Y_orig = [] for x1 in X1vals: for x2 in X2vals: for x3 in X3vals: Y_pred.append(rappsip([x1, x2, x3])) Y_orig.append(sinc([x1, x2, x3], 3)) print("#################") print("Min max for n=3 after first iteration") print(min(Y_pred), max(Y_pred)) print(min(Y_orig), max(Y_orig)) l22 = np.sum((np.array(Y_pred) - np.array(Y_orig))**2) l22 = l22 / (len(X1vals) * len(X2vals) * len(X3vals)) print("#################") print("\nMean error after the first approximation = %f\n" % (l22)) print("#################") print("#################") print("#################") print("#################") # exit(1) # ############################################## # Plotting import matplotlib.pyplot as plt if file: with open(file, 'r') as fn: datastore = json.load(fn) iterinfo = datastore['iterationinfo'] iterinfono = len(iterinfo) for iterno in range(iterinfono): if file: with open(file, 'r') as fn: datastore = json.load(fn) iterinfo = datastore['iterationinfo'] datastore['pcoeff'] = iterinfo[iterno]['pcoeff'] datastore['qcoeff'] = iterinfo[iterno]['qcoeff'] rappsip = RationalApproximationSIP(datastore) fig = plt.figure(figsize=(15, 15)) for num, s in enumerate(['x1=-1', 'x2=-1', 'x3-1']): other1 = [] other2 = [] Y_pred = [] Y_orig = [] q_pred = [] for x2 in X1vals: for x3 in X1vals: if (num == 0): X111 = [lb, x2, x3] if (num == 1): X111 = [x2, lb, x3] if (num == 2): X111 = [x2, x3, lb] other1.append(x2) other2.append(x3) Y_pred.append(rappsip(X111)) Y_orig.append(sinc(X111, 3)) X111 = rappsip._scaler.scale(np.array(X111)) q_pred.append(rappsip.denom(X111)) # Y_pred = np.reshape(np.array(Y_pred), [len(other1), len(other2)]) # Y_orig = np.reshape(np.array(Y_orig), [len(other1), len(other2)]) # q_pred = np.reshape(np.array(q_pred), [len(other1), len(other2)]) ax = fig.add_subplot(3, 3, 3 * num + 1, projection='3d') ax.plot3D(other1, other2, Y_orig, "b.", alpha=0.5) ax.set_xlabel("x2") ax.set_ylabel("x3") ax = fig.add_subplot(3, 3, 3 * num + 2, projection='3d') ax.plot3D(other1, other2, Y_pred, "r.", alpha=0.5) ax.set_xlabel("x2") ax.set_ylabel("x3") ax = fig.add_subplot(3, 3, 3 * num + 3, projection='3d') ax.plot3D(other1, other2, q_pred, "g.", alpha=0.5) ax.set_xlabel("x2") ax.set_ylabel("x3") plt.savefig("/Users/mkrishnamoorthy/Desktop/sinc/iter" + str(iterno) + ".pdf") plt.clf() exit(1) # ############################################## dim = 4 fndesc = "%s%s_%s_p%d_q%d_ts%s_d%d_lb%s_ub%s" % ( fname, noisestr, ts, m, n, ts, dim, lbdesc[0], ubdesc[1]) file = folder + "/" + fndesc + '/out/' + fndesc + "_p" + str( m) + "_q" + str(n) + "_ts" + ts + ".json" if not os.path.exists(file): print("%s not found" % (file)) if file: with open(file, 'r') as fn: datastore = json.load(fn) iterinfo = datastore['iterationinfo'] print("#################") for iter in iterinfo: print(iter['robOptInfo']['robustArg']) print("#################") rappsip = RationalApproximationSIP(datastore) X1vals = np.arange(lb, ub, 0.3) X2vals = np.arange(lb, ub, 0.3) X3vals = np.arange(lb, ub, 0.3) X4vals = np.arange(lb, ub, 0.3) for x1 in X1vals: for x2 in X2vals: for x3 in X3vals: for x4 in X4vals: Y_pred.append(rappsip([x1, x2, x3, x4])) Y_orig.append(sinc([x1, x2, x3, x4], 4)) print("min max for n=4 after final iteration") print(min(Y_pred), max(Y_pred)) print(min(Y_orig), max(Y_orig)) l22 = np.sum((np.array(Y_pred) - np.array(Y_orig))**2) l22 = l22 / (len(X1vals) * len(X2vals) * len(X3vals) * len(X4vals)) print(len(X1vals) * len(X2vals) * len(X3vals) * len(X4vals)) print("\nUnscaled\n") print(datastore['scaler']) print("#################") for iter in iterinfo: x = rappsip._scaler.unscale(iter['robOptInfo']['robustArg']) print(x) print("#################") print("#################") print("Min max for n=4 after final iteration") print(min(Y_pred), max(Y_pred)) print(min(Y_orig), max(Y_orig)) print("#################") print("#################") print("Mean error after the final approximation = %f\n" % (l22)) print("#################") datastore['pcoeff'] = iterinfo[0]['pcoeff'] datastore['qcoeff'] = iterinfo[0]['qcoeff'] rappsip = RationalApproximationSIP(datastore) lb = larr[0] ub = uarr[1] Y_pred = [] Y_orig = [] for x1 in X1vals: for x2 in X2vals: for x3 in X3vals: for x4 in X4vals: Y_pred.append(rappsip([x1, x2, x3, x4])) Y_orig.append(sinc([x1, x2, x3, x4], 4)) print("#################") print("Min max for n=4 after final iteration") print(min(Y_pred), max(Y_pred)) print(min(Y_orig), max(Y_orig)) print("#################") l22 = np.sum((np.array(Y_pred) - np.array(Y_orig))**2) l22 = l22 / (len(X1vals) * len(X2vals) * len(X3vals) * len(X4vals)) print("#################") print("\nMean error after the first approximation = %f\n" % (l22))