Materials · Practice · ML / AI

Cross-entropy in Excel: BCE and CE step by step

A walk through a workbook: logit → sigmoid or softmax → loss formula. What every sheet and every column shows, why logits must not go into the formula directly, and a practice sheet that checks itself. The workbook can be downloaded.

Javharbek Abdulatipov · 35 min read
4 sheets2 charts3 tasks
$$\begin{gathered}\mathrm{BCE}=-\frac1n\sum_{i=1}^{n}\bigl[y_i\ln\hat y_i+(1-y_i)\ln(1-\hat y_i)\bigr]\\[6pt]\mathrm{CE}=-\sum_{k=1}^{K}y_k\ln\hat y_k\end{gathered}$$

What is in the workbook

XLSX
The workbook

4 sheets: BCE, Categorical CE, Practice, Answers. Yellow cells are inputs you can change; everything else recalculates by itself. Opens in Excel, LibreOffice Calc and Google Sheets (File → Import).

The workbook teaches one chain that stands behind the output of any classifier: the network produces a logit, any number from $-\infty$ to $+\infty$; a “translator” (sigmoid for two classes, softmax for $K$ classes) turns it into a probability; and only the probability goes into the loss formula. Each sheet walks this chain from left to right and shows what happens if the middle step is skipped.

SheetWhat is insideWhat it teaches
BCE4 blocks: the computation for 5 objects; the same logits without sigmoid; a table and a chart of the two penalty branches; how sigmoid squeezes a logitbinary cross-entropy, NaN from logits, the role of sigmoid
Categorical CE3 blocks: the computation for 3 classes; logits without softmax; a table and a chart of $-\ln x$categorical cross-entropy, a negative loss with no floor, the role of softmax
Practice3 tasks: BCE for three objects, CE after softmax, softmax by handcompute it yourself; a ✓ or ✗ appears next to the answer (tolerance ±0.005)
Answersreference answers to all tasks, as formulascheck your working

The cell colours are the same everywhere:

  • yellow: inputs, the labels $y$ and the logits $z$; you can change them
  • light blue: the sigmoid or softmax “translator”, a separate step that is not part of the loss formula
  • red: the loss formula itself; the lighter, the deeper inside the brackets
  • green and red zones in blocks 3: where the logarithms exist and where they do not
How to read this walk-through

The sections follow the sheets and blocks of the workbook. Every number on the page is computed from the same inputs as in the file and matches it to three decimals. Under each sheet there is “The workbook in the browser”: the same computation that you can change right here. The logarithm is natural everywhere: $\ln$.

The BCE sheet: the computation step by step

Block 1 of the sheet holds five objects of a binary task: the label $y=1$ (“yes”) or $y=0$ (“no”) and the logit $z$ produced by the network. The computation has three steps, like the columns of the table: input → sigmoid → the BCE formula.

$$\hat y_i=\sigma(z_i)=\frac{1}{1+e^{-z_i}},\qquad \mathrm{BCE}=-\frac1n\sum_{i=1}^{n}\bigl[y_i\ln\hat y_i+(1-y_i)\ln(1-\hat y_i)\bigr]$$
$y_i$
the correct answer for object $i$: 1 or 0
$z_i$
the logit, the raw output of the network, any number
$\hat y_i=\sigma(z_i)$
the predicted probability of “yes”
$n$
the number of objects, here $n=5$
$i$$y$logit $z$$\hat y=\sigma(z)$$y\ln\hat y$$(1-y)\ln(1-\hat y)$sum of the two
112.200.900−0.1050.000−0.105
20−1.400.1980.000−0.220−0.220
310.400.599−0.5130.000−0.513
40−0.600.3540.000−0.437−0.437
511.400.802−0.2200.000−0.220
Σ over all $i$−1.496
$n$5
BCE = −Σ / n0.299
Every row, step by step

Step 2 (the light blue column): $\hat y=1/(1+e^{-z})$. Step 3 (the red columns): two terms and their sum. In each row one term is zeroed by the factor $y$ or $1-y$.

Row 1

$$\begin{gathered}\hat y_1 = \frac{1}{1+e^{-2.20}} = \frac{1}{1+0.1108} = 0.900\\y_1\ln\hat y_1 = 1\cdot\ln 0.900 = -0.105\\(1-y_1)\ln(1-\hat y_1) = 0\cdot\ln 0.100 = 0\end{gathered}$$

Row 2

