Index Error during backpropagation in a multilayer neural network.

Joined
Jun 15, 2023
Messages
4
Reaction score
0
Hello everyone! I'm facing an issue with my code and I'm unsure about its nature. I would greatly appreciate help. Thank you in advance for your help!

Here is the error.
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\----------\-----------\untitled0.py:94
neuron.backward_pass(targets, learning_rate)

File c:\users\--------------\documents\-----------------\------------\untitled0.py:61 in backward_pass
error[j] += self.weights[layer + 1][j] * delta[layer + 1][j]

IndexError: list index out of range

Here is the code.
Python:
import random
import math

def sigmoid(x):
    return 1 / (1 + math.exp(-x))
def sigmoid_derivative(x):
    return sigmoid(x) * (1 - sigmoid(x))
class Neuron:
    def __init__(self, num_inputs, num_outputs):
        self.num_layers = 22
        self.weights = []
        self.bias = []
        self.z = []
        self.output = []

        for layer in range(self.num_layers):
            if layer == 0:
                self.weights.append([[random.uniform(-1, 1) for _ in range(num_inputs)] for _ in range(10)])
                self.bias.append([random.uniform(-1, 1) for _ in range(10)])
            elif layer == self.num_layers - 1:
                self.weights.append([[random.uniform(-1, 1) for _ in range(10)] for _ in range(num_outputs)])
                self.bias.append([random.uniform(-1, 1) for _ in range(num_outputs)])
            else:
                self.weights.append([[random.uniform(-1, 1) for _ in range(10)] for _ in range(10)])
                self.bias.append([random.uniform(-1, 1) for _ in range(10)])

    def forward_pass(self, inputs):
        self.output = [inputs]

        for layer in range(self.num_layers):
            layer_output = []
            for i in range(len(self.weights[layer])):
                neuron_input = 0
                for j in range(len(self.output[layer])):
                    neuron_input += self.weights[layer][i][j] * self.output[layer][j]
                neuron_input += self.bias[layer][i]
                layer_output.append(sigmoid(neuron_input))
            self.z.append(layer_output)
            self.output.append(layer_output)

    def backward_pass(self, target, learning_rate):
        delta = [0] * self.num_layers
        error = [target[i] - self.output[self.num_layers][i] for i in range(len(target))]
        delta[self.num_layers - 1] = [error[i] * sigmoid_derivative(self.z[self.num_layers - 1][i]) for i in range(len(error))]
        for layer in range(self.num_layers - 2, 0, -1):
            error = [0] * len(self.bias[layer])
            for i in range(len(self.weights[layer])):
                for j in range(len(self.weights[layer][i])):
                    error[j] += self.weights[layer + 1][j][i] * delta[layer + 1][j]
            delta[layer] = [error[i] * sigmoid_derivative(self.z[layer - 1][i]) for i in range(len(error))]
        for layer in range(self.num_layers):
            for i in range(len(self.weights[layer])):
                for j in range(len(self.weights[layer][i])):
                    self.weights[layer][i][j] += learning_rate * delta[layer][i] * self.output[layer][j]
                self.bias[layer][i] += learning_rate * delta[layer][i]
training_data = [
    {'inputs': [1, 0, 0, 0, 0, 0, 0, 0, 0, 0], 'targets': [1, 0, 0, 0, 0, 0]},
]
neuron = Neuron(10, 6)
iteration = 1000
learning_rate = 0.1

for it in range(iteration):
    for data in training_data:
        inputs = data['inputs']
        targets = data['targets']
        neuron.forward_pass(inputs)
        neuron.backward_pass(targets, learning_rate)
test_inputs = [[1, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
for i in range(len(test_inputs)):
    neuron.forward_pass(test_inputs[i])
    output = neuron.output[neuron.num_layers]
    print(output)
 

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,790
Messages
2,569,637
Members
45,346
Latest member
EstebanCoa

Latest Threads

Top