1

I am trying to do a forward propagation through the following code.

import numpy as np
# Create Forward propagation class
class NeuralNetwork:
    def _init_(self):
        self.w1 = np.array([[0.82, 0.53, 0.44],
                           [0.15, 0.61, 0.39],
                           [0.11, 0.41, 0.5],
                           [0.45, 0.23, 0.68]])
        self.w2 = np.array([[0.49, 0.71],
                           [0.23, 0.21],
                           [0.44, 0.45]]) # assumed w18=0.45
        self.b1 = 0.5
        self.b2 = 0.5

    def sigmoid(self,x):
        return 1.0/(1.0+np.exp(-x))

    def ForwardPropagation(self,x):
        self.a = np.matmul(x,self.w1) + self.b1
        self.h1 = self.sigmoid(self.a)
        self.o = np.matmul(self.h1,self.w2) + self.b2      
        h2 = self.sigmoid(self.o)
        return h2


x = np.array([[2, 5, 1, 4]])
NN = NeuralNetwork()    
h2 = NN.ForwardPropagation(x)
h2

However it gives me the following error: AttributeError: 'NeuralNetwork' object has no attribute 'w1'

Can you please help me understand what am I doing wrong ?

1 Answers1

1

The problem lies in the _init_(self) method of your class. What you want is a constructor to your class which would initialize the weights and biases of your Neural Network. The constructor to a python class is __init__(self). Notice the double underscore instead of just the one which you have in your code. Because of this error, the interpreter does not see your _init_(self) as a constructor and hence does not initialize your weights and biases.

Hope this helps.

ntd
  • 2,052
  • 3
  • 13
  • 19