473,395 Members | 1,624 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 2131
bu*****@engr.orst.edu (Yaroslav Bulatov) wrote in
news:4d**************************@posting.google.c om:
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.append([sigmoid(innerproduct
(u,activations[-1])) for u in layer]+[1])
sigmas, new_weights = [],deepcopy(weights)
for i in range(len(weights))[::-1]:
sigma_row = []
for j, o in enumerate(activations[i+1][:-1]):
if not sigmas: err = dp[1][j]-o
else: err = innerproduct(sigmas[-1], [weights[i][k][j] for k in
range(len(weights[i+1]))])
sigma_row.append(o*(1-o)*err)
for k in range(len(weights[i][j])): new_weights[i][j][k]
+=nu*sigma_row[-1]*activations[i][k]
sigmas.append(sigma_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.append([sigmoid(innerproduct
(u,activations[-1])) for u in layer]+[1])
sigmas, new_weights = [],deepcopy(weights)
for i in range(len(weights))[::-1]:
sigma_row = []
for j, o in enumerate(activations[i+1][:-1]):
if not sigmas: err = dp[1][j]-o
else: err = innerproduct(sigmas[-1], [weights[i][k][j] for k in
range(len(weights[i+1]))])
sigma_row.append(o*(1-o)*err)
for k in range(len(weights[i][j])): new_weights[i][j][k]
+=nu*sigma_row[-1]*activations[i][k]
sigmas.append(sigma_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(weights):
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('+formula+')')
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
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...
0
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....
0
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...
1
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...
2
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...
0
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...
3
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...
0
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...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
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...

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.