Page 1 of 4 123 ... LastLast
Results 1 to 10 of 40

Thread: Simple neural network in python

  1. #1
    Join Date
    Oct 2006
    Location
    Austin, Texas
    Beans
    2,712
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Simple neural network in python

    This is a little demo I wrote to create a very minimal neural network in python.

    It is dumbed down model of a simple neural net, there are two input neurons and one output. No middle layers.

    The list "test" is the learning table. This one is set up to teach the neural network to act as an OR gate.

    A neural network learns very similar to the way the human brain learns, by reaction. It is given input and it determines whether or not that input is correct, then it adjusts it's memory to to receive the input different next time.

    If you burn your hand on the stove, you learn not to put your hand on the stove. But, if you put your hand on the stove while it's off, you learn it's OK... But you're brain reacts to patterns in the same way this neural network does... The key is to learn the relationship between on/off burn/ok.

    Anyway, here it is, when the neural net learns one of the learning tables elements, it will be printed with a star next to it. If you'd like to see it in real time, remove the "time.sleep(1)"

    Code:
    import time
    import random
    
    # Learning rate:
    # Lower  = slower
    # Higher = less precise
    rate=.2
    
    # Create random weights
    inWeight=[random.uniform(0, 1), random.uniform(0, 1)]
    
    # Start neuron with no stimuli
    inNeuron=[0.0, 0.0]
    
    # Learning table (or gate)
    test =[[0.0, 0.0, 0.0]]
    test+=[[0.0, 1.0, 1.0]]
    test+=[[1.0, 0.0, 1.0]]
    test+=[[1.0, 1.0, 1.0]]
    
    # Calculate response from neural input
    def outNeuron(midThresh):
    	global inNeuron, inWeight
    	s=inNeuron[0]*inWeight[0] + inNeuron[1]*inWeight[1]
    	if s>midThresh:
    		return 1.0
    	else:
    		return 0.0
    
    # Display results of test
    def display(out, real):
    		if out == real:
    			print str(out)+" should be "+str(real)+" ***"
    		else:
    			print str(out)+" should be "+str(real)
    
    while 1:
    	# Loop through each lesson in the learning table
    	for i in range(len(test)):
    		# Stimulate neurons with test input
    		inNeuron[0]=test[i][0]
    		inNeuron[1]=test[i][1]
    		# Adjust weight of neuron #1
    		# based on feedback, then display
    		out = outNeuron(2)
    		inWeight[0]+=rate*(test[i][2]-out)
    		display(out, test[i][2])
    		# Adjust weight of neuron #2
    		# based on feedback, then display
    		out = outNeuron(2)
    		inWeight[1]+=rate*(test[i][2]-out)
    		display(out, test[i][2])
    		# Delay
    		time.sleep(1)
    Anyone who is interested in working on a project involving NN's or GA's who would like another programmer, please contact me. I work mostly in C, C++, and Python.

  2. #2
    Join Date
    Oct 2005
    Location
    Davao, Philippines
    Beans
    4,831

    Re: Simple neural network in python

    wow, machine learning is way advance for me
    i always had some interest on that field, but failed to grasp some of those topics.

  3. #3
    Join Date
    Oct 2006
    Location
    Austin, Texas
    Beans
    2,712
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: Simple neural network in python

    Genetic algorithms are super sweet... I'm about to start a small project involving GA's really soon. Neural networks are a little more complicated but very easy on a level as small as this demo. 2 inputs, 1 output and no hidden layers is simple. Things don't really get too bad until you have middle layers and you need really complex back propagation algorithms. If you ever need any help feel free to contact me. I am a huge fan of AI.

  4. #4
    Join Date
    Oct 2005
    Location
    Davao, Philippines
    Beans
    4,831

    Re: Simple neural network in python

    thanks, whats your opinion on orange machine python api, is it any good? or have you used it?

  5. #5
    Join Date
    Oct 2006
    Location
    Austin, Texas
    Beans
    2,712
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: Simple neural network in python

    I've actually never used it. I've heard of it though. I like to write my own routines for stuff like that, it gives me a better grip of what is actually going on. It looks pretty good though.

  6. #6
    Join Date
    Oct 2005
    Location
    Davao, Philippines
    Beans
    4,831

    Re: Simple neural network in python

    i see. about the project that involves NN and GA , is it an open source project, can you tell more about it. perhaps others with similar interest will spot this thread. and might be interested

  7. #7
    Join Date
    Oct 2006
    Location
    Austin, Texas
    Beans
    2,712
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: Simple neural network in python

    Well, I'm willing to join any project, but the one I am about to start deals with genetic algorithms. It probably wont need more than me to write, but if anyone wants to contribute it will definitely be open source. It will essentially be a rewrite of a program I wrote a while back: Project Darwin (http://p13.wikispaces.com/LifeProject) (the exe works pretty well with wine. It was written in Basic4GL, I plan to port it to Basic4SDL sometime soon which is another project I am working on... It is interesting to write programs for the language you are working on!)

    But the new one will either be in C++ or python (not sure yet, opting for python so others can play with the source, but it may get very large so I prefer C++ for large projects). It will PROBABLY be portable, but right now I am more concerned with getting it to run smoothly on linux (linux is free so other OS users can stop complaining, just download linux!). I am also going to improve A LOT of the fundamentals behind the code because it will be in C++/Python instead of BASIC. And I plan to make a more natural environment, plants/soil/water/light, a mini ecosystem for my little organism's to grow in.

    The ability to save/load cultures should also be present, this will allow people to raise an efficient culture and see how it exists next to another culture... Possibly carnivorous behavior as well. I guess I'll see how it pans out (the design process will revolve around natural selection too, lol).

    But yeah, if anyone wants to pitch in, great!

  8. #8
    Join Date
    Nov 2005
    Beans
    593
    Distro
    Kubuntu 7.10 Gutsy Gibbon

    Re: Simple neural network in python

    For a project, I wrote a neural network in Python. I wasn't satisfied with the performance, even when using psyco, and I rewrote it in C++. It got so much faster! So I recommend you write it in C++, then bind it to the rest of your Python program with Boost Python.

  9. #9
    Join Date
    Oct 2006
    Location
    Austin, Texas
    Beans
    2,712
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: Simple neural network in python

    Oh... Yeah, I use python/c++ binding all the time (I use python.h instead of boost though, I don't like all of the extra baggage that's involved with using boost).. Anyway, it depends on how you implement it... I used to think that C++ was just ALWAYS better than python, but I've discovered that depending on the types of functions and routines, python has the potential to be c++ or at least near-c++ speed.

    Once again this is pretty dependent on what you are doing, if you use a lot of modules that where written in c/c++, then yes, straight c++ will be faster because you are ditching the wrapping layer between them. But anyway... I've a lot of NN work in C++ and I agree, it is very easy to write and very fast, but I don't think this python version is too much slower. (I actually had to add a delay so you can see what is going on in the output).

    But, that's one of the advantages to python... If you need to use C++ stuff for speed or lack of library support, you can just whip up a module!

  10. #10
    Join Date
    Nov 2005
    Beans
    593
    Distro
    Kubuntu 7.10 Gutsy Gibbon

    Re: Simple neural network in python

    In that case, Python was a lot slower. I don't have the profile results anymore, but the difference was unbelievable. Well, you can always write it in Python, profile, and translate the slowest bits to C++ if you need it. Maybe you won't (lucky you).

Page 1 of 4 123 ... LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •