{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "alpha-bahrain",
   "metadata": {},
   "source": [
    "<img width=\"800px\" src=\"../fidle/img/header.svg\"></img>\n",
    "\n",
    "# <!-- TITLE --> [SCRATCH1] - Scratchbook\n",
    "<!-- DESC --> A scratchbook for small examples\n",
    "<!-- AUTHOR : Jean-Luc Parouty (CNRS/SIMaP) -->\n",
    "\n",
    "## Objectives :\n",
    " - Take a quick look at thousands of little things\n",
    "\n",
    "## Inside this scratchbook :\n",
    "\n",
    "[1 - LSTM Keras layer](#1---LSTM-Keras-layer)  \n",
    "[2 - TimeseriesGenerator](#2---TimeseriesGenerator)  \n",
    "[3 - Upsampling](#3---Upsampling)  \n",
    "[4 - Reduce mean](#4---Reduce-mean)  \n",
    "[5 - Gradient tape](#5---Gradient-tape)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "accessory-church",
   "metadata": {},
   "source": [
    "# One init to rule them all"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "floppy-organic",
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow import keras\n",
    "from tensorflow.keras.callbacks import TensorBoard\n",
    "from tensorflow.keras.preprocessing.sequence import TimeseriesGenerator\n",
    "\n",
    "import numpy as np\n",
    "import math, random"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "danish-rebound",
   "metadata": {},
   "source": [
    "## 1 - LSTM Keras layer\n",
    "[Back to home](#)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "opposite-plasma",
   "metadata": {},
   "outputs": [],
   "source": [
    "inputs  = tf.random.normal([32, 20, 8])\n",
    "\n",
    "lstm    = tf.keras.layers.LSTM(16)\n",
    "output  = lstm(inputs)\n",
    "\n",
    "print('Inputs shape is : ', inputs.shape)\n",
    "print('Output shape is : ', output.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "forbidden-murray",
   "metadata": {},
   "outputs": [],
   "source": [
    "lstm = tf.keras.layers.LSTM(18, return_sequences=True, return_state=True)\n",
    "\n",
    "output, memory_state, carry_state = lstm(inputs)\n",
    "\n",
    "print('Output shape : ', output.shape)\n",
    "print('Memory state : ', memory_state.shape)\n",
    "print('Carry  state : ', carry_state.shape)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "verified-fruit",
   "metadata": {},
   "outputs": [],
   "source": [
    "# --- See the last vector of the output\n",
    "output[-1,-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "homeless-library",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ---- Memory state is the last output\n",
    "memory_state[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "preliminary-psychiatry",
   "metadata": {},
   "outputs": [],
   "source": [
    "carry_state[-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41d326b2-376e-49d6-9429-07016d98dc09",
   "metadata": {},
   "source": [
    "## 2 - TimeseriesGenerator\n",
    "[Back to home](#)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "42276389-4ea6-42d1-93bc-6650062ef86a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from keras.preprocessing.sequence import TimeseriesGenerator\n",
    "\n",
    "# ---- Define a dataset\n",
    "\n",
    "series = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])\n",
    "\n",
    "# ---- Generator\n",
    "\n",
    "generator = TimeseriesGenerator(series, series, length=5, batch_size=1)\n",
    "\n",
    "# ---- Samples\n",
    "\n",
    "nb_batch = len(generator)\n",
    "\n",
    "print(f'Number of batch : {nb_batch}\\n')\n",
    "for i in range(nb_batch):\n",
    "    x, y = generator[i]\n",
    "    print(f'#{i} : {x} => {y}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "67e3c888-aaa4-4166-90a1-cdb63920fd7d",
   "metadata": {},
   "source": [
    "## 3 - Upsampling\n",
    "[Back to home](#)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20f12cf0-1fdb-4b53-92c6-d03b140e42d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.array([1,2,3,4])\n",
    "x = x.reshape(2,2)\n",
    "print('\\nInitial : ', x.shape)\n",
    "print(x)\n",
    "\n",
    "x = x.reshape((1,2,2,1))\n",
    "print('\\nReshape as a batch of (2,2) vectors : ', x.shape)\n",
    "print(x)\n",
    "\n",
    "y = tf.keras.layers.UpSampling2D( size=(2, 2), interpolation=\"nearest\" )(x)\n",
    "\n",
    "y = np.array(y)\n",
    "print('\\ny shape : ',y.shape)\n",
    "\n",
    "y = y.reshape(4,4)\n",
    "print('\\n After a (4,4) reshape :')\n",
    "print(y)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e8fb1472",
   "metadata": {},
   "source": [
    "## 4 - Reduce mean\n",
    "[Back to home](#)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "09ac4e52-8953-41d9-b712-e6a83a9ae860",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "\n",
    "c = np.array([[3.,4], [5.,6], [7.,8]])\n",
    "print(np.mean(c,1))\n",
    "\n",
    "print(tf.reduce_mean(c,1))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "72be9368",
   "metadata": {},
   "source": [
    "## 5 - Gradient tape\n",
    "[Back to home](#)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13fcc722",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "\n",
    "# ---- My function f\n",
    "#\n",
    "def f(x):\n",
    "  y = x**2 + 4*x - 5\n",
    "  return y\n",
    "\n",
    "# ---- Examples :\n",
    "#\n",
    "print('f(1) is : ', f(1))\n",
    "print('f(2) is : ', f(2))\n",
    "\n",
    "# ---- With a tensor :\n",
    "#\n",
    "x = tf.Variable(2.0)\n",
    "\n",
    "print('f(x) is : ', f(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6fab93ce",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ---- Derivative function of f(x)\n",
    "#\n",
    "def g(x):\n",
    "    y = 2*x + 4\n",
    "    return y\n",
    "\n",
    "print('Derivative of f(x) for x=3 : ', g(3))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3f3f0c4c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ---- Derivative using tf\n",
    "#\n",
    "with tf.GradientTape() as tape:\n",
    "    x = tf.Variable(3.0)\n",
    "    y = f(x)\n",
    "\n",
    "dy = tape.gradient(y, x)  # dy/dx\n",
    "\n",
    "print(dy)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "43f9a625",
   "metadata": {},
   "source": [
    "---\n",
    "<img width=\"80px\" src=\"../fidle/img/logo-paysage.svg\"></img>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.2 ('fidle-env')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.2"
  },
  "vscode": {
   "interpreter": {
    "hash": "b3929042cc22c1274d74e3e946c52b845b57cb6d84f2d591ffe0519b38e4896d"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}