首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在python中只使用numpy的3层神经网络

在python中只使用numpy的3层神经网络
EN

Stack Overflow用户
提问于 2017-11-21 12:13:37
回答 2查看 564关注 0票数 1

我正在创建一个简单的神经网络,只使用python.I中的numpy,我遵循本教程,https://iamtrask.github.io/2015/07/12/basic-python-network/和我在上面提到的链接中修改了3层神经网络的代码,作为below.As,我需要随时调用单独的方法。但它给了我跟随的错误。我想知道的是为什么我会犯这个错误?因为我是python的初学者,所以我搞不懂为什么会有这个错误?所以请有人帮我解决这个问题。

代码语言:javascript
复制
 import numpy as np

    class NeuralNetwork():
        def __init__(self):

            self.X = np.array([[0, 0, 1],
                  [0, 1, 1],
                  [1, 0, 1],
                  [1, 1, 1]])

            self.y = np.array([[0],
                  [1],
                  [1],
                  [0]])

            np.random.seed(1)

            # randomly initialize our weights with mean 0
            self.syn0 = 2 * np.random.random((3, 4)) - 1
            self.syn1 = 2 * np.random.random((4, 1)) - 1

        def nonlin(x, deriv=False):
            if (deriv == True):
                return x * (1 - x)

            return 1 / (1 + np.exp(-x))

        def train(self,steps):
            for j in xrange(steps):

                # Feed forward through layers 0, 1, and 2
                l0 = self.X
                print("came 1")
                l1 = self.nonlin(np.dot(l0, self.syn0))
                print("came 2")
                l2 = self.nonlin(np.dot(l1, self.syn1))

                # how much did we miss the target value?
                l2_error = self.y - l2

                if (j % 10000) == 0:
                    print "Error:" + str(np.mean(np.abs(l2_error)))

                # in what direction is the target value?
                # were we really sure? if so, don't change too much.
                l2_delta = l2_error * self.nonlin(l2, deriv=True)

                # how much did each l1 value contribute to the l2 error (according to the weights)?
                l1_error = l2_delta.dot(self.syn1.T)

                # in what direction is the target l1?
                # were we really sure? if so, don't change too much.
                l1_delta = l1_error * self.nonlin(l1, deriv=True)

                self.syn1 += l1.T.dot(l2_delta)
                self.syn0 += l0.T.dot(l1_delta)

            print("Output after training:")
            print(l2)

    if __name__ == '__main__':
        ann=NeuralNetwork()
        ann.train(6000)

我正在收到的错误如下所示

代码语言:javascript
复制
Traceback (most recent call last):
  File "C:/Users/Ssa/Desktop/Neural-Network-using-numpy-master/Neural-Network-using-numpy-master/outbreak_test/outbreak_test4.py", line 63, in <module>
    ann.train(6000)
  File "C:/Users/Ssa/Desktop/Neural-Network-using-numpy-master/Neural-Network-using-numpy-master/outbreak_test/outbreak_test4.py", line 34, in train
    l1 = self.nonlin(np.dot(l0, self.syn0))
  File "C:/Users/Ssa/Desktop/Neural-Network-using-numpy-master/Neural-Network-using-numpy-master/outbreak_test/outbreak_test4.py", line 23, in nonlin
    if (deriv == True):
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Process finished with exit code 1
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-11-21 12:18:32

问题是,您已经将函数nonlin定义为非类成员函数.这意味着,函数的第一个参数不是self (对对象的引用)。您可以使代码以两种不同的方式工作:

1)将nonlin函数更改为:

代码语言:javascript
复制
def nonlin(self, x, deriv=True):
    ...

2)使nonlin函数的静态方法:

代码语言:javascript
复制
@staticmethod
def nonlin(x, deriv=True):
    ...

您可以找到关于第二种方法here的更多信息。这两种方法都是有效的,但在我看来,第一种方法似乎更适合面向对象编程。

票数 2
EN

Stack Overflow用户

发布于 2017-11-21 12:18:26

nonlin需要使用self参数,否则,self将被视为xx将被视为deriv

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47412893

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档