generated from slds-lmu/lecture_template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
separating cross-entropy and source coding
- Loading branch information
1 parent
4c98a45
commit 94f7988
Showing
5 changed files
with
98 additions
and
67 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
|
||
# Binary Cross-Entropy Loss function for true value y and predicted probability p | ||
def binary_cross_entropy(y, p): | ||
return -(y * np.log(p) + (1 - y) * np.log(1 - p)) | ||
|
||
# Predicted probabilities | ||
p = np.linspace(0.01, 0.99, 100) # Avoiding the extreme values 0 and 1 for numerical stability | ||
|
||
# Calculate the loss for true values 0 and 1 | ||
loss_for_1 = binary_cross_entropy(1, p) | ||
loss_for_0 = binary_cross_entropy(0, p) | ||
|
||
# Plotting | ||
plt.figure(figsize=(10, 6)) | ||
plt.plot(p, loss_for_1, label='True value: 1') | ||
plt.plot(p, loss_for_0, label='True value: 0', color='orange') | ||
plt.title('Binary Cross-Entropy Loss') | ||
plt.xlabel('p') | ||
plt.ylabel('Binary Cross-Entropy Loss') | ||
plt.legend() | ||
plt.grid(True) | ||
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters