From cde7d0a96bd36d2774b6832a75d00de5c365ec54 Mon Sep 17 00:00:00 2001 From: yorozuya-2003 Date: Wed, 28 Dec 2022 23:08:25 +0530 Subject: [PATCH 1/2] Added loss function and unit test for Poisson Loss --- MLlib/loss_func.py | 33 +++++++++++++++++++++++++++++++++ MLlib/tests/test_loss_func.py | 26 ++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 MLlib/tests/test_loss_func.py diff --git a/MLlib/loss_func.py b/MLlib/loss_func.py index f182f09..77ba73e 100644 --- a/MLlib/loss_func.py +++ b/MLlib/loss_func.py @@ -425,3 +425,36 @@ def loss(X, Y, W): y_pred = np.dot(X, W).T L = np.sum(np.true_divide((np.abs(Y - y_pred) * 100), Y)) / X.shape[0] return L + + +class PoisonLoss(): + """ + Calculate Poisson Loss. + """ + + @staticmethod + def loss(X, Y, W): + """ + Calculate Poisson Loss. + + PARAMETERS + ========== + + X: ndarray(dtype=float) + Input vector + Y: ndarray(dtype=float) + Output vector + W: ndarray(dtype=float) + Weights + + RETURNS + ======= + + float or ndarray + array of Poisson losses or + float value of Poisson loss + (depending on the parameters) + """ + + y_pred = np.dot(X, W).T + return np.mean(y_pred - Y * np.log(y_pred), axis=-1) diff --git a/MLlib/tests/test_loss_func.py b/MLlib/tests/test_loss_func.py new file mode 100644 index 0000000..484341d --- /dev/null +++ b/MLlib/tests/test_loss_func.py @@ -0,0 +1,26 @@ +from MLlib.loss_func import PoisonLoss +import numpy as np + + +def test_poisson_loss(): + X = np.array([[0.32794909, 0.69075792], + [0.9059869, 0.77822567], + [0.77191135, 0.73130334], + [0.78795424, 0.89521624], + [0.96298287, 0.29203895], + [0.17946441, 0.75993963], + [0.05451229, 0.85694996], + [0.2915702, 0.0805041], + [0.57499626, 0.10640506], + [0.95203463, 0.78138885], + [0.51214417, 0.8854669], + [0.94145457, 0.84719198]]) + Y = np.array([[0.78936729, 0.8704933, 0.88695031, 0.98672232, 0.29796081, + 0.59689156, 0.88899604, 0.85520577, 0.18910425, 0.84729253, + 0.53142304, 0.7346871]]) + W = np.array([[0.15736584], + [0.3036601]]) + + poissonLoss = np.array([1.19509275]) + + assert np.isclose(PoisonLoss.loss(X, Y, W), poissonLoss).all() From 7bce8a544d63ce4e6bcaa3167085defa12dc24f1 Mon Sep 17 00:00:00 2001 From: yorozuya-2003 Date: Tue, 3 Jan 2023 16:47:20 +0530 Subject: [PATCH 2/2] Added derivative method for Poisson Loss class --- MLlib/loss_func.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/MLlib/loss_func.py b/MLlib/loss_func.py index 77ba73e..d1095ea 100644 --- a/MLlib/loss_func.py +++ b/MLlib/loss_func.py @@ -458,3 +458,29 @@ def loss(X, Y, W): y_pred = np.dot(X, W).T return np.mean(y_pred - Y * np.log(y_pred), axis=-1) + + @staticmethod + def derivative(X, Y, W): + """ + Calculate derivative for Poisson Loss method. + + PARAMETERS + ========== + + X: ndarray(dtype=float) + Input vector + Y: ndarray(dtype=float) + Output vector + W: ndarray(dtype=float) + Weights + + RETURNS + ======= + + ndarray + array of derivatives + """ + + M = X.shape[0] + y_pred = np.dot(X, W).T + return np.dot(1 - Y / y_pred, X) / M