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

丽莎·谢克斯

我正在使用 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):
    ...

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

神经网络 - 仅使用输出层中的一个神经元预测 MNIST 数字

来自分类Dev

神经网络中的“层”是什么

来自分类Dev

在神经网络中使用卷积层

来自分类Dev

使用NumPy用Python制作的Tunning神经网络

来自分类Dev

如何使用TensorFlow和python在MNIST数据上创建2层神经网络

来自分类Dev

什么是冻结/解冻神经网络中的一层?

来自分类Dev

如何在神经网络中删除整个隐藏层?

来自分类Dev

为什么在神经网络中需要隐藏层?

来自分类Dev

使用反向分页对多层神经网络中的一个随机层进行权重更新?

来自分类Dev

神经网络回归:缩放输出还是使用线性层?

来自分类Dev

使用numpy进行单变量回归的神经网络仅给出线性结果

来自分类Dev

消除神经网络层的偏差

来自分类Dev

设计神经网络的隐藏层

来自分类Dev

2 层神经网络不收敛

来自分类Dev

Encog神经网络隐藏层中的最佳神经元数量

来自分类Dev

如何计算神经网络隐藏层中神经元的目标输出?

来自分类Dev

python神经网络解释

来自分类Dev

Python神经网络编码

来自分类Dev

Swift中的神经网络

来自分类Dev

神经网络中的权重

来自分类Dev

神经网络中的动量

来自分类Dev

Swift中的神经网络

来自分类Dev

WEKA 中的神经网络

来自分类Dev

使用神经网络的OCR

来自分类Dev

使用Keras的神经网络

来自分类Dev

神经网络隐藏层与卷积隐藏层直觉

来自分类Dev

在张量流中的神经网络中添加层时出错

来自分类Dev

python中的神经网络接收场可视化

来自分类Dev

如何在python中修改神经网络的代价函数