Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions MLlib/loss_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,3 +425,55 @@ 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 BinaryCrossentropy():
"""
Binary Crossentropy loss Function
"""

@staticmethod
def loss(X, W, Y):
"""
Calculate Binary Crossentropy Loss

PARAMETERS
==========
X: ndarray(dtype = float, ndim = 1)
input vector

Y: ndarray(dtype = float)
output vector

W: ndarray(dtype = float)
Weights

RETURNS
=======

"""
y_pred = np.dot(X, W).T
L = -1 * (Y[0] * log(y_pred[0]) + Y[1] * log(y_pred[1]))
return L

@staticmethod
def derivative(X, W, Y):
"""
Calculate derivative for Binary crossentropy loss function.

PARAMETERS
==========

X:ndarray(dtype=float,ndim=1)
input vector
Y:ndarray(dtype=float)
output vector
W:ndarray(dtype=float)
Weights

RETURNS
=======

array of derivates
"""
return np.dot((np.dot(X, W).T - Y),X)