0
$\begingroup$

I wanna implement the back-propagation algorithm in python with the next code

class MLP(object):
   def __init__(self, num_inputs=3, hidden_layers=[3, 3], num_outputs=2):
      self.num_inputs = num_inputs
      self.hidden_layers = hidden_layers
      self.num_outputs = num_outputs

        
      layers = [num_inputs] + hidden_layers + [num_outputs]

      weights = []
      bias = []
      for i in range(len(layers) - 1):
         w = np.random.rand(layers[i], layers[i + 1])
         b=np.random.randn(layers[i+1]).reshape(1, layers[i+1])  
         weights.append(w)
         bias.append(b)
      self.weights = weights
      self.bias = bias

      activations = []
      for i in range(len(layers)):
         a = np.zeros(layers[i])
         activations.append(a)
      self.activations = activations

      dW=[]
      db=[]
      for i in range(len(layers)-1):
         derW=np.zeros((layers[i], layers[i+1]))
         derb=np.zeros((layers[i+1])).reshape(1, layers[i+1])
         dW.append(derW)  
         db.append(derb)
      self.dW=dW  
      self.db=db


   def forward_propagate(self, inputs):
     activations = inputs
     
     self.activations[0] = activations
     for i, w in enumerate(self.weights):
        activations = self._sigmoid((np.matmul(activations, w)+self.bias[i))
         self.activations[i+1] = activations.T
     return activations

   def back_propagate(self,error):
      for i in reversed(range(len(self.dW))):
         activations=self.activations[i+1]
         delta = np.multiply(self._sigmoid(activations),error)
         print("This is delta: {} ".format(delta))
         current_activations=self.activations[i]
         current_activations = current_activations.reshape(current_activations.shape[0],-1)
         print("This is the current activations: {} ".format(current_activations))
         self.dW[i] = 1/delta.shape[0]*np.dot(current_activations,delta)

   def train(self, inputs, targets, epochs, learning_rate):
     for i in range(epochs):
       sum_errors = 0
       for j, input in enumerate(inputs):
         target = targets[j]
         output = self.forward_propagate(input)

         error = target - output
         self.back_propagate(error)

   def _sigmoid(self, x):
     y = 1.0 / (1 + np.exp(-x))
     return y

So I created the next dummy data in order to verify everything is correct

items = np.array([[random()/2 for _ in range(2)] for _ in range(1000)])
targets = np.array([[i[0] + i[1]] for i in items])
    
mlp = MLP(2, [5], 1)
    
mlp.train(items, targets, 2, 0.1)

but when I run the code I have the next error

ValueError: shapes (2,1) and (5,1) not aligned: 1 (dim 1) != 5 (dim 0)

I understand the error because when I printed the delta and current activations values I have the next ones:

This delta: [[-0.67139682]] 
This is the current activations: [[ 0.11432486]
 [-0.38246416]
 [-0.85207878]
 [ 0.73210993]
 [ 0.76603196]] 
This is delta: [[-1.45663835]
 [-1.2793182 ]
 [-0.76875725]
 [-0.90048138]
 [-0.86253739]] 
This is the current activations: [[0.08248608]
 [0.12631125]] 

So what I really want is that the current activation [[-0.67139682]] multiply with this delta value

[[0.08248608]
 [0.12631125]]

and this current activations

[[ 0.11432486]
 [-0.38246416]
 [-0.85207878]
 [ 0.73210993]
 [ 0.76603196]] 

multiply with this delta value

[[-1.45663835]
 [-1.2793182 ]
 [-0.76875725]
 [-0.90048138]
 [-0.86253739]] 

but I don't know how to do that. Any help?

$\endgroup$

1 Answer 1

0
$\begingroup$

I believe you should change:

self.dW[i] = 1/delta.shape[0]*np.dot(current_activations,delta)

to

self.dW[i] = 1/delta.shape[0]*np.dot(current_activations,delta.T)

in the back propagation function. This will help you to avoid the error.

$\endgroup$
1
  • $\begingroup$ but it is ok if in the forward propgate the activations are already transpose? $\endgroup$
    – Al.Vioky
    Aug 18, 2022 at 20:46

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.