Skip to content

Binary Crossentropy loss #191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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)