Skip to content

Commit 2c5bfd1

Browse files
committed
differences for PR #624
1 parent 133225d commit 2c5bfd1

5 files changed

Lines changed: 54 additions & 49 deletions

File tree

1-introduction.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ In most neural networks, neurons are aggregated into layers. Signals travel from
133133
The image below shows an example of a neural network with three layers, each circle is a neuron, each line is an edge and the arrows indicate the direction data moves in.
134134

135135
![
136-
Image credit: Glosser.ca, CC BY-SA 3.0 <https://creativecommons.org/licenses/by-sa/3.0>, via Wikimedia Commons,
136+
Image credit: Glosser.ca, CC BY-SA 3.0 <https://creativecommons.org/licenses/by-sa/3.0>, via Wikimedia Commons,
137137
[original source](https://commons.wikimedia.org/wiki/File:Colored_neural_network.svg)
138138
](fig/01_neural_net.png){
139139
alt='A diagram of a three layer neural network with an input layer, one hidden layer, and an output layer.'
@@ -487,12 +487,16 @@ Keras also benefits from a very good set of [online documentation](https://keras
487487
Follow the [setup instructions](learners/setup.md#packages) to install Keras, Seaborn and scikit-learn.
488488

489489
## Testing Keras Installation
490-
Keras is available as a module within TensorFlow, as described in the [setup instructions](learners/setup.md#packages).
490+
Keras is available as a standalone package, as described in the [setup instructions](learners/setup.md#packages).
491491
Let's therefore check whether you have a suitable version of TensorFlow installed.
492492
Open up a new Jupyter notebook or interactive python console and run the following commands:
493493
```python
494-
import tensorflow
495-
print(tensorflow.__version__)
494+
# Note: Before importing Keras, we have to instruct it to use PyTorch as the backend.
495+
import os
496+
os.environ['KERAS_BACKEND'] = 'torch'
497+
498+
import keras
499+
print(keras.__version__)
496500
```
497501
```output
498502
2.17.0

2-keras.md

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ the same results (assuming you give the same integer) every time it is called.
281281
```python
282282
from sklearn.model_selection import train_test_split
283283

284-
X_train, X_test, y_train, y_test = train_test_split(features, target, test_size=0.2, random_state=0, shuffle=True, stratify=target)
284+
x_train, x_test, y_train, y_test = train_test_split(features, target, test_size=0.2, random_state=0, shuffle=True, stratify=target)
285285
```
286286

287287
::: callout
@@ -302,19 +302,21 @@ This is a good time for switching instructor and/or a break.
302302
### Keras for neural networks
303303

304304
Keras is a machine learning framework with ease of use as one of its main features.
305-
It is part of the tensorflow python package and can be imported using `from tensorflow import keras`.
305+
It is a standalone python package that supports multiple deep learning frameworks as backends, and it can be imported using `import keras`.
306+
Here, we will use Keras with the PyTorch backend.
306307

307308
Keras includes functions, classes and definitions to define deep learning models, cost functions and optimizers (optimizers are used to train a model).
308309

309310
Before we move on to the next section of the workflow we need to make sure we have Keras imported.
310311
We do this as follows:
311312
```python
312-
from tensorflow import keras
313+
import keras
313314
```
314315

315316
For this episode it is useful if everyone gets the same results from their training.
316317
Keras uses a random number generator at certain points during its execution.
317-
Therefore we will need to set two random seeds, one for numpy and one for tensorflow:
318+
Therefore, we will need to set two random seeds: one for NumPy and one for PyTorch.
319+
We can use a built-in Keras function to achieve this in one line of code:
318320
```python
319321
keras.utils.set_random_seed(2)
320322
```
@@ -348,7 +350,7 @@ and outputs a layer needs and therefore how many edges need to be created.
348350
This means we need to inform Keras how big our input is going to be. We do this by instantiating a `keras.Input` class and tell it how big our input is, thus the number of columns it contains.
349351

350352
```python
351-
inputs = keras.Input(shape=(X_train.shape[1],))
353+
inputs = keras.Input(shape=(x_train.shape[1],))
352354
```
353355

354356
We store a reference to this input class in a variable so we can pass it to the creation of
@@ -369,7 +371,7 @@ for inputs that are 0 and below and the identity function (returning the same va
369371
for inputs above 0.
370372
This is a commonly used activation function in deep neural networks that is proven to work well.
371373

372-
Next we see an extra set of parenthenses with inputs in them. This means that after creating an
374+
Next we see an extra set of parenthenses with `inputs` in them. This means that after creating an
373375
instance of the Dense layer we call it as if it was a function.
374376
This tells the Dense layer to connect the layer passed as a parameter, in this case the inputs.
375377

@@ -383,7 +385,7 @@ output_layer = keras.layers.Dense(3, activation="softmax")(hidden_layer)
383385

384386
Because we chose the one-hot encoding, we use three neurons for the output layer.
385387

386-
The `softmax` activation ensures that the three output neurons produce values in the range
388+
The [`softmax`](https://keras.io/api/layers/activations/#softmax-function) activation ensures that the three output neurons produce values in the range
387389
(0, 1) and they sum to 1.
388390
We can interpret this as a kind of 'probability' that the sample belongs to a certain
389391
species.
@@ -403,10 +405,10 @@ Keras distinguishes between two types of weights, namely:
403405

404406
- trainable parameters: these are weights of the neurons that are modified when we train the model in order to minimize our loss function (we will learn about loss functions shortly!).
405407

406-
- non-trainable parameters: these are weights of the neurons that are not changed when we train the model. These could be for many reasons - using a pre-trained model, choice of a particular filter for a convolutional neural network, and statistical weights for batch normalization are some examples.
408+
- non-trainable parameters: these are weights of the neurons that are not changed when we train the model. These could be for many reasons - using a pre-trained model, choice of a particular filter for a convolutional neural network, and statistical weights for batch normalization are some examples.
407409

408410
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.
409-
:::
411+
:::
410412

411413

412414
::: instructor
@@ -483,9 +485,9 @@ Model: "functional"
483485
Non-trainable params: 0 (0.00 B)
484486
485487
```
486-
The model has 83 trainable parameters. Each of the 10 neurons in the in the `dense` hidden layer is connected to each of
487-
the 4 inputs in the input layer resulting in 40 weights that can be trained. The 10 neurons in the hidden layer are also
488-
connected to each of the 3 outputs in the `dense_1` output layer, resulting in a further 30 weights that can be trained.
488+
The model has 83 trainable parameters. Each of the 10 neurons in the in the `dense` hidden layer is connected to each of
489+
the 4 inputs in the input layer resulting in 40 weights that can be trained. The 10 neurons in the hidden layer are also
490+
connected to each of the 3 outputs in the `dense_1` output layer, resulting in a further 30 weights that can be trained.
489491
By default `Dense` layers in Keras also contain 1 bias term for each neuron, resulting in a further 10 bias values for the
490492
hidden layer and 3 bias terms for the output layer. `40+30+10+3=83` trainable parameters.
491493

@@ -524,7 +526,7 @@ So in total 8 extra parameters.
524526
```python
525527
model = keras.Sequential(
526528
[
527-
keras.Input(shape=(X_train.shape[1],)),
529+
keras.Input(shape=(x_train.shape[1],)),
528530
keras.layers.Dense(10, activation="relu"),
529531
keras.layers.Dense(3, activation="softmax"),
530532
]
@@ -571,13 +573,13 @@ This is a measure for how close the distribution of the three neural network out
571573
It is lower if the distributions are more similar.
572574

573575
For more information on the available loss functions in Keras you can check the
574-
[documentation](https://www.tensorflow.org/api_docs/python/tf/keras/losses).
576+
[documentation](https://keras.io/api/losses/).
575577

576578
Next we need to choose which optimizer to use and, if this optimizer has parameters, what values
577579
to use for those. Furthermore, we need to specify how many times to show the training samples to the optimizer.
578580

579581
Once more, Keras gives us plenty of choices all of which have their own pros and cons,
580-
but for now let us go with the widely used [Adam optimizer](https://www.tensorflow.org/api_docs/python/tf/keras/optimizers/Adam).
582+
but for now let us go with the widely used [Adam optimizer](https://keras.io/api/optimizers/adam/).
581583
Adam has a number of parameters, but the default values work well for most problems.
582584
So we will use it with its default parameters.
583585

@@ -600,7 +602,7 @@ One training epoch means that every sample in the training data has been shown
600602
to the neural network and used to update its parameters.
601603

602604
```python
603-
history = model.fit(X_train, y_train, epochs=100)
605+
history = model.fit(x_train, y_train, epochs=100)
604606
```
605607

606608
The fit method returns a history object that has a history attribute with the training loss and
@@ -673,7 +675,7 @@ trained network.
673675
This will return a `numpy` matrix, which we convert
674676
to a pandas dataframe to easily see the labels.
675677
```python
676-
y_pred = model.predict(X_test)
678+
y_pred = model.predict(x_test)
677679
prediction = pd.DataFrame(y_pred, columns=target.columns)
678680
prediction
679681
```
@@ -822,7 +824,7 @@ many hyperparameter and model architecture choices.
822824
We will go into more depth of these choices in later episodes.
823825
For now it is important to realize that the parameters we chose were
824826
somewhat arbitrary and more careful consideration needs to be taken to
825-
pick hyperparameter values.
827+
pick hyperparameter values.
826828

827829

828830
## 10. Share model
@@ -844,7 +846,7 @@ This loaded model can be used as before to predict.
844846

845847
```python
846848
# use the pretrained model here
847-
y_pretrained_pred = pretrained_model.predict(X_test)
849+
y_pretrained_pred = pretrained_model.predict(x_test)
848850
pretrained_prediction = pd.DataFrame(y_pretrained_pred, columns=target.columns.values)
849851

850852
# idxmax will select the column for each row with the highest value

fig/.gitkeep

Whitespace-only changes.

md5sum.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"index.md" "8b5609014b8028029f48266bc751663e" "site/built/index.md" "2025-05-06"
66
"links.md" "8184cf4149eafbf03ce8da8ff0778c14" "site/built/links.md" "2025-02-11"
77
"workshops.md" "db285c697b5f062098913c08119130ff" "site/built/workshops.md" "2025-10-23"
8-
"episodes/1-introduction.md" "3602dbf5bab0ed4e21679de5f27d3f47" "site/built/1-introduction.md" "2025-12-09"
9-
"episodes/2-keras.md" "ab5ebd62fd3e6cc2ad69bd805d26f7f1" "site/built/2-keras.md" "2025-09-03"
8+
"episodes/1-introduction.md" "41c91d12c3e45b5bc5f339a0a4aae666" "site/built/1-introduction.md" "2025-12-09"
9+
"episodes/2-keras.md" "5583f21576ffb21ba9b37c7c5aab7add" "site/built/2-keras.md" "2025-12-09"
1010
"episodes/3-monitor-the-model.md" "8eda70a03e5225033ba05563b517ec88" "site/built/3-monitor-the-model.md" "2025-09-02"
1111
"episodes/4-advanced-layer-types.md" "eaca1f96ead3140467a4ba7f069284fa" "site/built/4-advanced-layer-types.md" "2025-09-03"
1212
"episodes/5-transfer-learning.md" "65ed8dff158123d8271100fd8d18fd3b" "site/built/5-transfer-learning.md" "2025-12-09"
@@ -17,6 +17,6 @@
1717
"instructors/schedule.md" "332b32d24f144b29a280176e6b5d015f" "site/built/schedule.md" "2025-03-10"
1818
"instructors/survey-templates.md" "ea5d46e7b54d335f79e57a7bc31d1c5c" "site/built/survey-templates.md" "2025-02-11"
1919
"learners/reference.md" "6a11d5269dc9d1d31d4016086580e838" "site/built/reference.md" "2025-12-09"
20-
"learners/setup.md" "2e741a6d76091da5c832ab010380f0e1" "site/built/setup.md" "2025-09-01"
20+
"learners/setup.md" "dbf2c7a18b7bc9ab5d404024b2e48930" "site/built/setup.md" "2025-12-09"
2121
"paper/paper.md" "2b05562e0f9d393818ad4e7ea2d5c152" "site/built/paper.md" "2025-10-20"
2222
"profiles/learner-profiles.md" "ef0f26dd0874387d80ed3fd468b99e23" "site/built/learner-profiles.md" "2025-02-11"

setup.md

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Open a terminal (Mac/Linux) or Command Prompt (Windows) and run the following co
2828

2929
::: spoiler
3030

31-
### On Linux/macOs
31+
### On Linux/MacOS
3232

3333
```shell
3434
python3 -m venv dl_workshop
@@ -50,7 +50,7 @@ py -m venv dl_workshop
5050

5151
::: spoiler
5252

53-
### On Linux/macOs
53+
### On Linux/MacOS
5454

5555
```shell
5656
source dl_workshop/bin/activate
@@ -74,32 +74,24 @@ Remember that you need to activate your environment every time you restart your
7474

7575
::: spoiler
7676

77-
### On Linux/macOs
77+
### On Linux/MacOS
7878

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

83-
Note for MacOS users: there is a package `tensorflow-metal` which accelerates the training of machine learning models with TensorFlow on a recent Mac with a Silicon chip (M1/M2/M3).
84-
However, the installation is currently broken in the most recent version (as of January 2025), see the [developer forum](https://developer.apple.com/forums/thread/772147).
85-
8683
:::
8784

8885
::: spoiler
8986

9087
### On Windows
9188

9289
```shell
93-
py -m pip install jupyter seaborn scikit-learn pandas tensorflow pydot
90+
py -m pip install jupyter seaborn scikit-learn pandas keras torch pydot
9491
```
9592

9693
:::
97-
98-
Note: Tensorflow makes Keras available as a module too.
99-
100-
An [optional challenge in episode 2](episodes/2-keras.md) requires installation of Graphviz
101-
and instructions for doing that can be found
102-
[by following this link](https://graphviz.org/download/).
94+
An [optional challenge in episode 2](episodes/2-keras.md) requires installation of Graphviz. Instructions for doing that can be found [by following this link](https://graphviz.org/download/).
10395

10496
## Starting Jupyter Lab
10597

@@ -108,7 +100,7 @@ Jupyter Lab is compatible with Firefox, Chrome, Safari and Chromium-based browse
108100
Note that Internet Explorer and Edge are *not* supported.
109101
See the [Jupyter Lab documentation](https://jupyterlab.readthedocs.io/en/latest/getting_started/accessibility.html#compatibility-with-browsers-and-assistive-technology) for an up-to-date list of supported browsers.
110102

111-
To start Jupyter Lab, open a terminal (Mac/Linux) or Command Prompt (Windows),
103+
To start Jupyter Lab, open a terminal (Mac/Linux) or Command Prompt (Windows),
112104
make sure that you activated the virtual environment you created for this course,
113105
and type the command:
114106

@@ -121,31 +113,38 @@ To check whether all packages installed correctly, start a jupyter notebook in j
121113
explained above. Run the following lines of code:
122114
```python
123115
import sklearn
124-
print('sklearn version: ', sklearn.__version__)
116+
print(f'Sklearn version: {sklearn.__version__}')
125117

126118
import seaborn
127-
print('seaborn version: ', seaborn.__version__)
119+
print(f'Seaborn version: {seaborn.__version__}')
128120

129121
import pandas
130-
print('pandas version: ', pandas.__version__)
122+
print(f'Pandas version: {pandas.__version__}')
123+
124+
import torch
125+
print(f'PyTorch version: {torch.__version__}')
126+
127+
# Note: Before importing Keras, we have to instruct it to use PyTorch as the backend.
128+
import os
129+
os.environ['KERAS_BACKEND'] = 'torch'
131130

132-
import tensorflow
133-
print('Tensorflow version: ', tensorflow.__version__)
131+
import keras
132+
print(f'Keras version: {keras.__version__}')
134133
```
135134

136135
This should output the versions of all required packages without giving errors.
137136
Most versions will work fine with this lesson, but:
138-
- For Keras and Tensorflow, the minimum version is 2.12.0
137+
- For Keras, the minimum version is 2.12.0
139138
- For sklearn, the minimum version is 1.2.2
140139

141140
## Fallback option: cloud environment
142141
If a local installation does not work for you, it is also possible to run this lesson in [Binder Hub](https://mybinder.org/v2/gh/carpentries-lab/deep-learning-intro/scaffolds). This should give you an environment with all the required software and data to run this lesson, nothing which is saved will be stored, please copy any files you want to keep. Note that if you are the first person to launch this in the last few days it can take several minutes to startup. The second person who loads it should find it loads in under a minute. Instructors who intend to use this option should start it themselves shortly before the workshop begins.
143142

144-
Alternatively you can use [Google colab](https://colab.research.google.com/). If you open a jupyter notebook here, the required packages are already pre-installed. Note that google colab uses jupyter notebook instead of Jupyter Lab.
143+
Alternatively you can use [Google Colab](https://colab.research.google.com/). If you open a jupyter notebook here, the required packages are already pre-installed. Note that Google Colab uses jupyter notebook instead of Jupyter Lab.
145144

146145
## Downloading the required datasets
147146

148-
Download the [weather dataset prediction csv][weatherdata] and [Dollar street dataset (4 files in total)][dollar-street]
147+
Download the [Weather dataset prediction csv][weatherdata] and [Dollar street dataset (4 files in total)][dollar-street]
149148

150149
[dollar-street]: https://zenodo.org/api/records/10970014/files-archive
151150
[jupyter]: http://jupyter.org/

0 commit comments

Comments
 (0)