$$\begin{gathered}\hat y_2 = \frac{1}{1+e^{-(-1.40)}} = \frac{1}{1+4.0552} = 0.198\\y_2\ln\hat y_2 = 0\cdot\ln 0.198 = 0\\(1-y_2)\ln(1-\hat y_2) = 1\cdot\ln 0.802 = -0.220\end{gathered}$$

Row 3

$$\begin{gathered}\hat y_3 = \frac{1}{1+e^{-0.40}} = \frac{1}{1+0.6703} = 0.599\\y_3\ln\hat y_3 = 1\cdot\ln 0.599 = -0.513\\(1-y_3)\ln(1-\hat y_3) = 0\cdot\ln 0.401 = 0\end{gathered}$$

Row 4

$$\begin{gathered}\hat y_4 = \frac{1}{1+e^{-(-0.60)}} = \frac{1}{1+1.8221} = 0.354\\y_4\ln\hat y_4 = 0\cdot\ln 0.354 = 0\\(1-y_4)\ln(1-\hat y_4) = 1\cdot\ln 0.646 = -0.437\end{gathered}$$

Row 5

$$\begin{gathered}\hat y_5 = \frac{1}{1+e^{-1.40}} = \frac{1}{1+0.2466} = 0.802\\y_5\ln\hat y_5 = 1\cdot\ln 0.802 = -0.220\\(1-y_5)\ln(1-\hat y_5) = 0\cdot\ln 0.198 = 0\end{gathered}$$

The sum over all rows is $\Sigma = -1.496$ and there are $n=5$ objects, so $\mathrm{BCE}=-\Sigma/n = -(-1.496)/5 = 0.299$. The last grey row of the sheet computes the same thing another way, with SUMPRODUCT over both branches, and also gives $0.299$.

Only one term is alive in each row

For $y=1$, $\ln\hat y$ remains; for $y=0$, $\ln(1-\hat y)$. So the penalty of a row is always $-\ln$(the probability the model gave to the right answer). In block 1 these probabilities are $0.900$, $0.802$, $0.599$, $0.646$ and $0.802$. Rows 2 and 5 have the same penalty, $-0.220$: the logits $-1.4$ and $1.4$ are symmetric, and $\sigma(-z)=1-\sigma(z)$.

Try it (as the sheet suggests)

  • Logit of row 1: $2.2 \to 6$. Then $\hat y_1 = 0.998$, the row penalty is $0.002$, almost zero, and BCE falls from $0.299$ to $0.279$.
  • Logit of row 1: $2.2 \to -3$ with $y=1$. Then $\hat y_1 = 0.047$ and the row penalty is $3.049$: more than the other four rows together ($1.391$). BCE grows to $0.888$: one confident mistake outweighs everything else.

Logits without sigmoid

Block 2 takes the same labels and logits and feeds the logits into the BCE formula directly, as if $z$ were already a probability. BCE has two branches, $\ln z$ and $\ln(1-z)$, and both must exist in the computation, even the one that is later multiplied by zero.

$i$$y$$z$$\ln z$$\ln(1-z)$$y\ln z+(1-y)\ln(1-z)$
112.20.788noneNaN
20−1.4none0.875NaN
310.4−0.916−0.511−0.916 (in $(0, 1)$ by chance)
40−0.6none0.470NaN
511.40.336noneNaN

What came out, row by row:

  • Row 1, $z=2.2$: $\ln 2.2 \approx 0.788 > 0$ is a “penalty” with the opposite sign, and $\ln(1-2.2)=\ln(-1.2)$ does not exist.
  • Rows 2 and 4, $z<0$: $\ln z$ does not exist; row 5, $z=1.4$: $\ln(1-z)$ does not exist.
  • Only row 3 ($z=0.4$) landed in $(0, 1)$ “by chance” and gave a number, $\ln 0.4\approx-0.916$. Even that is meaningless: $0.4$ is a logit, not a probability; the real probability here is $\sigma(0.4)=0.599$.
Why a zero factor does not help

In row 1 the second branch is multiplied by $1-y=0$, so it seems it need not be computed. But the computer evaluates the logarithm first and multiplies afterwards: in floating point $\ln(-1.2)=\mathrm{NaN}$, $\ln 0 = -\infty$, while $0\cdot\mathrm{NaN}=\mathrm{NaN}$ and $0\cdot(-\infty)=\mathrm{NaN}$. In Excel the same thing shows up as a #NUM! error, so the sheet catches it with IFERROR and writes “NaN”.

