Advertisement

Critique my Neural Network (And what are good resources?)

Started by March 16, 2010 09:33 AM
3 comments, last by Narf the Mouse 14 years, 8 months ago
I've hand-coded an AND NOT neural network based on a webpage I found. I'm looking for ways to develop it further, such as, of course, not having to hand-code it. Unfortunately, too many websites are either not for beginners or only a brief introduction. I do not, at the moment, have the money for a programming book, although I may be able to spend some in a ~week. Now, on to the source:

    class Program
    {
        static void Main(string[] args)
        {
            Neuron[][] neurons = new Neuron[3][];
            neurons[2] = new Neuron[] { new Neuron(1.0, NeuronType.Output) };
            neurons[1] = new Neuron[] { new Neuron(0.5, new NeuronLink[] { new NeuronLink(neurons[2][0], 1.0) }) };
            neurons[0] = new Neuron[]
            { 
                new Neuron(0.0, new NeuronLink[] { new NeuronLink(neurons[1][0], 1.0) }, NeuronType.Input), 
                new Neuron(0.0, new NeuronLink[] { new NeuronLink(neurons[1][0], -1.0) }, NeuronType.Input) 
            };

            neurons[0][0].ReceiveInput(false);
            neurons[0][1].ReceiveInput(false);

            foreach (Neuron[] neuronsIn in neurons)
            {
                foreach (Neuron neuron in neuronsIn)
                {
                    neuron.Activate();
                }
            }

            Console.WriteLine(neurons[2][0].Read());
            Console.ReadKey(true);
        }
    }


    class Neuron
    {
        public Neuron(Double threshhold)
        {
            this.Threshhold = threshhold;
            this.Links = new NeuronLink[0];
        }


        public Neuron(Double threshhold, NeuronLink[] links)
        {
            this.Threshhold = threshhold;
            this.Links = links;
        }


        public Neuron(Double threshhold, NeuronType type)
        {
            this.Type = type;
            this.Threshhold = threshhold;
            this.Links = new NeuronLink[0];
        }


        public Neuron(Double threshhold, NeuronLink[] links, NeuronType type)
        {
            this.Type = type;
            this.Threshhold = threshhold;
            this.Links = links;
        }


        public NeuronType Type = NeuronType.Hidden;
        public Double Threshhold;
        public Double Input = 0;


        public NeuronLink[] Links;


        public void ReceiveInput(Boolean input)
        {
            if (input)
                this.Input += 1;
            else
                this.Input += -1;
        }


        public void ReceiveInput(Double input)
        {
            this.Input += input;
        }


        public void Activate()
        {
            if (this.Type != NeuronType.Output)
            {
                if (this.Input >= this.Threshhold ||
                    this.Type == NeuronType.Input)
                {
                    foreach (NeuronLink neuronLink in this.Links)
                    {
                        neuronLink.Fire(this.Input);
                    }
                }
                this.Input = 0;
            }
        }


        public Boolean Read()
        {
            return this.Input >= this.Threshhold;
        }
    }


    enum NeuronType
    {
        Input,
        Hidden,
        Output,
    }


    class NeuronLink
    {
        public NeuronLink(Neuron link, Double weight)
        {
            this.Link = link;
            this.Weight = weight;
        }


        public Neuron Link;
        public Double Weight;


        public void Fire(Double input)
        {
            this.Link.ReceiveInput(input * Weight);
        }
    }


[Edited by - Narf the Mouse on March 16, 2010 1:42:07 PM]
Well, my Neural Network can feed forward and back propagate. I can train it on any AND operation (AFAIK), but it doesn't quite get OR operations.

I'm using two inputs (The first and second booleans), two "hidden" neurons and one output neuron. (Three layers)

I can post source code, but it's a bit of a mess and uncommented.
Advertisement
Re: Neural networks

Quote: Original post by Narf the MouseI do not, at the moment, have the money for a programming book, although I may be able to spend some in a ~week.


You might try the Usenet comp.ai.neural-nets FAQ, at least as a start:


http://www.faqs.org/faqs/ai-faq/neural-nets/part1/

[Edited by - Predictor on March 17, 2010 10:14:04 AM]
Thanks for the link. I've stuck it in a tab.
That Faq seems very technical; is there anything that's more for beginners?

This topic is closed to new replies.

Advertisement