1

I started implementing the backend of neural network but got stuck in a code of python. The below is the code for neural Network. While i was making use of the userdefined class in one of the application to be made, i got an error by name attributeError. Please help me out solving it. I tried all the indentation syntax but nothing works.

class NeuralNetwork:
    def __init__(self,layers,alpha=0.1):
        
        self.W=[]
        self.layers=layers
        self.alpha=alpha
        
        
        for i in np.arange(0,len(layers)-2):
            w=np.random.randn(layers[i]+1,layers[i+1]+1)
            self.W.append(w/np.sqrt(layers[i]))
        
        w=np.random.randn(layers[-2]+1,layers[-1])
        self.W.append(w/np.sqrt(layers[-2]))
    
    
    def __repr__(self):
        
        return "NeuralNetwork: {}".format("-".join(str(l) for l in self.layers ))
    
    def sigmoid(self,x):
        #compute and return sigmoid activation for a given input vlaue
        return 1.0/(1+np.exp(-x))
    
    def sigmoid_deriv(self,x):
        return x*(1-x) 
    
    
    def fit(self,X,y,epochs=1000,displayUpdate=100): 
        X=np.c_[X,np.ones((X.shape[0]))]
        for epoch in np.arange(0,epochs):
            for(x,target) in zip(X,y):
                self.fit_partial(x,target)     
            if epoch==0 or (epoch+1)% displayUpdate==0:
                loss=self.calculate_loss(X,y)
                
                print("[INFO] epoch={},loss={:.7f}".format(epoch+1,loss))
                
                
    
    def fit_partial(self,x,y):
        
        A=[np.atleast_2d(x)]
        
        #FeedForward
        
        for layer in np.arange(0,len(self.W)):
            
            net=A[layer].dot(self.W[layer]) 
            
            out=self.sigmoid(net)
            
            A.append(out)
            
        #Backward
        error=A[-1]-y
        D=[error.self.sigmoid_deriv(A[-1])]
        
        for layer in np.arange(len(A)-2,0,-1):
            
            delta=D[-1].dot(self.W[layer].T)
            delta=delta*self.sigmoid_deriv(A[layer])
            D.append(delta)
            
            
        D=D[::-1]
        
        for layer in np.arange(0,len(self.W)):
            
            self.W[layer] += -self.alpha*A[layer].T.dot(D[layer])
            
            
    def predict(self,X,addBias=True): 
        p=np.atleast_2d(X)
        if addBias: 
            p=np.c_[p,np.ones((p.shape[0]))]
        for layer in np.arange(0,len(self.W)): 
            p=self.sigmoid(np.dot(p,self.W[layer]))
        return p
    
    def calculate_loss(self,X,targets):
        targets=np.atleast_2d(targets)
        predictions=self.predict(X,addBias=False)
        loss=0.5*np.sum((predictions-targets)**2)
        return loss

The Below is the error log which was popped as soon as i run used the defined class in a classification problem.

AttributeError                            Traceback (most recent call last)
<ipython-input-7-6d9ffad8d4e7> in <module>
      1 nn=NeuralNetwork([2,2,1],alpha=0.5)
----> 2 nn.fit(X,y,epochs=20000)
      3 
      4 for (x,target) in zip(X,y):
      5     pred=nn.predict(x)[0][0]

D:\BackPropagation\neuralnetwork.ipynb in fit(self, X, y, epochs, displayUpdate)
     42     "    def sigmoid_deriv(self,x):\n",
     43     "        return x*(1-x) \n",
---> 44     "    \n",
     45     "    \n",
     46     "    def fit(self,X,y,epochs=1000,displayUpdate=100):\n",

D:\BackPropagation\neuralnetwork.ipynb in fit_partial(self, x, y)
     69     "            \n",
     70     "            \n",
---> 71     "            out=self.sigmoid(net)\n",
     72     "            \n",
     73     "            A.append(out)\n",

**AttributeError: 'numpy.ndarray' object has no attribute 'self'**

3
  • what is type(net) Commented Aug 13, 2020 at 19:29
  • How do you run this code? Commented Aug 14, 2020 at 20:08
  • Well you need to import the class and use this particular code in place of keras or tensorflow. This has the same functionality of what any neural network has. Use the fit method to train the model and predict method to predict. Commented Aug 15, 2020 at 21:57

1 Answer 1

1

According to the Neural Network Back-end theory, the error must be multiplied to the sigmoid of the previous layer activation to get the value of the delta and not the dot product of it. so the errors. self will not be recognized.

change the following code

error.self.sigmoid_deriv(A[-1])

to

error*self.sigmoid_deriv(A[-1])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.