Asif Imran
Asif Imran

Reputation: 13

Python bug regarding attribute

I recently coded the following:

class Neural_Network(object):
def _init_(self):
    self.inputLayerSize = 2
    self.outputLayerSize = 1
    self.hiddenLayerSize = 2

    # Weights
    self.W1 = np.random.randn(self.inputLayerSize,\
                             self.hiddenLayerSize)
    self.W2 = np.random.randn(self.hiddenLayerSize,\
                             self.outputLayerSize)

    def forward(self, X):

        self.z2 = np.dot(X, self.W1)
        self.a2 = self.sigmoid(self.z2)
        self.z3 = np.dot(self.a2, self.W2)
        yHat = self.sigmoid(self.z3)
        return yHat

    def sigmoid(self, z):
        return 1/(1+np.exp(-z))

Now, the problem is when I am doing the following: -

NN = Neural_Network()

and

yHat = NN.forward(X)

I am getting the following error: -

AttributeError Traceback (most recent call last) in () ----> 1 yHat = NN.forward(X=0.9)

AttributeError: 'Neural_Network' object has no attribute 'forward'

I am new to Python but really interested to learn. Can you please tell me what I am ding wring here?

By the way I am doing this in jupyter-notebook

Best

Upvotes: 0

Views: 122

Answers (1)

Alex
Alex

Reputation: 19124

Two problesms:

  1. It's __init__ (double underscore) not _init_.
  2. Your forward and sigmoid functions are defined in the __init__ method instead of in the class scope.

Try this:

class Neural_Network(object):
    def __init__(self):
        ...

    def forward(self, X):
        ...

    def sigmoid(self, z):
        ...

Upvotes: 3

Related Questions