-1
univariate_past_history = 100
univariate_future_target = 0

x_train_uni, y_train_uni = univariate_data(uni_data, 0, TRAIN_SPLIT,
                                           univariate_past_history,
                                           univariate_future_target)
x_val_uni, y_val_uni = univariate_data(uni_data, TRAIN_SPLIT, None,
                                       univariate_past_history,
                                       univariate_future_target)

The value to be predicted is y[0].numpy() and the predicted value is simple_lstm_model.predict(x)[0] how do I calculate the mean squared error between two of them?

BATCH_SIZE = 256
BUFFER_SIZE = 10000

train_univariate = tf.data.Dataset.from_tensor_slices((x_train_uni, y_train_uni))
train_univariate = train_univariate.cache().shuffle(BUFFER_SIZE).batch(BATCH_SIZE).repeat()

val_univariate = tf.data.Dataset.from_tensor_slices((x_val_uni, y_val_uni))
val_univariate = val_univariate.batch(BATCH_SIZE).repeat()

simple_lstm_model = tf.keras.models.Sequential([
    tf.keras.layers.LSTM(8, input_shape=x_train_uni.shape[-2:]),
    tf.keras.layers.Dense(1)
])

simple_lstm_model.compile(optimizer='adam', loss='mae')
for x, y in val_univariate.take(1):
    print(simple_lstm_model.predict(x).shape)
EVALUATION_INTERVAL = 200
EPOCHS = 10

simple_lstm_model.fit(train_univariate, epochs=EPOCHS,
                      steps_per_epoch=EVALUATION_INTERVAL,
                      validation_data=val_univariate, validation_steps=50)

for x, y in val_univariate.take(3):
  plot = show_plot([x[0].numpy(), y[0].numpy(),
                    simple_lstm_model.predict(x)[0]], 0, 'Simple LSTM model')
  plot.show()
print(simple_lstm_model.predict(x)[0])
expected = y[0].numpy()
predicted = simple_lstm_model.predict(x)[0]
print(mean_squared_error(expected,predicted))

if I do like the above i get this error TypeError: Singleton array 0.05017540446704798 cannot be considered a valid collection.

Thanks in Advance

1 Answers1

1

The squared error between two scalars is (y_true - y_pred)**2. This is a scalar and we can understand how bad the error is or how good the model is from this scalar quantity.

The squared error of two vectors is a vector but since we want a scalar to understand the deviation between the two vectors we take the mean and hence MSE.

You can use below code to calculate MSE

# Assuming y_train_uni is a list
# Error for single sample
y_pred = simple_lstm_model.predict(x).numpy()[0]
y_true = np.array(y_train_uni)[0]
print (np.power(y_true - y_pred, 2))

# Error for multiple samples
y_pred = simple_lstm_model.predict(x).numpy()
y_true = np.array(y_train_uni)
print (np.power(y_true - y_pred, 2).mean(())
mujjiga
  • 16,186
  • 2
  • 33
  • 51
  • I get this error, please help AttributeError: 'numpy.ndarray' object has no attribute 'numpy'. y[0].numpy() is the true future and simple_lstm_model.predict(x)[0] is the model prediction – Richard Phillips Roy Jun 02 '20 at 12:01
  • 1
    @RichardPhillipsRoy check the type using `type(simple_lstm_model.predict(x))`. If it is numpy already you can use `y_pred = simple_lstm_model.predict(x)` as there is no need to type conversion. – mujjiga Jun 02 '20 at 12:24