-
Notifications
You must be signed in to change notification settings - Fork 0
/
loss.py
33 lines (23 loc) · 843 Bytes
/
loss.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import numpy as np
from tensor import Tensor
class Loss:
def loss(self, predicted: Tensor, actual: Tensor) -> float:
raise NotImplementedError
def grad(self, predcited: Tensor, actual: Tensor) -> Tensor:
raise NotImplementedError
class MSE(Loss):
'''
Mean Squared error loss
'''
def loss(self, predicted: Tensor, actual: Tensor) -> float:
return np.sum((predicted - actual) ** 2)
def grad(self, predcited: Tensor, actual: Tensor) -> Tensor:
return 2 * (predcited - actual)
class Cross_emptropy(Loss):
'''
Cross emptropy loss
'''
def loss(self, predicted: Tensor, actual: Tensor) -> float:
return -np.sum(actual * np.log(predicted))
def grad(self, predcited: Tensor, actual: Tensor) -> Tensor:
return -(actual * np.reciprocal(predcited))