mastersom
mastersom

Reputation: 545

keras mean squared error loss function for 3 dimensional time series output

I would like to verify my loss function because i have read that there are issues with the mse loss function in keras. Consider a lstm model in keras predicting a 3d time series as multi targets (y1, y2, y3). Suppose the shape of a batch of output sequences is (10, 31, 1) Will the loss function below take the squared difference between the predicted and true output, then take the mean of the 310 samples, resulting in a single loss value? How would this operation happen if the 3 outputs were concatenated as (10, 31, 3)

def mse(y_true, y_pred):
            return keras.backend.mean(keras.backend.square(y_pred - y_true), axis=1)

Upvotes: 4

Views: 2132

Answers (1)

giser_yugang
giser_yugang

Reputation: 6176

If you want to get a single loss value, you need not to set axis.

import keras.backend as K

def mse(y_true, y_pred):
    return K.mean(K.square(y_pred - y_true))

y_true = K.random_normal(shape=(10,31,3))
y_pred = K.random_normal(shape=(10,31,3))

loss = mse(y_true, y_pred)
print(K.eval(loss))

# print
2.0196152

Upvotes: 5

Related Questions