神经网络 sigmoid 函数 "takes exactly 1 argument (2 given)"

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

尝试通过神经网络前向传播一些数据

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))

当我运行:
NN = Neural_Network() yHat = NN.forward(X)

为什么会出现错误:TypeError: sigmoid() takes exactly 1 argument (2 given)

当我运行: print NN.W1

我得到:[[ 1.034435 -0.19260378 -2.73767483] [-0.66502157 0.86653985 -1.22692781]]

(也许这是 numpy 点函数返回太多维度的问题?)

*注意:我在 jupyter notebook 中 运行ning 并且 %pylab inline

您缺少 sigmoid 函数的 self 参数。 def sigmoid(z): -> def sigmoid(self, z):self.sigmoid(self.z3) 有效地调用了 sigmoid,第一个参数是 self,第二个参数是 self.z3

(那个或你的代码缩进关闭,自代码运行以来看起来不太可能)