Softmax Function
• Cross Entropy loss comes in hand with hand with the Softmax layer.
• The Softmax layer turns all the class probabilities to values that sum up to 1.
𝑒 𝑧𝑗 K = Number of classes
𝜎(𝑧)𝑗 = 𝐾 Do this for j = 1…..K (for all output neurons)
σ𝑘=1 𝑒 𝑧𝑘
Final Layer
Outputs 𝐾
𝑒 𝑧𝑘 = 0.058 + 2.36 + 1.32 = 3.73
𝑘=1
+ =1 𝑒 −2.85 = 0.058
0.058
3.73
= 0.016
2.36
𝑒 0.86 = 2.36 = 0.631
3.73
[Link] 0.016 + 0.631 + 0.353 = 1
𝑒 0.28 = 1.32 1.32
= 0.353
3.73
Softmax Function
maximum
[Link]
Softmax from Scratch in Python
From Scratch
Using PyTorch to Verify
0.3479
print(tens[0]/[Link]())
Softmax in Python
list = []
for pred in predictions:
[Link]([Link](pred))
sum = sum(list)
outputs = []
for value in list:
[Link](value/sum)
lower temp (< 1) more confident (sharper)
Temperature Softmax higher temp (> 1) less confident
• Sometimes, we may want our distribution to be more or less confident
• We can control this by dividing the logits (pre-softmax) by a temperature
temp = 1 temp = 0.5 temp = 0.1
1 1 1
0.8
0.15
0.05
Dog Cat Horse Dog Cat Horse Dog Cat Horse
temp = 5 temp = 10 temp = 100
1 1 1
uniform
Dog Cat Horse Dog Cat Horse Dog Cat Horse