tags:

views:

91

answers:

3

I am trying to implement a simple neural net. I want to print the initial pattern, weights, activation. I then want it to print the learning process (i.e. every pattern it goes through as it learns). I am as yet unable to do this - it returns the initial and final pattern (whn I put print p in appropriate places), but nothing else. Hints and tips appreciated - I'm a complete newbie to Python!

#!/usr/bin/python
import random

p = [ [1, 1, 1, 1, 1],
      [1, 1, 1, 1, 1],
      [0, 0, 0, 0, 0],
      [1, 1, 1, 1, 1],
      [1, 1, 1, 1, 1] ] # pattern I want the net to learn
n = 5
alpha = 0.01
activation = []   # unit activations
weights = []      # weights
output = []       # output



def initWeights(n):  # set weights to zero, n is the number of units
    global weights 
    weights = [[[0]*n]*n]   # initialised to zero


def initNetwork(p):  # initialises units to activation
    global activation
    activation = p

def updateNetwork(k): # pick unit at random and update k times
    for l in range(k):
        unit = random.randint(0,n-1)
        activation[unit] = 0
        for i in range(n):
            activation[unit] += output[i] * weights[unit][i]
        output[unit] = 1 if activation[unit] > 0 else -1

def learn(p):
    for i in range(n):
        for j in range(n):
            weights += alpha * p[i] * p[j]
+7  A: 

You have a problem with the line:

weights = [[[0]*n]*n]

When you use*, you multiply object references. You are using the same n-len array of zeroes every time. This will cause:

>>> weights[0][1][0] = 8
>>> weights
[[[8, 0, 0], [8, 0, 0], [8, 0, 0]]]

The first item of all the sublists is 8, because they are one and the same list. You stored the same reference multiple times, and so modifying the n-th item on any of them will alter all of them.

Santiago Lezica
What's the best way to generate a null array then? List comprehensions?
jlv
`[[0 for _ in range(n)] for _ in range(n)]`
THC4k
Thanks kindly, THC.
jlv
A null list is not the same as a list of null lists. You can use the technique above to generate a single null list, it's multiplying that list where the problem arises. I'd go for list comprehension with xrange.
Santiago Lezica
Didn't see THC's answer. That's right, only you should use xrange if n is big.
Santiago Lezica
More generally, using `*` on any mutable object will create the same issue of shared mutability
Daenyth
A: 

I have tried adding

initWeights(n)
initNetwork(p)
updateNetwork(k)
learn(p)

to my code. It then tells me I haven't defined k (fair enough). I want k to be infinite, but even if I put a random number as k, it still returns the error

"IndexError: list index out of range"

I can't quite understand what this error is when I Google it, care to shed any light?

EDIT: I can't edit my initial question, no edit link underneath it?

EDIT: When I try

output.append()

nothing changes. How would I correctly implement it in this instance?

luser
Please edit your question with subsequent information. Don't post updates here... this is place for answers....
eumiro
Line 44? Your code does not have 44 lines...
eumiro
A: 

this the line is where you get : "IndexError: list index out of range"

output[unit] = 1 if activation[unit] > 0 else -1

because output = [] , you should do output.append() or ...

singularity