Skip to content
Snippets Groups Projects
Commit 8967e639 authored by Falguni Ghosh's avatar Falguni Ghosh
Browse files

Upload New File

parent 320f5dd3
Branches
No related tags found
No related merge requests found
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]):
#print(a[i])
loss = loss + (-np.log(np.sum(a[i]) + np.finfo(float).eps))
#print(loss)
#prediction_tensor_i = prediction_tensor[i, :]
#relevant_prediction_tensor_i = prediction_tensor[i, :][label_tensor[i, :] == 1]
#print(relevant_prediction_tensor_i)
#loss = loss - np.sum(np.log(relevant_prediction_tensor_i + epsilon))
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])
return error_tensor
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment