Pankaj
Pankaj

Reputation: 55

sigmoid() takes 1 positional argument but 2 were given

Why am I getting this error

sigmoid() takes 1 positional argument but 2 were given

while using function yHat = NN.forward(X)??

class Neural_Networks(object):
    def __init__(self):
        self.inputLayerSize =2
        self.outputLayerSize =1
        self.hiddenLayerSize = 3
        #weights
        self.W1 = np.random.randn(self.inputLayerSize, self.hiddenLayerSize)
        self.W2 = np.random.randn(self.hiddenLayerSize, self.outputLayerSize)

    def forward(self,X):
        #propogates input through network
        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(z):
        return 1/(1+np.exp(-z))

Upvotes: 2

Views: 8830

Answers (1)

jonyfries
jonyfries

Reputation: 854

You are using it as an instance method so you must include self as the first argument

class Neural_Networks(object):
    def __init__(self):
        self.inputLayerSize =2
        self.outputLayerSize =1
        self.hiddenLayerSize = 3
        #weights
        self.W1 = np.random.randn(self.inputLayerSize, self.hiddenLayerSize)
        self.W2 = np.random.randn(self.hiddenLayerSize, self.outputLayerSize)

    def forward(self,X):
        #propogates input through network
        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))

Conversely if you want to use sigmoid as a class method than you'll need to add a @staticmethod decorator to it eg:

@staticmethod
def sigmoid(z):
    return 1/(1+np.exp(-z))

Making it a static method is likely the right option since you don't use self in the method.

Upvotes: 6

Related Questions