# Licensed under the Apache License, Version 2.0 (the "License");
# based on http://www.heatonresearch.com/encog/
# This example shows how to construct an Encog neural
# network to predict the output from the XOR operator. This example
# uses backpropagation to train the neural network.
from org.encog.ml.data.basic import BasicMLDataSet
from org.encog.neural.networks import BasicNetwork
from org.encog.neural.networks.layers import BasicLayer
from org.encog.neural.networks.training.propagation.resilient import ResilientPropagation
from org.encog.engine.network.activation import ActivationSigmoid
XOR_INPUT = [ [ 0.0, 0.0 ], [1.0, 0.0], [0.0, 1.0], [1.0, 1.0]]
XOR_IDEAL = [ [ 0.0 ], [1.0], [1.0], [0.0]]
network = BasicNetwork()
network.addLayer(BasicLayer(None,True,2))
network.addLayer(BasicLayer(ActivationSigmoid(),True,3))
network.addLayer(BasicLayer(ActivationSigmoid(),False,1))
network.getStructure().finalizeStructure()
network.reset()
# train
trainingSet = BasicMLDataSet(XOR_INPUT, XOR_IDEAL)
train = ResilientPropagation(network, trainingSet) # train network
# iterations
epoch=1
error=1
while (error > 0.01):
train.iteration()
error=train.getError()
if (epoch%10==0): print "Epoch #", epoch ," Error:",error
epoch+=1
print "-> Test Trained Neural Network"
for pair in trainingSet:
output = network.compute(pair.getInput())
print pair.getInput().getData(0), "," , pair.getInput().getData(1), ", actual=", output.getData(0), ",ideal=",pair.getIdeal().getData(0)
Ads help maintain this website.