Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class NeuralNode
- {
- private double value;
- private double[] weights; //weights going out of this node
- private double bias;
- private double delta;
- public NeuralNode(double val)
- {
- value = val;
- }
- public void setValue(double val)
- {
- value = val;
- }
- public double getValue()
- {
- return value;
- }
- public void setWeights(double[] weights)//sets weights from this node to the connecting nodes of the next layer
- {
- this.weights = weights;
- }
- public double getWeightTo(int i)
- {
- return weights[i];
- }
- public double getBias()
- {
- return bias;
- }
- public void setBias(double bias)
- {
- this.bias = bias;
- }
- public double getDelta()
- {
- return delta;
- }
- public void setDelta(double delta)
- {
- this.delta = delta;
- }
- }
- =================================================
- public class NeuralLayer
- {
- NeuralNode[] nodes;
- public NeuralLayer(int numNodes)
- {
- nodes = new NeuralNode[numNodes];
- for (int i=0; i<nodes.length; i++)
- {
- nodes[i] = new NeuralNode(0.0);
- }
- }
- public void setNodeAt(int pos, NeuralNode node)
- {
- nodes[pos] = node;
- }
- public NeuralNode getNodeAt(int i)
- {
- return nodes[i];
- }
- public NeuralNode[] getNodes()
- {
- return nodes;
- }
- public int getSize()
- {
- return nodes.length;
- }
- public String toString()
- {
- String s = "";
- for (int i=0; i<nodes.length; i++)
- s += (nodes[i].getValue() + " ");
- return s;
- }
- }
- ==================================================
- import java.lang.*;
- /** Neural Network class has an array of layers
- * each layer has an array of nodes
- * and each node has an array of weights coming from it
- * excluding those in the output layer.
- */
- public class NeuralNetwork
- {
- private static NeuralLayer[] layers;
- private NeuralLayer inputs;
- private double[] targets;
- private double trainingRate;
- //numLayers would be an int array telling how many nodes per layer
- public NeuralNetwork(NeuralLayer inputLayer, double[] targetValues, int[] numLayers)
- {
- layers = new NeuralLayer[numLayers.length];
- for (int i=0; i<layers.length; i++)
- layers[i] = new NeuralLayer(numLayers[i]);
- inputs = inputLayer;
- targets = targetValues;
- layers[0] = inputs;
- }
- public NeuralNetwork(double[] inputs, double[] targetValues, int[] numLayers)
- {
- layers = new NeuralLayer[numLayers.length];
- for (int i=0; i<layers.length; i++)
- layers[i] = new NeuralLayer(numLayers[i]);
- NeuralNode[] inputNodes = new NeuralNode[inputs.length];
- for (int i=0; i<inputs.length; i++)
- {
- inputNodes[i] = new NeuralNode(inputs[i]);
- }
- this.inputs = new NeuralLayer(inputNodes.length);
- targets = targetValues;
- layers[0] = this.inputs;
- }
- public void initializeRandomWeights() //set random weights and bias
- {
- NeuralLayer nextLayer; //layer of nodes recieving the weights
- double[] rWeights; //random weights
- double rBias;
- for (int L=0; L<layers.length-1; L++)
- {
- for (int n=0; n<layers[L].getSize(); n++)
- { //I initially had this statement after "nextLayer = layers[l+1];" but I realized it
- rWeights = randomWeights(L); //has to be in this loop
- rBias = Math.random()*2; //or else it would send the same array to all the nodes
- layers[L].getNodeAt(n).setBias(rBias);
- layers[L].getNodeAt(n).setWeights(rWeights);
- }
- }
- }
- public double[] randomWeights(int layer)
- {
- int size = layers[layer+1].getSize();
- double[] w = new double[size];
- for (int i=0; i<size; i++)
- w[i] = Math.random()*2;
- return w;
- }
- public double getWeightFrom(int layer, int nodeFrom, int nodeTo) //layer,
- { // node in the layer,
- return layers[layer].getNodeAt(nodeFrom).getWeightTo(nodeTo); //which node in the next layer
- }
- public double sigmoid(double x)
- {
- return 1.0/(1.0 + Math.exp(-x));
- }
- public double sigmoidDerivative(double x)
- {
- return sigmoid(x)*(1 - sigmoid(x));
- }
- public double calcNode(int layer, int node)
- {
- double sum = 0.0;
- for (int i=0; i<layers[layer-1].getSize(); i++)
- {
- sum += layers[layer-1].getNodeAt(i).getValue()*getWeightFrom(layer-1, i, node);
- }
- sum += layers[layer].getNodeAt(node).getBias();
- return sigmoid(sum);
- }
- public void forward()
- {
- NeuralNode node;
- for(int L=1; L<layers.length; L++)
- {
- for (int n=0; n<layers[L].getSize(); n++)
- {
- node = layers[L].getNodeAt(n);
- node.setValue(calcNode(L, n));
- }
- }
- }
- public void calcOutputDelta() //sets the deltas for each node in output layer
- {
- int t = 0;
- double val;
- NeuralNode[] outputs = layers[layers.length-1].getNodes();
- for(NeuralNode output: outputs)
- {
- val = output.getValue();
- output.setDelta(val*(1-val)*(targets[t]-val));
- t++;
- }
- }
- public void updateWeights(int layer) //updates the weights for the layer sending out those weights
- {
- NeuralLayer nextLayer = layers[layer+1];
- double[] weights = new double[nextLayer.getSize()];
- double nextNodeDelta; //delta of next layer's node; output is of current layer
- double weightDiff; //change in weight
- NeuralLayer L = layers[layer];
- for (int n=0; n<L.getSize(); n++)
- {
- for (int w=0; w<nextLayer.getSize(); w++)
- {
- nextNodeDelta = nextLayer.getNodeAt(w).getDelta();
- weightDiff = (-1*trainingRate)*nextNodeDelta*L.getNodeAt(n).getValue();
- weights[w] = L.getNodeAt(n).getWeightTo(w) + weightDiff;
- }
- L.getNodeAt(n).setWeights(weights);
- }
- }
- public void updateBias(int layer) //updates the bias's for any layer except the input layer
- {
- NeuralNode node;
- double newBias;
- for (int n=0; n<layers[layer].getSize(); n++)
- {
- node = layers[layer].getNodeAt(n);
- newBias = (-1*trainingRate)*node.getDelta();
- node.setBias(newBias);
- }
- }
- public void calcLayerDelta(int L) //caluculates the delta value for nodes of a layer
- {
- double summation = 0.0;
- NeuralNode node;
- for(int n=0; n<layers[L].getSize(); n++)
- {
- node = layers[L].getNodeAt(n);
- for(int m=0; m<layers[L+1].getSize(); m++)
- summation += (layers[L+1].getNodeAt(m).getDelta()*getWeightFrom(L, n, m));
- node.setDelta(node.getValue()*(1-node.getValue())*summation);
- }
- }
- public void runAll(int numTimes) //number of iterations for running the network
- {
- initializeRandomWeights();
- int count=0;
- trainingRate = .9;
- while (count < numTimes)
- {
- forward();
- calcOutputDelta();
- System.out.println("outputs: " + layers[layers.length-1].toString());
- for (int L=layers.length-1; L>0; L--)
- {
- updateWeights(L-1);
- updateBias(L);
- calcLayerDelta(L-1);
- }
- count++;
- //trainingRate *= .9999;
- }
- }
- public static void main(String[] args)
- {
- int[] design = new int[] {1, 3, 1}; //2 nodes in input layer, 3 in hidden, and 2 in output
- NeuralLayer input = new NeuralLayer(1);
- input.setNodeAt(0, new NeuralNode(2.0));
- //input.setNodeAt(1, new NeuralNode(1.0));
- System.out.println("inputs: " + input.toString());
- double[] target = new double[]{4.0};
- String t = "";
- for (int i=0; i<target.length; i++)
- t+=target[i] + " ";
- System.out.println("targets: " + t);
- NeuralNetwork net = new NeuralNetwork(input, target, design);
- net.runAll(20);
- //System.out.println("outputs: " + layers[layers.length-1].toString());
- }
- }
Add Comment
Please, Sign In to add comment