Need help with this Python code.

Joined
Mar 31, 2023
Messages
95
Reaction score
8
Hello everyone! I've recently started working on a code, and I've encountered a typical and rather simple error that I'm unable to resolve.
I would appreciate help as soon as possible.
Apologies, the terms are in French.
Here is the error I'm encountering.

Code:
Traceback (most recent call last):

  File ~\Nouveau dossier\pkgs\spyder_kernels\py3compat.py:356 in compat_exec
    exec(code, globals, locals)

  File c:\users\-----\documents\spyder\----------\---------\-------\untitled1.py:118
    neurone(x1[i], x2[i], x3[i], x4[i], x5[i], x6[i], x7[i], x8[i], weights_, biases_)

  File c:\users\-----\documents\spyder\----------\---------\-------\untitled1.py:38 in neurone
    f[j] = sum(entrée[k][0] * weights_[0][j + k * nombre_neurone] for k in range(len(entrée))) + biases_[0][j]

  File c:\users\-----\documents\spyder\----------\---------\-------\untitled1.py:38 in <genexpr>
    f[j] = sum(entrée[k][0] * weights_[0][j + k * nombre_neurone] for k in range(len(entrée))) + biases_[0][j]

IndexError: list index out of range
Here the code:
Python:
import random

x1,x2,x3,x4,x5,x6,x7,x8 = [1],[1],[1],[1],[1],[1],[1],[1] # Random data
yt = [[1,0,0]] # Random  output

nombre_neurone = 13
nombre_sortie = 3
weights_ = []
biases_ = []
entrée = [x1, x2, x3, x4, x5, x6, x7, x8]
nombreW = (len(entrée) * nombre_neurone) + (nombre_neurone * nombre_sortie)  # Nombre de sorties
nombreB = nombre_neurone + nombre_sortie

def neurone(x1_, x2_, x3_, x4_, x5_, x6_, x7_, x8_, weights_, biases_):
    f = [0.0] * nombre_neurone
    y = [0] * nombre_neurone
    f2 = [0.0] * nombre_sortie
    y2 = [0] * nombre_sortie

    # Calculer les valeurs de f
    for j in range(nombre_neurone):
        f[j] = sum(entrée[k][0] * weights_[0][j + k * nombre_neurone] for k in range(len(entrée))) + biases_[0][j]

    for j in range(nombre_sortie):
        for k in range(nombre_neurone):
            f2[j] += weights_[1][j][k] * f[k]
        f2[j] += biases_[1][j]

    for j in range(nombre_neurone):
        y[j] = 1 if f[j] > 0 else 0

    for j in range(nombre_sortie):
        y2[j] = 1 if f2[j] > 0 else 0
    print(y2)


def DDG(inputs, yt):
    learning_rate = 0.001
    numIterations = 5000

    global weights
    global weights1
    global biases
    global biases1

    numDataPoints = len(yt)
    for iteration in range(numIterations):
        for i in range(numDataPoints):
            f = [0.0] * nombre_neurone
            y = [0] * nombre_neurone
            f2 = [0.0] * nombre_sortie
            y2 = [0] * nombre_sortie

            # Calculer les valeurs de f
            for j in range(nombre_neurone):
                f[j] = sum(inputs[k][i] * weights[j * len(inputs) + k] for k in range(len(inputs))) + biases[j]

            for j in range(nombre_sortie):
                for k in range(nombre_neurone):
                    f2[j] += weights1[j][k] * f[k]
                f2[j] += biases1[j]

            for j in range(nombre_neurone):
                y[j] = 1 if f[j] > 0 else 0

            for j in range(nombre_sortie):
                y2[j] = 1 if f2[j] > 0 else 0

            for j in range(nombre_sortie):
                erreur = y2[j] - yt[i][j]
                for k in range(nombre_neurone):
                    for L in range(len(yt)):
                        for Q in range(len(inputs)):
                            weights1[j][k] += learning_rate * ((1 / numDataPoints) * erreur * inputs[Q][L])
                    biases1[j] += learning_rate * ((1 / numDataPoints) * erreur)

    weights_ = [weights, weights1]
    biases_ = [biases, biases1]


# Initialiser les poids et les biais avec des valeurs aléatoires
biases = [0.0] * nombreB
weights = [0.0] * nombreW
biases1 = [0.0] * nombre_sortie
weights1 = [[0.0] * nombre_neurone for _ in range(nombre_sortie)]

for i in range(nombreW):
    weights[i] = random.uniform(-2.0, 2.0)

for i in range(nombreB):
    biases[i] = random.uniform(-2.0, 2.0)

for i in range(nombre_neurone):
    for j in range(nombre_sortie):
        weights1[j][i] = random.uniform(-2.0, 2.0)

for i in range(nombre_sortie):
    biases1[i] = random.uniform(-2.0, 2.0)

DDG(entrée, yt)
for i in range(len(yt)):
    neurone(x1[i], x2[i], x3[i], x4[i], x5[i], x6[i], x7[i], x8[i], weights_, biases_)

Thank you for any possible response.
 
Joined
Jun 15, 2023
Messages
4
Reaction score
0
Hello Phro0244, I verified your code and found that there were several errors here. Is the correct code, tell me if it works for your use, give me some news.
Python:
import random

x1,x2,x3,x4,x5,x6,x7,x8 = [1],[1],[1],[1],[1],[1],[1],[1] # Random data
yt = [[1,0,0]] # Random  output

nombre_neurone = 13
nombre_sortie = 3
weights_ = []
biases_ = []
entrée = [x1, x2, x3, x4, x5, x6, x7, x8]
nombreW = (len(entrée) * nombre_neurone) + (nombre_neurone * nombre_sortie)  # Nombre de sorties
nombreB = nombre_neurone + nombre_sortie

def neurone(inputs, weights, biases):
    f = [0.0] * nombre_neurone
    y = [0] * nombre_neurone
    f2 = [0.0] * nombre_sortie
    y2 = [0] * nombre_sortie

    # Calculer les valeurs de f
    for j in range(nombre_neurone):
        f[j] = (weights[j * 2] * inputs[0]) + (weights[j * 2 + 1] * inputs[1]) + (weights[j * 2 + 2] * inputs[2]) + (weights[j * 2 + 3] * inputs[3]) + (weights[j * 2 + 4] * inputs[4]) + (weights[j * 2 + 5] * inputs[5]) + (weights[j * 2 + 6] * inputs[6]) + (weights[j * 2 + 7] * inputs[7]) + biases[j]


    for j in range(nombre_neurone):
        y[j] = 1 if f[j] > 0 else 0

    for j in range(nombre_sortie):
        y2[j] = 1 if f2[j] > 0 else 0
    print(y2)
    
def DDG(inputs, yt):
    learning_rate = 0.01
    numIterations = 1000

    global weights
    global weights1

    numDataPoints = len(yt)
    for iteration in range(numIterations):
        for i in range(numDataPoints):
            f = [0.0] * nombre_neurone
            y = [0] * nombre_neurone
            f2 = [0.0] * nombre_sortie
            y2 = [0] * nombre_sortie

            # Calculer les valeurs de f
            for j in range(nombre_neurone):
                f[j] = (weights[j * 2] * inputs[0][i]) + (weights[j * 2 + 1] * inputs[1][i]) + (weights[j * 2 + 2] * inputs[2][i]) + (weights[j * 2 + 3] * inputs[3][i]) + (weights[j * 2 + 4] * inputs[4][i]) + (weights[j * 2 + 5] * inputs[5][i]) + (weights[j * 2 + 6] * inputs[6][i]) + (weights[j * 2 + 7] * inputs[7][i]) + biases[j]

            for j in range(nombre_neurone):
                y[j] = 1 if f[j] > 0 else 0

            for j in range(nombre_sortie):
                y2[j] = 1 if f2[j] > 0 else 0

            for j in range(nombre_sortie):
                erreur = y2[j] - yt[i][j]
                for k in range(nombre_neurone):
                    for L in range(len(yt)):
                        for Q in range(len(inputs)):
                            weights[j * L] += learning_rate * ((1 / numDataPoints) * erreur * inputs[Q][L])
                    biases1[j] += learning_rate * ((1 / numDataPoints) * erreur)
inputs = [x1,x2,x3,x4,x5,x6,x7,x8]
weights = []
weights1 = []
biases = []
biases1 = []
for i in range(nombreW):
    weights.append(random.uniform(-2,2))
for i in range(nombreB):
    biases.append(random.uniform(-2,2))
for i in range(nombreW):
    weights.append(random.uniform(-2,2))
for i in range(nombreB):
    biases1.append(random.uniform(-2,2))
DDG(inputs,yt)
donnee_testes = [[0,0,0,0,0,0,0,0]] # random test data
for i in range(len(donnee_testes)):
    neurone(donnee_testes[i],weights,biases)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,060
Latest member
BuyKetozenseACV

Latest Threads

Top