473,714 Members | 3,850 Online

# Neural Network in 16 lines

Just for fun :)
http://yaroslav.hopto.org/russianwik...hp/neural-impl

Is there a more compact way?

Yaroslav
Jul 18 '05 #1
1 2151
bu*****@engr.or st.edu (Yaroslav Bulatov) wrote in
news:4d******** *************** ***@posting.goo gle.com:
Just for fun :)
http://yaroslav.hopto.org/russianwik...hp/neural-impl

Is there a more compact way?

Yaroslav

Here's the function from the link:

def train_smallest( data, weights, nu=1):
for it in range(1000):
for dp in data:
activations = [dp[0]+[1]]
for layer in weights: activations.app end([sigmoid(innerpr oduct
(u,activations[-1])) for u in layer]+[1])
sigmas, new_weights = [],deepcopy(weigh ts)
for i in range(len(weigh ts))[::-1]:
sigma_row = []
for j, o in enumerate(activ ations[i+1][:-1]):
if not sigmas: err = dp[1][j]-o
else: err = innerproduct(si gmas[-1], [weights[i][k][j] for k in
range(len(weigh ts[i+1]))])
sigma_row.appen d(o*(1-o)*err)
for k in range(len(weigh ts[i][j])): new_weights[i][j][k]
+=nu*sigma_row[-1]*activations[i][k]
sigmas.append(s igma_row)
weights = new_weights
return weights

--------------------------------------------------------------------
And here's the whole program:

# Python implementation of feed-forward neural network

import Gnuplot
from math import *
from operator import mul
from copy import deepcopy
from Numeric import innerproduct

def sigmoid(x): return 1/(1+exp(-x))

# Trains Neural Network, returns
def train_smallest( data, weights, nu=1):
for it in range(1000):
for dp in data:
activations = [dp[0]+[1]]
for layer in weights: activations.app end([sigmoid(innerpr oduct
(u,activations[-1])) for u in layer]+[1])
sigmas, new_weights = [],deepcopy(weigh ts)
for i in range(len(weigh ts))[::-1]:
sigma_row = []
for j, o in enumerate(activ ations[i+1][:-1]):
if not sigmas: err = dp[1][j]-o
else: err = innerproduct(si gmas[-1], [weights[i][k][j] for k in
range(len(weigh ts[i+1]))])
sigma_row.appen d(o*(1-o)*err)
for k in range(len(weigh ts[i][j])): new_weights[i][j][k]
+=nu*sigma_row[-1]*activations[i][k]
sigmas.append(s igma_row)
weights = new_weights
return weights

# graphs given neural network through gnuplot
def graphNN(weights ):
g = Gnuplot.Gnuplot (debug=0)
g('set yrange [0:1]')

prev_row = ['x', 'y']

g('s(x) = 1/(1+exp(-x))')

# convert neural network to analytic form
for i, row in enumerate(weigh ts):
new_row = []
for j, unit in enumerate(row):
name = "r%dc%d(x,y )" %(i, j)
new_row.append( name)
formula = ''
for k, weight in enumerate(unit) :
if k>0: formula+='+'
if k < len(unit)-1:
formula+='%f*%s ' % (weight, prev_row[k])
else: # bias unit
formula+='%f' % (weight)
g(name+'=s('+fo rmula+')')
prev_row = new_row

g('splot[-2:2][-2:2][] %s' % (name,))
raw_input()

# XOR data
data = [[[1,1],[1]], [[-1,-1], [1]], [[1,-1],[0]], [[-1,1],[0]] ]

# Starting weights (leads to global optimum)
start_weights = [[[-1,-1, -1],[1,-1, -1]],[[1,1,0]]]

# Train the network
weights = train_smallest( data, start_weights)

# Visualize it
graphNN(weights )
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 1 2022 by: Aum | last post by: Hi, Can anyone please recommend a python-accessible library for neural network simulation? I've already got code for standard feed-forward, back-propagation networks, but am needing something which does recurrent networks. Also desirable is self-organising maps and Hopfield nets. 0 1254 by: I. Myself | last post by: The VizANN package is a free download from annevolve.sf.net. It contains a python program that graphically demonstrates a recurrent binary neural network. There is also an explanatory text file. Mitchell Timin -- "In theory, there is no difference between theory and practice. In practice, there is." 0 981 by: I. Myself | last post by: Release Name: vizann-2.0 This freeware program may be downloaded from http://sourceforge.net/projects/annevolve. *Notes:* This is a program to graphically demonstrate the operational details of two types of ANN (Artificial Neural Network) when used to implement the XOR function. The program is 100% GUI, meaning that there is no 1 3249 by: AndreaM | last post by: Hi all, I'm looking for a C++ (or at least C) library that implements both standard feedforward neural networks and also recurrent neural networks (RNN)... Any suggestion? I tried FANN but it doesn't have any RNN support, ANNIE seems hard to compile and however RNN support seems to be limited. Thanks in advance for any help! Andrea 2 2336 by: mwojc | last post by: Hi! I released feed-forward neural network for python (ffnet) project at sourceforge. Implementation is extremelly fast (code written mostly in fortran with thin python interface, scipy optimizers involved) and very easy to use. I'm announcing it here because you, folks, are potential users/testers. If anyone is interested please visit http://ffnet.sourceforge.net (and then post comments if any...) 0 2128 by: buzzer | last post by: i would like to build a software coding which can classify image and pattern using artificial neural networks the idea is it should be able to do feature extraction on a certain image (can consist of characters/pictures) and let the network learn about the difference of each extracted image and finally able to decide what the image is and classify it. is there any code for the feature extraction part?? or any coding or ideas on how to... 3 2939 by: CoreyWhite | last post by: A friend of mine just was over at my house explaining Neural Networks and I understood it as well as I could. Here is my own explination. A neural network has to first run in a loop 1,000's of times given it's input and output. It then naturally learns the simplest algorithm to generate that input and output, using a networked matrix of numbers that are run through a filter, and compared to the input. The algorithm to train a neural... 0 1574 by: YellowFin Announcements | last post by: Yellowfin has announced a new partnership with Extol, one of Malaysia's largest ICT security solutions providers, in which Extol will integrate their predictive analytics application into Yellowfin. Neural Networks for predictive analysis As part of their risk management business, Extol have developed a neural network predictive analysis engine, and are now working with Yellowfin to develop a usable front end that is integrated with... 0 1050 by: dr3amxstar | last post by: Hi this might not be totally related but im just trying my luck here. I am doing a project - protein secondary structure prediction using Artificial Neural Network. I have written out the full code, it is able to compile and run but the output is not what i want. The code is too long so i decided not to post it.I have tried debugging for weeks but no luck so far. Has anyone done this topic before so that i can ask for advice? or is there any... 0 8808 by: marktang | last post by: ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of... 0 8712 by: Hystou | last post by: Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,... 0 9179 by: jinu1996 | last post by: In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is... 1 9077 by: Hystou | last post by: Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,... 0 9023 by: tracyyun | last post by: Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some... 0 7954 by: agi2029 | last post by: Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea... 1 6638 by: isladogs | last post by: The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will... 0 5959 by: conductexam | last post by: I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();... 2 2525 by: muto222 | last post by: How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.