calculate the mean binary crossentropy bce loss between true labels and predicted probabilities bce loss quantifies dissimilarity between true labels 0 or 1 and predicted probabilities it s widely used in binary classification tasks bce ytrue lnypred 1 ytrue ln1 ypred reference https en wikipedia orgwikicrossentropy parameters ytrue true binary labels 0 or 1 ypred predicted probabilities for class 1 epsilon small constant to avoid numerical instability truelabels np array0 1 1 0 1 predictedprobs np array0 2 0 7 0 9 0 3 0 8 binarycrossentropytruelabels predictedprobs 0 2529995012327421 truelabels np array0 1 1 0 1 predictedprobs np array0 3 0 8 0 9 0 2 binarycrossentropytruelabels predictedprobs traceback most recent call last valueerror input arrays must have the same length calculate the mean binary focal crossentropy bfce loss between true labels and predicted probabilities bfce loss quantifies dissimilarity between true labels 0 or 1 and predicted probabilities it s a variation of binary crossentropy that addresses class imbalance by focusing on hard examples bcfe alpha 1 ypredgamma ytrue logypred 1 alpha ypredgamma 1 ytrue log1 ypred reference lin et al 2018https arxiv orgpdf1708 02002 pdf parameters ytrue true binary labels 0 or 1 ypred predicted probabilities for class 1 gamma focusing parameter for modulating the loss default 2 0 alpha weighting factor for class 1 default 0 25 epsilon small constant to avoid numerical instability truelabels np array0 1 1 0 1 predictedprobs np array0 2 0 7 0 9 0 3 0 8 binaryfocalcrossentropytruelabels predictedprobs 0 008257977659239775 truelabels np array0 1 1 0 1 predictedprobs np array0 3 0 8 0 9 0 2 binaryfocalcrossentropytruelabels predictedprobs traceback most recent call last valueerror input arrays must have the same length clip predicted probabilities to avoid log0 calculate categorical crossentropy cce loss between true class labels and predicted class probabilities cce ytrue lnypred reference https en wikipedia orgwikicrossentropy parameters ytrue true class labels onehot encoded ypred predicted class probabilities epsilon small constant to avoid numerical instability truelabels np array1 0 0 0 1 0 0 0 1 predprobs np array0 9 0 1 0 0 0 2 0 7 0 1 0 0 0 1 0 9 categoricalcrossentropytruelabels predprobs 0 567395975254385 truelabels np array1 0 0 1 predprobs np array0 9 0 1 0 0 0 2 0 7 0 1 categoricalcrossentropytruelabels predprobs traceback most recent call last valueerror input arrays must have the same shape truelabels np array2 0 1 1 0 0 predprobs np array0 9 0 1 0 0 0 2 0 7 0 1 categoricalcrossentropytruelabels predprobs traceback most recent call last valueerror ytrue must be onehot encoded truelabels np array1 0 1 1 0 0 predprobs np array0 9 0 1 0 0 0 2 0 7 0 1 categoricalcrossentropytruelabels predprobs traceback most recent call last valueerror ytrue must be onehot encoded truelabels np array1 0 0 0 1 0 predprobs np array0 9 0 1 0 1 0 2 0 7 0 1 categoricalcrossentropytruelabels predprobs traceback most recent call last valueerror predicted probabilities must sum to approximately 1 calculate the mean hinge loss for between true labels and predicted probabilities for training support vector machines svms hinge loss max0 1 true pred reference https en wikipedia orgwikihingeloss args ytrue actual values ground truth encoded as 1 or 1 ypred predicted values truelabels np array1 1 1 1 1 pred np array4 0 3 0 7 5 10 hingelosstruelabels pred 1 52 truelabels np array1 1 1 1 1 1 pred np array4 0 3 0 7 5 10 hingelosstruelabels pred traceback most recent call last valueerror length of predicted and actual array must be same truelabels np array1 1 10 1 1 pred np array4 0 3 0 7 5 10 hingelosstruelabels pred traceback most recent call last valueerror ytrue can have values 1 or 1 only calculate the mean huber loss between the given ground truth and predicted values the huber loss describes the penalty incurred by an estimation procedure and it serves as a measure of accuracy for regression models huber loss 0 5 ytrue ypred2 if ytrue ypred delta delta ytrue ypred 0 5 delta2 otherwise reference https en wikipedia orgwikihuberloss parameters ytrue the true values ground truth ypred the predicted values truevalues np array0 9 10 0 2 0 1 0 5 2 predictedvalues np array0 8 2 1 2 9 4 2 5 2 np isclosehuberlosstruevalues predictedvalues 1 0 2 102 true truelabels np array11 0 21 0 3 32 4 0 5 0 predictedprobs np array8 3 20 8 2 9 11 2 5 0 np isclosehuberlosstruelabels predictedprobs 1 0 1 80164 true truelabels np array11 0 21 0 3 32 4 0 predictedprobs np array8 3 20 8 2 9 11 2 5 0 huberlosstruelabels predictedprobs 1 0 traceback most recent call last valueerror input arrays must have the same length calculate the mean squared error mse between ground truth and predicted values mse measures the squared difference between true values and predicted values and it serves as a measure of accuracy for regression models mse 1n ytrue ypred2 reference https en wikipedia orgwikimeansquarederror parameters ytrue the true values ground truth ypred the predicted values truevalues np array1 0 2 0 3 0 4 0 5 0 predictedvalues np array0 8 2 1 2 9 4 2 5 2 np isclosemeansquarederrortruevalues predictedvalues 0 028 true truelabels np array1 0 2 0 3 0 4 0 5 0 predictedprobs np array0 3 0 8 0 9 0 2 meansquarederrortruelabels predictedprobs traceback most recent call last valueerror input arrays must have the same length calculates the mean absolute error mae between ground truth observed and predicted values mae measures the absolute difference between true values and predicted values equation mae 1n absytrue ypred reference https en wikipedia orgwikimeanabsoluteerror parameters ytrue the true values ground truth ypred the predicted values truevalues np array1 0 2 0 3 0 4 0 5 0 predictedvalues np array0 8 2 1 2 9 4 2 5 2 np isclosemeanabsoluteerrortruevalues predictedvalues 0 16 true truevalues np array1 0 2 0 3 0 4 0 5 0 predictedvalues np array0 8 2 1 2 9 4 2 5 2 np isclosemeanabsoluteerrortruevalues predictedvalues 2 16 false truelabels np array1 0 2 0 3 0 4 0 5 0 predictedprobs np array0 3 0 8 0 9 5 2 meanabsoluteerrortruelabels predictedprobs traceback most recent call last valueerror input arrays must have the same length calculate the mean squared logarithmic error msle between ground truth and predicted values msle measures the squared logarithmic difference between true values and predicted values for regression models it s particularly useful for dealing with skewed or largevalue data and it s often used when the relative differences between predicted and true values are more important than absolute differences msle 1n log1 ytrue log1 ypred2 reference https insideaiml comblogmeansquaredlogarithmicerrorloss1035 parameters ytrue the true values ground truth ypred the predicted values truevalues np array1 0 2 0 3 0 4 0 5 0 predictedvalues np array0 8 2 1 2 9 4 2 5 2 meansquaredlogarithmicerrortruevalues predictedvalues 0 0030860877925181344 truelabels np array1 0 2 0 3 0 4 0 5 0 predictedprobs np array0 3 0 8 0 9 0 2 meansquaredlogarithmicerrortruelabels predictedprobs traceback most recent call last valueerror input arrays must have the same length calculate the mean absolute percentage error between ytrue and ypred mean absolute percentage error calculates the average of the absolute percentage differences between the predicted and true values formula ytrueiyprediytruein source https stephenallwright comgoodmapescore parameters ytrue np ndarray numpy array containing truetarget values ypred np ndarray numpy array containing predicted values returns float the mean absolute percentage error between ytrue and ypred examples ytrue np array10 20 30 40 ypred np array12 18 33 45 meanabsolutepercentageerrorytrue ypred 0 13125 ytrue np array1 2 3 4 ypred np array2 3 4 5 meanabsolutepercentageerrorytrue ypred 0 5208333333333333 ytrue np array34 37 44 47 48 48 46 43 32 27 26 24 ypred np array37 40 46 44 46 50 45 44 34 30 22 23 meanabsolutepercentageerrorytrue ypred 0 064671076436071 calculate the perplexity for the ytrue and ypred compute the perplexity which useful in predicting language model accuracy in natural language processing nlp perplexity is measure of how certain the model in its predictions perplexity loss exp1n lnpx reference https en wikipedia orgwikiperplexity args ytrue actual label encoded sentences of shape batchsize sentencelength ypred predicted sentences of shape batchsize sentencelength vocabsize epsilon small floating point number to avoid getting inf for log0 returns perplexity loss between ytrue and ypred ytrue np array1 4 2 3 ypred np array 0 28 0 19 0 21 0 15 0 15 0 24 0 19 0 09 0 18 0 27 0 03 0 26 0 21 0 18 0 30 0 28 0 10 0 33 0 15 0 12 perplexitylossytrue ypred 5 0247347775367945 ytrue np array1 4 2 3 ypred np array 0 28 0 19 0 21 0 15 0 15 0 24 0 19 0 09 0 18 0 27 0 30 0 10 0 20 0 15 0 25 0 03 0 26 0 21 0 18 0 30 0 28 0 10 0 33 0 15 0 12 0 30 0 10 0 20 0 15 0 25 perplexitylossytrue ypred traceback most recent call last valueerror sentence length of ytrue and ypred must be equal ytrue np array1 4 2 11 ypred np array 0 28 0 19 0 21 0 15 0 15 0 24 0 19 0 09 0 18 0 27 0 03 0 26 0 21 0 18 0 30 0 28 0 10 0 33 0 15 0 12 perplexitylossytrue ypred traceback most recent call last valueerror label value must not be greater than vocabulary size ytrue np array1 4 ypred np array 0 28 0 19 0 21 0 15 0 15 0 24 0 19 0 09 0 18 0 27 0 03 0 26 0 21 0 18 0 30 0 28 0 10 0 33 0 15 0 12 perplexitylossytrue ypred traceback most recent call last valueerror batch size of ytrue and ypred must be equal matrix to select prediction value only for true class getting the matrix containing prediction for only true class calculating perplexity for each sentence calculate the mean binary cross entropy bce loss between true labels and predicted probabilities bce loss quantifies dissimilarity between true labels 0 or 1 and predicted probabilities it s widely used in binary classification tasks bce σ y_true ln y_pred 1 y_true ln 1 y_pred reference https en wikipedia org wiki cross_entropy parameters y_true true binary labels 0 or 1 y_pred predicted probabilities for class 1 epsilon small constant to avoid numerical instability true_labels np array 0 1 1 0 1 predicted_probs np array 0 2 0 7 0 9 0 3 0 8 binary_cross_entropy true_labels predicted_probs 0 2529995012327421 true_labels np array 0 1 1 0 1 predicted_probs np array 0 3 0 8 0 9 0 2 binary_cross_entropy true_labels predicted_probs traceback most recent call last valueerror input arrays must have the same length clip predictions to avoid log 0 calculate the mean binary focal cross entropy bfce loss between true labels and predicted probabilities bfce loss quantifies dissimilarity between true labels 0 or 1 and predicted probabilities it s a variation of binary cross entropy that addresses class imbalance by focusing on hard examples bcfe σ alpha 1 y_pred gamma y_true log y_pred 1 alpha y_pred gamma 1 y_true log 1 y_pred reference lin et al 2018 https arxiv org pdf 1708 02002 pdf parameters y_true true binary labels 0 or 1 y_pred predicted probabilities for class 1 gamma focusing parameter for modulating the loss default 2 0 alpha weighting factor for class 1 default 0 25 epsilon small constant to avoid numerical instability true_labels np array 0 1 1 0 1 predicted_probs np array 0 2 0 7 0 9 0 3 0 8 binary_focal_cross_entropy true_labels predicted_probs 0 008257977659239775 true_labels np array 0 1 1 0 1 predicted_probs np array 0 3 0 8 0 9 0 2 binary_focal_cross_entropy true_labels predicted_probs traceback most recent call last valueerror input arrays must have the same length clip predicted probabilities to avoid log 0 calculate categorical cross entropy cce loss between true class labels and predicted class probabilities cce σ y_true ln y_pred reference https en wikipedia org wiki cross_entropy parameters y_true true class labels one hot encoded y_pred predicted class probabilities epsilon small constant to avoid numerical instability true_labels np array 1 0 0 0 1 0 0 0 1 pred_probs np array 0 9 0 1 0 0 0 2 0 7 0 1 0 0 0 1 0 9 categorical_cross_entropy true_labels pred_probs 0 567395975254385 true_labels np array 1 0 0 1 pred_probs np array 0 9 0 1 0 0 0 2 0 7 0 1 categorical_cross_entropy true_labels pred_probs traceback most recent call last valueerror input arrays must have the same shape true_labels np array 2 0 1 1 0 0 pred_probs np array 0 9 0 1 0 0 0 2 0 7 0 1 categorical_cross_entropy true_labels pred_probs traceback most recent call last valueerror y_true must be one hot encoded true_labels np array 1 0 1 1 0 0 pred_probs np array 0 9 0 1 0 0 0 2 0 7 0 1 categorical_cross_entropy true_labels pred_probs traceback most recent call last valueerror y_true must be one hot encoded true_labels np array 1 0 0 0 1 0 pred_probs np array 0 9 0 1 0 1 0 2 0 7 0 1 categorical_cross_entropy true_labels pred_probs traceback most recent call last valueerror predicted probabilities must sum to approximately 1 clip predictions to avoid log 0 calculate the mean hinge loss for between true labels and predicted probabilities for training support vector machines svms hinge loss max 0 1 true pred reference https en wikipedia org wiki hinge_loss args y_true actual values ground truth encoded as 1 or 1 y_pred predicted values true_labels np array 1 1 1 1 1 pred np array 4 0 3 0 7 5 10 hinge_loss true_labels pred 1 52 true_labels np array 1 1 1 1 1 1 pred np array 4 0 3 0 7 5 10 hinge_loss true_labels pred traceback most recent call last valueerror length of predicted and actual array must be same true_labels np array 1 1 10 1 1 pred np array 4 0 3 0 7 5 10 hinge_loss true_labels pred traceback most recent call last valueerror y_true can have values 1 or 1 only calculate the mean huber loss between the given ground truth and predicted values the huber loss describes the penalty incurred by an estimation procedure and it serves as a measure of accuracy for regression models huber loss 0 5 y_true y_pred 2 if y_true y_pred delta delta y_true y_pred 0 5 delta 2 otherwise reference https en wikipedia org wiki huber_loss parameters y_true the true values ground truth y_pred the predicted values true_values np array 0 9 10 0 2 0 1 0 5 2 predicted_values np array 0 8 2 1 2 9 4 2 5 2 np isclose huber_loss true_values predicted_values 1 0 2 102 true true_labels np array 11 0 21 0 3 32 4 0 5 0 predicted_probs np array 8 3 20 8 2 9 11 2 5 0 np isclose huber_loss true_labels predicted_probs 1 0 1 80164 true true_labels np array 11 0 21 0 3 32 4 0 predicted_probs np array 8 3 20 8 2 9 11 2 5 0 huber_loss true_labels predicted_probs 1 0 traceback most recent call last valueerror input arrays must have the same length calculate the mean squared error mse between ground truth and predicted values mse measures the squared difference between true values and predicted values and it serves as a measure of accuracy for regression models mse 1 n σ y_true y_pred 2 reference https en wikipedia org wiki mean_squared_error parameters y_true the true values ground truth y_pred the predicted values true_values np array 1 0 2 0 3 0 4 0 5 0 predicted_values np array 0 8 2 1 2 9 4 2 5 2 np isclose mean_squared_error true_values predicted_values 0 028 true true_labels np array 1 0 2 0 3 0 4 0 5 0 predicted_probs np array 0 3 0 8 0 9 0 2 mean_squared_error true_labels predicted_probs traceback most recent call last valueerror input arrays must have the same length calculates the mean absolute error mae between ground truth observed and predicted values mae measures the absolute difference between true values and predicted values equation mae 1 n σ abs y_true y_pred reference https en wikipedia org wiki mean_absolute_error parameters y_true the true values ground truth y_pred the predicted values true_values np array 1 0 2 0 3 0 4 0 5 0 predicted_values np array 0 8 2 1 2 9 4 2 5 2 np isclose mean_absolute_error true_values predicted_values 0 16 true true_values np array 1 0 2 0 3 0 4 0 5 0 predicted_values np array 0 8 2 1 2 9 4 2 5 2 np isclose mean_absolute_error true_values predicted_values 2 16 false true_labels np array 1 0 2 0 3 0 4 0 5 0 predicted_probs np array 0 3 0 8 0 9 5 2 mean_absolute_error true_labels predicted_probs traceback most recent call last valueerror input arrays must have the same length calculate the mean squared logarithmic error msle between ground truth and predicted values msle measures the squared logarithmic difference between true values and predicted values for regression models it s particularly useful for dealing with skewed or large value data and it s often used when the relative differences between predicted and true values are more important than absolute differences msle 1 n σ log 1 y_true log 1 y_pred 2 reference https insideaiml com blog meansquared logarithmic error loss 1035 parameters y_true the true values ground truth y_pred the predicted values true_values np array 1 0 2 0 3 0 4 0 5 0 predicted_values np array 0 8 2 1 2 9 4 2 5 2 mean_squared_logarithmic_error true_values predicted_values 0 0030860877925181344 true_labels np array 1 0 2 0 3 0 4 0 5 0 predicted_probs np array 0 3 0 8 0 9 0 2 mean_squared_logarithmic_error true_labels predicted_probs traceback most recent call last valueerror input arrays must have the same length calculate the mean absolute percentage error between y_true and y_pred mean absolute percentage error calculates the average of the absolute percentage differences between the predicted and true values formula σ y_true i y_pred i y_true i n source https stephenallwright com good mape score parameters y_true np ndarray numpy array containing true target values y_pred np ndarray numpy array containing predicted values returns float the mean absolute percentage error between y_true and y_pred examples y_true np array 10 20 30 40 y_pred np array 12 18 33 45 mean_absolute_percentage_error y_true y_pred 0 13125 y_true np array 1 2 3 4 y_pred np array 2 3 4 5 mean_absolute_percentage_error y_true y_pred 0 5208333333333333 y_true np array 34 37 44 47 48 48 46 43 32 27 26 24 y_pred np array 37 40 46 44 46 50 45 44 34 30 22 23 mean_absolute_percentage_error y_true y_pred 0 064671076436071 calculate the perplexity for the y_true and y_pred compute the perplexity which useful in predicting language model accuracy in natural language processing nlp perplexity is measure of how certain the model in its predictions perplexity loss exp 1 n σ ln p x reference https en wikipedia org wiki perplexity args y_true actual label encoded sentences of shape batch_size sentence_length y_pred predicted sentences of shape batch_size sentence_length vocab_size epsilon small floating point number to avoid getting inf for log 0 returns perplexity loss between y_true and y_pred y_true np array 1 4 2 3 y_pred np array 0 28 0 19 0 21 0 15 0 15 0 24 0 19 0 09 0 18 0 27 0 03 0 26 0 21 0 18 0 30 0 28 0 10 0 33 0 15 0 12 perplexity_loss y_true y_pred 5 0247347775367945 y_true np array 1 4 2 3 y_pred np array 0 28 0 19 0 21 0 15 0 15 0 24 0 19 0 09 0 18 0 27 0 30 0 10 0 20 0 15 0 25 0 03 0 26 0 21 0 18 0 30 0 28 0 10 0 33 0 15 0 12 0 30 0 10 0 20 0 15 0 25 perplexity_loss y_true y_pred traceback most recent call last valueerror sentence length of y_true and y_pred must be equal y_true np array 1 4 2 11 y_pred np array 0 28 0 19 0 21 0 15 0 15 0 24 0 19 0 09 0 18 0 27 0 03 0 26 0 21 0 18 0 30 0 28 0 10 0 33 0 15 0 12 perplexity_loss y_true y_pred traceback most recent call last valueerror label value must not be greater than vocabulary size y_true np array 1 4 y_pred np array 0 28 0 19 0 21 0 15 0 15 0 24 0 19 0 09 0 18 0 27 0 03 0 26 0 21 0 18 0 30 0 28 0 10 0 33 0 15 0 12 perplexity_loss y_true y_pred traceback most recent call last valueerror batch size of y_true and y_pred must be equal matrix to select prediction value only for true class getting the matrix containing prediction for only true class calculating perplexity for each sentence
import numpy as np


