Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nwatab/9e8c492fc7f01a9f37f9d5fb33ee3406 to your computer and use it in GitHub Desktop.
Save nwatab/9e8c492fc7f01a9f37f9d5fb33ee3406 to your computer and use it in GitHub Desktop.
Tensorflow 2.4.1 has an issue in metrics calculation
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Tensorflow 2.4.1 has an issue in metrics calculation",
"provenance": [],
"collapsed_sections": [],
"toc_visible": true,
"include_colab_link": true
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/asterisk37n/9e8c492fc7f01a9f37f9d5fb33ee3406/tensorflow-2-4-1-has-an-issue-in-metrics-calculation.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "EZK_OjvBY4Pj",
"outputId": "2927dfc1-a559-44c2-aa7d-9018cf6e0ae9"
},
"source": [
"import tensorflow as tf\n",
"import numpy as np\n",
"import random\n",
"import os\n",
"\n",
"print(tf.__version__)\n",
"\n",
"tf.random.set_seed(33)\n",
"os.environ['PYTHONHASHSEED'] = str(33)\n",
"np.random.seed(33)\n",
"random.seed(33)\n",
"\n",
"model = tf.keras.models.Sequential(\n",
" tf.keras.layers.Dense(1, input_shape=(1,))\n",
")\n",
"def my_metric_fn(y_true, y_pred):\n",
" squared_difference = tf.square(y_true - y_pred)\n",
" loss = tf.reduce_mean(squared_difference, axis=-1)\n",
" tf.print(y_true.shape, y_pred.shape, loss, tf.reduce_mean(squared_difference))\n",
" return loss\n",
"model.compile(optimizer='adam', loss='mean_squared_error', metrics=[my_metric_fn])\n",
"x = np.random.rand(4,1)\n",
"y = x ** 2\n",
"history = model.fit(x=x, y=y, batch_size=2, epochs=1)\n",
"print(history.history)"
],
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"text": [
"2.4.1\n",
"TensorShape([2, 1]) TensorShape([2, 1]) [0.216433451 0.167138502] 0.191785976\n",
"1/2 [==============>...............] - ETA: 0s - loss: 0.1918 - my_metric_fn: 0.1918TensorShape([2, 1]) TensorShape([2, 1]) [0.0477369316 0.0422783121] 0.0450076237\n",
"2/2 [==============================] - 0s 12ms/step - loss: 0.1429 - my_metric_fn: 0.1429\n",
"{'loss': [0.1183968037366867], 'my_metric_fn': [0.1183968037366867]}\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ky-fLxMOZbV7",
"outputId": "facde67f-525d-4138-9204-1b263ae72ac8"
},
"source": [
"(0.191785976 + 0.0450076237)/2 # Where has 0.1429 come from?"
],
"execution_count": 2,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"0.11839679985"
]
},
"metadata": {
"tags": []
},
"execution_count": 2
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment