简体   繁体   English

在Python中仅使用numpy的3层神经网络

[英]3 layer neural network using only numpy in python

I'm creating a simple neural network using only numpy in python.I'm following this tutorial https://iamtrask.github.io/2015/07/12/basic-python-network/ and I changed the code of 3 layer neural network in above mentioned link as below.As I need to call separate methods whenever I want. 我正在仅使用python中的numpy创建一个简单的神经网络。我正在关注本教程https://iamtrask.github.io/2015/07/12/basic-python-network/,并且更改了3层的代码上面提到的链接中的神经网络如下。我需要随时调用单独的方法。 But it gives me following error. 但这给了我以下错误。 What I want to know is why am I getting this error? 我想知道的是为什么我会收到此错误? As I'm a very beginner to python I can't figure out why I'm getting this error? 因为我是python的初学者,所以我不知道为什么会收到此错误? So please someone kindly help me to get through this problem. 因此,请有人帮助我解决这个问题。

 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)

Error I'm getting is shown below 我收到的错误如下所示

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

The problem is, that you have defined the function nonlin as a non-class member function. 问题是,您已将函数nonlin定义为非类成员函数。 This means, that the first argument of the function is not self (a reference to the object). 这意味着该函数的第一个参数不是self (对对象的引用)。 You can make your code working in two different ways: 您可以使代码以两种不同的方式工作:

1) Change the nonlin function to this: 1)将nonlin函数更改为此:

def nonlin(self, x, deriv=True):
    ...

2) Make the nonlin function static method: 2)使nonlin函数成为静态方法:

@staticmethod
def nonlin(x, deriv=True):
    ...

You can find more information about the second approach here . 您可以在此处找到有关第二种方法的更多信息。 Both methods are valid, but the first one seems to be a better fit for object oriented programming in my opinion. 两种方法都是有效的,但我认为第一种方法似乎更适合面向对象的编程。

nonlin需要接受一个self参数,否则, self将被视为x ,而x视为deriv

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM