简体   繁体   中英

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. 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? 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. This means, that the first argument of the function is not self (a reference to the object). You can make your code working in two different ways:

1) Change the nonlin function to this:

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

2) Make the nonlin function static method:

@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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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