One such row is enough: a mean with a NaN is NaN, the gradients are NaN, and after the optimizer step the network weights are NaN too. Training is not “bad”, it is dead. The result of the block: BCE without sigmoid is NaN, BCE with sigmoid is 0.299.

Sigmoid solves both problems at once: $\hat y\in(0, 1)$ and $1-\hat y\in(0, 1)$, so both logarithms always exist and both are $\le 0$, which makes every penalty $\ge 0$.

The workbook in the browser

The same computation as in the sheet: change the yellow cells and the rest recalculates.

Two branches and two walls

Block 3 explains why: the table plugs different $x$ into both BCE branches, both probabilities and “logit” values outside $(0, 1)$. The yellow $x$ in the sheet can be changed, and the chart under the table (the sheet draws it for $\hat y$ from $0.02$ to $0.98$) shows both branches in full.

$x$for $y=1$: $-\ln x$for $y=0$: $-\ln(1-x)$zone
−1none−0.693logit
0none0.000logit
0.014.6050.010probability
0.12.3030.105probability
0.50.6930.693probability
0.90.1052.303probability
0.990.0104.605probability
10.000nonelogit
2−0.693nonelogit
10−2.303nonelogit

BCE has two “walls”. The $y=1$ branch, the penalty $-\ln x$, goes to infinity near zero: a confident “no” when the right answer is “yes”. The $y=0$ branch, the penalty $-\ln(1-x)$, goes to infinity near one. Between the walls, at $x=0.5$, both branches give the same penalty $\ln 2 \approx 0.693$: the price of complete uncertainty.

Outside $(0, 1)$ everything breaks: for $x\le0$ there is no $\ln x$, for $x\ge1$ there is no $\ln(1-x)$, for $x>1$ the $y=1$ branch gives a negative penalty (at $x=10$ it is $-\ln 10\approx-2.303$ and it keeps falling with no floor), and for $x<0$ the $y=0$ branch breaks the same way: at $x=-1$ its penalty is $-\ln 2\approx-0.693$. A network that is allowed a negative loss will simply inflate the logit.

Drag the point: the green band holds the probabilities where sigmoid lands; the red bands are the area of logits. The switch chooses which branch counts as the penalty.

How sigmoid squeezes a logit

Block 4 shows the “translator” itself. The columns of the sheet are the intermediate steps of $\hat y = 1/(1+e^{-z})$: first $e^{-z}$, then $\hat y$, then $1-\hat y$.

$z$$e^{-z}$$\hat y=\dfrac{1}{1+e^{-z}}$$1-\hat y$
−1022026.46580.00001.0000
−320.08550.04740.9526
−12.71830.26890.7311
01.00000.50000.5000
10.36790.73110.2689
30.04980.95260.0474
100.00001.00000.0000

Why the result is always in $(0, 1)$: $e^{-z}>0$ for any $z$, so the denominator $1+e^{-z}>1$ and $0<\hat y<1$. At $z=0$, exactly $\hat y=0.5$: complete uncertainty; large positive $z$ give a confident “yes”, large negative ones a confident “no”. The table is symmetric: $\sigma(-z)=1-\sigma(z)$, so the rows $z$ and $-z$ swap the columns $\hat y$ and $1-\hat y$.

Move the logit: the curve is $\sigma(z)$; below are the intermediate values, as in the columns of the sheet.
Deeper: “−1000 or +1000” holds in mathematics, not in a computer

The sheet says that for any logit $\hat y$ stays strictly between 0 and 1. Mathematically, it does. But in floating point, for $z \gtrsim 36.74$ (float64; in float32 already for $z \gtrsim 16.64$) the term $e^{-z}$ is lost next to one, $\hat y$ rounds to exactly $1$ and $1-\hat y$ to exactly $0$. With $y=0$ the penalty $-\ln(1-\hat y)$ becomes $+\infty$.

On the other side, $e^{-z}$ overflows for $z<-709.78$: in the sheet itself, entering $z=-1000$ gives a #NUM! error across the row, while $z=1000$ gives exactly $\hat y=1$ and $1-\hat y=0$.

That is why libraries do not compute sigmoid and the logarithm separately. They compute the penalty straight from the logit with the stable form $\max(z,0)-yz+\ln\bigl(1+e^{-|z|}\bigr)$; this is what nn.BCEWithLogitsLoss does. It is exactly the same number as $-[y\ln\sigma(z)+(1-y)\ln(1-\sigma(z))]$, without overflows.

