<jupyter_start><jupyter_text>Losses> Basic losses used in keratorch<jupyter_code># default_exp losses
# export
import torch
import torch.nn as nn
import torch.nn.functional as F
# hide
import matplotlib.pyplot as plt
import torch
torch.Tensor.ndim = property(lambda x: x.dim())

%matplotlib inline<jupyter_output><empty_output><jupyter_text>MSE loss<jupyter_code># export
mse_loss = mean_squared_loss = mse = F.mse_loss
e = torch.arange(-3, 3, 0.1)
plt.plot(e, [mse_loss(e_, torch.zeros_like(e_)) for e_ in e]);<jupyter_output><empty_output><jupyter_text>MAE Loss<jupyter_code># export
mae_loss = mean_absolute_error = mae = F.l1_loss
e = torch.arange(-3, 3, 0.1)
plt.plot(e, [mae(e_, torch.zeros_like(e_)) for e_ in e]);<jupyter_output><empty_output><jupyter_text>Binary Crossentropy Loss<jupyter_code># export
binary_cross_entropy = bce_loss = bce = F.binary_cross_entropy
e = torch.arange(-3, 3, 0.1)
print('Red dot shows the loss when probability is zero and the true target is zero.')
plt.plot(torch.sigmoid(e), [bce(torch.sigmoid(e_), torch.zeros_like(e_)) for e_ in e])
plt.scatter(0, 0, c='r')
plt.xlabel('calculated probability')
plt.ylabel('loss')
plt.show()<jupyter_output>Red dot shows the loss when probability is zero and the true target is zero.<jupyter_text>Cross Entropy LossCross entropy loss for multi class problems.<jupyter_code># export
ce_loss = F.cross_entropy<jupyter_output><empty_output><jupyter_text>Cross Entropy for Softmax<jupyter_code># export
def ce4softmax(input, target):
    eps = 1e-9
    logp = torch.log(input + eps)
    return ce_loss(logp, target)
# hide
from nbdev.export import *
notebook2script()<jupyter_output>Converted Activations.ipynb.
Converted Layers.ipynb.
Converted Model.ipynb.
Converted data.ipynb.
Converted index.ipynb.
Converted losses.ipynb.
{'fraction_non_alphanumeric': 0.11666666666666667, 'fraction_numerical': 0.010555555555555556, 'mean_word_length': 6.321138211382114, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '413938', 'license': 'Apache-2.0', 'n_tokens_mistral': 717, 'n_tokens_neox': 699, 'n_words': 162, 'path': 'losses.ipynb', 'repo_name': 'sachinruk/keraTorch', 'size': '1800'}