t = BackpropTrainer(n, learningrate = 0.01 ,
                    momentum = mom)
#train the neural network from the train DataSet

cterrori=1.0
print "trainer momentum:"+str(mom)
for iter in range(25):
  t.trainOnDataset(trndata, 1000)
  ctrndata = mv.calculateModuleOutput(n,trndata)
  cterr = v.MSE(ctrndata,trndata['target'])
  relerr = abs(cterr-cterrori)
  cterrori = cterr
  print 'iteration:',iter+1,'MSE error:',cterr
  myplot(trndata,ctrndata,iter=iter+1)
  if cterr < 1.e-5 or relerr < 1.e-7:
    break
#write the network using xml file     
myneuralnet = os.path.join(os.getcwd(),'myneuralnet.xml')
if os.path.isfile(myneuralnet):
    NetworkWriter.appendToFile(n,myneuralnet)
else:
    NetworkWriter.writeToFile(n,myneuralnet)
    
#calculate the test DataSet based on the trained Neural Network
ctsts = mv.calculateModuleOutput(n,tsts)
tserr = v.MSE(ctsts,tsts['target'])
print 'MSE error on TSTS:',tserr
myplot(trndata,ctrndata,tsts,ctsts)

pylab.show()
Exemple #2
0
t = BackpropTrainer(n, learningrate=0.01, momentum=mom)
#train the neural network from the train DataSet

cterrori = 1.0
print("trainer momentum:" + str(mom))
for iter in range(25):
    t.trainOnDataset(trndata, 1000)
    ctrndata = mv.calculateModuleOutput(n, trndata)
    cterr = v.MSE(ctrndata, trndata['target'])
    relerr = abs(cterr - cterrori)
    cterrori = cterr
    print('iteration:', iter + 1, 'MSE error:', cterr)
    myplot(trndata, ctrndata, iter=iter + 1)
    if cterr < 1.e-5 or relerr < 1.e-7:
        break
#write the network using xml file
myneuralnet = os.path.join(os.getcwd(), 'myneuralnet.xml')
if os.path.isfile(myneuralnet):
    NetworkWriter.appendToFile(n, myneuralnet)
else:
    NetworkWriter.writeToFile(n, myneuralnet)

#calculate the test DataSet based on the trained Neural Network
ctsts = mv.calculateModuleOutput(n, tsts)
tserr = v.MSE(ctsts, tsts['target'])
print('MSE error on TSTS:', tserr)
myplot(trndata, ctrndata, tsts, ctsts)

pylab.show()