Summary of the BCE sheet

  • BCE is cross-entropy for two classes written through a single number $\hat y$: the probability of “yes” is $\hat y$, the probability of “no” is $1-\hat y$.
  • The formula knows only about $\hat y\in(0, 1)$. Logits live on the whole line: fed in directly, they give NaN or a penalty with the opposite sign.
  • Sigmoid squeezes any logit into $(0, 1)$. Softmax for two classes gives exactly a sigmoid: $\mathrm{softmax}(z_1,z_2)_1=\sigma(z_1-z_2)$; it is one and the same idea.
  • Dividing by $n$ is only there so that the loss does not depend on the batch size; the shape of the penalty comes from $-\ln$.
  • In code: nn.BCEWithLogitsLoss expects logits (applies sigmoid itself, and more stably), nn.BCELoss expects probabilities.
Pythonimport numpy as np

# labels and logits from the BCE sheet
y = np.array([1, 0, 1, 0, 1])
z = np.array([2.2, -1.4, 0.4, -0.6, 1.4])

# step 2: sigmoid; step 3: the BCE formula
p = 1 / (1 + np.exp(-z))
bce = -np.mean(y * np.log(p) + (1 - y) * np.log(1 - p))
print(round(bce, 3))       # 0.299

# the same straight from logits, stably (as nn.BCEWithLogitsLoss does)
s = np.maximum(z, 0) - z * y + np.log1p(np.exp(-np.abs(z)))
print(round(s.mean(), 3))  # 0.299

# PyTorch:
# nn.BCEWithLogitsLoss() expects logits
# nn.BCELoss() expects probabilities

The Categorical CE sheet: the computation step by step

The second sheet is the same chain for $K=3$ classes. The input is a one-hot label vector ($y_k=1$ only for the correct class, here $k=2$) and three logits. The translator is softmax: it turns logits into probabilities that sum to 1.

$$\hat y_k=\mathrm{softmax}(z)_k=\frac{e^{z_k}}{\sum_{j=1}^{K}e^{z_j}},\qquad \mathrm{CE}=-\sum_{k=1}^{K}y_k\ln\hat y_k$$
$z_k$
the logit of class $k$
$\hat y_k=\dfrac{e^{z_k}}{\sum_j e^{z_j}}$
softmax: the probability of class $k$
$y_k$
1 for the correct class, 0 for the others
$K$
the number of classes, here $K=3$
$k$$y$logit $z$$\hat y=\mathrm{softmax}(z)$$\ln\hat y$$y\ln\hat y$
1010.231−1.4640.000
2120.629−0.464−0.464
300.50.140−1.9640.000
$\Sigma\hat y$1.000Σ−0.464
CE = −Σ0.464

How the numbers of the light blue column and the $\ln\hat y$ column come about:

1. The exponentials of the logits and their sum:

$$\begin{gathered}e^{1}=2.718\\e^{2}=7.389\\e^{0.5}=1.649\\\sum_j e^{z_j} = 11.756\end{gathered}$$

2. Divide each exponential by the sum; that is softmax. The sheet’s check $\Sigma\hat y = 1$ always holds, whatever the logits:

$$\begin{gathered}\hat y_1=\frac{2.718}{11.756}=0.231\\\hat y_2=\frac{7.389}{11.756}=0.629\\\hat y_3=\frac{1.649}{11.756}=0.140\end{gathered}$$

3. The logarithms of the probabilities. Note that $\ln\hat y_k = z_k - \ln\sum_j e^{z_j}$ and $\ln 11.756 = 2.464$. So all three logarithms are the logits minus one and the same number:

$$\begin{gathered}\ln\hat y_1=1-2.464=-1.464\\\ln\hat y_2=2-2.464=-0.464\\\ln\hat y_3=0.5-2.464=-1.964\end{gathered}$$

4. In the $y\ln\hat y$ column only one term survives: the one with $y=1$. So CE is $-\ln$ of the probability of the correct class (which is what the grey row of the sheet checks):

$$\mathrm{CE} = -(0\cdot\ln\hat y_1 + 1\cdot\ln\hat y_2 + 0\cdot\ln\hat y_3) = -\ln 0.629 = 0.464$$

This also shows that CE depends only on how far the logit of the correct class stands out from the rest: $\mathrm{CE}=\ln\sum_j e^{z_j}-z_{\text{correct}}$.

Try it (as the sheet suggests)

  • Logit of class 2: $2 \to 5$. Then $\hat y_2=0.971$ and $\mathrm{CE}=0.029$, almost zero.
  • Logit of class 2: $2 \to -2$. Then $\hat y_2=0.030$ and $\mathrm{CE}=3.505$, a large penalty. In both cases the sum of $\hat y$ stays equal to 1.

