from org.joone.engine import NeuralNetListener
from org.joone.engine import SigmoidLayer,FullSynapse,Monitor
from org.joone.io import MemoryInputSynapse
from org.joone.engine.learning import TeachingSynapse
from org.joone.net import NeuralNet
from java.lang import System
class joone(NeuralNetListener):
mills=0
err=-1
def Go(self,epochs,inputArray):
print "Running NN for ", epochs, " epochs"
input=SigmoidLayer() # create 3 layers
hidden=SigmoidLayer()
output=SigmoidLayer()
input.setLayerName("input")
hidden.setLayerName("hidden")
output.setLayerName("output")
input.setRows(2); hidden.setRows(3); output.setRows(1)
synapse_IH = FullSynapse() # input -> hidden conn.
synapse_HO = FullSynapse() # hidden -> output conn.
synapse_IH.setName("IH")
synapse_HO.setName("HO")
input.addOutputSynapse(synapse_IH) # Connect the input layer with the hidden layer
hidden.addInputSynapse(synapse_IH)
hidden.addOutputSynapse(synapse_HO) # Connect the hidden layer with the output layer
output.addInputSynapse(synapse_HO)
inputStream = MemoryInputSynapse()
inputStream.setInputArray(inputArray)
inputStream.setAdvancedColumnSelector("1,2") # The first two columns contain the input
input.addInputSynapse(inputStream) # set input data
trainer = TeachingSynapse()
samples = MemoryInputSynapse() # file containing the desired responses
samples.setInputArray(inputArray) #The output values are on the third column of the file
samples.setAdvancedColumnSelector("3")
trainer.setDesired(samples)
output.addOutputSynapse(trainer) # Connects the Teacher to the last layer
nnet = NeuralNet() # Creates a new NeuralNet
nnet.addLayer(input, NeuralNet.INPUT_LAYER) # insert all layers
nnet.addLayer(hidden, NeuralNet.HIDDEN_LAYER)
nnet.addLayer(output, NeuralNet.OUTPUT_LAYER)
monitor = nnet.getMonitor()
monitor.setTrainingPatterns(4) # of rows (patterns) contained in the input file
monitor.setTotCicles(epochs) # How many times the net must be trained on the input patterns
monitor.setLearningRate(0.7)
monitor.setMomentum(0.6)
monitor.setLearning(True) # The net must be trained
monitor.setSingleThreadMode(True) # Set to false for multi-thread mode
monitor.addNeuralNetListener(self)
self.mills = System.currentTimeMillis()
nnet.randomize(0.5)
nnet.go(True)
def netStopped(self,e):
delay = System.currentTimeMillis() - self.mills
print "Training finished after ",delay," ms"
mon = e.getSource()
self.err=mon.getGlobalError()
def cicleTerminated(self,e): pass
def netStarted(self,e): pass
def errorChanged(self,e):
mon = e.getSource()
c = mon.getCurrentCicle()
cl = c / 1000
if ((cl * 1000) == c): print c," cycles remaining - Error = ", mon.getGlobalError()
def getError(self): return self.err
def netStoppedError(self,e): pass
inputArray=[[0.0, 0.0, 0.0],[0.0, 1.0, 1.0],[1.0, 0.0, 1.0],[1.0, 1.0, 0.0]]
xor=joone()
xor.Go(20000,inputArray)
print "Global RMSE after training=", xor.getError()