Multilayer Neural Network-Same value for Accuracy and Loss function in validation data

Viewed 19

I want to implement a Multilayer Perceptron from Scratch but when I try to implement de Loss and Accuracy function with the code below.

def MSE(y_hat, y_verdadero): return np.average((y_hat-y_true)**2)
def l_MSE(y,y_hat): return np.square(np.subtract(y,y_hat)).mean()

def deriva(self,y,y_hat): return (2*(y-y_hat))/len(y_hat)  

def train(self,X_train, y_train,x_val, y_val, epochs,batch_size, learning_rate=0.1,eps=1e-8,beta1=0.9,beta2=0.999, alp=0.9):
    N = X_train.shape[0]
    n_batches = int(np.floor(N/batch_size))
    for epoch in range(epochs):
      train_loss = []
      train_accuracy = []
      val_loss = []
      val_accuracy = []
      l = 0
      acc = 0
      temp = 0

      for j, inputs in enumerate(X_train):
        inputs_val = y_train[j]
        for batch in range(n_batches):
          x = np.squeeze(X_train[batch*batch_size:batch_size+batch*batch_size]).T
          y = y_train[batch*batch_size:batch_size+batch*batch_size]
          y_hat= self.forward_propagation(entradas)
          error=self.deriva(inputs_val,y_hat)
          self.back_propagation(error,learning_rate,eps, beta1, beta2, alp)
          l += self.l_met(y_train[batch*batch_size:batch_size+batch*batch_size],y_hat)
          acc += self.met(y_hat, y_train[batch*batch_size:batch_size+batch*batch_size])

      l = l/(n_batches + (N%batch_size))
      acc = acc/(n_batches + temp)

      train_loss.append(l)
      train_accuracy.append(acc)

      #### Validation
      if x_val.any():
        y_val_hat= self.backward_propagation(x_val)
        val_acc = self.met(y_val_hat,y_val)
        val_l = self.l_met(y_val, y_val_hat)
      val_accuracy.append(val_acc)
      val_loss.append(val_l)

      print("Epoch: {}, Train Loss: {}, Train_acc: {}, val_loss: {}, val_Accuracy: {}".format(epoch+1,l,acc,val_l,val_acc))

I have these results.

mlp = MLP(7, [6,4,2], 1,activacion='tanh', metric='MSE', optimi='Adam')
mlp.entrenamiento(x_train,y_train,x_val,y_val,2,16)  

Epoch: 1, Train Loss: 6.29417848988904, Train_acc: 8.056548467057972, val_loss: 0.23105948477750346, val_Accuracy: 0.23105948477750346

Epoch: 2, Train Loss: 6.278152206975698, Train_acc: 8.036034824928894, val_loss: 0.2307267807610978, val_Accuracy: 0.2307267807610978

As you can see the val_loss and val_Accuracy are the same. I understand what happend but I don't know hot to fix it. Any help?

0 Answers
Related