Python Forum

Full Version: Multilayer Perceptron Neural Network
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am establishing a multilayer perceptron neural network. Each layer has random number of perceptron. In order to connect perceptrons from
one layer to another one, I need to pick random number of perceptrons. The code is below. But the error message I get is

>>
Error:
TypeError: Population must be a sequence. For dicts or sets, use sorted(d).
<<

Here is the code

import numpy as np
import random


class MultiLayer_test():

    def __init__(self):
        self.H_LayerNo = 5
        self.MaxPercNo = 10 # Assume that we only generate 10 perceptron on each layer


    def main_Layering(self):
        PercNo = []
        Layers = []
        Ex_N = []
        #IndexCh = []


        for i in range(0, self.H_LayerNo):

            # Number of perceptrons per layer
            PercNo.append(random.randint(1, self.MaxPercNo))

            # Create tuples representing the layers
            Layers.append([np.zeros((1, PercNo[i]))]) #[np.zeros((1, PercNo[i]))] [random.random(PercNo[i])]

            # Number of transmitters per layer
            Ex_N.append(random.randint(1, PercNo[i])) #PercNo[i]

            # Get the indices of excited neurons
            Idx[i] = random.sample(np.asarray(Layers[i]),Ex_N[i])

if __name__ == '__main__':
    runner = MultiLayer_test()
    runner.main_Layering()
In line 31 you are passing random.sample() a tuple. It does not like that. Check the type of np.asarray(Layers[i]),Ex_N[i]
I updated the code as follows. But, now, it returns with empty range sometimes.

Error:
ValueError: empty range for randrange() (1, 1, 0)
Here is the updated code.

import numpy as np
import random


class MultiLayer_test():

    def __init__(self):
        self.H_LayerNo = 12
        self.MaxPercNo = 10 # MAximum perceptron, neuron, numbers per layer

   
    def main_Layering(self):
        PercNo = []
        Layers = []
        Ex_N = []

        for i in range(0, self.H_LayerNo):
            # Number of perceptrons per layer
            PercNo.append(random.randint(1, self.MaxPercNo))
            # Create tuples representing the layers
            Layers.append([np.zeros((1, PercNo[i]))]) #[np.zeros((1, PercNo[i]))] [random.random(PercNo[i])]
            # Excited neurons
            Ex_N.append(random.randint(1, PercNo[i]))

            #i = None
            #Layers_stacked = np.zeros((self.H_LayerNo,len(Layers[i])))
        for i in range(0, self.H_LayerNo):
            pos = [random.randrange(1, len(Layers[i][0][0][:])) for _ in range(Ex_N[i])]
            Layers[i][0][0][pos] = 1



        print('Number of excited nuerons; ', Ex_N)
        print('Number of perceptrons on each layer; ', PercNo)
        print('Layers with perceptrons; ', Layers)
        return PercNo, Layers

if __name__ == '__main__':
    runner = MultiLayer_test()
    runner.main_Layering()
Minor point - when posting it makes is easier on those looking at the code that you post the whole error message as there is additional info that is useful.

In this case, looks like the error is in line 28. You call randrange(1,1,0). That means the range starts at one, ends with 1, with a step of 0. I don't know what you expect that to yield, but Python objects!