Skip to content

Commit 7abf8b4

Browse files
ashwinviscarschnosvenvanderburg
authored
Visualize model plot and update model summary for ep. 2 (#536)
* Updated output from model.summary() call Looks cleaner and it should match with what the learner would get. Got this by using the following versions of the package with Python 3.12. Name: tensorflow Version: 2.18.0 Name: keras Version: 3.8.0 * Model output names it functional now * Describe the memory footprint showing the model summary output * Tweak the model summary so that it can be viewed without scrolling * Add plot_model function and show its output * Add pydot dependency to setup * Move the instructor note before the challenge * Reword: data type * Fix alt text for 02_plot_model.png * Typo: a graph -> in graph form * Fix grammar. Co-authored-by: Ashwin V. Mohanan <[email protected]> * Add installation instructions for Graphviz to setup * A minor typo: follow -> follows * Rewrite instructions for checking if Graphviz works Co-authored-by: Sven van der Burg <[email protected]> * Change instructor note for optional question 3 visualizing the model --------- Co-authored-by: Carsten Schnober <[email protected]> Co-authored-by: Sven van der Burg <[email protected]>
1 parent 3015aab commit 7abf8b4

File tree

3 files changed

+82
-20
lines changed

3 files changed

+82
-20
lines changed

episodes/2-keras.Rmd

+76-18
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,13 @@ Keras distinguishes between two types of weights, namely:
410410
If these reasons are not clear right away, don't worry! In later episodes of this course, we will touch upon a couple of these concepts.
411411
:::
412412

413+
414+
::: instructor
415+
For optional question 3 in the challenge below named 'Visualizing the model', the goal is to visualize the network. It supplements the textual explanation of output from `model.summary()`.
416+
You could choose to show and discuss the resulting visualization to the learners, so that learners who did not finish the optional exercise can also learn from the visualization of the model.
417+
:::
418+
419+
413420
::: challenge
414421
## Create the neural network
415422
With the code snippets above, we defined a Keras model with 1 hidden layer with
@@ -419,13 +426,37 @@ With the code snippets above, we defined a Keras model with 1 hidden layer with
419426
2. What happens to the number of parameters if we increase or decrease the number of neurons
420427
in the hidden layer?
421428

429+
#### (optional) Visualizing the model
430+
Optionally, you can also visualize the same information as `model.summary()` in graph form.
431+
This step requires the command-line tool `dot` from Graphviz installed, you installed it by following the setup instructions.
432+
You can check that the installation was successful by executing `dot -V` in the command line. You should get something
433+
as follows:
434+
435+
```sh
436+
$ dot -V
437+
dot - graphviz version 2.43.0 (0)
438+
```
439+
440+
3. (optional) Provided you have `dot` installed, execute the `plot_model` function
441+
as shown below.
442+
443+
```python
444+
keras.utils.plot_model(
445+
model,
446+
show_shapes=True,
447+
show_layer_names=True,
448+
show_layer_activations=True,
449+
show_trainable=True
450+
)
451+
```
452+
422453
#### (optional) Keras Sequential vs Functional API
423454
So far we have used the [Functional API](https://keras.io/guides/functional_api/) of Keras.
424455
You can also implement neural networks using [the Sequential model](https://keras.io/guides/sequential_model/).
425456
As you can read in the documentation, the Sequential model is appropriate for **a plain stack of layers**
426457
where each layer has **exactly one input tensor and one output tensor**.
427458

428-
3. (optional) Use the Sequential model to implement the same network
459+
4. (optional) Use the Sequential model to implement the same network
429460

430461
:::: solution
431462
## Solution
@@ -435,37 +466,61 @@ model.summary()
435466
```
436467

437468
```output
438-
Model: "model_1"
439-
_________________________________________________________________
440-
Layer (type) Output Shape Param #
441-
=================================================================
442-
input_1 (InputLayer) [(None, 4)] 0
443-
_________________________________________________________________
444-
dense (Dense) (None, 10) 50
445-
_________________________________________________________________
446-
dense_1 (Dense) (None, 3) 33
447-
=================================================================
448-
Total params: 83
449-
Trainable params: 83
450-
Non-trainable params: 0
451-
_________________________________________________________________
469+
Model: "functional"
470+
471+
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┓
472+
┃ Layer (type) ┃ Output Shape ┃ Param # ┃
473+
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━┩
474+
│ input_layer (InputLayer) │ (None, 4) │ 0 │
475+
├────────────────────────────┼────────────────┼────────────┤
476+
│ dense (Dense) │ (None, 10) │ 50 │
477+
├────────────────────────────┼────────────────┼────────────┤
478+
│ dense_1 (Dense) │ (None, 3) │ 33 │
479+
└────────────────────────────┴────────────────┴────────────┘
480+
481+
Total params: 83 (332.00 B)
482+
483+
Trainable params: 83 (332.00 B)
484+
485+
Non-trainable params: 0 (0.00 B)
486+
452487
```
453488
The model has 83 trainable parameters. Each of the 10 neurons in the in the `dense` hidden layer is connected to each of
454489
the 4 inputs in the input layer resulting in 40 weights that can be trained. The 10 neurons in the hidden layer are also
455490
connected to each of the 3 outputs in the `dense_1` output layer, resulting in a further 30 weights that can be trained.
456491
By default `Dense` layers in Keras also contain 1 bias term for each neuron, resulting in a further 10 bias values for the
457492
hidden layer and 3 bias terms for the output layer. `40+30+10+3=83` trainable parameters.
458493

494+
The value `(332.00 B)` next to it describes the memory footprint for model weights and this depends on their data type.
495+
Take a look at what `model.dtype` is.
496+
497+
```python
498+
print(model.dtype)
499+
```
500+
501+
```output
502+
float32
503+
```
504+
The model weights are represented using `float32` data type, which consumes 32 bits or 4 bytes for each weight.
505+
We have 83 parameters, and therefore in total, the model requires `83*4=332` bytes of memory to load
506+
into the computer's memory.
507+
459508
If you increase the number of neurons in the hidden layer the number of
460509
trainable parameters in both the hidden and output layer increases or
461510
decreases in accordance with the number of neurons added.
462511
Each extra neuron has 4 weights connected to the input layer, 1 bias term, and 3 weights connected to the output layer.
463512
So in total 8 extra parameters.
464513

465-
*The name in quotes within the string `Model: "model_1"` may be different in your view; this detail is not important.*
514+
*The name in quotes within the string `Model: "functional"` may be different in your view; this detail is not important.*
515+
516+
#### (optional) Visualizing the model
517+
3. Upon executing the `plot_model` function, you should see the following image.
518+
519+
![Output of *keras.utils.plot_model()* function][plot-model]
520+
466521

467522
#### (optional) Keras Sequential vs Functional API
468-
3. This implements the same model using the Sequential API:
523+
4. This implements the same model using the Sequential API:
469524
```python
470525
model = keras.Sequential(
471526
[
@@ -550,7 +605,7 @@ history = model.fit(X_train, y_train, epochs=100)
550605
The fit method returns a history object that has a history attribute with the training loss and
551606
potentially other metrics per training epoch.
552607
It can be very insightful to plot the training loss to see how the training progresses.
553-
Using seaborn we can do this as follow:
608+
Using seaborn we can do this as follows:
554609
```python
555610
sns.lineplot(x=history.epoch, y=history.history['loss'])
556611
```
@@ -815,6 +870,9 @@ Length: 69, dtype: object
815870
[sex_pairplot]: fig/02_sex_pairplot.png "Pair plot grouped by sex"
816871
{alt='Grid of scatter plots and histograms comparing observed values of the four physicial attributes (features) measured in the penguins sampled, with data points coloured according to the sex of the individual sampled. The pair plot shows similarly-shaped distribution of values observed for each feature in male and female penguins, with the distribution of measurements for females skewed towards smaller values.'}
817872

873+
[plot-model]: fig/02_plot_model.png "Output of keras.utils.plot_model() function"
874+
{alt='A directed graph showing the three layers of the neural network connected by arrows. First layer is of type InputLayer. Second layer is of type Dense with a relu activation. The third layer is also of type Dense, with a softmax activation. The input and output shapes of every layer are also mentioned. Only the second and third layers contain trainable parameters.'}
875+
818876
[training_curve]: fig/02_training_curve.png "Training Curve"
819877
{alt='Training loss curve of the neural network training which depicts exponential decrease in loss before a plateau from ~10 epochs'}
820878

episodes/fig/02_plot_model.png

99.9 KB
Loading

learners/setup.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ Remember that you need to activate your environment every time you restart your
7777
### On Linux/macOs
7878

7979
```shell
80-
python3 -m pip install jupyter seaborn scikit-learn pandas tensorflow
80+
python3 -m pip install jupyter seaborn scikit-learn pandas tensorflow pydot
8181
```
8282

8383
::: spoiler
@@ -102,13 +102,17 @@ python -m pip install tensorflow-metal
102102
### On Windows
103103

104104
```shell
105-
py -m pip install jupyter seaborn scikit-learn pandas tensorflow
105+
py -m pip install jupyter seaborn scikit-learn pandas tensorflow pydot
106106
```
107107

108108
:::
109109

110110
Note: Tensorflow makes Keras available as a module too.
111111

112+
An [optional challenge in episode 2](episodes/2-keras.Rmd) requires installation of Graphviz
113+
and instructions for doing that can be found
114+
[by following this link](https://graphviz.org/download/).
115+
112116
## Starting Jupyter Lab
113117

114118
We will teach using Python in [Jupyter Lab][jupyter], a programming environment that runs in a web browser.

0 commit comments

Comments
 (0)