Search code examples
pythonneural-networkmatrix-multiplicationsigmoid

neural net sigmoid function "takes exactly 1 argument (2 given)"


trying to forward propagate some data through a neural net

import numpy as np
import matplotlib.pyplot as plt

class Neural_Network(object):
def __init__(self):        
    #Define Hyperparameters
    self.inputLayerSize = 2
    self.outputLayerSize = 1
    self.hiddenLayerSize = 3

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

def forward(self, X):
    #Propagate inputs though 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):
    # apply sigmoid activation function
    return 1/(1+np.exp(-z))

When I run:
NN = Neural_Network() yHat = NN.forward(X)

Why do I get the error:TypeError: sigmoid() takes exactly 1 argument (2 given)

when I run: print NN.W1

i get: [[ 1.034435 -0.19260378 -2.73767483] [-0.66502157 0.86653985 -1.22692781]]

(perhaps this is a problem with the numpy dot function returning too many dimensions?)

*note: i am running in jupyter notebook and %pylab inline


Solution

  • You are missing a self argument for the sigmoid function. def sigmoid(z): -> def sigmoid(self, z):. This self.sigmoid(self.z3) is effectively calling sigmoid with self as the first parameter and self.z3 as the second.

    (That or your code indentation is off which doesn't look likely since the code runs)