public class NeuralNetwork extends Object
NeuralNetwork implements a back-propagation neural
network, with one input layer, one hidden layer and one output layer.
The transfer function is the sigmoid.
This neuralNetwork class can be stored on disk in XML form (through
the marshal(java.io.OutputStream) and unmarshal(java.io.InputStream) methods).
The class also allows in-memory backup() and restore(jhpro.nnet.NeuralNetwork.Backup)
operation, mainly used to save the most performant weight values during the
network training.
| Modifier and Type | Class and Description |
|---|---|
static class |
NeuralNetwork.Backup
Class
Backup is an opaque class that encapsulates a
snapshot of a NeuralNetwork internal memory (its weights). |
static interface |
NeuralNetwork.Monitor
Interface
Monitor allows to plug a monitor to a Neural
Network instance, and inform the monitor about the progress of
the training activity. |
| Constructor and Description |
|---|
NeuralNetwork(int inputSize,
int hiddenSize,
int outputSize,
double amplitude,
String[] inputLabels,
String[] outputLabels)
Create a neural network, with specified number of cells in each
layer, and default values.
|
NeuralNetwork(int inputSize,
int hiddenSize,
int outputSize,
double amplitude,
String[] inputLabels,
String[] outputLabels,
double learningRate,
double momentum,
double maxError,
int epochs)
Create a neural network, with specified number of cells in each
layer, and specific parameters
|
| Modifier and Type | Method and Description |
|---|---|
NeuralNetwork.Backup |
backup()
Return a backup of the internal memory of this network.
|
void |
dump()
Dumps the network
|
int |
getHiddenSize()
Report the number of cells in the hidden layer
|
String[] |
getInputLabels()
Report the input labels, if any.
|
int |
getInputSize()
Report the number of cells in the input layer
|
String[] |
getOutputLabels()
Report the output labels, if any.
|
int |
getOutputSize()
Report the size of the output layer
|
void |
marshal(OutputStream os)
Marshal the NeuralNetwork to its XML file
|
void |
restore(NeuralNetwork.Backup backup)
Restore the internal memory of a Network, from a previous Backup.
|
double[] |
run(double[] inputs,
double[] hiddens,
double[] outputs)
Run the neural network on an array of input values, and return the
computed output values.
|
void |
setEpochs(int epochs)
Set the number of iterations for training the network with a
given input.
|
void |
setLearningRate(double learningRate)
Set the learning rate.
|
void |
setMaxError(double maxError)
Set the maximum error level.
|
void |
setMomentum(double momentum)
Set the momentum value.
|
void |
stop()
A means to externally stop the current training.
|
double |
train(double[][] inputs,
double[][] desiredOutputs,
NeuralNetwork.Monitor monitor)
Train the neural network on a collection of input patterns,
so that it delivers the expected outputs within maxError.
|
static NeuralNetwork |
unmarshal(InputStream in)
Unmarshal the provided XML stream to allocate the corresponding
NeuralNetwork.
|
public NeuralNetwork(int inputSize,
int hiddenSize,
int outputSize,
double amplitude,
String[] inputLabels,
String[] outputLabels)
inputSize - number of cells in input layerhiddenSize - number of cells in hidden layeroutputSize - number of cells in output layeramplitude - amplitude (<=1.0) for initial random valuesinputLabels - array of labels for input cells, or nulloutputLabels - array of labels for output cells, or nullpublic NeuralNetwork(int inputSize,
int hiddenSize,
int outputSize,
double amplitude,
String[] inputLabels,
String[] outputLabels,
double learningRate,
double momentum,
double maxError,
int epochs)
inputSize - number of cells in input layerhiddenSize - number of cells in hidden layeroutputSize - number of cells in output layeramplitude - amplitude ( <= 1.0) for initial random valuesinputLabels - array
o f
labels
for
input
cells,
or
nulloutputLabels - array of labels for output cells, or nulllearningRate - learning rate factormomentum - momentum from last adjustmentmaxError - threshold to stop trainingepochs - number of epochs in trainingpublic static NeuralNetwork unmarshal(InputStream in) throws JAXBException
in - the input stream that contains the network definition in XML
format. The stream is not closed by this methodJAXBException - raised when unmarshalling goes wrongpublic NeuralNetwork.Backup backup()
public void dump()
public int getHiddenSize()
public String[] getInputLabels()
public int getInputSize()
public String[] getOutputLabels()
public int getOutputSize()
public void marshal(OutputStream os) throws JAXBException
os - the XML output stream, which is not closed by this methodJAXBException - raised when marshalling goes wrongpublic void restore(NeuralNetwork.Backup backup)
backup - a backup previously madepublic double[] run(double[] inputs,
double[] hiddens,
double[] outputs)
inputs - the provided input valueshiddens - provided buffer for hidden values, or nulloutputs - preallocated array for the computed output values, or null
if not already allocatedpublic void setEpochs(int epochs)
epochs - number of iterationspublic void setLearningRate(double learningRate)
learningRate - the learning rate to use for each iteration
(typically in the 0.0 .. 1.0 range)public void setMaxError(double maxError)
maxError - maximum errorpublic void setMomentum(double momentum)
momentum - the fraction of previous move to be reported on the next
correctionpublic void stop()
public double train(double[][] inputs,
double[][] desiredOutputs,
NeuralNetwork.Monitor monitor)
inputs - the provided patterns of values for input cellsdesiredOutputs - the corresponding desired values for output cellsmonitor - a monitor interface to be kept informed (or null)DataMelt-1.1. http://jwork.org/dmelt/ ©