Search code examples
pythontensorflowlstm

How to find the MSE and MAPE metrics on test data with tensorflow


I have the following LSTM model. How I can check the MSE or MAPE metrics on the test data?

import pandas as pd
import numpy as np
import tensorflow as tf
from tensorflow import keras
from keras.models import Sequential
from keras.layers import LSTM, Dense, Dropout
X_train = np.random.randn(100, 5, 1)
Y_train = np.random.randn(100, 1)
X_test = np.random.randn(20, 1)

model = Sequential()
model.add(LSTM(64, activation='relu', input_shape=(X_train.shape[1], X_train.shape[2]), return_sequences=True))
model.add(LSTM(32, activation='relu', return_sequences=False))
model.add(Dropout(0.2))
model.add(Dense(Y_train.shape[1]))
model.compile(optimizer='adam', loss='mse')
history = model.fit(X_train, Y_train, epochs=2, batch_size=100, validation_split=0.1, verbose=1)
prediction = model.predict(X_test) 

Solution

  • Something like this should work:

    mape_loss = keras.metrics.mean_absolute_percentage_error(Y_test, prediction)
    mse_loss = keras.metrics.mean_squared_error(Y_test, prediction)
    

    Documentation