def model_inference_full(self,values,modelId): xInference = np.atleast_2d(np.array(values).astype(np.float32)) if self.preload_models == True: if 'cnn' in modelId.lower(): model = self.cnn_ae_model else: model = self.ae_model else: if 'cnn' in modelId.lower(): model = load_model(self.basePath + "Models/Autoencoder/Full/CNN-AE.h5") else: model = load_model(self.basePath + "Models/Autoencoder/Full/AE.h5") X_predict = np.atleast_2d(xInference) if 'cnn' in modelId.lower(): X_predict = X_predict[...,np.newaxis] predict = model.predict(X_predict) mse = mean_squared_error(X_predict,predict).numpy().flatten()[0].astype(float) return mse
def model_inference_lite(self,values,modelId): xInference = np.atleast_2d(np.array(values).astype(np.float32)) if self.preload_models == True: interpreter = self.cnn_ae_lite_model else: interpreter = tflite.Interpreter(model_path=self.basePath + "Models/Autoencoder/Lite/CNN-AE-Lite.tflite") interpreter.allocate_tensors() # Get input and output tensors. input_details = interpreter.get_input_details() output_details = interpreter.get_output_details() # Test model on random input data. input_shape = input_details[0]['shape'] # input_data = np.array(np.random.random_sample(input_shape), dtype=np.float32) input_data = xInference.reshape(input_shape).astype(np.float32) num_samples = 1 interpreter.set_tensor(input_details[0]['index'], input_data) interpreter.invoke() # The function `get_tensor()` returns a copy of the tensor data. # Use `tensor()` in order to get a pointer to the tensor. output_data = interpreter.get_tensor(output_details[0]['index']).reshape(input_shape) input_data = np.repeat(input_data,num_samples,axis=0) mse = mean_squared_error(output_data,input_data).numpy().flatten()[0].astype(float) return mse
def metric(y_true, y_pred): """ Parameters ---------- y_true : Keras tensor Keras tensor including the ground truth. Since the keras tensor includes an extra column to store the index of the data sample in the training set this column is ignored. y_pred : Keras tensor Keras tensor with the predictions of the contamination model (no data index). """ return mean_squared_error(y_true[:, :nout], y_pred[:, :nout])
def metric(y_true, y_pred): """ Parameters ---------- y_true : Keras tensor Keras tensor including the ground truth y_pred : Keras tensor Keras tensor including the predictions of a heteroscedastic model. The predictions follow the order: (mean_0, S_0, mean_1, S_1, ...) with S_i the log of the variance for the ith output. """ if nout > 1: y_out = K.reshape(y_pred[:, 0::nout], K.shape(y_true)) else: y_out = K.reshape(y_pred[:, 0], K.shape(y_true)) return mean_squared_error(y_true, y_out)
def evaluate_autoencoder(y_pred, y_test): mse = mean_squared_error(y_pred, y_test) r2 = r2_score(y_test, y_pred) corr, _ = pearsonr(y_pred.flatten(), y_test.flatten()) # print('Mean squared error: {}%'.format(mse)) return {'mse': mse, 'r2_score': r2, 'correlation': corr}
def mse(y_true, y_pred): return mean_squared_error(y_true, y_pred)
def last_time_step_mse(Y_true, Y_pred): return mean_squared_error(Y_true[:, -1], Y_pred[:, -1])
def rmse(y, y_hat): return tf.math.sqrt(mean_squared_error(y, y_hat))
loss_object(y_valid, model(x_valid))) print('%d epochs, %d iterations: MSE train/valid = %.6f/%.6f' % (epoch, iteration, mse_train, mse_valid)) model.summary() # Out-of-sample test (Evaluation) y_test_pred = model(x_test) true_price = sc.inverse_transform(y_test) pred_price = sc.inverse_transform(y_test_pred) true_open = true_price[:, 0] pred_open = pred_price[:, 0] mse = metrics.mean_squared_error(true_open, pred_open).numpy().mean() rmse = math.sqrt(mse) mae = metrics.mean_absolute_error(true_open, pred_open).numpy().mean() mape = metrics.mean_absolute_percentage_error(true_open, pred_open).numpy().mean() print('MSE - %.6f' % mse) print('RMSE - %.6f' % rmse) print('MAE - %.6f' % mae) print('MAPE - %.6f' % mape) # Loss plotting plt.figure(figsize=(12, 5)) plt.subplot(1, 2, 1) plot_loss_history(loss_history, n_epochs) plt.subplot(1, 2, 2)