All pastes #398075 Raw Edit

Someone

public java v1 · immutable
·published 2007-03-16 22:50 UTC
rendered paste body
package nn;import java.util.Random;public class Network{    private Neuron[] inputs = null;    private Neuron output = null;    private Random random = new Random();        public Network(int numInput)    {        inputs = new Neuron[numInput];        for (int i=0;i<numInput;i++)        {            inputs[i] = new Neuron(numInput);        }        output = new Neuron(numInput);    }    public boolean test(double[] input, double expected)    {        double result = 0;        for (int i=0;i<input.length;i++)        {            result += inputs[i].f(input);        }        System.out.println("inp:" +input[0] +"res:"+result);        if (result != expected)        {            for (int i=0;i<inputs.length;i++)            {                this.inputs[i].addWeight((expected - result) * (i+1));            }            return false;        }                return true;    }    public static void main(String[] args)    {        Network net = new Network(1);        while (!net.test(new double[]{1},0) && !net.test(new double[]{0},1))        {        }    }    public class Neuron    {        private double[] weights = null;            public Neuron(int numInput)        {            weights = new double[numInput];            for (int i=0;i<numInput;i++)            {                weights[i] = random.nextDouble() - random.nextDouble();            }        }        public double f(double[] input)        {            double returnValue = 0;            for (int i=0;i<input.length;i++)            {                returnValue += input[i] * weights[i];            }            return returnValue;        }        public void addWeight(double delta)        {            for (int i=0;i<weights.length;i++)            {                weights[i] += delta;            }        }    }}