簡體   English   中英

在Python中僅使用numpy的3層神經網絡

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

我正在僅使用python中的numpy創建一個簡單的神經網絡。我正在關注本教程https://iamtrask.github.io/2015/07/12/basic-python-network/,並且更改了3層的代碼上面提到的鏈接中的神經網絡如下。我需要隨時調用單獨的方法。 但這給了我以下錯誤。 我想知道的是為什么我會收到此錯誤? 因為我是python的初學者,所以我不知道為什么會收到此錯誤? 因此,請有人幫助我解決這個問題。

 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)

我收到的錯誤如下所示

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

問題是,您已將函數nonlin定義為非類成員函數。 這意味着該函數的第一個參數不是self (對對象的引用)。 您可以使代碼以兩種不同的方式工作:

1)將nonlin函數更改為此:

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

2)使nonlin函數成為靜態方法:

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

您可以在此處找到有關第二種方法的更多信息。 兩種方法都是有效的,但我認為第一種方法似乎更適合面向對象的編程。

nonlin需要接受一個self參數,否則, self將被視為x ,而x視為deriv

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM