【问题标题】:Simple Neural Network numpy error missing "exe"简单的神经网络 numpy 错误缺少“exe”
【发布时间】:2021-01-25 03:46:52
【问题描述】:

我正在尝试在 python 中为简单的神经网络运行此代码,但是提示错误“模块'numpy'没有属性'exe'”。我试过网上搜索,但无法弄清楚问题出在哪里,代码如下:

import numpy as np

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

class NeuralNetwork(object):
def __init__(self):
    #parameters
    self.inputsize= 3
    self.outputsize= 1
    self.hiddensize=4
    self.learning_rate=0.005
    
    #(3x4) weight matrix from input layer to hidden layer
    self.w0= np.random.randn(self.inputsize, self.hiddensize) 
    #(4x1) weight matrix from hidden layer to output layer 
    self.w1=np.random.randn(self.hiddensize, self.outputsize)

def feedforward(self, x):  
    #forward propegation through the network
    self.z = np.dot(x, self.w0) #dot product with input and first set of weights
    self.z2= self.sigmoid(self.z) #activation function
    self.z3= np.dot(self.z2, self.w1) #dot product with hidden layer and second set of weights
    output= self.sigmoid(self.z3)
    return output
def sigmoid(self, s, deriv=False):
    if (deriv==True):
        return s*(1-s)
    return 1/(1+np.exe(-s))

def backward(self, x, y, output):
    #backward propegation through the network
    self.output_error= y - output #error in output
    self.output_delta= self.output_error * self.sigmoid(output, deriv=True)
    #hidden layer error & delta
    self.z2_error=self.output_delta.dot(self.w1.T)
    self.z2_delta=self.z2_error * self.sigmoid(self.z2, deriv=True)
    #updating weights
    
    self.w0 += self.learning_rate*(x.T.dot(self.z2_delta))
    self.w1 += self.learning_rate*(self.z2.T.dot(self.output_delta))
    
def train(self, x, y):
    output=self.feedforward(x)
    self.backward(x,y, output)

到目前为止没有错误,但是当我运行循环时

NN=NeuralNetwork()
for i in range(1000):
    NN.train(x,y)
    
print("predicted output:  " + str(NN.feedforward(x)))

提示的错误是

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-42-f9adb58b2d65> in <module>
      1 NN=NeuralNetwork()
      2 for i in range(1000):
----> 3     NN.train(x, y)

<ipython-input-39-83a1cb894a8f> in train(self, x, y)
     39 
     40     def train(self, x, y):
---> 41         output=self.feedforward(x)
     42         self.backward(x,y, output)
     43 

<ipython-input-39-83a1cb894a8f> in feedforward(self, x)
     15         #forward propegation through the network
     16         self.z = np.dot(x, self.w0) #dot product with input and first set of weights
---> 17         self.z2= self.sigmoid(self.z) #activation function
     18         self.z3= np.dot(self.z2, self.w1) #dot product with hidden layer and second set of weights
     19         output= self.sigmoid(self.z3)

<ipython-input-39-83a1cb894a8f> in sigmoid(self, s, deriv)
     22         if (deriv==True):
     23             return s*(1-s)
---> 24         return 1/(1+np.exe(-s))
     25 
     26     def backward(self, x, y, output):

~\anaconda3\lib\site-packages\numpy\__init__.py in __getattr__(attr)
    217                 return Tester
    218             else:
--> 219                 raise AttributeError("module {!r} has no attribute "
    220                                      "{!r}".format(__name__, attr))
    221 

AttributeError: module 'numpy' has no attribute 'exe'

这是我第一次发帖,如有错误请见谅

【问题讨论】:

  • 错字。它应该是 np.exp 而不是 np.exe

标签: python python-3.x numpy deep-learning neural-network


【解决方案1】:

试试这个 sigmoid 函数:

def sigmoid(self, s, deriv=False):
    if (deriv==True):
        return s*(1-s)
    return 1/(1+np.exp(-s))

【讨论】:

    【解决方案2】:

    在您的sigmoid 函数中,您使用的是np.exe,它应该是np.exp

    Numpy 没有任何名为exe 的函数,所以你得到的是AttributeError

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-08
      • 1970-01-01
      • 1970-01-01
      • 2019-03-28
      • 2017-11-20
      • 1970-01-01
      • 2016-08-18
      • 2011-06-10
      相关资源
      最近更新 更多