How to compare the previous instance model output and current model output

Hi, I would like to compare the previous model output and the current output in the loss function.

I build a regression model.

I want to get the previous regression value y_pred and compare it with the current regression value y_pred and use that value, to calculate loss function in my custom loss function.

I couldn’t find anywhere the way to get the previous model output.

Please let me know if you know about this.

Hi @Min-9404, To calculate loss for previous prediction and for the current prediction you can define a custom loss function like

def custom_loss(y_true, y_pred_previous, y_pred):
    mse = tf.keras.losses.mean_squared_error(y_true, y_pred_previous)
    diff_loss = tf.keras.losses.mean_squared_error(y_pred_previous, y_pred)
    total_loss = mse + diff_loss
    return total_loss

Thank You.

Hi, Thank you for the reply.

My question is how to call y_pred_previous. I think in the loss function it is not possible to call y_pred_previous.

How to save the previous result and call was my question.

Anyway, thanks a lot!

Hi @Min-9404, Yes, for the first time the y_pred_previous will be unknown. so you can’t compare the previous output to the current output. Once you get y_pred_previous you can compare it from the 2nd prediction onwards. Thank You.