From 1939d6be7b69485701f96edd827e8aeb6dc6aab4 Mon Sep 17 00:00:00 2001 From: "Chanyub.park" Date: Sun, 6 May 2018 23:30:13 +0900 Subject: [PATCH] add some losses: mae, hinge, etc --- R/losses.R | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/R/losses.R b/R/losses.R index 2edc1f6..090ae70 100644 --- a/R/losses.R +++ b/R/losses.R @@ -2,9 +2,28 @@ get_losses <- function(loss){ out <- switch(loss, "MSE" = mean_square_error, + "MAE" = mean_absolute_error, stop("loss function not implemented")) } mean_square_error <- function(y, y_pred){ return(rowMeans((y_pred - y)^2)) -} \ No newline at end of file +} + +mean_absolute_error <- function(y, y_pred){ + return(rowMeans(abs(y_pred - y))) +} + +squared_hinge <- function(y, y_pred){ + return(rowMeans((pmax(1 - y * y_pred, 0))^2)) +} + +hinge <- function(y, y_pred){ + return(rowMeans(pmax(1 - y * y_pred, 0))) +} + +categorical_hinge <- function(y, y_pred){ + pos <- rowSums(y * y_pred) + neg <- pmax((1 - y) * y_pred) + return(pmax(0, neg - pos + 1)) +}