{ "cells": [ { "cell_type": "markdown", "id": "alpha-bahrain", "metadata": {}, "source": [ "<img width=\"800px\" src=\"../fidle/img/00-Fidle-header-01.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" ] }, { "cell_type": "markdown", "id": "accessory-church", "metadata": {}, "source": [ "# One init to rule them all" ] }, { "cell_type": "code", "execution_count": 1, "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" ] }, { "cell_type": "code", "execution_count": 2, "id": "opposite-plasma", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Inputs shape is : (32, 20, 8)\n", "Output shape is : (32, 16)\n" ] } ], "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": 3, "id": "forbidden-murray", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Output shape : (32, 20, 18)\n", "Memory state : (32, 18)\n", "Carry state : (32, 18)\n" ] } ], "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": 4, "id": "verified-fruit", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "<tf.Tensor: shape=(18,), dtype=float32, numpy=\n", "array([-0.20923303, 0.00193496, 0.05929745, 0.0429938 , -0.02835345,\n", " 0.14096233, 0.07420755, 0.1777523 , 0.1205566 , -0.03841979,\n", " -0.02402029, 0.16098973, 0.10468155, -0.06480312, -0.02497844,\n", " 0.09700071, -0.24351674, 0.04884451], dtype=float32)>" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# --- See the last vector of the output\n", "output[-1,-1]" ] }, { "cell_type": "code", "execution_count": 5, "id": "homeless-library", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "<tf.Tensor: shape=(18,), dtype=float32, numpy=\n", "array([-0.20923303, 0.00193496, 0.05929745, 0.0429938 , -0.02835345,\n", " 0.14096233, 0.07420755, 0.1777523 , 0.1205566 , -0.03841979,\n", " -0.02402029, 0.16098973, 0.10468155, -0.06480312, -0.02497844,\n", " 0.09700071, -0.24351674, 0.04884451], dtype=float32)>" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# ---- Memory state is the last output\n", "memory_state[-1]" ] }, { "cell_type": "code", "execution_count": 6, "id": "preliminary-psychiatry", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "<tf.Tensor: shape=(18,), dtype=float32, numpy=\n", "array([-0.3245376 , 0.00296011, 0.13041827, 0.10711877, -0.05223516,\n", " 0.4009896 , 0.21599025, 0.4260387 , 0.30799934, -0.0799172 ,\n", " -0.06359857, 0.29457492, 0.18084048, -0.14462015, -0.04707906,\n", " 0.15726675, -0.38622206, 0.09004797], dtype=float32)>" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "carry_state[-1]" ] }, { "cell_type": "markdown", "id": "41d326b2-376e-49d6-9429-07016d98dc09", "metadata": {}, "source": [ "## 2 - TimeseriesGenerator" ] }, { "cell_type": "code", "execution_count": 27, "id": "42276389-4ea6-42d1-93bc-6650062ef86a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Number of batch : 10\n", "\n", "#0 : [[1 2 3 4 5]] => [6]\n", "#1 : [[2 3 4 5 6]] => [7]\n", "#2 : [[3 4 5 6 7]] => [8]\n", "#3 : [[4 5 6 7 8]] => [9]\n", "#4 : [[5 6 7 8 9]] => [10]\n", "#5 : [[ 6 7 8 9 10]] => [11]\n", "#6 : [[ 7 8 9 10 11]] => [12]\n", "#7 : [[ 8 9 10 11 12]] => [13]\n", "#8 : [[ 9 10 11 12 13]] => [14]\n", "#9 : [[10 11 12 13 14]] => [15]\n" ] } ], "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": "code", "execution_count": null, "id": "4d94892b-d3a5-448d-aa2b-28c3a01a4b72", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.7" } }, "nbformat": 4, "nbformat_minor": 5 }