Backpropogation neural network training/analysis
Code: "neural_net_analysis.py". Programming language: Python DMelt Version 1.4. Last modified: 12/20/1970. License: Pro
https://datamelt.org/code/cache/neural_net_analysis_899.py
To run this script using the DMelt IDE, copy the above URL link to the menu [File]→[Read script from URL] of the DMelt IDE.


from java.util import Random
from jhplot  import *
from jhplot.io import *

Events=1000
input=PND('Data')
out=PND('Output')
r= Random()
for i in range(Events):
    mass=r.nextDouble()*10
    acceler=r.nextDouble()*10
    force=mass*acceler
    input.add([mass,acceler])
    out.add([force])
d={'input':input,'out':out}

# scale output
input=d['input']
out=d["out"]

# scale_input=input.rescale(1)
# scale_output=out.rescale(1)
input.standardize()
scale=out.rescale(0)

# print input
# print out

# import  sys
# sys.exit(0)

input=d['input'].getRows('input',0,(int)(0.5*Events))
out=d['out'].getRows('result',0,(int)(0.5*Events))

net = HNeuralNet()
net.addFeedForwardLayer(2)
net.addFeedForwardLayer(2)
net.addFeedForwardLayer(1)
net.reset()

net.setData(input, out)
print net.trainBackpropagation(1,5000,0.01,0.025,0.002)
print "Epoch error=",net.getEpochError()
net.save("test.eg")
net.showNetwork()

min,max =(int)(0.5*Events),Events
input=d['input'].getRows('input',min,max)
out=d["out"].getRows('result',min,max)

net=HNeuralNet()
net.read('test.eg');

pred=net.predict(input)
pred.rescale(scale)
out.rescale(scale)

d={'predicted':pred,'expected':out}
predicted=d['predicted']
expected=d['expected']
ratio=predicted.copy('ratio')
ratio.oper(expected,'/')

c1=HPlot()
c1.visible()
c1.setRange(0,2,0,100)

h=H1D('ratio',50,0,2.)
h.fill(ratio)
c1.draw(h)
c1.drawStatBox(h)

for i in range( (int)(0.5*Events)): # check first 100 events
   p=predicted.getRow(i)
   x=expected.getRow(i)
   d1=p.get(0)
   d2=x.get(0)
   print i,'predicted=',d1,' expected=',d2


Ads help maintain this website.