Logits without softmax

Block 2 again feeds logits in directly, now into $\mathrm{CE}=-\sum_k y_k\ln z_k$. Here all three logits happen to be positive, so the logarithms exist, but the result is worse than an error: CE without softmax $= -\ln 2 \approx -0.693$, a negative loss, whereas with softmax CE $= 0.464$.

$k$$y$$z$$\ln z$$y\ln z$
1010.0000.000
2120.6930.693
300.5−0.6930.000
CE without softmax−0.693

The cross-entropy of real probability distributions is never negative: $\mathrm{CE}(y,\hat y)=H(y)+\mathrm{KL}(y\,\|\,\hat y)\ge 0$, and for a one-hot label it is simply $-\ln$ of a probability that is at most 1. A negative number is a sure sign that something other than probabilities went into the formula.

  • Logit of the correct class $\to 100$: without softmax the loss is $-\ln 100 \approx -4.605$, at $1000$ it is $-6.908$, and so on with no floor. The network will learn only to inflate this logit. With softmax the loss is bounded below by 0.
  • Logit of the correct class $\to -1$: without softmax, an error, since a negative number has no logarithm. With softmax everything is computed; the loss is just large.

The workbook in the browser

The same computation as in the sheet: change the yellow cells and the rest recalculates.

Why only $(0, 1]$

Block 3 of the second sheet is the same “map” as in BCE, but with a single branch $-\ln x$: CE penalizes only the probability of the correct class. The green zone is $(0, 1]$: from a huge penalty near zero to zero at $x=1$. The red zones are logits: for $x\le 0$ there is no logarithm, for $x>1$ the penalty is negative and falls with no floor.

$x$$-\ln x$zone
−1nonelogit
0nonelogit
0.014.605probability
0.12.303probability
0.50.693probability
0.90.105probability
10.000probability
2−0.693logit
10−2.303logit
1000−6.908logit
Drag the point: the curve $-\ln x$ and its zones. Unlike BCE, there is only one wall, at zero.

Summary of the Categorical CE sheet

  • The CE formula knows only about probabilities: numbers from 0 to 1 that sum to 1.
  • Logits are any numbers from $-\infty$ to $+\infty$. Fed into CE directly, they give either an error or a negative loss with no floor.
  • Softmax is a translator: it squeezes any logits into $(0, 1)$ with a sum of 1. That is why CE and softmax always come as a pair.
  • In code, check who applies softmax: nn.CrossEntropyLoss expects logits (softmax inside), nn.NLLLoss expects log-probabilities (log_softmax).
Pythonimport numpy as np

# logits from the Categorical CE sheet; correct class k = 2 (index 1)
z = np.array([1.0, 2.0, 0.5])
k = 1

lse = np.log(np.sum(np.exp(z)))   # ln Σ e^z
p = np.exp(z - lse)               # step 2: softmax
print(np.round(p, 3), round(p.sum(), 3))
# [0.231 0.629 0.14 ] 1.0
print(round(-np.log(p[k]), 3))    # 0.464
print(round(lse - z[k], 3))       # 0.464: CE = ln Σ e^z − z_k

# PyTorch:
# nn.CrossEntropyLoss() expects logits (softmax inside)
# nn.NLLLoss() expects log-probabilities (log_softmax)
Deeper: why BCE is CE for two classes

Take two classes with logits $z_1, z_2$. Softmax gives $\hat y_1=\dfrac{e^{z_1}}{e^{z_1}+e^{z_2}}=\dfrac{1}{1+e^{-(z_1-z_2)}}=\sigma(z_1-z_2)$ and $\hat y_2=1-\hat y_1$.

CE with a one-hot label $(y,\,1-y)$ equals $-[y\ln\hat y_1+(1-y)\ln(1-\hat y_1)]$, exactly the BCE term. So a binary classifier with a single logit $z$ is softmax over the pair $(z, 0)$, and BCE is a special case of CE.

Self-checking practice

The Practice sheet has three calculator tasks: enter the answer in the yellow cell and a ✓ or ✗ appears next to it. The tolerance is $\pm 0.005$; the logarithm is natural. Below are the same tasks right on the page; numbers can be written with a comma or a dot.

To stay within the tolerance, do not round intermediate values to fewer than three decimals. The answer for a term is negative (the logarithm of a probability), while BCE and CE are positive: there is a minus at the end.

