News: No news is good news...

Login  |  Register

Author Topic: Need help with Python programming [fixed]  (Read 1085 times)

0 Members and 1 Guest are viewing this topic.

Offline Hanith

  • Junior Member
  • **
  • Posts: 229
  • Country: 00
  • Not on speaking terms with my dice.
  • Armies: Eldar
Need help with Python programming [fixed]
« on: April 13, 2012, 08:35:50 AM »
As the header suggests, I need some help with my program. The problem is included in the code snippet and I think I've included enough to make it runable by itself so you may be able to run it and see what is going on.
If you can't, the problem is after I generate my mass of random numbers. I can print them just fine using my getWeight(index) function if this is done immediately after generating them and storing them. When I attempt to print them after appending that list to a list of lists, i only retrieve the first number (which somehow does not exists in any of the lists) and then get the typical index out of bound exception. The code is as follows:

Code: [Select]
import wx
import random
from AIUnit import AIUnit

class finalProject(wx.Frame):

    global inputUnit
    inputUnit = []

#Defines and draws the GIU
    def __init__(self, parent, id):
        #Defines the frame which contains all GIU panels
        wx.Frame.__init__(self, parent, id, "Final Project", size=(800, 600))
        #Defines the panel which hold all componants
        global panel
        panel = wx.Panel(self)

        #defines the "exit" button
        self.Bind(wx.EVT_CLOSE, self.haltProg)

        #defines the new net button
        initNetworkButton = wx.Button(panel, label='New Network', pos=(50, 230), size=(80,50))
        self.Bind(wx.EVT_BUTTON, self.initialize, initNetworkButton)

    #action carried out when clicking "X" on window
    def haltProg(self, event):
        self.Destroy()

    #initializes the network with random weights and proper connections
    def initialize(self, event):
        global inputUnit
        inputUnit = []

        #INITIALIZES AND CONSTRUCTS LOWER NETWORKS
        #initializes and constructs input layer
        for i in range(0, 16, 1): #Number of input blocks
            tempUnit = []
            for j in range(0, 16, 1): #number of individual input units per block
                tempUnit.append(AIUnit())
                for k in range(0, 8, 1): #number of connections per input unit
                   tempUnit[j].setWeight(random.random(), -1)
                   #print tempUnit[j].getWeight(k)           #Print here works
                   #print str(i) + '-' + str(j) + '-' + str(k)  #Counting through all indices works
            inputUnit.append(tempUnit)
            print inputUnit[i][j].getWeight(7)                 #print here works

        print ''
        for i in range(0, 16, 1):
            for j in range(0, 16, 1):
                for k in range(0, 8, 1):
                    print str(i) + '-' + str(j) + '-' + str(k)   #count breaks at 0-0-1
                    print inputUnit[i][j].getWeight(k)         #printing only returns 1'st number (which isn't actual first number) then breaks

#Auto update/refresh and draw frame
if __name__=='__main__':
    app=wx.PySimpleApp()
    frame=finalProject(parent=None,id=-1)
    frame.Show()
    app.MainLoop()

Here is the AIUnit code in its entirety:

Code: [Select]
#This class acts to track information regarding a node used
# in AI infrastructure (mainly backpropagation networks)
# This was written by Samuel H. Kemp to aid with "Final Project"
class AIUnit:

    global output, delta, weight
    #This tracks this units most recent output
    output = 0

    #This tracks this units most recent delta
    delta = 0

    #This tracks the weights of this units various connections
    weight = []

    #Initialized the object
    def __init__(self):
        global output, delta, weight
        output = 0
        delta = 0
        weight = []
       
    #The following set this units various attributes
    def setOutput(self, nOutput):
        global output
        output = nOutput

    def setDelta(self, nDelta):
        global delta
        delta = nDelta

    #If index is -1, append a new one to the list
    def setWeight(self, nWeight, index):
        global weight

        if index == -1:
            weight.append(nWeight)
        else:
            weight[index] = nWeight

    #The following retieve this units various attributes
    def getOutput(self):
        global output
        return output

    def getDelta(self):
        global delta
        return delta

    def getWeight(self, index):
        global weight
        return weight[index]
   

Any help would be greatly appreciated.
« Last Edit: April 27, 2012, 12:31:12 PM by Hanith »
For me: 16.667% chance of failure equates to a 83.334% failure rate.

Offline Hanith

  • Junior Member
  • **
  • Posts: 229
  • Country: 00
  • Not on speaking terms with my dice.
  • Armies: Eldar
Re: Need help with Python programming
« Reply #1 on: April 17, 2012, 11:00:08 AM »
    I've decided to move my final project onto something simpler and requiring less troublesome to code (This project here was supposed to take a string of numbers and (using a series of multi-layer backpropogation AI networks) generate a mathematical formula that can reproduce the given string of numbers in turn and beyond. The project I've changed it to will use the same rough idea of AI networks but will be used to predict the prices of stocks based on that particular company's yearly, monthly, weekly, daily, and 15-minuts trends)

    While this problem won't be a pressing matter any more I would still like to know what is causing this issue. This way if it happens again I'll know why.
For me: 16.667% chance of failure equates to a 83.334% failure rate.

Offline Hanith

  • Junior Member
  • **
  • Posts: 229
  • Country: 00
  • Not on speaking terms with my dice.
  • Armies: Eldar
Re: Need help with Python programming
« Reply #2 on: April 27, 2012, 12:30:56 PM »
Discovered the problem. I was declaring my AIUnits attributes in the global namespace (This makes any new AIUnits made after overwrite the existing ones due to the "__init__" constructor. Fixed by removing all the global keywords in the AIUnit class and adding "self.X" in front of every call to change the attribute (where X = the attribute). Example:

Code: [Select]
#If index is -1, append a new one to the list
    def setWeight(self, nWeight, index):
        global weight

        if index == -1:
            weight.append(nWeight)
        else:
            weight[index] = nWeight

is now

Code: [Select]
#If index is -1, append a new one to the list
    def setWeight(self, nWeight, index):

        if index == -1:
            self.weight.append(nWeight)
        else:
            self.weight[index] = nWeight
For me: 16.667% chance of failure equates to a 83.334% failure rate.

 


Powered by EzPortal