TypeError: unsubscriptable object

Cyrille Artho cartho at mordor.ch
Thu Jun 8 03:17:31 EDT 2000


> What's possible (but it's not possible to determine this from your snippets)
> is that you accidentally assign something else to W elsewhere.
self.W is equal to
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
just before the assignment.
self.W[i][j] = self.W[i][j]  + ...
I attached the entire script to this post (it's not too big).
--
Regards,
Cyrille Artho - http://artho.com/ - Tel. +41 - [0]1 - 313 08 92
Alcohol is the anesthesia by which we endure the operation of life.
                -- George Bernard Shaw
-------------- next part --------------
import copy
import random
import whrandom
import math

class NN:
    global n0, alpha, nInput, nNeuron
    n0 = 0.2
    alpha = 0.01
    nInput = 2
    nNeuron = 2
    
    def __init__(self):
        self.V = [0, 0, 0]
        self.W = [[0,0,0], [0,0,0], [0,0,0]]


    def playRound(self): # one round of rolling dice
        X1 = random.choice(range(5)) + 1
        X2 = random.choice(range(5)) + 1
        X3 = random.choice(range(5)) + 1
        I = [0, 0, 0]
        I[0] = 1
        I[1] = min(X1, X2)
        I[2] = abs(X1 - X2)
        S = copy.deepcopy(I)

        # calculate output
        P = copy.deepcopy(I)
        sum = 0
        for i in range(nNeuron+1):
            # input
            for j in range(nInput+1):
                sum = sum + self.W[i][j]*I[j]
            P[i] = 1 / (1 + math.exp(-sum))
            if (P[i] > whrandom.random()):
                S[i] = 1
            else:
                S[i] = 0
            # hidden layer
            sum = 0
            sum = sum + self.V[i]*S[i]
        P = 1 / (1 + math.exp(-sum))
        if (P > whrandom.random()):
            O = 1
        else:
            O = 0

        # analyze result
        YA = I[2]
        if (abs(X1 - 3.5) >= abs(X2 - 3.5)):
            Xmax = X1
        else:
            Xmax = X2
        YB = abs(Xmax - X3)

        if (O == 1):
            Y = YA
        else:
            Y = YB

        if (Y == max(YA, YB)):
            r = 1
        else:
            r = 0

        n = n0 * (max(YA, YB) - min(YA, YB))

        # change weights
        for i in range(nNeuron):
            for j in range(nInput+1):
                print self.W
                print self.W[i][j]
                self.W[i][j] = self.W[i][j]  + n * (
                    r(S[i] - P[i]) - alpha * (1 - r)
                    * (S[i] - (1 - P[i]))) * I[j]
            self.V[i] = self.V[i] + n * (
                r(O - P) - alpha * (1 - r) * (O - (1 - P))) * S[i]

if __name__ == '__main__':
    n = NN()
    n.playRound()


More information about the Python-list mailing list