def binary_cross_entropy(
    y_true: np.ndarray, y_pred: np.ndarray, epsilon: float = 1e-15
) -> float:
    
    if len(y_true) != len(y_pred):
        raise ValueError("Input arrays must have the same length.")

    y_pred = np.clip(y_pred, epsilon, 1 - epsilon)  
    bce_loss = -(y_true * np.log(y_pred) + (1 - y_true) * np.log(1 - y_pred))
    return np.mean(bce_loss)


def binary_focal_cross_entropy(
    y_true: np.ndarray,
    y_pred: np.ndarray,
    gamma: float = 2.0,
    alpha: float = 0.25,
    epsilon: float = 1e-15,
) -> float:
    
    if len(y_true) != len(y_pred):
        raise ValueError("Input arrays must have the same length.")
    
    y_pred = np.clip(y_pred, epsilon, 1 - epsilon)

    bcfe_loss = -(
        alpha * (1 - y_pred) ** gamma * y_true * np.log(y_pred)
        + (1 - alpha) * y_pred**gamma * (1 - y_true) * np.log(1 - y_pred)
    )

    return np.mean(bcfe_loss)


def categorical_cross_entropy(
    y_true: np.ndarray, y_pred: np.ndarray, epsilon: float = 1e-15
) -> float:
    
    if y_true.shape != y_pred.shape:
        raise ValueError("Input arrays must have the same shape.")

    if np.any((y_true != 0) & (y_true != 1)) or np.any(y_true.sum(axis=1) != 1):
        raise ValueError("y_true must be one-hot encoded.")

    if not np.all(np.isclose(np.sum(y_pred, axis=1), 1, rtol=epsilon, atol=epsilon)):
        raise ValueError("Predicted probabilities must sum to approximately 1.")

    y_pred = np.clip(y_pred, epsilon, 1)  
    return -np.sum(y_true * np.log(y_pred))


def hinge_loss(y_true: np.ndarray, y_pred: np.ndarray) -> float:
    
    if len(y_true) != len(y_pred):
        raise ValueError("Length of predicted and actual array must be same.")

    if np.any((y_true != -1) & (y_true != 1)):
        raise ValueError("y_true can have values -1 or 1 only.")

    hinge_losses = np.maximum(0, 1.0 - (y_true * y_pred))
    return np.mean(hinge_losses)


def huber_loss(y_true: np.ndarray, y_pred: np.ndarray, delta: float) -> float:
    
    if len(y_true) != len(y_pred):
        raise ValueError("Input arrays must have the same length.")

    huber_mse = 0.5 * (y_true - y_pred) ** 2
    huber_mae = delta * (np.abs(y_true - y_pred) - 0.5 * delta)
    return np.where(np.abs(y_true - y_pred) <= delta, huber_mse, huber_mae).mean()


def mean_squared_error(y_true: np.ndarray, y_pred: np.ndarray) -> float:
    
    if len(y_true) != len(y_pred):
        raise ValueError("Input arrays must have the same length.")

    squared_errors = (y_true - y_pred) ** 2
    return np.mean(squared_errors)


def mean_absolute_error(y_true: np.ndarray, y_pred: np.ndarray) -> float:
    
    if len(y_true) != len(y_pred):
        raise ValueError("Input arrays must have the same length.")

    return np.mean(abs(y_true - y_pred))


def mean_squared_logarithmic_error(y_true: np.ndarray, y_pred: np.ndarray) -> float:
    
    if len(y_true) != len(y_pred):
        raise ValueError("Input arrays must have the same length.")

    squared_logarithmic_errors = (np.log1p(y_true) - np.log1p(y_pred)) ** 2
    return np.mean(squared_logarithmic_errors)


def mean_absolute_percentage_error(
    y_true: np.ndarray, y_pred: np.ndarray, epsilon: float = 1e-15
) -> float:
    
    if len(y_true) != len(y_pred):
        raise ValueError("The length of the two arrays should be the same.")

    y_true = np.where(y_true == 0, epsilon, y_true)
    absolute_percentage_diff = np.abs((y_true - y_pred) / y_true)

    return np.mean(absolute_percentage_diff)


def perplexity_loss(
    y_true: np.ndarray, y_pred: np.ndarray, epsilon: float = 1e-7
) -> float:
    

    vocab_size = y_pred.shape[2]

    if y_true.shape[0] != y_pred.shape[0]:
        raise ValueError("Batch size of y_true and y_pred must be equal.")
    if y_true.shape[1] != y_pred.shape[1]:
        raise ValueError("Sentence length of y_true and y_pred must be equal.")
    if np.max(y_true) > vocab_size:
        raise ValueError("Label value must not be greater than vocabulary size.")

    
    filter_matrix = np.array(
        [[list(np.eye(vocab_size)[word]) for word in sentence] for sentence in y_true]
    )

    
    true_class_pred = np.sum(y_pred * filter_matrix, axis=2).clip(epsilon, 1)

    
    perp_losses = np.exp(np.negative(np.mean(np.log(true_class_pred), axis=1)))

    return np.mean(perp_losses)


if __name__ == "__main__":
    import doctest

    doctest.testmod()