From 4d41a41d82274f132b9c1f2619bba5976fa8af1e Mon Sep 17 00:00:00 2001 From: Falguni Ghosh <falguni.ghosh@fau.de> Date: Sun, 15 Oct 2023 21:00:29 +0000 Subject: [PATCH] Upload New File --- 1_DL_base/Loss.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 1_DL_base/Loss.py diff --git a/1_DL_base/Loss.py b/1_DL_base/Loss.py new file mode 100644 index 0000000..e33f580 --- /dev/null +++ b/1_DL_base/Loss.py @@ -0,0 +1,22 @@ +import numpy as np + + +class CrossEntropyLoss: + + def __init__(self): + self.prediction_tensor = None + + def forward(self, prediction_tensor, label_tensor): + loss = 0 + a = prediction_tensor * label_tensor + for i in range(a.shape[0]): + loss = loss + (-np.log(np.sum(a[i]) + np.finfo(float).eps)) + + self.prediction_tensor = np.copy(prediction_tensor) + return loss + + def backward(self, label_tensor): + error_tensor = np.empty(label_tensor.shape) + for i in range(label_tensor.shape[0]): + error_tensor[i] = - label_tensor[i]/(self.prediction_tensor[i] + np.finfo(float).eps) + return error_tensor -- GitLab