public class BackPropagationStrategy implements IStrategy, Serializable { [...] public void updateWeightsAndBias(NeuralNetwork neuralNetwork, double[] inputs) { for (int i = neuralNetwork.getLayers().size() - 1; i >= 0; i--) { Layer layer = neuralNetwork.getLayers().get(i); for (Neuron neuron : layer.getNeurons()) { [...] Synapse bias = neuron.getBias(); double delta = learning * 1.0; bias.setWeight(bias.getWeight() + delta + this.momentum * bias.getDelta()); bias.setDelta(delta); } } } [...]
/** * Create an instance of {@link Synapse } * */ public Synapse createSynapse() { return new Synapse(); }
n.setBias( weights[ wtsIndex++ ] ); for ( int k = 0; k < inputfieldNames.length; k++ ) { Synapse con = new Synapse(); con.setFrom( "" + k ); con.setWeight( weights[ wtsIndex++ ] ); n.getCons().add( con ); n.setBias( weights[ wtsIndex++ ] ); for ( int k = 0; k < hiddenSize; k++ ) { Synapse con = new Synapse(); con.setFrom( "" + ( k + inputfieldNames.length ) ); con.setWeight( weights[ wtsIndex++ ] ); n.getCons().add( con );
NeuralLayer l2 = (NeuralLayer) n2.getExtensionsAndNeuralLayersAndNeuralInputs().get( 3 ); assertEquals( l1.getNeurons().get( 4 ).getCons().get( 2 ).getWeight(), l2.getNeurons().get( 4 ).getCons().get( 2 ).getWeight(), 1e-9 ); assertEquals( weights[ ( inputfieldNames.length + 1 ) * 4 + 3 ], l2.getNeurons().get( 4 ).getCons().get( 2 ).getWeight(), 1e-9 );
NeuralLayer l2 = (NeuralLayer) n2.getExtensionsAndNeuralLayersAndNeuralInputs().get( 3 ); assertEquals( l1.getNeurons().get( 4 ).getCons().get( 2 ).getWeight(), l2.getNeurons().get( 4 ).getCons().get( 2 ).getWeight(), 1e-9 ); assertEquals( weights[ ( inputfieldNames.length + 1 ) * 4 + 3 ], l2.getNeurons().get( 4 ).getCons().get( 2 ).getWeight(), 1e-9 );
public class Neuron implements Serializable { [...] private Synapse bias; public Neuron(IActivation activation) { [...] this.bias = new Synapse(this); this.bias.setWeight(0.5); // Set initial weight OR keep the random number already set } public void updateOutput(double[] inputs) { double sumWeights = this.calculateSumWeights(inputs); this.output = this.activation.activate(sumWeights + this.bias.getWeight() * 1.0); } [...]