Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Fidle
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Talks
Fidle
Commits
5f92f14e
Commit
5f92f14e
authored
3 years ago
by
Jean-Luc Parouty
Browse files
Options
Downloads
Patches
Plain Diff
Update scratchbook with upsampling example
parent
0814d097
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Misc/Scratchbook.ipynb
+62
-1
62 additions, 1 deletion
Misc/Scratchbook.ipynb
with
62 additions
and
1 deletion
Misc/Scratchbook.ipynb
+
62
−
1
View file @
5f92f14e
...
...
@@ -233,10 +233,71 @@
" print(f'#{i} : {x} => {y}')"
]
},
{
"cell_type": "markdown",
"id": "67e3c888-aaa4-4166-90a1-cdb63920fd7d",
"metadata": {},
"source": [
"## 3 - Upsampling"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "20f12cf0-1fdb-4b53-92c6-d03b140e42d1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Initial : (2, 2)\n",
"[[1 2]\n",
" [3 4]]\n",
"\n",
"Reshape as a batch of (2,2) vectors : (1, 2, 2, 1)\n",
"[[[[1]\n",
" [2]]\n",
"\n",
" [[3]\n",
" [4]]]]\n",
"\n",
"y shape : (1, 4, 4, 1)\n",
"\n",
" After a (4,4) reshape :\n",
"[[1 1 2 2]\n",
" [1 1 2 2]\n",
" [3 3 4 4]\n",
" [3 3 4 4]]\n"
]
}
],
"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": "code",
"execution_count": null,
"id": "
4d94892b-d3a5-448d-aa2b-28c3a01a4b72
",
"id": "
09ac4e52-8953-41d9-b712-e6a83a9ae860
",
"metadata": {},
"outputs": [],
"source": []
...
...
%% 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
(
'
\n
Initial :
'
,
x
.
shape
)
print
(
x
)
x
=
x
.
reshape
((
1
,
2
,
2
,
1
))
print
(
'
\n
Reshape 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
(
'
\n
y 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
```
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment