0

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

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 dot 函数返回太多维度的问题?)

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

4

1 回答 1

1

您缺少self该函数的参数sigmoiddef sigmoid(z):-> def sigmoid(self, z):。这self.sigmoid(self.z3)实际上是调用sigmoidwithself作为第一个参数和self.z3第二个参数。

(那或您的代码缩进已关闭,这看起来不太可能,因为代码运行)

于 2016-08-09T17:23:07.477 回答