2

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]
1

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

5 Comments

What's the best way to generate a null array then? List comprehensions?
[[0 for _ in range(n)] for _ in range(n)]
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.
Didn't see THC's answer. That's right, only you should use xrange if n is big.
More generally, using * on any mutable object will create the same issue of shared mutability
0

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 ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.