# The pattern file is described in
# http://www.nnwj.de/pattern-file-structure.html
"""
Note: A pattern file must be read after the connectLayers() method had been called.
The general structure of a pattern file is as follows:
In the first line:
Number of patterns
In the second line:
Length of input patterns (same as number of neurons in input layer)
In the third line:
Length of target patterns (same as number of neurons in output layer)
Each following line:
The input pattern [whitespace] the target pattern
"""
# Conversion file.
"""
The conversion file of a Backpropagation Net must be used to convert ASCII characters (the net input) to an internal binary representation. The number of binary values that represent the ASCII characters can be changed freely, but has to be the same for each character.
The general structure of a conversion file is as follows:
In the first line:
Number of conversions
Each following line:
- At first position: The ASCII character
- and then: The binary representation (the length of this representation doesn't matter, but must be the same for each conversion)
"""
from jhplot import *
from jhpro.nnet import *
from jhplot.io import *
http="http://datamelt.org/examples/data/"
print Web.get(http+"ascii2bin.cnv") # ascii value -> 6 binary values
print Web.get(http+"towns.pat") # ascii value -> 6 binary values
def compare(recallPattern, targetPattern):
acc=0; xlen=len(recallPattern)
rp,tp = list(recallPattern), list(targetPattern)
for i in range(xlen):
if ( rp[i]==tp[i] ): acc=acc+1
if ( acc == xlen ): return " correct!"
return " ("+str(acc) + "/" + str(xlen) + ": " + str( ((acc*100)/xlen)) + "%)"
def output(bpn):
print "\rcycle: ",bpn.getLearningCycle(), "\n\n\r input: target: output: error:\r"
for x in range(bpn.getNumberOfPatterns()):
print " ",bpn.getInputPattern(x)," ",bpn.getTargetPattern(x)," ",bpn.getOutputPattern(x), " ", bpn.getPatternError(x), "\r"
for r in range (bpn.getNumberOfPatterns()):
recall = bpn.recall( bpn.getInputPattern(r) )
accuracy = compare( recall, bpn.getTargetPattern(r) )
print " recall:'",bpn.getInputPattern(r),"': ",recall, " ", accuracy, "\r"
neterror = bpn.getError();
print "\rminerror:", bpn.getMinimumError(), "\n\rneterror:", neterror
print "\n\rtime :", bpn.getElapsedTime(), " sec" ;
print "\r-----------------------------------------------------";
bpn=BackpropagationNet()
bpn.readConversionFile( "ascii2bin.cnv" ) # 1 ascii value -> 6 binary values
bpn.addNeuronLayer( 10 ) # input layer for towns (must be 10, see pattern file)
bpn.addNeuronLayer( 4 ) # hidden layer (number of neurons may vary)
bpn.addNeuronLayer( 7 ) # output layer for country (must be 7, see pattern file)
bpn.connectLayers()
print "\n\rNet structure:"
for i in range (bpn.getNumberOfLayers()):
print "layer ", i,": ", bpn.getNumberOfNeurons(i)," neurons"
print "weights: ", bpn.getNumberOfWeights()
print "Reading pattern file ..."
bpn.readPatternFile( "towns.pat" )
print "Nr of patterns=",bpn.getNumberOfPatterns()
bpn.setLearningRate( 0.25 )
bpn.setMinimumError( 1E-06 ) # minimum error to stop
bpn.setAccuracy( 0.1 )
bpn.setMaxLearningCycles( -1 ) # -1 = no maximum cycle
bpn.setDisplayStep(500) # show avery 500 steps
bpn.resetTime()
while ( bpn.finishedLearning()==False ):
bpn.learn()
if bpn.displayNow(): output(bpn) # display the current results
output(bpn)
from jhplot import *
IO.write(bpn,"backpropagation.jser")
print "\n\rFINISHED and saved as backpropagation.jser"
Ads help maintain this website.