📄 neuralnetwork.java
字号:
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(fos);
// transform source into result will do save
transformer.setOutputProperty("encoding","iso-8859-2");
transformer.setOutputProperty("indent","yes");
transformer.transform(source, result);
}
public static class SetOfIOPairs {
Vector<IOPair> pairs;
public static class IOPair { // TU SOM PRIDAL STATIC, posovdne do tam nebolo
Vector<Double> inputs;
Vector<Double> outputs;
public IOPair(Vector<Double> inputs, Vector<Double> outputs) {
// corrected warning
//this.inputs = (Vector<Double>)inputs.clone();
//this.outputs = (Vector<Double>)outputs.clone();
this.inputs = new Vector<Double>(inputs);
this.outputs = new Vector<Double>(outputs);
}
}
public SetOfIOPairs() {
this.pairs = new Vector<IOPair>();
}
public void addIOPair(Vector<Double> inputs, Vector<Double> outputs) {
this.addIOPair(new IOPair(inputs,outputs));
}
public void addIOPair(IOPair pair) {
this.pairs.add(pair);
}
int size() {
return pairs.size();
}
}
private class NeuralInput {
double weight;
int index;
Neuron neuron;
NeuralInput(double weight, Neuron neuron) {
this.neuron = neuron;
this.weight = weight;
this.index = this.neuron.numberOfInputs();
//System.out.println("Created neural input "+this.index+" with weight "+this.weight);
}
} // end class NeuralInput
private class Neuron {
private Vector<NeuralInput> listInputs = new Vector<NeuralInput>();//holds list of inputs
int index;
public double threshold;
public double output;
NeuralLayer neuralLayer;
// initializes all neuron weights to 1 parameter specifies number of weights
Neuron(double threshold, NeuralLayer neuralLayer) {
this.threshold = threshold;
this.neuralLayer = neuralLayer;
this.index = this.neuralLayer.numberOfNeurons();
}
Neuron(int numberOfInputs, double threshold, NeuralLayer neuralLayer) {
this.threshold = threshold;
this.neuralLayer = neuralLayer;
this.index = this.neuralLayer.numberOfNeurons();
for (int i=0; i<numberOfInputs; i++) {
this.listInputs.add(new NeuralInput(1.0,this));
}
}
public int numberOfInputs() {
return this.listInputs.size();
}
public NeuralInput getInput (int index) {
return this.listInputs.elementAt(index);
}
} // end class Neuron
private class NeuralLayer {
//holds list od neurons
private Vector<Neuron> listNeurons = new Vector<Neuron>();
int index;
NeuralNetwork neuralNetwork;
NeuralLayer(NeuralNetwork neuralNetwork) {
this.neuralNetwork = neuralNetwork;
this.index = this.neuralNetwork.numberOfLayers();
}
// initializes all neurons in layer
NeuralLayer(int numberOfNeurons, NeuralNetwork neuralNetwork) {
this.neuralNetwork = neuralNetwork;
this.index = this.neuralNetwork.numberOfLayers();
// ak sa jedna o najnizsiu vrstvu (0), kazdy neuron bude mat iba 1 vstup
for (int i=0; i<numberOfNeurons;i++) {
if (this.index == 0) {
this.listNeurons.add(new Neuron(1,0.0,this));
/* prahy neuronov najnizsej vrstvy su vzdy 0.0, vrstva iba distribuuje vstupy, aspon tak to vyplyva
* z algoritmu na strane 111 */
} else { // v opacnom pripade bude mat neuron tolko vstupov kolko je neuronov nizsej vrstvy
this.listNeurons.add(
/* prahy neuronou na vyssich vrstvach budu tiez 0.0, ale nemusia byt */
new Neuron(this.neuralNetwork.getLayer(this.index-1).numberOfNeurons(), 0.0, this)
);
}
}
//System.out.println("Created neural layer "+this.index+" with "+numberOfNeurons+" neurons");
} // end constructor
public int numberOfNeurons() {
return this.listNeurons.size();
}
public boolean isLayerTop() {
return (this.index == this.neuralNetwork.numberOfLayers()-1);
}
public boolean isLayerBottom() {
return (this.index == 0);
}
public NeuralLayer upperLayer() {
if (this.isLayerTop()) return null;
return this.neuralNetwork.getLayer(index+1);
}
public NeuralLayer lowerLayer() {
if (this.isLayerBottom()) return null;
return this.neuralNetwork.getLayer(index-1);
}
public Neuron getNeuron(int index) {
return this.listNeurons.elementAt(index);
}
} // end class NeuralLayer
private class Gradients {
Vector<Vector<Double>> thresholds;
Vector<Vector<Vector<Double>>> weights;
NeuralNetwork neuralNetwork;
Gradients(NeuralNetwork network) {
this.neuralNetwork = network;
this.initGradients();
}
public void initGradients() {
this.thresholds = new Vector<Vector<Double>>();
this.weights = new Vector<Vector<Vector<Double>>>();
//System.out.println("init for threshold gradient "+this.toString());
for (int il = 0; il < this.neuralNetwork.numberOfLayers(); il++) {
this.thresholds.add(new Vector<Double>());
this.weights.add(new Vector<Vector<Double>>());
for (int in = 0; in < this.neuralNetwork.getLayer(il).numberOfNeurons(); in++) {
this.thresholds.elementAt(il).add(0.0);
this.weights.elementAt(il).add(new Vector<Double>());
for (int ii = 0; ii < this.neuralNetwork.getLayer(il).getNeuron(in).numberOfInputs(); ii++) {
this.weights.elementAt(il).elementAt(in).add(0.0);
} // for each input
} // for each neuron
} // for each layer
}
public void resetGradients() { //resets to 0
for (int il = 0; il < this.neuralNetwork.numberOfLayers(); il++) {
for (int in = 0; in < this.neuralNetwork.getLayer(il).numberOfNeurons(); in++) {
this.setThreshold(il,in,0.0);
for (int ii = 0; ii < this.neuralNetwork.getLayer(il).getNeuron(in).numberOfInputs(); ii++) {
this.setWeight(il,in,ii,0.0);
}
}
}
}
public double getThreshold(int il, int in) {
return thresholds.elementAt(il).elementAt(in).doubleValue();
}
public void setThreshold(int il, int in, double value) {
thresholds.elementAt(il).setElementAt(value, in);
}
public void incrementThreshold(int il, int in, double value) {
this.setThreshold(il,in,this.getThreshold(il,in) + value);
}
public double getWeight (int il, int in, int ii) {
return weights.elementAt(il).elementAt(in).elementAt(ii).doubleValue();
}
public void setWeight (int il, int in, int ii, double value) {
weights.elementAt(il).elementAt(in).setElementAt(value,ii);
}
public void incrementWeight(int il, int in, int ii, double value) {
this.setWeight(il,in,ii,this.getWeight(il,in,ii) + value);
}
public double getGradientAbs() {
double currE = 0;
for (int il=1;il<neuralNetwork.numberOfLayers();il++) {
currE += this.vectorAbs(thresholds.elementAt(il));
currE += this.doubleVectorAbs(weights.elementAt(il));
}
return currE;
//for (Vector<Double> vector : this.thresholds) currE += this.vectorAbs(vector);
//for (Vector<Vector<Double>> doubleVector : this.weights) currE += this.doubleVectorAbs(doubleVector);
//return currE;
}
private double doubleVectorAbs(Vector<Vector<Double>> doubleVector) {
double totalX = 0;
for (Vector<Double> vector : doubleVector) {
totalX += Math.pow(vectorAbs(vector),2);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -