Skip to content

Commit

Permalink
Merge pull request rasbt#140 from rasbt/tf2.3.1-fix
Browse files Browse the repository at this point in the history
Fix tf version dimension issue
rasbt authored Nov 26, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents dcd0707 + 76cce9a commit 0460d56
Showing 2 changed files with 31 additions and 11 deletions.
23 changes: 17 additions & 6 deletions ch15/ch15_part1.ipynb
Original file line number Diff line number Diff line change
@@ -697,18 +697,29 @@
}
],
"source": [
"from distutils.version import LooseVersion as Version\n",
"\n",
"\n",
"####### Binary Crossentropy\n",
"bce_probas = tf.keras.losses.BinaryCrossentropy(from_logits=False)\n",
"bce_logits = tf.keras.losses.BinaryCrossentropy(from_logits=True)\n",
"\n",
"logits = tf.constant([0.8])\n",
"probas = tf.keras.activations.sigmoid(logits)\n",
"\n",
"tf.print(\n",
" 'BCE (w Probas): {:.4f}'.format(\n",
" bce_probas(y_true=[1], y_pred=probas)),\n",
" '(w Logits): {:.4f}'.format(\n",
" bce_logits(y_true=[1], y_pred=logits)))\n",
"if Version(tf.__version__) >= '2.3.0':\n",
" tf.print(\n",
" 'CCE (w Probas): {:.4f}'.format(\n",
" cce_probas(y_true=[[0, 0, 1]], y_pred=probas)),\n",
" '(w Logits): {:.4f}'.format(\n",
" cce_logits(y_true=[[0, 0, 1]], y_pred=logits)))\n",
" \n",
"else:\n",
" tf.print(\n",
" 'CCE (w Probas): {:.4f}'.format(\n",
" cce_probas(y_true=[0, 0, 1], y_pred=probas)),\n",
" '(w Logits): {:.4f}'.format(\n",
" cce_logits(y_true=[0, 0, 1], y_pred=logits)))\n",
"\n",
"\n",
"####### Categorical Crossentropy\n",
@@ -1260,7 +1271,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.1"
"version": "3.8.2"
}
},
"nbformat": 4,
19 changes: 14 additions & 5 deletions ch15/ch15_part1.py
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@
import pandas as pd
import matplotlib.pyplot as plt
import os
from distutils.version import LooseVersion as Version

# *Python Machine Learning 3rd Edition* by [Sebastian Raschka](https://sebastianraschka.com) & [Vahid Mirjalili](http://vahidmirjalili.com), Packt Publishing Ltd. 2019
#
@@ -271,11 +272,19 @@ def conv2d(X, W, p=(0, 0), s=(1, 1)):
logits = tf.constant([[1.5, 0.8, 2.1]])
probas = tf.keras.activations.softmax(logits)

tf.print(
'CCE (w Probas): {:.4f}'.format(
cce_probas(y_true=[0, 0, 1], y_pred=probas)),
'(w Logits): {:.4f}'.format(
cce_logits(y_true=[0, 0, 1], y_pred=logits)))
if Version(tf.__version__) >= '2.3.0':
tf.print(
'CCE (w Probas): {:.4f}'.format(
cce_probas(y_true=[[0, 0, 1]], y_pred=probas)),
'(w Logits): {:.4f}'.format(
cce_logits(y_true=[[0, 0, 1]], y_pred=logits)))

else:
tf.print(
'CCE (w Probas): {:.4f}'.format(
cce_probas(y_true=[0, 0, 1], y_pred=probas)),
'(w Logits): {:.4f}'.format(
cce_logits(y_true=[0, 0, 1], y_pred=logits)))

####### Sparse Categorical Crossentropy
sp_cce_probas = tf.keras.losses.SparseCategoricalCrossentropy(

0 comments on commit 0460d56

Please sign in to comment.