1.0 / math.sech(beta * x))**2 xmoid1 = lambda x: x xmoidp1 = lambda x: 1.0 xmoidpp1 = lambda x: 0.0 relu = lambda x: max(0.0, x) relup = lambda x: 0.0 if (x <= 0.0) else 1.0 relupp = lambda x: 0.0 #bias = [[0.01],[0.01],[0.001],[0.0001],[0.00001],[0.0000001]] bias = [[0.01], [0.01], [0.001]] bias = [] #machine = myfeedforward.MyFeedForward([3,60,120,1],[xmoid1,sigmoid,sigmoid,xmoid1],[xmoidp1,sigmoidp,sigmoidp,xmoidp1],[xmoidpp1,sigmoidpp,sigmoidpp,xmoidpp1]) machine = myfeedforward.MyFeedForward([3, 60, 120, 1], [xmoid1, relu, relu, xmoid1], [xmoidp1, relup, relup, xmoidp1], [xmoidpp1, relupp, relupp, xmoidpp1]) if os.path.isfile("tutorial7.weights"): with open("tutorial7.weights", 'rb') as ff: weights = pickle.loads(ff.read()) else: weights = machine.initial_w() for i in range(len(weights)): weights[i] *= 1.0 nw = len(weights) print("weights=", weights) print("nw=", nw) m = [0.0] * len(weights)
penaltyp = lambda x: ap * 0.5 * bp * ((1 / math.cosh(bp * (x - xp)))**2 - (1.0 / math.cosh(bp * (x + xp)))**2) sigmoid = lambda x: 0.5 * (math.tanh(beta * x) + 1.0) sigmoidp = lambda x: 0.5 * beta * (1.0 / math.cosh(beta * x))**2 sigmoidpp = lambda x: 0.5 * (-2.0) * beta * beta * math.tanh(beta * x) * ( 1.0 / math.sech(beta * x))**2 xmoid1 = lambda x: x xmoidp1 = lambda x: 1.0 xmoidpp1 = lambda x: 0.0 #bias = [[0.01],[0.01],[0.001],[0.0001],[0.00001],[0.0000001]] bias = [[0.01], [0.01], [0.001]] bias = [] machine = myfeedforward.MyFeedForward( [3, 60, 120, 1], [xmoid1, sigmoid, sigmoid, xmoid1], [xmoidp1, sigmoidp, sigmoidp, xmoidp1], [xmoidpp1, sigmoidpp, sigmoidpp, xmoidpp1]) if os.path.isfile("tutorial6.weights"): with open("tutorial6.weights", 'rb') as ff: weights = pickle.loads(ff.read()) else: weights = machine.initial_w() for i in range(len(weights)): weights[i] *= 1.0 nw = len(weights) print("weights=", weights) print("nw=", nw) m = [0.0] * len(weights)
def main(): # import sys import getopt usage = \ """ esmiles to nn program Usage: mleval10 -f nn_file.dat -n hidden layers esmiles hidden_layers = "2 1" -h prints this message """ print("\nmleval10 Arrows version") hidden_layers = [2,1] nnfilename = "" opts, args = getopt.getopt(sys.argv[1:], "hn:f:") for o, a in opts: if '-n' in o: hidden_layers = [eval(x) for x in a.strip().split()] if '-f' in o: nnfilename = a if o in ("-h","--help"): print(usage) exit() esmiles = args[0] print("esmiles=",esmiles) #weights_filename = "tutorial10" weights_filename = nnfilename.replace(".dat","") for ix in hidden_layers: weights_filename += "-"+str(ix) weights_filename += ".weights" #param_filename = "tutorial10" param_filename = nnfilename.replace(".dat","") for ix in hidden_layers: param_filename += "-"+str(ix) param_filename += ".param" ##### Read in params #### print("Param_filename:", param_filename) if os.path.isfile(param_filename): print("reading params") with open(param_filename,'rb') as ff: params = pickle.loads(ff.read()) ninput = params[0] nframes = params[1] emin = params[2] emax = params[3] emid = params[4] edif = params[5] print() print("Training Data:") print("ninput=",ninput) print("nframes=",nframes) print() print("ReScaling Energies Parameters:") print("emin=",emin) print("emax=",emax) print("emid=",emid) print("edif=",edif) energy_dictionary = {'C':-23725.067263640653, 'H':-320.1587671072026, 'N':-34207.75150198355, 'O':-47067.1469030725 } energy_type = "gaq" symbol = ['C','H','N','O'] maxatoms = 25 maxcharge = 4 maxmult = 10 esmiles0 = esmiles.replace(" ","%20") #arrows_url = 'https://arrows.emsl.pnnl.gov/api/esmiles/\"' + esmiles0.replace("/","arrowslash").strip() + '\"' arrows_url = 'https://arrows.emsl.pnnl.gov/api/esmiles2xyz/\"' + esmiles0.replace("/","arrowslash").strip() + '\"' print("nnfilename = ", nnfilename) print() print("esmiles =",esmiles) print("symbols =",symbol) #print("energy_type =",energy_type) print("energy_dictionary =",energy_dictionary) print("maxatoms =",maxatoms) print("maxcharge =",maxcharge) print("maxmult =",maxmult) print("arrows_url =",arrows_url) try: rr = geturlresult(arrows_url) esmiles_dict = json.loads(rr) except: try: rr = geturlresult(arrows_url) esmiles_dict = json.loads(rr) except: print(" - API Failed") print("\nlen esmiles_dictionary_all=",len(esmiles_dict)) if (len(esmiles_dict)<=0): exit() keys = {} count = 0 n = len(symbol) for i in range(n): key = symbol[i] keys[key] = count count += 1 for i in range(n): for j in range(n): symi = symbol[i] symj = symbol[j] if (symi<symj): key = symi + symj else: key = symj + symi if (key not in keys): keys[key] = count count += 1 for i in range(n): for j in range(n): for k in range(n): symi = symbol[i] symj = symbol[j] symk = symbol[k] if (symj<symk): key = symj + symi + symk else: key = symk + symi + symj if (key not in keys): keys[key] = count count += 1 print("keys=",keys) eoln = "\n" msg = eoln; msg += "Fetched the following entry:" msg += "mformula = " + esmiles_dict['mformula'] + eoln msg += "iupac = " + esmiles_dict['iupac'] + eoln msg += "smiles = " + esmiles_dict['smiles'] + eoln msg += "csmiles = " + esmiles_dict['csmiles'] + eoln msg += "esmiles = " + esmiles_dict['esmiles'] + eoln msg += "InChI = " + esmiles_dict['inchi'] + eoln msg += "InChiKey = " + esmiles_dict['inchikey'] + eoln #msg += "cid = " + esmiles_dict['cid'] + eoln #msg += "cas = " + str(esmiles_dict['cas']) + eoln #if (esmiles_dict['kegg'] is not None): msg += "kegg = " + esmiles_dict['kegg'] + eoln msg += "bonding_string = " + esmiles_dict['bonding_string'] + eoln msg += "covalent_string = " + esmiles_dict['covalent_string'] + eoln msg += "charge = " + str(esmiles_dict['charge']) + eoln msg += "mult = " + str(esmiles_dict['mult']) + eoln print(msg) charge = esmiles_dict['charge'] mult = esmiles_dict['mult'] xyz_blob = esmiles_dict['xyz_blob'] msg2 = xyzdat_bonding_strings(xyz_blob) print("msg2=",msg2) bonding1 = re.split('(\d+)', msg2.strip().split(":")[0]) bonding2 = re.split('(\d+)', msg2.strip().split(":")[1]) bonding3 = re.split('(\d+)', msg2.strip().split(":")[2]) print("Bonding1=",bonding1) print("Bonding2=",bonding2) print("Bonding3=",bonding3) nkeys = len(keys) input_hash = [0]* (nkeys+ (2*maxcharge+1) + maxmult) input_hash[nkeys + maxcharge + charge] = 1 input_hash[nkeys + (2*maxcharge+1) + (mult-1)] = 1 x1 = bonding1[0:-1:2] + bonding2[0:-1:2] + bonding3[0:-1:2] y1 = bonding1[1::2] + bonding2[1::2] + bonding3[1::2] keystop = False for x in x1: if (x not in keys): keystop = True if keystop: print("Exiting because key not in symbols\n") exit() for i in range(len(x1)): input_hash[keys[x1[i]]] = eval(y1[i]) eref = 0.0 for sym in symbol: nsym = input_hash[keys[sym]] eref += nsym*energy_dictionary[sym] natoms = eval(xyz_blob.strip().split("\n")[0]) inputlayer = [0]*len(input_hash) for i in range(len(inputlayer)): inputlayer[i] = input_hash[i] for i in range(nkeys): inputlayer[i] = input_hash[i]/natoms #outputlayer = (output_hash - eref)/natoms/627.509469 if (natoms>maxatoms): print("Exiting because natoms>maxatoms\n") exit() print("input_hash =",input_hash) #print("output_hash=",output_hash) print() print() print("NN Input:") #print("Id =",Id) #print("esmiles =",outesmiles) #print("theory =",theory) #print("xc =",xc) #print("basis =",basis) #print("charge =",charge) #print("solvation_type =",solvation_type) print("mult =",mult) print("natoms =",natoms) for sym in symbol: print("number of " + sym + "-atoms = ",input_hash[keys[sym]],energy_dictionary[sym]) print("eref (kcal/mol) =",eref) print("ML E to kcal/mol =",natoms*627.509469) #print("eref =",eref,(output_hash-eref),(output_hash-eref)/natoms/627.509469) print() print("nkeys =",len(keys)) print("keys =",keys) print() print("sizeqm =",2*maxcharge+1+maxmult) print("len(inputlayer) =",len(inputlayer)) print() print("InputLayer =",inputlayer) #print("OutputLayer =",outputlayer) print() nnstr = " inputLayer: " for x in inputlayer: nnstr += str(x) + " " nnstr += ":inputLayer " print(nnstr) #beta = 2.0 #sigmoid = lambda x: 1.0/(1.0+math.exp(-x)) #sigmoidp = lambda x: math.exp(-x)/(1.0+math.exp(-x))**2 #sigmoidpp = lambda x: math.exp(-x)*(math.exp(-x)-1.0)/(1.0+math.exp(-x))**3 #ap = 1.0 #xp = 4.5 #bp = 3.0 #penalty = lambda x: ap*(0.5*(math.tanh(bp*(x-xp)) - math.tanh(bp*(x+xp))) + 1.0) #penaltyp = lambda x: ap*0.5*bp*( (1/math.cosh(bp*(x-xp)))**2 - (1.0/math.cosh(bp*(x+xp)))**2) # #sigmoid = lambda x: 0.5*(math.tanh(beta*x)+1.0) #sigmoidp = lambda x: 0.5*beta*(1.0/math.cosh(beta*x))**2 #sigmoidpp = lambda x: 0.5*(-2.0)*beta*beta*math.tanh(beta*x)*(1.0/math.sech(beta*x))**2 xmoid1 = lambda x: x xmoidp1 = lambda x: 1.0 xmoidpp1 = lambda x: 0.0 relu = lambda x: max(0.0,x) relup = lambda x: 0.0 if (x<=0.0) else 1.0 relupp = lambda x: 0.0 #bias = [[0.01],[0.01],[0.001],[0.0001],[0.00001],[0.0000001]] #bias = [[0.01],[0.01],[0.001]] #bias = [] ### check/create network topology ### print() print("creating network topology - ninput x hidden_layers x 1") print(" - hidden_layers = ( ninput *",hidden_layers,")") print() ### define the network topology ### ntwrk0 = [ninput] ntwrkf = [xmoid1] ntwrkp = [xmoidp1] ntwrkpp= [xmoidpp1] for h in range(len(hidden_layers)): ntwrk0.append(hidden_layers[h]*ninput) ntwrkf.append(relu) ntwrkp.append(relup) ntwrkpp.append(relupp) ntwrk0.append(1) ntwrkf.append(xmoid1) ntwrkp.append(xmoidp1) ntwrkpp.append(xmoidpp1) #network = [[ninput,2*ninput,ninput,1],[xmoid1,relu,relu,xmoid1],[xmoidp1,relup,relup,xmoidp1],[xmoidpp1,relupp,relupp,xmoidpp1]] #machine = myfeedforward.MyFeedForward(ninput,network[0],network[1],network[2],network[3]) machine = myfeedforward.MyFeedForward(ntwrk0,ntwrkf,ntwrkp,ntwrkpp) print("Weights_filename:", weights_filename) if os.path.isfile(weights_filename): print("reading weights") with open(weights_filename,'rb') as ff: weights = pickle.loads(ff.read()) nw = len(weights) print("nw=",nw) ### generate current ytrain ### ytrain=machine.evaluate(inputlayer,weights) print("ytrain=",ytrain) scaled_energy = ytrain[0] energy = 2*edif*scaled_energy + emin final_energy = natoms*627.509469*energy + eref print("energy=",energy) print("final energy=",final_energy,final_energy/23.06/27.2116) return
def main(): # import sys import getopt usage = \ """ esmiles to nn program Usage: tutorial10 -f nn_file.dat -n hidden layers -b nbatch -p nepoch hidden_layers = "2 1" -h prints this message """ print("\ntutorial10 Arrows version") hidden_layers = [2, 1] nbatch = 25 nepoch = 100 mlfilename = "nn_chno_b3lyp.dat" opts, args = getopt.getopt(sys.argv[1:], "hn:b:p:f:") for o, a in opts: if '-n' in o: hidden_layers = [eval(x) for x in a.strip().split()] if '-b' in o: nbatch = eval(a) if '-p' in o: nepoch = eval(a) if '-f' in o: mlfilename = a if o in ("-h", "--help"): print(usage) exit() #weights_filename = "tutorial10" weights_filename = mlfilename.replace(".dat", "") for ix in hidden_layers: weights_filename += "-" + str(ix) weights_filename += ".weights" #param_filename = "tutorial10" param_filename = mlfilename.replace(".dat", "") for ix in hidden_layers: param_filename += "-" + str(ix) param_filename += ".param" ##### Read in fei file lazilly and do a simple print out of the data #### nframes = 0 emin = +99999.99 emax = -99999.99 energies = [] xinputs = [] ids = [] for (id, xdata, energy) in read_ml_urlfile(mlfilename): #print("nframes=",nframes) xinputs.append(xdata) energies.append(energy) ids.append(id) if (energy < emin): emin = energy if (energy > emax): emax = energy nframes += 1 emid = (emax + emin) / 2.0 edif = (emax - emin) / 2.0 ninput = len(xinputs[0]) nframes0 = 0 print() print("Training Data:") print("ninput=", ninput) print("nframes=", nframes) print() print("ReScaling Energies Parameters:") print("emin=", emin) print("emax=", emax) print("emid=", emid) print("edif=", edif) ### writeout param ### with open(param_filename, 'wb') as ff: ff.write(pickle.dumps((ninput, nframes, emin, emax, emid, edif))) scaled_energies = [0.0] * nframes for i in range(nframes): scaled_energies[i] = (energies[i] - emin) / (2 * edif) #beta = 2.0 #sigmoid = lambda x: 1.0/(1.0+math.exp(-x)) #sigmoidp = lambda x: math.exp(-x)/(1.0+math.exp(-x))**2 #sigmoidpp = lambda x: math.exp(-x)*(math.exp(-x)-1.0)/(1.0+math.exp(-x))**3 #ap = 1.0 #xp = 4.5 #bp = 3.0 #penalty = lambda x: ap*(0.5*(math.tanh(bp*(x-xp)) - math.tanh(bp*(x+xp))) + 1.0) #penaltyp = lambda x: ap*0.5*bp*( (1/math.cosh(bp*(x-xp)))**2 - (1.0/math.cosh(bp*(x+xp)))**2) # #sigmoid = lambda x: 0.5*(math.tanh(beta*x)+1.0) #sigmoidp = lambda x: 0.5*beta*(1.0/math.cosh(beta*x))**2 #sigmoidpp = lambda x: 0.5*(-2.0)*beta*beta*math.tanh(beta*x)*(1.0/math.sech(beta*x))**2 xmoid1 = lambda x: x xmoidp1 = lambda x: 1.0 xmoidpp1 = lambda x: 0.0 relu = lambda x: max(0.0, x) relup = lambda x: 0.0 if (x <= 0.0) else 1.0 relupp = lambda x: 0.0 #bias = [[0.01],[0.01],[0.001],[0.0001],[0.00001],[0.0000001]] #bias = [[0.01],[0.01],[0.001]] #bias = [] ### check/create network topology ### print() print("creating network topology - ninput x hidden_layers x 1") print(" - hidden_layers = ( ninput *", hidden_layers, ")") print() ### define the network topology ### ntwrk0 = [ninput] ntwrkf = [xmoid1] ntwrkp = [xmoidp1] ntwrkpp = [xmoidpp1] for h in range(len(hidden_layers)): ntwrk0.append(hidden_layers[h] * ninput) ntwrkf.append(relu) ntwrkp.append(relup) ntwrkpp.append(relupp) ntwrk0.append(1) ntwrkf.append(xmoid1) ntwrkp.append(xmoidp1) ntwrkpp.append(xmoidpp1) #network = [[ninput,2*ninput,ninput,1],[xmoid1,relu,relu,xmoid1],[xmoidp1,relup,relup,xmoidp1],[xmoidpp1,relupp,relupp,xmoidpp1]] #machine = myfeedforward.MyFeedForward(ninput,network[0],network[1],network[2],network[3]) machine = myfeedforward.MyFeedForward(ntwrk0, ntwrkf, ntwrkp, ntwrkpp) print("Weights_filename:", weights_filename) if os.path.isfile(weights_filename): print("reading weights") with open(weights_filename, 'rb') as ff: weights = pickle.loads(ff.read()) else: print("creating initial weights") weights = machine.initial_w() for i in range(len(weights)): weights[i] *= 1.0 nw = len(weights) print("nw=", nw) print() print("Minimization Parameters:") print("nepoch =", nepoch) print("nbatch =", nbatch) ### generate current ytrain ### ytrain = ytrain_generate(machine, weights, xinputs) ###plot the relative energies using Turtle graphics### plot = xyplotter.xyplotter(0.0, 0.0, 1.0, 1.0, "Scaled Energies Plot", 2) plot2 = xyplotter.xyplotter(0.0, 0.0, 1.0, 1.0, "Scaled Diff Energies Plot", 3) plot3 = xyplotter.xyplotter(0.0, 0.0, 1.0, 1.0, "Scaled Energies Correlation Plot", 5) plot4 = xyplotter.xyplotter(0.0, 0.0, 1.0, 1.0, "Scaled Exp. Energy Plot", 6) plot5 = xyplotter.xyplotter(0.0, 0.0, 1.0, 1.0, "Scaled Pred. Energy Plot", 7) iydiff = plot_pathenergy(plot, scaled_energies[nframes0:], ytrain[nframes0:]) yminmax = plot_pathdiff(plot2, scaled_energies[nframes0:], ytrain[nframes0:]) iydiff3 = plot_energycorrelation(plot3, scaled_energies[nframes0:], ytrain[nframes0:]) iydiff4 = plot_pathenergy0(plot4, scaled_energies[nframes0:], "red") iydiff4 = plot_pathenergy0(plot5, ytrain[nframes0:], "blue") plot.print1("energy.ps") plot2.print1("diffenergy.ps") plot3.print1("energy_correlation.ps") plot4.print1("Exp_energy.ps") plot5.print1("Pred_energy.ps") id_min = ids[iydiff[0][0]] id_max = ids[iydiff[1][0]] print("iydiff=", iydiff, " idmin=", id_min, " idmax=", id_max) #enter0 = input(" -- start simulation --- ") print() print(" --- start training ---") print() alpha = 0.0001 beta1 = 0.9 beta2 = 0.999 eps = 1e-8 beta1t = 1.0 beta2t = 1.0 m = [0.0] * nw v = [0.0] * nw gsum = [0.0] * nw for epoch in range(nepoch): batch = 0 error1 = 0.0 for j in range(nw): gsum[j] = 0.0 for i in range(nframes): batch += 1 gg = machine.w_energy_gradient(xinputs[i], [scaled_energies[i]], weights) error = gg[0] g1 = gg[1] error1 += error for j in range(nw): gsum[j] += g1[j] if ((batch >= nbatch) or (i >= (nframes - 1))): xx = 1.0 / float(batch) for j in range(nw): gsum[j] *= xx beta1t *= beta1 beta2t *= beta2 if (beta1t < 1.0e-20): beta1t = 0.0 if (beta2t < 1.0e-20): beta2t = 0.0 alphat = alpha * math.sqrt(1.0 - beta2t) / (1.0 - beta1t) for j in range(nw): m[j] = beta1 * m[j] + (1.0 - beta1) * gsum[j] v[j] = beta2 * v[j] + (1.0 - beta2) * (gsum[j] * gsum[j]) weights[j] -= alphat * m[j] / (math.sqrt(v[j]) + eps) batch = 0 for j in range(nw): gsum[j] = 0.0 ### generate current ytrain ### ytrain = ytrain_generate(machine, weights, xinputs) ### plot the relative scaled_energies using turtle graphicsl ### iydiff = plot_pathenergy(plot, scaled_energies[nframes0:], ytrain[nframes0:]) yminmax = plot_pathdiff(plot2, scaled_energies[nframes0:], ytrain[nframes0:]) iydiff3 = plot_energycorrelation(plot3, scaled_energies[nframes0:], ytrain[nframes0:]) iydiff4 = plot_pathenergy0(plot4, scaled_energies[nframes0:], "red") iydiff4 = plot_pathenergy0(plot5, ytrain[nframes0:], "blue") id_min = ids[iydiff[0][0]] id_max = ids[iydiff[1][0]] ### print out update ### msg = "epoch=%5d nframes=%5d nbatch=%3d " % (epoch + 1, nframes, nbatch) msg += "| min=%5d %6.3f %6.3f %8.3e %5s " % ( iydiff[0][0], iydiff[0][1], iydiff[0][2], iydiff[0][3], id_min) msg += "| max=%5d %6.3f %6.3f %8.3e %5s " % ( iydiff[1][0], iydiff[1][1], iydiff[1][2], iydiff[1][3], id_max) msg += "|| error = %12.8e" % (math.sqrt(error1)) print(msg) ### writeout weights ### with open(weights_filename, 'wb') as ff: ff.write(pickle.dumps(weights)) ### end for epoch ### ### wait for return so that plot can be seen ### x = input("--Press return to finish--") ### writeout weights ### with open(weights_filename, 'wb') as ff: ff.write(pickle.dumps(weights)) plot.print1("final_energy.ps") plot2.print1("final_diffenergy.ps") plot3.print1("final_energy_correlation.ps") plot4.print1("Exp_energy.ps") plot5.print1("Pred_energy.ps") return