lundi 27 juin 2016

Java Backpropagation Algorithm is very slow

I have a big problem. I try to create a neural network and want to train it with a backpropagation algorithm. I found this tutorial here http://mattmazur.com/2015/03/17/a-step-by-step-backpropagation-example/ and tried to recreate it in Java. And when I use the training data he uses, I get the same results as him. Without backpropagation my TotalError is nearly the same as his. And when I use the back backpropagation 10 000 time like him, than I get the nearly the same error. But he uses 2 Input Neurons, 2 Hidden Neurons and 2 Outputs but I'd like to use this neural network for OCR, so I need definitely more Neurons. But if I use for example 49 Input Neurons, 49 Hidden Neurons and 2 Output Neurons, It takes very long to change the weights to get a small error. (I believe it takes forever.....). I have a learningRate of 0.5. In the constructor of my network, I generate the neurons and give them the same training data like the one in the tutorial and for testing it with more neurons, I gave them random weights, inputs and targets. So can't I use this for many Neurons, does it takes just very long or is something wrong with my code ? Shall I increase the learning rate, the bias or the start weight? Hopefully you can help me.

package de.Marcel.NeuralNetwork;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Random;

public class Network {
    private ArrayList<Neuron> inputUnit, hiddenUnit, outputUnit;

    private double[] inHiWeigth, hiOutWeigth;
    private double hiddenBias, outputBias;

    private double learningRate;

    public Network(double learningRate) {
        this.inputUnit = new ArrayList<Neuron>();
        this.hiddenUnit = new ArrayList<Neuron>();
        this.outputUnit = new ArrayList<Neuron>();

        this.learningRate = learningRate;

        generateNeurons(2,2,2);

        calculateTotalNetInputForHiddenUnit();
        calculateTotalNetInputForOutputUnit();
    }

    public double calcuteLateTotalError () {
        double e = 0;
        for(Neuron n : outputUnit) {
            e += 0.5 * Math.pow(Math.max(n.getTarget(), n.getOutput()) - Math.min(n.getTarget(), n.getOutput()), 2.0);
        }

        return e;
    }

    private void generateNeurons(int input, int hidden, int output) {
        // generate inputNeurons
        for (int i = 0; i < input; i++) {
            Neuron neuron = new Neuron();

            // for testing give each neuron an input
            if(i == 0) {
                neuron.setInput(0.05d);
            } else if(i == 1) {
                neuron.setOutput(0.10d);
            }

            inputUnit.add(neuron);
        }

        // generate hiddenNeurons
        for (int i = 0; i < hidden; i++) {
            Neuron neuron = new Neuron();

            hiddenUnit.add(neuron);
        }

        // generate outputNeurons
        for (int i = 0; i < output; i++) {
            Neuron neuron = new Neuron();

            if(i == 0) {
                neuron.setTarget(0.01d);
            } else if(i == 1) {
                neuron.setTarget(0.99d);
            }

            outputUnit.add(neuron);
        }

        // generate Bias
        hiddenBias = 0.35;
        outputBias = 0.6;

        // generate connections
        double startWeigth = 0.15;
        // generate inHiWeigths
        inHiWeigth = new double[inputUnit.size() * hiddenUnit.size()];
        for (int i = 0; i < inputUnit.size() * hiddenUnit.size(); i += hiddenUnit.size()) {
            for (int x = 0; x < hiddenUnit.size(); x++) {
                int z = i + x;
                inHiWeigth[z] = round(startWeigth, 2, BigDecimal.ROUND_HALF_UP);

                startWeigth += 0.05;
            }
        }

        // generate hiOutWeigths
        hiOutWeigth = new double[hiddenUnit.size() * outputUnit.size()];
        startWeigth += 0.05;
        for (int i = 0; i < hiddenUnit.size() * outputUnit.size(); i += outputUnit.size()) {
            for (int x = 0; x < outputUnit.size(); x++) {
                int z = i + x;
                hiOutWeigth[z] = round(startWeigth, 2, BigDecimal.ROUND_HALF_UP);

                startWeigth += 0.05;
            }
        }
    }

