Skip to content
Snippets Groups Projects
Commit 5f92f14e authored by Jean-Luc Parouty's avatar Jean-Luc Parouty
Browse files

Update scratchbook with upsampling example

parent 0814d097
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:alpha-bahrain tags:
<img width="800px" src="../fidle/img/00-Fidle-header-01.svg"></img>
# <!-- TITLE --> [SCRATCH1] - Scratchbook
<!-- DESC --> A scratchbook for small examples
<!-- AUTHOR : Jean-Luc Parouty (CNRS/SIMaP) -->
## Objectives :
- Take a quick look at thousands of little things
## Inside this scratchbook :
[1 - LSTM Keras layer](#1---LSTM-Keras-layer)
%% Cell type:markdown id:accessory-church tags:
# One init to rule them all
%% Cell type:code id:floppy-organic tags:
``` python
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.callbacks import TensorBoard
from tensorflow.keras.preprocessing.sequence import TimeseriesGenerator
import numpy as np
import math, random
```
%% Cell type:markdown id:danish-rebound tags:
## 1 - LSTM Keras layer
%% Cell type:code id:opposite-plasma tags:
``` python
inputs = tf.random.normal([32, 20, 8])
lstm = tf.keras.layers.LSTM(16)
output = lstm(inputs)
print('Inputs shape is : ', inputs.shape)
print('Output shape is : ', output.shape)
```
%% Output
Inputs shape is : (32, 20, 8)
Output shape is : (32, 16)
%% Cell type:code id:forbidden-murray tags:
``` python
lstm = tf.keras.layers.LSTM(18, return_sequences=True, return_state=True)
output, memory_state, carry_state = lstm(inputs)
print('Output shape : ', output.shape)
print('Memory state : ', memory_state.shape)
print('Carry state : ', carry_state.shape)
```
%% Output
Output shape : (32, 20, 18)
Memory state : (32, 18)
Carry state : (32, 18)
%% Cell type:code id:verified-fruit tags:
``` python
# --- See the last vector of the output
output[-1,-1]
```
%% Output
<tf.Tensor: shape=(18,), dtype=float32, numpy=
array([-0.20923303, 0.00193496, 0.05929745, 0.0429938 , -0.02835345,
0.14096233, 0.07420755, 0.1777523 , 0.1205566 , -0.03841979,
-0.02402029, 0.16098973, 0.10468155, -0.06480312, -0.02497844,
0.09700071, -0.24351674, 0.04884451], dtype=float32)>
%% Cell type:code id:homeless-library tags:
``` python
# ---- Memory state is the last output
memory_state[-1]
```
%% Output
<tf.Tensor: shape=(18,), dtype=float32, numpy=
array([-0.20923303, 0.00193496, 0.05929745, 0.0429938 , -0.02835345,
0.14096233, 0.07420755, 0.1777523 , 0.1205566 , -0.03841979,
-0.02402029, 0.16098973, 0.10468155, -0.06480312, -0.02497844,
0.09700071, -0.24351674, 0.04884451], dtype=float32)>
%% Cell type:code id:preliminary-psychiatry tags:
``` python
carry_state[-1]
```
%% Output
<tf.Tensor: shape=(18,), dtype=float32, numpy=
array([-0.3245376 , 0.00296011, 0.13041827, 0.10711877, -0.05223516,
0.4009896 , 0.21599025, 0.4260387 , 0.30799934, -0.0799172 ,
-0.06359857, 0.29457492, 0.18084048, -0.14462015, -0.04707906,
0.15726675, -0.38622206, 0.09004797], dtype=float32)>
%% Cell type:markdown id:41d326b2-376e-49d6-9429-07016d98dc09 tags:
## 2 - TimeseriesGenerator
%% Cell type:code id:42276389-4ea6-42d1-93bc-6650062ef86a tags:
``` python
from keras.preprocessing.sequence import TimeseriesGenerator
# ---- Define a dataset
series = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
# ---- Generator
generator = TimeseriesGenerator(series, series, length=5, batch_size=1)
# ---- Samples
nb_batch = len(generator)
print(f'Number of batch : {nb_batch}\n')
for i in range(nb_batch):
x, y = generator[i]
print(f'#{i} : {x} => {y}')
```
%% Output
Number of batch : 10
#0 : [[1 2 3 4 5]] => [6]
#1 : [[2 3 4 5 6]] => [7]
#2 : [[3 4 5 6 7]] => [8]
#3 : [[4 5 6 7 8]] => [9]
#4 : [[5 6 7 8 9]] => [10]
#5 : [[ 6 7 8 9 10]] => [11]
#6 : [[ 7 8 9 10 11]] => [12]
#7 : [[ 8 9 10 11 12]] => [13]
#8 : [[ 9 10 11 12 13]] => [14]
#9 : [[10 11 12 13 14]] => [15]
%% Cell type:code id:4d94892b-d3a5-448d-aa2b-28c3a01a4b72 tags:
%% Cell type:markdown id:67e3c888-aaa4-4166-90a1-cdb63920fd7d tags:
## 3 - Upsampling
%% Cell type:code id:20f12cf0-1fdb-4b53-92c6-d03b140e42d1 tags:
``` python
x = np.array([1,2,3,4])
x = x.reshape(2,2)
print('\nInitial : ', x.shape)
print(x)
x = x.reshape((1,2,2,1))
print('\nReshape as a batch of (2,2) vectors : ', x.shape)
print(x)
y = tf.keras.layers.UpSampling2D( size=(2, 2), interpolation="nearest" )(x)
y = np.array(y)
print('\ny shape : ',y.shape)
y = y.reshape(4,4)
print('\n After a (4,4) reshape :')
print(y)
```
%% Output
Initial : (2, 2)
[[1 2]
[3 4]]
Reshape as a batch of (2,2) vectors : (1, 2, 2, 1)
[[[[1]
[2]]
[[3]
[4]]]]
y shape : (1, 4, 4, 1)
After a (4,4) reshape :
[[1 1 2 2]
[1 1 2 2]
[3 3 4 4]
[3 3 4 4]]
%% Cell type:code id:09ac4e52-8953-41d9-b712-e6a83a9ae860 tags:
``` python
```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment