惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园_首页
Engineering at Meta
Engineering at Meta
量子位
A
About on SuperTechFans
阮一峰的网络日志
阮一峰的网络日志
Recent Announcements
Recent Announcements
博客园 - 司徒正美
V
Visual Studio Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The GitHub Blog
The GitHub Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
F
Fortinet All Blogs
Martin Fowler
Martin Fowler
腾讯CDC
Jina AI
Jina AI
C
Check Point Blog
H
Help Net Security
罗磊的独立博客
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
V2EX
爱范儿
爱范儿
I
InfoQ

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
Some Loss Functions and Their Intuitive Explanations
2017-03-13 · via jdhao's digital space

Loss functions are frequently used in supervised machine learning to minimize the differences between the predicted output of the model and the ground truth labels. In other words, it is used to measure how good our model can predict the true class of a sample from the dataset. Here I would like to list some frequently-used loss functions and give my intuitive explanation.

Cross Entropy Loss

Cross Entropy Loss is usually used in classification problems. In essence, it is a measure of difference between the desired probablity distribution and the predicted probablity distribution. Suppose the classification is binary classification problem, the label are \({0, 1}\). Then the loss function for a single sample in the dataset is expressed as:

\[-y \log(p)-(1-y) \log(1-p)\ ,\]

where \(y\) is the label of the sample, and \(p\) is the predicted probability of the sample belonging to class 1.

For \(K\)-class (\(K >2\) ) classification problems, the predicted probablity output for a single sample is a vector of length \(K\): \([p_0, p_1, \ldots, p_{K-1}]\). The Cross Entropy Loss is extended as: \[-log (p_k)\ ,\] where \(k\) is the ground truth label for the sample. If \(p_k\) equals 1, then there is no loss incurred for that sample.

We often see the term “Softmax Loss” in literature or blog post. In fact, there is no such thing as “Softmax Loss”, as is discussed on this Quora post. Suppose the original prediction for a sample is \([x_0, x_1, \ldots, x_{K-1}]\), we can get the normalized probablity \([p_0, p_1, \ldots, p_{K-1}]\), where we have:

\[p_k = \frac{\exp(x_k)}{\sum_{i=0}^{K-1}\exp(x_i)}\ .\]

So the Cross Entropy Loss really is:

\[-\log \frac{\exp(x_k)}{\sum_{i=0}^{K-1}\exp(x_i)}\ .\]

In brief, the so-called Softmax Loss is just Softmax function followed by Cross Entropy Loss.

Contrastive Loss

Contrastive Loss is often used in image retrieval tasks to learn discriminative features for images. During training, an image pair is fed into the model with their ground truth relationship \(y\): \(y\) equals 1 if the two images are similar and 0 otherwise. The loss function for a single pair is:

\[yd^2+(1-y)\max (margin-d, 0)^2\ ,\]

where \(d\) is the Euclidean distance between the two image features (suppose their features are \(f_1\) and \(f_2\)): \(d = \Vert f_1 - f_2\Vert_2\). The \(margin\) term is used to “tighten” the constraint: if two images in a pair are dissimilar, then their distance shoud be at least \(margin\), or a loss will be incurred.

Triplet Loss

Triplet Loss is another loss commonly used in CNN-based image retrieval. During training process, an image triplet \((I_a, I_n, I_p)\) is fed into the model as a single sample, where \(I_a\), \(I_n\) and \(I_p\) represent the anchor, postive and negative images respectively. The idea behind is that distance between anchor and positive images should be smaller than that between anchor and negative images. The formal definition is:

\[\max \left( {\Vert f_a- f_p \Vert}^2 - {\Vert f_a - f_n \Vert}^2 + m, 0\right)\ .\]

In the above equation, \(m\) is a margin term used to “stretch” the distance differences between similar and dissimilar pair in the triplet, \(f_a, f_p, f_n\) are the feature embeddings for the anchor, postive and negative images.


References