Answers and solutions (the Answers sheet)

Task 1. Each term is the $\ln$ of the probability of the right answer: for $y=1$ it is $\ln\hat y$, for $y=0$ it is $\ln(1-\hat y)$.

$$\begin{gathered}i=1:\ 1\cdot\ln 0.8 = -0.223\\i=2:\ 1\cdot\ln(1-0.3) = \ln 0.7 = -0.357\\i=3:\ \ln 0.5 = -0.693\end{gathered}$$
$$\mathrm{BCE} = -\frac{(-0.223) + (-0.357) + (-0.693)}{3} = 0.424$$

Task 2. The probabilities are already after softmax; only the term of the correct class $k=3$ survives.

$$\begin{gathered}k=1:\ 0\cdot\ln 0.1 = 0\\k=2:\ 0\cdot\ln 0.3 = 0\\k=3:\ 1\cdot\ln 0.6 = -0.511\end{gathered}$$
$$\mathrm{CE} = -(-0.511) = 0.511$$

Task 3. Softmax by hand for the logits $z=(0, 1, 2)$, correct class $k=3$.

$$\begin{gathered}e^0=1.000\\e^1=2.718\\e^2=7.389\\\sum = 11.107\end{gathered}$$
$$\hat y = \Bigl(\tfrac{1.000}{11.107},\ \tfrac{2.718}{11.107},\ \tfrac{7.389}{11.107}\Bigr) = (0.090,\ 0.245,\ 0.665)$$
$$\mathrm{CE} = -\ln\hat y_3 = -\ln 0.665 = 0.408$$

Check through the logits: $\mathrm{CE}=\ln\sum_j e^{z_j}-z_3=\ln 11.107-2=0.408$.

Common mistakes

  • Logits in a formula that expects probabilities. nn.BCELoss or nn.NLLLoss on top of the raw network output: this is blocks 2 of both sheets, NaN or a negative loss.
  • A double translator. Sigmoid before nn.BCEWithLogitsLoss or softmax before nn.CrossEntropyLoss: the function applies it a second time. There is no error, but the probabilities get squeezed towards the middle, the loss almost stops changing, and training stalls.
  • The wrong logarithm. The formulas use the natural $\ln$. On a calculator the log key often means $\log_{10}$: $\log_{10}0.8\approx-0.097$ instead of $\ln 0.8\approx-0.223$.
  • A lost minus. The logarithm of a probability is negative, so the “sum of the two” column is negative, while BCE and CE are positive: $\mathrm{BCE}=-\Sigma/n$.
  • The wrong branch. For $y=0$ the penalty is $-\ln(1-\hat y)$, not $-\ln\hat y$: it is the probability of “no” that is penalized.
  • Rounding too early. If you round $e^{z}$ or $\hat y$ to one or two decimals, the answer falls outside the $\pm0.005$ tolerance.

Summary

BCE (2 classes)Categorical CE ($K$ classes)
network outputone logit $z$$K$ logits $z_1,\dots,z_K$
translator$\sigma(z)=1/(1+e^{-z})$$\mathrm{softmax}(z)_k=e^{z_k}/\sum_j e^{z_j}$
penalty per object$-[y\ln\hat y+(1-y)\ln(1-\hat y)]$$-\sum_k y_k\ln\hat y_k=-\ln\hat y_{\text{correct}}$
workbook example$\mathrm{BCE}=0.299$ (5 objects)$\mathrm{CE}=0.464$ (1 object)
without the translatorNaN$-\ln 2\approx-0.693$ and below
PyTorchBCEWithLogitsLoss (logits)CrossEntropyLoss (logits)
  • Logit → translator → formula: the loss formula understands only probabilities.
  • The penalty of an object is always $-\ln$ of the probability of the right answer: $0$ for a confident right answer, $\ln 2\approx0.693$ for complete uncertainty, $+\infty$ for a confident mistake.
  • More on the functions themselves in the cheat sheet: BCE, cross-entropy, sigmoid, softmax.

Check yourself

1. The model gave $\hat y=0.9$ and the right answer is $y=0$. What is the term of the row (the “sum of the two” column)?
2. Why can’t the logit $z=2.2$ go into BCE directly?
3. Logits $(1, 2, 0.5)$, the correct class is the second. What is CE?
4. CE without softmax came out as $-0.693$. What does that tell you?
5. What does nn.CrossEntropyLoss expect as input?
6. What is $\ln\hat y_2-\ln\hat y_1$ for the logits $(1, 2, 0.5)$?

← Materials