label: 'y data' } } }); </script> ''' % (xlabel) msg4 += "</html>\n" return msg4 ####################### main program ############################## ### started from scratch - build up path from l=0 ### plot = xyplotter.xyplotter(0.0, 0.0, 1.0, 1.0, "Scaled Energies Plot", 2) ##### Read in fei file lazilly and do a simple print out of the data #### nframes = 0 emin = +99999.99 emax = -99999.99 energies = [] d1min = +999999.9 d1max = -999999.9 dist1 = [] d2min = +999999.9 d2max = -999999.9 dist2 = []
sigmoidp, sigmoidp, sigmoidp, xmoidp3, sigmoidp, xmoidp2, sigmoidp, xmoidp1 ], [ sigmoidpp, sigmoidpp, sigmoidpp, xmoidpp3, sigmoidpp, xmoidpp2, sigmoidpp, xmoidpp1 ], bias) #machine = myfeedforward.MyFeedForward([1,1,1,1,1,1,1],[sigmoid,sigmoid,sigmoid,sigmoid,xmoid3,xmoid2,xmoid1],[sigmoidp,sigmoidp,sigmoidp,sigmoidp,xmoidp3,xmoidp2,xmoidp1],[sigmoidpp,sigmoidpp,sigmoidpp,sigmoidpp,xmoidpp3,xmoidpp2,xmoidpp1],bias) #machine = myfeedforward.MyFeedForward([1,1,1,1,1,1],[sigmoid,sigmoid,sigmoid,sigmoid,xmoid2,xmoid1],[sigmoidp,sigmoidp,sigmoidp,sigmoidp,xmoidp2,xmoidp1],[sigmoidpp,sigmoidpp,sigmoidpp,sigmoidpp,xmoidpp2,xmoidpp1],bias) weights = machine.initial_w() for i in range(len(weights)): weights[i] = 1.0 plot = xyplotter.xyplotter(-1.0, 0.0, 1.0, 1.0, "Simple Plot for Raymond", 2) plote(plot, A, machine, weights) for i in range(1000000): x = 2.5 * random.random() - 1.25 e = A * x * x e1 = machine.evaluate([x], weights)[0] gg = machine.w_energy_gradient([x], [e], weights) error = gg[0] g1 = gg[1] for j in range(len(g1)): weights[j] -= alpha * g1[j] #print "x,e,e1,error,w=",x,e,e1,error,weights print x, e, e1, ( e1 - e)**2, error, weights[0], weights[1], weights[2], weights[3]
[xmoidpp1, sigmoidpp, sigmoidpp, xmoidpp1], bias) weights = machine.initial_w() for i in range(len(weights)): weights[i] *= 1.0 nw = len(weights) print "weights=", weights m = [0.0] * len(weights) v = [0.0] * len(weights) beta1t = 1.0 beta2t = 1.0 plot = xyplotter.xyplotter(-1.0, 0.0, 1.0, 1.0, "Spring Energies,xp=4.5", 4) plote(plot, xsdat, esdat, machine, weights) enter0 = raw_input(" -- start simulation --- ") error = 999999.9 ii = 0 for i in range(1000000): #x = 4.0*random.random()-2.0 #ii = random.randint(0,len(xdat)-1) xs = xsdat[ii] es = esdat[ii] ii = ((ii + 1) % len(xsdat))
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
#machine = myfeedforward.MyFeedForward([1,1,1,1,1,1,1],[sigmoid,sigmoid,sigmoid,sigmoid,xmoid3,xmoid2,xmoid1],[sigmoidp,sigmoidp,sigmoidp,sigmoidp,xmoidp3,xmoidp2,xmoidp1],[sigmoidpp,sigmoidpp,sigmoidpp,sigmoidpp,xmoidpp3,xmoidpp2,xmoidpp1],bias) #machine = myfeedforward.MyFeedForward([1,1,1,1,1,1],[sigmoid,sigmoid,sigmoid,sigmoid,xmoid2,xmoid1],[sigmoidp,sigmoidp,sigmoidp,sigmoidp,xmoidp2,xmoidp1],[sigmoidpp,sigmoidpp,sigmoidpp,sigmoidpp,xmoidpp2,xmoidpp1],bias) weights = machine.initial_w() for i in range(len(weights)): weights[i] *= 1.0 weights0 = weights[:] m = [0.0] * len(weights) v = [0.0] * len(weights) beta1t = 1.0 beta2t = 1.0 plot = xyplotter.xyplotter(-1.0, 0.0, 1.0, 1.0, "Spring Energies", 4) plote(plot, A, machine, weights, weights0) enter0 = raw_input(" -- start simulation --- ") for i in range(1000000): x = 4.0 * random.random() - 2.0 #e = A*x*x e = A * (x * x - 0.1 * x**6) e1 = machine.evaluate([x], weights)[0] gg = machine.w_energy_gradient([x], [e], weights) error = gg[0] g1 = gg[1] beta1t *= beta1
p += [float(s)] ok = p[0] in range(1, 6) if (p[0] == 1): ok = ok and (len(p) == 2) if (p[0] == 2): ok = ok and (len(p) == 4) if (p[0] == 3): ok = ok and (len(p) == 3) if (p[0] == 4): ok = ok and (len(p) == 5) if (p[0] == 5): ok = ok and (len(p) == 5) if ok: parameters.append(p) print("#parameters=", parameters, len(parameters)) #### define Atomic Feature Mapping #### afm = atomfm.AtomsFM(parameters) ## Use turtle plotter for running plots ### plot = xyplotter.xyplotter(0.0, 0.0, 1.0, 1.0, "Plot of Feature Functions", 4) ##### read in atoms lazilly - For debugging only generate features functions of one atom #### #### plotting the logarithm of the feature functions #### iatom = 0 # change the atom data = '' count = 0 for (symbols, rions, fions, energy) in read_fei_urlfile(feifilename): nion = len(symbols) framefm = [] #for ii in range(nion): # framefm.append(afm(rions,ii)) framefm.append(afm(rions, iatom)) sstr = "%d " % count for i in range(52):