-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsolution.py
1437 lines (1090 loc) · 48.1 KB
/
solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# ---
# jupyter:
# jupytext:
# cell_markers: '"""'
# formats: ipynb,py:percent
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.15.0
# kernelspec:
# display_name: Python [conda env:01_intro_ml]
# language: python
# name: conda-env-01_intro_ml-py
# ---
# %% [markdown]
"""
# Introduction to Machine Learning
"""
# %% [markdown] id="JWC5tKyP3x1e"
"""
Written by Morgan Schwartz and David Van Valen.
---
In this exercise, we are going to follow the basic workflow that is the foundation of any machine or deep learning project
1. Data wrangling
2. Model configuration and training
3. Model evaluation
Along the way, we will implement a linear classifier, test a random forest classifier and explore the role of feature engineering in traditional machine learning.
We are going to look at a collection of images of Jurkat cells published in the Broad Bioimage Collection ([BBBC048](https://bbbc.broadinstitute.org/BBBC048)). The cells were fixed and stained with PI (propidium iodide) to quantify DNA content and a MPM2 (mitotic protein monoclonal #2) antibody to identify mitotic cells. The goal is to predict the stage of the cell cycle from images like those shown below.
![](https://media.springernature.com/full/springer-static/image/art%3A10.1038%2Fs41467-017-00623-3/MediaObjects/41467_2017_623_Fig2_HTML.jpg?as=webp)
[Eulenberg et al. (2017) Reconstructing cell cycle and disease progression using deep learning. Nature communications](https://www.nature.com/articles/s41467-017-00623-3)
"""
# %% [markdown]
"""
<div class="alert alert-danger">
Set your python kernel to <code>01_intro_ml</code>
![](images/kernel-change.png)
</div>
"""
# %% [markdown]
"""
# Part A: The Linear Classifier
While deep learning might seem intimidating, don't worry. Its conceptual underpinnings are rooted in linear algebra and calculus - if you can perform matrix multiplication and take derivatives you can understand what is happening in a deep learning workflow. In this section, we will implement a simple linear classifier by hand and train it to predict cell cycle stages.
"""
# %% id="_DwKG_Gi3x1f"
from collections import Counter
import os
import imageio as iio
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import skimage
import sklearn
import sklearn.model_selection
import sklearn.ensemble
import tqdm.auto
# %% [markdown] id="rZ2jTjsd3x1g"
"""
## The supervised machine learning workflow
Recall from class the conceptual workflow for a supervised machine learning project.
- First, we create a <em>training dataset</em>, a paired collection of raw data and labels where the labels contain information about the "insight" we wish to extract from the raw data.
- Once we have training data, we can then use it to train a <em>model</em>. The model is a mathematical black box - it takes in data and transforms it into an output. The model has some parameters that we can adjust to change how it performs this mapping.
- Adjusting these parameters to produce outputs that we want is called training the model. To do this we need two things. First, we need a notion of what we want the output to look like. This notion is captured by a <em>loss function</em>, which compares model outputs and labels and produces a score telling us if the model did a "good" job or not on our given task. By convention, low values of the loss function's output (e.g. the loss) correspond to good performance and high values to bad performance. We also need an <em>optimization algorithm</em>, which is a set of rules for how to adjust the model parameters to reduce the loss
- Using the training data, loss function, and optimization algorithm, we can then train the model
- Once the model is trained, we need to evaluate its performance to see how well it performs and what kinds of mistakes it makes. We can also perform this kind of monitoring during training (this is actually a standard practice).
Because this workflow defines the lifecycle of most machine learning projects, this notebook is structured to go over each of these steps while constructing a linear classifier.
"""
# %% [markdown] id="rbIDLvJ23x1g"
"""
## Create training data
During the initial setup of this exercise, we downloaded the data and unzipped the relevant files using the script `setup.sh`.
"""
# %%
data_dir = "data/CellCycle"
sorted(os.listdir(data_dir))
# %% [markdown]
"""
The command above should generate the following output. If you see something different, please check that the `setup.sh` script ran correctly.
```
['66.lst~',
'Anaphase',
'G1',
'G2',
'Metaphase',
'Prophase',
'S',
'Telophase',
'img.lst',
'img.lst~']
```
The metadata for each file is stored in `img.lst` so we will first load this information to inform how we load the rest of the dataset.
"""
# %%
# Read the csv file using pandas
df = pd.read_csv(os.path.join(data_dir, "img.lst"), sep="\t", header=None)
# Rename columns to make the data easier to work with
df = df.rename(columns={1: "class", 2: "filepath"})
# Extract the channel information from the filepath column and create a new column with it
df["channel"] = (
df["filepath"]
.str.split("/", expand=True)[2]
.str.split("_", expand=True)[1]
.str.slice(2, 3)
)
# Extract the file ID from the filepath and save in its own column
df["id"] = df["filepath"].str.split("/", expand=True)[2].str.split("_", expand=True)[0]
# Look at the first few rows in the dataframe
df.head()
# %%
# Check the total number of unique classes in the dataset
df["class"].unique()
# %%
class_lut = ["Ana", "Meta", "Pro", "Telo", "G1", "G2", "S"]
# %% [markdown]
"""
For each `id` there are three images. One for each of the channels: phase, PI and MPM2. Let's take a look at a single image
"""
# %%
im_id = "12432"
# Load channel 3
filepath = df[(df["id"] == im_id) & (df["channel"] == "3")]["filepath"].values[0]
im3 = iio.imread(os.path.join(data_dir, filepath))
# Load channel 4
filepath = df[(df["id"] == im_id) & (df["channel"] == "4")]["filepath"].values[0]
im4 = iio.imread(os.path.join(data_dir, filepath))
# Load channel 6
filepath = df[(df["id"] == im_id) & (df["channel"] == "6")]["filepath"].values[0]
im6 = iio.imread(os.path.join(data_dir, filepath))
# Create a matplotlib subplot with one row and 3 columns
# Plot each of the three images
fig, ax = plt.subplots(1, 3, figsize=(10, 3))
ax[0].imshow(im3, cmap="Greys_r")
ax[0].set_title("phase")
ax[1].imshow(im4, cmap="Greys_r")
ax[1].set_title("PI")
ax[2].imshow(im6, cmap="Greys_r")
ax[2].set_title("MPM2")
# %% [markdown]
"""
Now we can load all of the images into a dataset. We will want to load each of the three channels for each image and create an array with the shape (w, h, ch). Then we will combine all images in the dataset into a single array.
"""
# %%
# Create empty lists to hold images and classes as we load them
ims = []
ys = []
# Iterate over each unique id in the dataset
for i, g in df.groupby("id"):
im = []
# Each row in the group corresponds to a different channel for a single image/id
for _, r in g.iterrows():
# Set the complete filepath for this image
path = os.path.join(data_dir, r["filepath"])
# Load in the data using imageio and append to a list
im.append(iio.imread(path))
# Stack a list of three images into a single
im = np.stack(im, axis=-1)
ims.append(im)
ys.append(r["class"])
X_data = np.stack(ims)
y_data = np.stack(ys)
print("X shape:", X_data.shape)
print("y shape:", y_data.shape)
# %% [markdown] id="WG4mmJ173x1h"
"""
In the previous cell, you probably observed that there are 4 dimensions rather than the 3 you might have been expecting. This is because while each image is (66, 66, 3), the full dataset has many images. The different images are stacked along the first dimension. The full size of the training images is (# images, 66, 66, 3).
"""
# %% [markdown]
"""
Let's take a look at a sample image from each class and plot each of the three channels separately.
"""
# %% id="5RIxP_hq3x1i"
# Iterate over each class in the dataset
for c in np.unique(y_data):
# Select a random index for the class of interest
i = np.random.choice(np.where(y_data == c)[0])
# Create a matplotlib subplot with one row and 3 columsn
fig, ax = plt.subplots(1, 3, figsize=(10, 3))
ax[0].set_ylabel(class_lut[c])
# Plot each of the three channels
for j, ch in enumerate(["phase contrast", "PI", "MPM2"]):
ax[j].imshow(X_data[i, ..., j], cmap="Greys_r")
ax[j].set_title(ch)
ax[j].xaxis.set_tick_params(labelbottom=False)
ax[j].yaxis.set_tick_params(labelleft=False)
ax[j].set_xticks([])
ax[j].set_yticks([])
# %% [markdown] id="2D_YhVir3x1i"
"""
For this exercise, we will want to flatten the training data into a vector and select a single channel to work with. We work with the phase channel first.
"""
# %%
# Record the original width so that we can use this for reshaping later
image_width = X_data.shape[1]
image_width
# %% colab={"base_uri": "https://localhost:8080/"} id="J3MJPnh-3x1j" outputId="c280504b-94c4-4888-dd02-875c789dee9f"
# Flatten the images 1d vectors
X_flat = np.reshape(X_data[..., 0], (-1, image_width * image_width))
print(X_flat.shape)
# %% [markdown]
"""
### Checking Class Balance
"""
# %% [markdown]
"""
<div class="alert alert-block alert-info">
#### Task 1.1
Let's check the balance of classes in this dataset (stored in `y_data`). There are at least three ways you could do this. Pick one to try.
- Count the number of items in each class using `np.unique` ([see docs](https://numpy.org/doc/stable/reference/generated/numpy.unique.html)).
- Use the `Counter` object which is imported from `collections` ([see docs](https://docs.python.org/3/library/collections.html#collections.Counter))
</div>
"""
# %%
##########################
######## To Do ###########
##########################
# Add your code to check class balances here
# You should end up with a count of number of items in each of the 7 classes
# %% tags=["solution"]
##########################
####### Solution #########
##########################
# Numpy option
np.unique(y_data, return_counts=True)
print("------\n")
# Counter option
print(Counter(y_data))
# %% [markdown]
"""
This dataset is highly inbalanced so we will want to correct the class balance before training.
"""
# %% [markdown] id="l2yrGjOL3x1j"
"""
### Split the training dataset into training and testing datasets
How do we know how well our model is doing? A common practice to evaluate models is to evaluate them on splits of the original training dataset. Splitting the data is important, because we want to see how models perform on data that was not used to train them. We split into
- The <em>training</em> dataset used to train the model
- A held out <em>testing</em> dataset used to evaluate the final trained version of the model
While there is no hard and fast rule, 80%/20% splits are a reasonable starting point.
"""
# %% id="hLQUiSoj3x1j"
# Split the dataset into training, validation, and testing splits
seed = 10
train_size = 0.8
X_train, X_test, y_train, y_test = sklearn.model_selection.train_test_split(
X_flat, y_data, train_size=train_size, random_state=seed
)
# %% [markdown]
"""
### Correct class imbalance
![](https://miro.medium.com/v2/resize:fit:1400/format:webp/1*6DlvkNY2TlFw2Veyvb4qdQ.jpeg)
(*Image by [Angelica Lo Duca](https://towardsdatascience.com/how-to-balance-a-dataset-in-python-36dff9d12704)*)
There are several ways to correct class imbalance. In this example, we are going to oversample underrepresented classes until we have an equal number of samples for each class.
It's important to note that we need to correct class imbalance after generating the train/test split in our dataset. When we are oversampling, we want to prevent samples that are used in our training dataset from also appearing in our testing dataset.
"""
# %% [markdown]
"""
<div class="alert alert-block alert-info">
#### Task 1.2
Complete the `balance_classes` function following the structure outlined in the comments. You can use `sklearn.utils.resample` to generate a new random set of `n_samples`.
Hint: You may want to use boolean indexing to select subsets of arrays. For example we can select all samples in class 3 with the following:
```python
xx = X_data[y_data == 3]
yy = y_data[y_data == 3]
```
</div>
"""
# %%
# sklearn.utils.resample?
# %%
##########################
######## To Do ###########
##########################
def balance_classes(X, y):
"""For a given multiclass dataset, upsample underrepresented classes
to match the number of samples in the majority class
Args:
X (np.array): Array of raw data
y (np.array): Array of class labels
Returns:
np.array: X
np.array: y
"""
classes = np.unique(y)
# Identify which class has the most samples
# Hint: use your code for counting the number of samples in each class
maj_samples = ...
maj_id = ...
# Collect new samples in an array
new_X, new_y = [], []
for c in classes:
# Resample the minority classes to match majority number of samples using sklearn.utils.resample
# Store the new samples in new_X and new_y
...
# Concatenate the list of arrays to create a single array
new_X = np.concatenate(new_X)
new_y = np.concatenate(new_y)
# Shuffle arrays to randomize sample order
new_X, new_y = sklearn.utils.shuffle(new_X, new_y)
return new_X, new_y
# %% tags=["solution"]
##########################
####### Solution #########
##########################
def balance_classes(X, y):
"""For a given multiclass dataset, upsample underrepresented classes
to match the number of samples in the majority class
Args:
X (np.array): Array of raw data
y (np.array): Array of class labels
Returns:
np.array: X
np.array: y
"""
classes = np.unique(y)
# Identify which class has the most samples
# Hint: use your code for counting the number of samples in each class
cls, n = np.unique(y, return_counts=True)
maj_samples = max(n)
maj_id = np.where(n == maj_samples)[0]
# Collect new samples in an array
new_X, new_y = [], []
for c in classes:
if c == maj_id:
xx = X[y == c]
yy = y[y == c]
else:
# Resample the minority classes to match majority number of samples using sklearn.utils.resample
xx, yy = sklearn.utils.resample(X[y == c], y[y == c], n_samples=maj_samples)
# Store the new samples in new_X and new_y
new_X.append(xx)
new_y.append(yy)
# Concatenate the list of arrays to create a single array
new_X = np.concatenate(new_X)
new_y = np.concatenate(new_y)
# Shuffle arrays to randomize sample order
new_X, new_y = sklearn.utils.shuffle(new_X, new_y)
return new_X, new_y
# %%
X_train, y_train = balance_classes(X_train, y_train)
X_test, y_test = balance_classes(X_test, y_test)
print(f"Train shape: X {X_train.shape}, y {y_train.shape}")
print(f"Test shape: X {X_test.shape}, y {y_test.shape}")
# %%
# Run this cell to check your class balance code
def check_balance(y):
cls, n = np.unique(y, return_counts=True)
max_n = max(n)
if not np.all(n == max_n):
raise ValueError('Sample is not balanced!')
check_balance(y_train)
check_balance(y_test)
# %% [markdown]
"""
### One-hot encoding
Currently, our data have labels that range from 0 to 6. While we know that each of these 7 classes is comparable, this encoding implies that some classes have more weight than others. Alternatively, we want to use a binary encoding so that all classes are seen as equivalent by the model.
Instead of representing each label with a number from 0 to 6, we will use an array of length 7 where each position in the array is a binary value encoding the class.
For example, `5` is encoded as `[0, 0, 0, 0, 1, 0, 0]`
"""
# %% [markdown]
"""
<div class="alert alert-block alert-info">
#### Task 1.3
In order to transform our data from integer to one-hot encoding, we will use `sklearn.preprocessing.LabelBinarizer` ([docs](https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.LabelBinarizer.html#sklearn.preprocessing.LabelBinarizer)). Take a look at the documentation to learn how to initialize and fit the `LabelBinarizer` and add your code below.
</div>
"""
# %%
##########################
######## To Do ###########
##########################
# Initialize and fit the LabelBinarizer
lb = ...
# %% tags=["solution"]
##########################
####### Solution #########
##########################
# Initialize and fit the LabelBinarizer
lb = sklearn.preprocessing.LabelBinarizer()
lb.fit(y_data)
# %%
lb.classes_
# %% [markdown]
"""
Run the following cell to check that you set up the `LabelBinarizer` correctly.
"""
# %%
# Check that the detected classes are correct
print(lb.classes_, "\n")
assert np.all(lb.classes_ == np.arange(7))
# Test a transformation
sample = lb.transform([1, 4])
print(sample)
target = np.zeros((2, 7))
target[0, 1] = 1
target[1, 4] = 1
assert np.all(sample == target)
# %% [markdown]
"""
Now we can apply the transformation to our train and test data.
"""
# %%
y_train = lb.transform(y_train)
y_test = lb.transform(y_test)
# %% [markdown]
"""
<div class="alert alert-success">
## Checkpoint 1
We have completed the data wrangling phase of this exercise
- Reshaping the data to fit our model
- Balancing classes
- Applying one hot encoding
</div>
"""
# %% [markdown] id="2fhlPKpe3x1j"
r"""
## The linear classifier
The linear classifier produces class scores that are a linear function of the pixel values. Mathematically, this can be written as $\vec{y} = W \vec{x}$, where $\vec{y}$ is the vector of class scores, $W$ is a matrix of weights and $\vec{x}$ is the image vector. The shape of the weights matrix is determined by the number of classes and the length of the image vector. In this case $W$ is 7 by 4356. Our learning task is to find a set of weights that maximize our performance on our classification task. We will solve this task by doing the following steps
- Randomly initializing a set of weights
- Defining a loss function that measures our performance on the classification task
- Use stochastic gradient descent to find "optimal" weights
"""
# %% [markdown] id="W0LQ99KR3x1j"
"""
### Create the matrix of weights
Properly initializing weights is essential for getting deep learning methods to work correctly. We are going to start with weights initizalized with zeros, but will invesigate other methods later in the exercise.
Lets create the linear classifier using object oriented programming, which will help with organization.
"""
# %%
class LinearClassifier:
def __init__(self, image_size=image_width * image_width, n_classes=7):
self.image_size = image_size
self.n_classes = n_classes
# Initialize weights
self._initialize_weights()
def _initialize_weights(self):
self.W = np.zeros((self.n_classes, self.image_size))
# %% [markdown] id="7yeA5geI3x1k"
"""
### Apply the softmax transform to complete the model outputs
Our `LinearClassifier` class needs a method to perform predictions - which in our case is performing matrix multiplication and then applying the softmax transform. The softmax functions transforms a vector of arbitrary real numbers and turns it into probabilities that we can use for our final prediction.
![](https://miro.medium.com/max/1400/1*gctBX5YHUUpBEK3MWD6r3Q.png)
(*Image by [Thomas Kurbiel](https://towardsdatascience.com/derivative-of-the-softmax-function-and-the-categorical-cross-entropy-loss-ffceefc081d1)*)
There is an excellent [derivation of the softmax function](https://towardsdatascience.com/derivative-of-the-softmax-function-and-the-categorical-cross-entropy-loss-ffceefc081d1) available on Towards Data Science if you are interested in the details of the math.
"""
# %%
def predict(self, X, epsilon=1e-5):
# Matrix multiplication of X by the weights
y = np.matmul(X, self.W.T)
# Apply softmax - epsilon added for numerical stability
y = np.exp(y) / np.sum(np.exp(y) + epsilon, axis=-1, keepdims=True)
return y
# Assign methods to class
setattr(LinearClassifier, "predict", predict)
# %% [markdown]
"""
Before we train the model, let's take a brief moment to check what the untrained model predictions look like.
"""
# %% colab={"base_uri": "https://localhost:8080/", "height": 1000} id="4tgJymU33x1l" outputId="736e325f-56eb-4bf7-9caf-bc6591c2a448"
lc = LinearClassifier()
fig, axes = plt.subplots(2, 4, figsize=(20, 10))
for i, j in enumerate(np.random.randint(X_test.shape[0], size=(8,))):
# Get an example image
X_sample = X_test[j, ...]
# Reshape flattened vector to image
X_reshape = np.reshape(X_sample, (66, 66))
# Predict the label
y_pred = lc.predict(X_sample)
# Display results
axes.flatten()[i].imshow(X_reshape, cmap="gray")
axes.flatten()[i].set_title(
"Label " + str(np.argmax(y_test[j])) + ", Prediction " + str(np.argmax(y_pred))
)
# %% [markdown] id="1iKRfu3J3x1l"
"""
<div class="alert alert-block alert-info">
#### Task 2.1
What do you notice about the initial results of the model?
</div>
"""
# %% [markdown] tags=["solution"]
"""
The model exclusively predicts a class of 0. This result makes sense given that we initialized the model with weights of 0.
"""
# %% [markdown] id="jv4Rc_xS3x1l"
r"""
## Stochastic gradient descent
To train this model, we will use stochastic gradient descent. In its simplest version, this algorithm consists of the following steps:
- Select several images from the training dataset at random
- Compute the gradient of the loss function with respect to the weights, given the selected images
- Update the weights using the update rule $W_{ij} \rightarrow W_{ij} - lr\frac{\partial loss}{\partial W_{ij}}$
Recall that the origin of this update rule is from multivariable calculus - the gradient tells us the direction in which the loss function increases the most. So to minimize the loss function we move in the opposite direction of the gradient.
Also recall from the course notes that for this problem we can compute the gradient analytically. The gradient is given by
\begin{equation}
\frac{\partial loss}{\partial W_{ij}} = \left(p_i - 1(i \mbox{ is correct}) \right)x_j,
\end{equation}
where $1$ is an indicator function that is 1 if the statement inside the parentheses is true and 0 if it is false.
A complete derivation of $\frac{\partial loss}{\partial W_{ij}}$ is included in the Towards Data Science [article](https://towardsdatascience.com/derivative-of-the-softmax-function-and-the-categorical-cross-entropy-loss-ffceefc081d1) recommended above if you are interested in the details.
"""
# %% id="XXHGyfz33x1l"
def grad(self, X, y_true, y_pred):
# Compute the gradients for each class and save in list
gradients = []
for i in range(self.n_classes):
# Calculate the difference between the class probability and true score
difference = y_pred[..., i] - y_true[..., i]
difference = np.expand_dims(difference, axis=-1)
grad = difference * X
gradients.append(grad)
gradient = np.stack(gradients, axis=1)
return gradient
def loss(self, X, y_true, y_pred):
loss = np.mean(-y_true * np.log(y_pred))
return loss
def fit(self, X_train, y_train, n_epochs, batch_size=1, learning_rate=1e-5):
loss_list = []
rng = np.random.default_rng()
# Iterate over epochs
for epoch in range(n_epochs):
print(f"Epoch {epoch}")
n_batches = int(np.floor(X_train.shape[0] / batch_size))
# Generate random index
index = np.arange(X_train.shape[0])
np.random.shuffle(index)
X_shfl = X_train[index]
y_shfl = y_train[index]
# Iterate over batches
for batch in tqdm.trange(n_batches):
beg = batch * batch_size
end = (
(batch + 1) * batch_size
if (batch + 1) * batch_size < X_train.shape[0]
else -1
)
X_batch = X_shfl[beg:end]
y_batch = y_shfl[beg:end]
# Skip empty batch if it shows up at the end of the epoch
if X_batch.shape[0] == 0:
continue
# Predict
y_pred = self.predict(X_batch)
# Compute the loss
loss = self.loss(X_batch, y_batch, y_pred)
loss_list.append(loss)
# Compute the gradient
gradient = self.grad(X_batch, y_batch, y_pred)
# Compute the mean gradient over all the example images
gradient = np.mean(gradient, axis=0, keepdims=False)
# Update the weights
self.W -= learning_rate * gradient
if np.count_nonzero(np.isnan(self.W)) != 0:
print(epoch, batch)
break
print('Final loss', loss)
return loss_list
# Assign methods to class
setattr(LinearClassifier, "grad", grad)
setattr(LinearClassifier, "loss", loss)
setattr(LinearClassifier, "fit", fit)
# %% [markdown]
"""
We're ready to train our model!
"""
# %%
# %%time
lc = LinearClassifier()
loss_log = lc.fit(X_train, y_train, n_epochs=10, batch_size=16)
# %% [markdown]
"""
Let's plot the loss curve to see how the model trained.
"""
# %%
def smooth(scalars, weight):
"""Compute the exponential moving average to smooth data
Credit: https://stackoverflow.com/questions/42281844/what-is-the-mathematics-behind-the-smoothing-parameter-in-tensorboards-scalar/49357445#49357445
"""
last = scalars[0] # First value in the plot (first timestep)
smoothed = list()
for point in scalars:
smoothed_val = last * weight + (1 - weight) * point # Calculate smoothed value
smoothed.append(smoothed_val) # Save it
last = smoothed_val # Anchor the last smoothed value
return smoothed
# %%
fig, ax = plt.subplots()
ax.plot(smooth(loss_log, 0.9))
ax.set_ylabel("loss")
# %% [markdown] id="Hh-GWNb-3x1m"
"""
## Evaluate the model
"""
# %% colab={"base_uri": "https://localhost:8080/", "height": 1000} id="T_wsv4zW3x1m" outputId="1d5928f9-1acb-43ec-8c58-ad756e538171"
# Visualize some predictions
fig, axes = plt.subplots(2, 4, figsize=(20, 10))
for i, j in enumerate(np.random.randint(X_test.shape[0], size=(8,))):
# Get an example image
X_sample = X_test[j]
# Reshape flattened vector to image
X_reshape = np.reshape(X_sample, (66, 66))
# Predict the label
y_pred = lc.predict(X_sample)
# Display results
axes.flatten()[i].imshow(X_reshape, cmap="gray")
axes.flatten()[i].set_title(
"Label " + str(np.argmax(y_test[j])) + ", Prediction " + str(np.argmax(y_pred))
)
# %% [markdown]
"""
In addition to inspecting the results of individual predictions, we can also look at summary statistics that capture model performance.
"""
# %%
def benchmark_performance(y_true, y_pred):
"""Calculates recall, precision, f1 and a confusion matrix for sample predictions
Args:
y_true (list): List of integers of true class values
y_pred (list): List of integers of predicted class value
Returns:
dict: Dictionary with keys `recall`, `precision`, `f1`, and `cm`
"""
_round = lambda x: round(x, 3)
metrics = {
"recall": _round(sklearn.metrics.recall_score(y_true, y_pred, average="macro")),
"precision": _round(
sklearn.metrics.precision_score(y_true, y_pred, average="macro")
),
"f1": _round(sklearn.metrics.f1_score(y_true, y_pred, average="macro")),
"cm": sklearn.metrics.confusion_matrix(y_true, y_pred, normalize=None),
"cm_norm": sklearn.metrics.confusion_matrix(y_true, y_pred, normalize="true"),
}
return metrics
# %% [markdown]
"""
<div class="alert alert-block alert-info">
#### Task 2.2
For each of the 4 metrics above, describe in your own words what this metric tells you about model performance.
- Recall
- Precision
- F1
- Confusion Matrix
</div>
"""
# %% [markdown]
"""
*Write your answers here*
- Recall
- Precision
- F1 Score
- Confusion Matrix
"""
# %% [markdown] tags=["solution"]
r"""
- Recall -- Measures the ratio of true positives to the total positives that the model should have identified. Captures the ability of the model to find all positive samples. $$\frac{\texttt{true positive}}{\texttt{true positive} + \texttt{false negative}}$$
- Precision -- Measures the ratio of true positives to all positive predictions. Captures the ability of the model to accurately identify positive samples without misclassifying negative samples.
$$\frac{\texttt{true positive}}{\texttt{true positive} + \texttt{false positive}}$$
- F1 -- Summary statistic that captures both precision and recall
$$\frac{2*\texttt{precision}*\texttt{recall}}{\texttt{precision}+\texttt{recall}}$$
- Confusion Matrix -- Captures the exact failure modes of the model by contrasting predicted classes with true classes.
"""
# %%
def plot_metrics(metrics, name, ax=None):
"""Plots a confusion matrix with summary statistics listed above the plot
The annotations on the confusion matrix are the total counts while
the colormap represents those counts normalized to the total true items
in that class.
Args:
metrics (dict): Dictionary output of `benchmark_performance`
name (str): Title for the plot
ax (optional, matplotlib subplot): Subplot axis to plot onto.
If not provided, a new plot is created
classes (optional, list): A list of the classes to label the X and y
axes. Defaults to [0, 1] for a two class problem.
"""
if ax is None:
fig, ax = plt.subplots(figsize=(5, 5))
cb = ax.imshow(metrics["cm_norm"], cmap="Greens", vmin=0, vmax=1)
classes = np.arange(metrics["cm"].shape[0])
ax.set_xticks(range(len(classes)), class_lut)
ax.set_yticks(range(len(classes)), class_lut)
ax.set_xlabel("Predicted Label")
ax.set_ylabel("True Label")
for i in range(len(classes)):
for j in range(len(classes)):
color = "green" if metrics["cm_norm"][i, j] < 0.5 else "white"
ax.annotate(
"{}".format(metrics["cm"][i, j]),
(j, i),
color=color,
va="center",
ha="center",
)
_ = plt.colorbar(cb, ax=ax)
_ = ax.set_title(
"{}\n"
"Recall: {}\n"
"Precision: {}\n"
"F1 Score: {}\n"
"".format(name, metrics["recall"], metrics["precision"], metrics["f1"])
)
# %%
def summarize_performance(model, X_train, y_train, X_test, y_test, title=''):
"""Quick function to generate predictions on the train and test splits,
benchmark and plot the results
"""
# Generate predictions and metrics for training data
y_pred = model.predict(X_train)
# Convert from one hot encoding to original class labels
y_pred = np.argmax(y_pred, axis=-1)
y_true = np.argmax(y_train, axis=-1)
train_metrics = benchmark_performance(y_true, y_pred)
# Generate predictions and metrics for test data
y_pred = model.predict(X_test)
# Convert from one hot encoding to original class labels
y_pred = np.argmax(y_pred, axis=-1)
y_true = np.argmax(y_test, axis=-1)
test_metrics = benchmark_performance(y_true, y_pred)
fig, ax = plt.subplots(1, 2, figsize=(10, 4))
plot_metrics(train_metrics, f"{title} Training", ax[0])
plot_metrics(test_metrics, f"{title} Testing", ax[1])
# %%
summarize_performance(lc, X_train, y_train, X_test, y_test, "Linear Classifier")
# %% [markdown]
"""
<div class="alert alert-block alert-info">
#### Task 2.3
What do you notice about the results after training the model?
</div>
"""
# %% [markdown]
"""
<div class="alert alert-block alert-success">
## Checkpoint 2
We have written a simple linear classifier, trained it and considered a few ways to evaluate model performance. Next we'll look at some other machine learning methods for classification.
</div>
"""
# %% [markdown]
"""
# Part B: Random Forest Classifier
Decisions trees are a useful tool for generating interpretable classification results. As shown in the example below, trees are constructed such that the data is split at each node according to a feature in the data. At the bottom of the tree, the leafs should correspond to a single class such that we can predict the class of the data depending on which leaf it is associated with.
![](https://miro.medium.com/v2/resize:fit:1262/format:webp/1*LMoJmXCsQlciGTEyoSN39g.jpeg)
(*Image by [Tony Yiu](https://towardsdatascience.com/understanding-random-forest-58381e0602d2)*)
A random forest classifer is much like what it sounds. It takes predictions from many different decision trees and assigns the class with the most votes. Ultimately this ensemble method of considering many different trees performs better than any one decision tree.
![](https://miro.medium.com/v2/resize:fit:1052/format:webp/1*VHDtVaDPNepRglIAv72BFg.jpeg)
(*Image by [Tony Yiu](https://towardsdatascience.com/understanding-random-forest-58381e0602d2)*)
In this exercise, we will use `scikit-learn's` implementation of `RandomForestClassifier` ([docs](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html)).
"""
# %%
# %%time
rfc = sklearn.ensemble.RandomForestClassifier()
rfc.fit(X_train, y_train)
# %%
summarize_performance(rfc, X_train, y_train, X_test, y_test, "Random Forest Classifier")
# %% [markdown]
"""
## Parameter Optimization
Our initial random forest classifier was trained using the default parameters provided by `sklearn`, but these often won't be the right values for our problem. In many situations, we may have some idea what a reasonable parameter value might be, but most of the time we will need to perform a grid search to select the optimal value. `sklearn` provides a class `RandomizedSearchCV` ([docs](https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.RandomizedSearchCV.html)) to perform a random search over a provided grid of parameters, which we can use to improve the performance of the random forest classifier.
"""
# %% [markdown]