text_plain_output_1.png
import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import torch

df = pd.read_csv('../input/mayo-clinic-strip-ai/train.csv')
df_subset = df[['patient_id']][:500]

pred = torch.tensor(np.linspace(0, 1, 500))
target = torch.ones(500, dtype=torch.float64)
df_subset['CE'], df_subset['LAA'] = [pred.numpy(), 1 - pred.numpy()]
df_subset

def HingeLoss(y_pred, y_true):
    """Average hinge loss (non-regularized)
    Parameters:
    ----------
    y_true: torch tensor of shape (n_samples,)
        True target, consisting of integers of two values. The positive lable must 
        be greater than the negative label
    y_predicted: torch tensor of shape (n_samples,)
        Prediction, as output by a decision function (floats)
    Returns:
    ----------
    list: tensor list of calculated loss
    mean: mean loss of the batch
        Hinge loss
    """
    list_ = torch.Tensor([max(0, 1 - x * y) for x, y in zip(y_pred, y_true)])
    return (list_, torch.mean(list_))

def BinaryCrossEntropy(y_pred, y_true):
    """Binary cross entropy loss
    Parameters:
    -----------
    y_true: tensor of shape (n_samples,)
        True target, consisting of integers of two values.
    y_pred: tensor of shape (n_samples,)
        Prediction, as output by a decision function (floats)
    Returns:
    -----------
    loss: float
        BCE loss
    """
    term_0 = y_true * torch.log(y_pred + 1e-07)
    term_1 = (1 - y_true) * torch.log(1 - y_pred + 1e-07)
    return (-(term_0 + term_1), -torch.mean(term_0 + term_1, axis=0))

BCE_list, loss = BinaryCrossEntropy(torch.sigmoid(pred), target)

pos_weight_1 = torch.tensor([0.5])
criterion = torch.nn.BCEWithLogitsLoss(pos_weight=pos_weight_1, reduction='none')
bce_weight_loss_1 = criterion(pred, target)
pos_weight_2 = torch.tensor([0.75])
criterion = torch.nn.BCEWithLogitsLoss(pos_weight=pos_weight_2, reduction='none')
bce_weight_loss_2 = criterion(pred, target)

plt.style.use('seaborn')
plt.figure(figsize=(8, 6))
plt.plot(pred.numpy(), bce_weight_loss_1.numpy(), 'r--', label='weight = 0.5', linewidth=2.0)
plt.plot(pred.numpy(), bce_weight_loss_2.numpy(), 'orange', label='weight = 0.75', linewidth=2.0)
plt.plot(pred.numpy(), Hinge_list.numpy(), color='teal', label='Hinge Loss', linewidth=2.0)
plt.plot(pred.numpy(), BCE_list.numpy(), color='cornflowerblue', label='Log Loss', linewidth=2.0)
plt.legend(loc='upper right')
plt.ylabel('$L(y=1, f(x))$')
plt.ylim(0, 0.8)
plt.xlim(0, 1.0)
plt.xlabel('$Y_{pred}$', fontsize=15)
plt.ylabel('$L(y=1, f(x))$', fontsize=15)
plt.title('Convex Loss Functions', fontsize=15)
plt.show()