Python Forum
Multilayer Perceptron Neural Network
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multilayer Perceptron Neural Network
#1
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()
Reply
#2
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]
Reply
#3
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()
Reply
#4
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Neural network and data analysis from clients survey result pthon3 2 1,914 Mar-17-2022, 02:21 AM
Last Post: jefsummers
  Neural Network importance weights / coefficients jkaustin 1 2,086 Nov-10-2020, 07:44 PM
Last Post: jefsummers
  Help with multiclass classification in perceptron code Nimo_47 0 3,750 Nov-09-2020, 10:32 PM
Last Post: Nimo_47
  Get error message in a GAN neural network tutorial jdude50 0 1,684 Oct-22-2020, 11:11 PM
Last Post: jdude50
  Explain Me Neural Network Ai's Harshil 2 2,026 Oct-22-2020, 04:50 AM
Last Post: Harshil
  construction of Neural Network for solving Differential equations arshad 0 1,625 Jun-04-2020, 09:20 AM
Last Post: arshad
  Neural Network mensagem error Dalpi 2 2,888 May-20-2020, 04:03 PM
Last Post: Dalpi
  Multilayer Perceptron Help (Regression Problem) mberge 0 1,579 May-04-2020, 12:52 AM
Last Post: mberge
  Single layer perceptron not outputting correct results mberge 0 1,417 Apr-29-2020, 10:17 PM
Last Post: mberge
  coding neural network programmer19890620 4 3,455 Feb-27-2020, 04:26 AM
Last Post: programmer19890620

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020