    private double round(double unrounded, int precision, int roundingMode)
    {
        BigDecimal bd = new BigDecimal(unrounded);
        BigDecimal rounded = bd.setScale(precision, roundingMode);
        return rounded.doubleValue();
    }

    private void calculateTotalNetInputForHiddenUnit() {
        // calculate totalnetinput for each hidden neuron
        for (int s = 0; s < hiddenUnit.size(); s++) {
            double net = 0;
            int x = (inHiWeigth.length / inputUnit.size());

            // calculate toAdd
            for (int i = 0; i < x; i++) {
                int v = i + s * x;
                double weigth = inHiWeigth[v];
                double toAdd = weigth * inputUnit.get(i).getInput();
                net += toAdd;
            }

            // add bias
            net += hiddenBias * 1;
            net = net *-1;
            double output =  (1.0 / (1.0 + (double)Math.exp(net)));
            hiddenUnit.get(s).setOutput(output);
        }
    }

    private void calculateTotalNetInputForOutputUnit() {
        // calculate totalnetinput for each hidden neuron
        for (int s = 0; s < outputUnit.size(); s++) {
            double net = 0;
            int x = (hiOutWeigth.length / hiddenUnit.size());

            // calculate toAdd
            for (int i = 0; i < x; i++) {
                int v = i + s * x;
                double weigth = hiOutWeigth[v];
                double outputOfH = hiddenUnit.get(s).getOutput();
                double toAdd = weigth * outputOfH;
                net += toAdd;
            }

            // add bias
            net += outputBias * 1;
            net = net *-1;
            double output = (double) (1.0 / (1.0 + Math.exp(net)));
            outputUnit.get(s).setOutput(output);
        }
    }

    private void backPropagate() {
        // calculate ouputNeuron weigthChanges
        double[] oldWeigthsHiOut = hiOutWeigth;
        double[] newWeights = new double[hiOutWeigth.length];
        for (int i = 0; i < hiddenUnit.size(); i += 1) {
            double together = 0;
            double[] newOuts = new double[hiddenUnit.size()];
            for (int x = 0; x < outputUnit.size(); x++) {
                int z = x * hiddenUnit.size() + i;
                double weigth = oldWeigthsHiOut[z];
                double target = outputUnit.get(x).getTarget();
                double output = outputUnit.get(x).getOutput();

                double totalErrorChangeRespectOutput = -(target - output);
                double partialDerivativeLogisticFunction = output * (1 - output);
                double totalNetInputChangeWithRespect = hiddenUnit.get(x).getOutput();
                double puttedAllTogether = totalErrorChangeRespectOutput * partialDerivativeLogisticFunction
                        * totalNetInputChangeWithRespect;
                double weigthChange = weigth - learningRate * puttedAllTogether;

                // set new weigth
                newWeights[z] = weigthChange;
                together += (totalErrorChangeRespectOutput * partialDerivativeLogisticFunction * weigth);
                double out = hiddenUnit.get(x).getOutput();
                newOuts[x] = out * (1.0 - out);
            }
            for (int t = 0; t < newOuts.length; t++) {
                inHiWeigth[t + i] = (double) (inHiWeigth[t + i] - learningRate * (newOuts[t] * together * inputUnit.get(t).getInput()));
            }
            hiOutWeigth = newWeights;
        }
    }
}

And my Neuron Class:

package de.Marcel.NeuralNetwork;

public class Neuron {
    private double input, output;
    private double target;

    public Neuron () {

    }

    public void setTarget(double target) {
        this.target = target;
    }

    public void setInput (double input) {
        this.input = input;
    }

    public void setOutput(double output) {
        this.output = output;
    }

    public double getInput() {
        return input;
    }

    public double getOutput() {
        return output;
    }

    public double getTarget() {
        return target;
    }
}

Aucun commentaire:

Enregistrer un commentaire