0

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

mleafer
  • 825
  • 2
  • 7
  • 15
  • Are you studying ANNs through Stephen Welch's (_Welchlabs_) tutorials? I think I've already seen that code there. – ForceBru Aug 09 '16 at 17:29
  • yea, pure oversight on my part though – mleafer Aug 09 '16 at 17:42
  • keep going, these tutorials are absolutely awesome! I myself learned a lot from them. You can also check out [this online book about ANNs](http://neuralnetworksanddeeplearning.com), it has many examples and very clean explanations. – ForceBru Aug 09 '16 at 17:45
  • thanks for the encouragement! :) and the reference (I have definitely come across this before and will revisit) – mleafer Aug 09 '16 at 18:08

1 Answers1

1

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)

zw324
  • 26,764
  • 16
  • 85
  • 118