├── .gitignore
├── codes
├── labs_lecture14
│ └── lab01
│ │ └── data
│ │ └── artificial_dataset.pickle
├── data
│ ├── misc
│ │ ├── profit_population.txt
│ │ └── profit_population_new.txt
│ ├── ptb
│ │ └── generate_ptb.ipynb
│ ├── mnist
│ │ └── generate_mnist.ipynb
│ ├── generate_all_data.py
│ └── fashion-mnist
│ │ └── generate_fashion_mnist.ipynb
├── installation
│ └── installation.ipynb
├── labs_lecture05
│ ├── lab02_epoch
│ │ ├── epoch_demo.ipynb
│ │ ├── epoch_exercise.ipynb
│ │ └── utils.py
│ ├── lab01_mlp
│ │ └── mlp_exercise.ipynb
│ ├── lab03_monitoring_loss
│ │ └── monitoring_loss_exercise.ipynb
│ └── lab04_test_set
│ │ └── test_set_exercise.ipynb
├── labs_lecture02
│ ├── lab02_pytorch_tensor1
│ │ ├── pytorch_tensor_part1_exercise.ipynb
│ │ └── pytorch_tensor_part1_solution.ipynb
│ └── lab03_pytorch_tensor2
│ │ └── pytorch_tensor_part2_exercise.ipynb
├── labs_lecture03
│ ├── lab01_linear_module
│ │ ├── linear_module_exercise.ipynb
│ │ ├── linear_module_solution.ipynb
│ │ └── linear_module_demo.ipynb
│ ├── lab02_softmax
│ │ └── softmax_demo.ipynb
│ ├── lab03_vanilla_nn
│ │ ├── vanilla_nn_exercise.ipynb
│ │ └── vanilla_nn_solution.ipynb
│ ├── lab05_minibatch_training
│ │ ├── minibatch_training_exercise.ipynb
│ │ └── utils.py
│ └── lab04_train_vanilla_nn
│ │ ├── train_vanilla_nn_exercise.ipynb
│ │ └── utils.py
├── labs_lecture08
│ ├── lab02_pool_layer
│ │ └── pool_layer_demo.ipynb
│ └── lab01_conv_layer
│ │ └── conv_layer_demo.ipynb
├── labs_lecture06
│ └── lab02_cifar_multilayer
│ │ └── gpu_demo.ipynb
├── labs_lecture04
│ └── lab01_cross_entropy
│ │ ├── cross_entropy_exercise.ipynb
│ │ └── utils.py
└── labs_lecture10
│ └── lab01_vrnn
│ └── utils.py
├── environment.yml
├── environment_windows.yml
├── LICENSE
└── README.md
/.gitignore:
--------------------------------------------------------------------------------
1 | **/*.DS_Store
2 | **/*.ipynb_checkpoints/
3 | tmp/
4 |
--------------------------------------------------------------------------------
/codes/labs_lecture14/lab01/data/artificial_dataset.pickle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xbresson/CE7454_2020/HEAD/codes/labs_lecture14/lab01/data/artificial_dataset.pickle
--------------------------------------------------------------------------------
/environment.yml:
--------------------------------------------------------------------------------
1 | # Xavier Bresson
2 |
3 | name: deeplearn_course
4 |
5 |
6 | channels:
7 |
8 | - pytorch # DL
9 | - https://conda.anaconda.org/rdkit # Molecule
10 | - dglteam # DGL
11 |
12 | dependencies:
13 |
14 | # basic
15 | - python=3.6
16 | - numpy=1.19.1
17 | - mkl=2020.2
18 | - ipython=7.16.1
19 | - jupyter=1.0.0
20 | - scipy=1.5.2
21 | - pandas=1.1.2
22 | - matplotlib=3.3.1
23 | - scikit-learn=0.23.2
24 | - networkx=2.5
25 | - seaborn=0.11.0
26 |
27 | - pytorch=1.1.0 # DL
28 | - torchvision=0.3.0 # DL
29 | - rdkit=2020.03.3.0 # Molecule
30 | - dgl=0.3 # DGL
31 |
32 | - pip:
33 | - gym==0.17.3 # RL
34 | - tensorboardx==2.1 # TSP
35 | - fastprogress==1.0.0 # TSP
--------------------------------------------------------------------------------
/environment_windows.yml:
--------------------------------------------------------------------------------
1 | # Xavier Bresson
2 |
3 | name: deeplearn_course
4 |
5 |
6 | channels:
7 |
8 | - pytorch # DL
9 | - https://conda.anaconda.org/rdkit # Molecule
10 | - dglteam # DGL
11 | - conda-forge # RL
12 |
13 | dependencies:
14 |
15 | # basic
16 | - python=3.6 #
17 | - numpy=1.19.1
18 | - mkl=2020.2
19 | - ipython=7.16.1
20 | - jupyter=1.0.0
21 | - scipy=1.5.2
22 | - pandas=1.1.2
23 | - matplotlib=3.3.2
24 | - scikit-learn=0.23.2
25 | - networkx=2.5
26 | - seaborn=0.11.0
27 |
28 | - pytorch-cpu=1.1.0 # DL
29 | - torchvision-cpu=0.3.0 # CV
30 | - rdkit=2020.03.2.0 # Molecule
31 | - dgl=0.3 # DGL
32 | - gym=0.17.3 # RL
33 | - tensorboardx=2.1 # TSP
34 | - fastprogress=1.0.0 # TSP
35 |
36 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2020 Xavier Bresson
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/codes/data/misc/profit_population.txt:
--------------------------------------------------------------------------------
1 | 6.1101,17.592
2 | 5.5277,9.1302
3 | 8.5186,13.662
4 | 7.0032,11.854
5 | 5.8598,6.8233
6 | 8.3829,11.886
7 | 7.4764,4.3483
8 | 8.5781,12
9 | 6.4862,6.5987
10 | 5.0546,3.8166
11 | 5.7107,3.2522
12 | 14.164,15.505
13 | 5.734,3.1551
14 | 8.4084,7.2258
15 | 5.6407,0.71618
16 | 5.3794,3.5129
17 | 6.3654,5.3048
18 | 5.1301,0.56077
19 | 6.4296,3.6518
20 | 7.0708,5.3893
21 | 6.1891,3.1386
22 | 20.27,21.767
23 | 5.4901,4.263
24 | 6.3261,5.1875
25 | 5.5649,3.0825
26 | 18.945,22.638
27 | 12.828,13.501
28 | 10.957,7.0467
29 | 13.176,14.692
30 | 22.203,24.147
31 | 5.2524,-1.22
32 | 6.5894,5.9966
33 | 9.2482,12.134
34 | 5.8918,1.8495
35 | 8.2111,6.5426
36 | 7.9334,4.5623
37 | 8.0959,4.1164
38 | 5.6063,3.3928
39 | 12.836,10.117
40 | 6.3534,5.4974
41 | 5.4069,0.55657
42 | 6.8825,3.9115
43 | 11.708,5.3854
44 | 5.7737,2.4406
45 | 7.8247,6.7318
46 | 7.0931,1.0463
47 | 5.0702,5.1337
48 | 5.8014,1.844
49 | 11.7,8.0043
50 | 5.5416,1.0179
51 | 7.5402,6.7504
52 | 5.3077,1.8396
53 | 7.4239,4.2885
54 | 7.6031,4.9981
55 | 6.3328,1.4233
56 | 6.3589,-1.4211
57 | 6.2742,2.4756
58 | 5.6397,4.6042
59 | 9.3102,3.9624
60 | 9.4536,5.4141
61 | 8.8254,5.1694
62 | 5.1793,-0.74279
63 | 21.279,17.929
64 | 14.908,12.054
65 | 18.959,17.054
66 | 7.2182,4.8852
67 | 8.2951,5.7442
68 | 10.236,7.7754
69 | 5.4994,1.0173
70 | 20.341,20.992
71 | 10.136,6.6799
72 | 7.3345,4.0259
73 | 6.0062,1.2784
74 | 7.2259,3.3411
75 | 5.0269,-2.6807
76 | 6.5479,0.29678
77 | 7.5386,3.8845
78 | 5.0365,5.7014
79 | 10.274,6.7526
80 | 5.1077,2.0576
81 | 5.7292,0.47953
82 | 5.1884,0.20421
83 | 6.3557,0.67861
84 | 9.7687,7.5435
85 | 6.5159,5.3436
86 | 8.5172,4.2415
87 | 9.1802,6.7981
88 | 6.002,0.92695
89 | 5.5204,0.152
90 | 5.0594,2.8214
91 | 5.7077,1.8451
92 | 7.6366,4.2959
93 | 5.8707,7.2029
94 | 5.3054,1.9869
95 | 8.2934,0.14454
96 | 13.394,9.0551
97 | 5.4369,0.61705
98 |
--------------------------------------------------------------------------------
/codes/data/misc/profit_population_new.txt:
--------------------------------------------------------------------------------
1 | 12.22020,35.18400
2 | 11.05540,18.26040
3 | 17.03720,27.32400
4 | 14.00640,23.70800
5 | 11.71960,13.64660
6 | 16.76580,23.77200
7 | 14.95280,8.69660
8 | 17.15620,24.00000
9 | 12.97240,13.19740
10 | 10.10920,7.63320
11 | 11.42140,6.50440
12 | 28.32800,31.01000
13 | 11.46800,6.31020
14 | 16.81680,14.45160
15 | 11.28140,1.43236
16 | 10.75880,7.02580
17 | 12.73080,10.60960
18 | 10.26020,1.12154
19 | 12.85920,7.30360
20 | 14.14160,10.77860
21 | 12.37820,6.27720
22 | 40.54000,43.53400
23 | 10.98020,8.52600
24 | 12.65220,10.37500
25 | 11.12980,6.16500
26 | 37.89000,45.27600
27 | 25.65600,27.00200
28 | 21.91400,14.09340
29 | 26.35200,29.38400
30 | 44.40600,48.29400
31 | 10.50480,-2.44000
32 | 13.17880,11.99320
33 | 18.49640,24.26800
34 | 11.78360,3.69900
35 | 16.42220,13.08520
36 | 15.86680,9.12460
37 | 16.19180,8.23280
38 | 11.21260,6.78560
39 | 25.67200,20.23400
40 | 12.70680,10.99480
41 | 10.81380,1.11314
42 | 13.76500,7.82300
43 | 23.41600,10.77080
44 | 11.54740,4.88120
45 | 15.64940,13.46360
46 | 14.18620,2.09260
47 | 10.14040,10.26740
48 | 11.60280,3.68800
49 | 23.40000,16.00860
50 | 11.08320,2.03580
51 | 15.08040,13.50080
52 | 10.61540,3.67920
53 | 14.84780,8.57700
54 | 15.20620,9.99620
55 | 12.66560,2.84660
56 | 12.71780,-2.84220
57 | 12.54840,4.95120
58 | 11.27940,9.20840
59 | 18.62040,7.92480
60 | 18.90720,10.82820
61 | 17.65080,10.33880
62 | 10.35860,-1.48558
63 | 42.55800,35.85800
64 | 29.81600,24.10800
65 | 37.91800,34.10800
66 | 14.43640,9.77040
67 | 16.59020,11.48840
68 | 20.47200,15.55080
69 | 10.99880,2.03460
70 | 40.68200,41.98400
71 | 20.27200,13.35980
72 | 14.66900,8.05180
73 | 12.01240,2.55680
74 | 14.45180,6.68220
75 | 10.05380,-5.36140
76 | 13.09580,0.59356
77 | 15.07720,7.76900
78 | 10.07300,11.40280
79 | 20.54800,13.50520
80 | 10.21540,4.11520
81 | 11.45840,0.95906
82 | 10.37680,0.40842
83 | 12.71140,1.35722
84 | 19.53740,15.08700
85 | 13.03180,10.68720
86 | 17.03440,8.48300
87 | 18.36040,13.59620
88 | 12.00400,1.85390
89 | 11.04080,0.30400
90 | 10.11880,5.64280
91 | 11.41540,3.69020
92 | 15.27320,8.59180
93 | 11.74140,14.40580
94 | 10.61080,3.97380
95 | 16.58680,0.28908
96 | 26.78800,18.11020
97 | 10.87380,1.23410
98 |
--------------------------------------------------------------------------------
/codes/installation/installation.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Installation Instructions"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": null,
13 | "metadata": {},
14 | "outputs": [],
15 | "source": [
16 | "# Git pull the codes from github to your colab machine\n",
17 | "# You will have this message:\n",
18 | " # Warning: This notebook was not authored by Google.\n",
19 | " # Click RUN ANYWAY\n",
20 | "!git clone https://github.com/xbresson/CE7454_2020.git"
21 | ]
22 | },
23 | {
24 | "cell_type": "code",
25 | "execution_count": null,
26 | "metadata": {},
27 | "outputs": [],
28 | "source": [
29 | "# Mount Google Drive to the colab machine\n",
30 | "# You will have this message:\n",
31 | " # Go to this URL in a browser: ---\n",
32 | " # Follow the URL, sign-in to Google login\n",
33 | " # ALLOW google drive to access to your google account \n",
34 | " # Copy-paste the code to the notebook\n",
35 | " # Enter your authorization code: ---\n",
36 | "from google.colab import drive\n",
37 | "drive.mount('/content/gdrive')\n",
38 | "\n",
39 | "# Copy github folder from colab machine to your google drive\n",
40 | "!cp -R /content/CE7454_2020 /content/gdrive/My\\ Drive/CE7454_2020_codes\n",
41 | "!rm -R /content/CE7454_2020"
42 | ]
43 | },
44 | {
45 | "cell_type": "code",
46 | "execution_count": null,
47 | "metadata": {},
48 | "outputs": [],
49 | "source": [
50 | "# Installation is done.\n",
51 | "# You can close this notebook."
52 | ]
53 | }
54 | ],
55 | "metadata": {
56 | "kernelspec": {
57 | "display_name": "Python 3",
58 | "language": "python",
59 | "name": "python3"
60 | },
61 | "language_info": {
62 | "codemirror_mode": {
63 | "name": "ipython",
64 | "version": 3
65 | },
66 | "file_extension": ".py",
67 | "mimetype": "text/x-python",
68 | "name": "python",
69 | "nbconvert_exporter": "python",
70 | "pygments_lexer": "ipython3",
71 | "version": "3.7.4"
72 | }
73 | },
74 | "nbformat": 4,
75 | "nbformat_minor": 2
76 | }
77 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # CE7454_2020
2 | Deep learning course CE7454, 2020
3 |
4 |
5 |
6 |
7 |
8 | ### Cloud Machine #1 : Google Colab (Free GPU)
9 |
10 | * Follow this Notebook installation :
11 | https://colab.research.google.com/github/xbresson/CE7454_2020/blob/master/codes/installation/installation.ipynb
12 |
13 | * Open your Google Drive :
14 | https://www.google.com/drive
15 |
16 | * Open in Google Drive Folder 'CE7454_2020' and go to Folder 'CE7454_2020/codes/'
17 | Select the notebook 'file.ipynb' and open it with Google Colab using Control Click + Open With Colaboratory
18 |
19 |
20 |
21 |
22 |
23 | ### Cloud Machine #2 : Binder (No GPU)
24 |
25 | * Simply [click here]
26 |
27 | [Click here]: https://mybinder.org/v2/gh/xbresson/CE7454_2020/master
28 |
29 |
30 |
31 |
32 |
33 | ### Local Installation for OSX & Linux
34 |
35 | * Open a Terminal and type
36 |
37 |
38 | ```sh
39 | # Conda installation
40 | curl -o ~/miniconda.sh -O https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh # Linux
41 | curl -o ~/miniconda.sh -O https://repo.continuum.io/miniconda/Miniconda3-latest-MacOSX-x86_64.sh # OSX
42 | chmod +x ~/miniconda.sh
43 | ./miniconda.sh
44 | source ~/.bashrc
45 |
46 | # Clone GitHub repo
47 | git clone https://github.com/xbresson/CE7454_2020.git
48 | cd CE7454_2020
49 |
50 | # Install python libraries
51 | conda env create -f environment.yml
52 | source activate deeplearn_course
53 |
54 | # Run the notebooks
55 | jupyter notebook
56 | ```
57 |
58 |
59 |
60 |
61 | ### Local Installation for Windows
62 |
63 | ```sh
64 | # Install Anaconda
65 | https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe
66 |
67 | # Open an Anaconda Terminal
68 | Go to Application => Anaconda3 => Anaconda Prompt
69 |
70 | # Install git : Type in terminal
71 | conda install git
72 |
73 | # Clone GitHub repo
74 | git clone https://github.com/xbresson/CE7454_2020.git
75 | cd CE7454_2020
76 |
77 | # Install python libraries
78 | conda env create -f environment_windows.yml
79 | conda activate deeplearn_course
80 |
81 | # Run the notebooks
82 | jupyter notebook
83 | ```
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
--------------------------------------------------------------------------------
/codes/labs_lecture05/lab02_epoch/epoch_demo.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Lab 02 : Training with epochs -- demo"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": 1,
13 | "metadata": {},
14 | "outputs": [],
15 | "source": [
16 | "import torch"
17 | ]
18 | },
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {},
22 | "source": [
23 | "### Lets make an artificial training set of 10 images (28x28 pixels)"
24 | ]
25 | },
26 | {
27 | "cell_type": "code",
28 | "execution_count": 2,
29 | "metadata": {},
30 | "outputs": [
31 | {
32 | "name": "stdout",
33 | "output_type": "stream",
34 | "text": [
35 | "torch.Size([10, 28, 28])\n"
36 | ]
37 | }
38 | ],
39 | "source": [
40 | "train_data = torch.rand(10,28,28)\n",
41 | "\n",
42 | "print(train_data.size())"
43 | ]
44 | },
45 | {
46 | "cell_type": "markdown",
47 | "metadata": {},
48 | "source": [
49 | "### Lets define a the random order in which we are going to visit these images:"
50 | ]
51 | },
52 | {
53 | "cell_type": "code",
54 | "execution_count": 3,
55 | "metadata": {},
56 | "outputs": [
57 | {
58 | "name": "stdout",
59 | "output_type": "stream",
60 | "text": [
61 | "tensor([3, 8, 0, 9, 7, 5, 1, 4, 2, 6])\n"
62 | ]
63 | }
64 | ],
65 | "source": [
66 | "shuffled_indices = torch.randperm(10)\n",
67 | "print(shuffled_indices)"
68 | ]
69 | },
70 | {
71 | "cell_type": "markdown",
72 | "metadata": {},
73 | "source": [
74 | "### Visit the training set in this random order and do minibatch of size 2"
75 | ]
76 | },
77 | {
78 | "cell_type": "code",
79 | "execution_count": 4,
80 | "metadata": {},
81 | "outputs": [
82 | {
83 | "name": "stdout",
84 | "output_type": "stream",
85 | "text": [
86 | "tensor([3, 8])\n",
87 | "tensor([0, 9])\n",
88 | "tensor([7, 5])\n",
89 | "tensor([1, 4])\n",
90 | "tensor([2, 6])\n"
91 | ]
92 | }
93 | ],
94 | "source": [
95 | "bs=2\n",
96 | "\n",
97 | "for count in range(0,10,bs):\n",
98 | " \n",
99 | " batch_of_indices = shuffled_indices[count:count+bs]\n",
100 | " \n",
101 | " print(batch_of_indices)\n",
102 | " \n",
103 | " batch_of_images = train_data[ batch_of_indices ]"
104 | ]
105 | },
106 | {
107 | "cell_type": "code",
108 | "execution_count": null,
109 | "metadata": {},
110 | "outputs": [],
111 | "source": []
112 | }
113 | ],
114 | "metadata": {
115 | "kernelspec": {
116 | "display_name": "Python 3",
117 | "language": "python",
118 | "name": "python3"
119 | },
120 | "language_info": {
121 | "codemirror_mode": {
122 | "name": "ipython",
123 | "version": 3
124 | },
125 | "file_extension": ".py",
126 | "mimetype": "text/x-python",
127 | "name": "python",
128 | "nbconvert_exporter": "python",
129 | "pygments_lexer": "ipython3",
130 | "version": "3.6.10"
131 | }
132 | },
133 | "nbformat": 4,
134 | "nbformat_minor": 2
135 | }
136 |
--------------------------------------------------------------------------------
/codes/labs_lecture02/lab02_pytorch_tensor1/pytorch_tensor_part1_exercise.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Lab 02: Manipulate PyTorch Tensors -- exercise"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": null,
13 | "metadata": {},
14 | "outputs": [],
15 | "source": [
16 | "import torch"
17 | ]
18 | },
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {},
22 | "source": [
23 | "### Make the matrices A and B below. Add them together to obtain a matrix C. Print these three matrices.\n",
24 | "$$\n",
25 | "A =\\begin{bmatrix}\n",
26 | "1 & 2 \\\\ 3 & 4\n",
27 | "\\end{bmatrix} \n",
28 | "\\qquad \n",
29 | "B =\\begin{bmatrix}\n",
30 | "10 & 20 \\\\ 30 & 40\n",
31 | "\\end{bmatrix} \\qquad C=A+B =?\n",
32 | "$$"
33 | ]
34 | },
35 | {
36 | "cell_type": "code",
37 | "execution_count": null,
38 | "metadata": {},
39 | "outputs": [],
40 | "source": [
41 | "\n",
42 | "# write your code here\n",
43 | "\n"
44 | ]
45 | },
46 | {
47 | "cell_type": "markdown",
48 | "metadata": {},
49 | "source": [
50 | "### Print the dimension, size and type of the matrix A. Remember, the commands are dim(), size() and type()"
51 | ]
52 | },
53 | {
54 | "cell_type": "code",
55 | "execution_count": null,
56 | "metadata": {},
57 | "outputs": [],
58 | "source": [
59 | "\n",
60 | "# write your code here\n",
61 | "\n"
62 | ]
63 | },
64 | {
65 | "cell_type": "markdown",
66 | "metadata": {},
67 | "source": [
68 | "### Convert the matrix A to be an integer matrix (type LongTensor). Remember, the command is long(). Then print the type to check it was indeed converted."
69 | ]
70 | },
71 | {
72 | "cell_type": "code",
73 | "execution_count": null,
74 | "metadata": {},
75 | "outputs": [],
76 | "source": [
77 | "\n",
78 | "# write your code here\n",
79 | "\n"
80 | ]
81 | },
82 | {
83 | "cell_type": "markdown",
84 | "metadata": {},
85 | "source": [
86 | "### Make a random 5 x 2 x 3 Tensor. The command is torch.rand. Then do the following: 1) Print the tensor, 2) Print its type, 3) Print its dimension, 4) Print its size, 5) Print the size of its middle dimension."
87 | ]
88 | },
89 | {
90 | "cell_type": "code",
91 | "execution_count": null,
92 | "metadata": {},
93 | "outputs": [],
94 | "source": [
95 | "\n",
96 | "# write your code here\n",
97 | "\n"
98 | ]
99 | },
100 | {
101 | "cell_type": "markdown",
102 | "metadata": {},
103 | "source": [
104 | "### Make 2 x 3 x 4 x 5 tensor filled with zeros then print it. (The command is torch.zeros). See if you can make sense of the display."
105 | ]
106 | },
107 | {
108 | "cell_type": "code",
109 | "execution_count": null,
110 | "metadata": {},
111 | "outputs": [],
112 | "source": [
113 | "\n",
114 | "# write your code here\n",
115 | "\n"
116 | ]
117 | },
118 | {
119 | "cell_type": "code",
120 | "execution_count": null,
121 | "metadata": {},
122 | "outputs": [],
123 | "source": []
124 | }
125 | ],
126 | "metadata": {
127 | "kernelspec": {
128 | "display_name": "Python 3",
129 | "language": "python",
130 | "name": "python3"
131 | },
132 | "language_info": {
133 | "codemirror_mode": {
134 | "name": "ipython",
135 | "version": 3
136 | },
137 | "file_extension": ".py",
138 | "mimetype": "text/x-python",
139 | "name": "python",
140 | "nbconvert_exporter": "python",
141 | "pygments_lexer": "ipython3",
142 | "version": "3.6.10"
143 | }
144 | },
145 | "nbformat": 4,
146 | "nbformat_minor": 2
147 | }
148 |
--------------------------------------------------------------------------------
/codes/labs_lecture03/lab01_linear_module/linear_module_exercise.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Lab 01 : Linear module -- exercise\n",
8 | "\n",
9 | "# Inspecting a linear module"
10 | ]
11 | },
12 | {
13 | "cell_type": "code",
14 | "execution_count": null,
15 | "metadata": {},
16 | "outputs": [],
17 | "source": [
18 | "import torch\n",
19 | "import torch.nn as nn"
20 | ]
21 | },
22 | {
23 | "cell_type": "markdown",
24 | "metadata": {},
25 | "source": [
26 | "### Make a linear module WITHOUT bias that takes input of size 2 and return output of size 3. "
27 | ]
28 | },
29 | {
30 | "cell_type": "code",
31 | "execution_count": null,
32 | "metadata": {},
33 | "outputs": [],
34 | "source": [
35 | "mod= # complete here\n",
36 | "print(mod)"
37 | ]
38 | },
39 | {
40 | "cell_type": "markdown",
41 | "metadata": {},
42 | "source": [
43 | "### Print the internal parameters of the module. Try to print the bias and double check that it return you \"None\""
44 | ]
45 | },
46 | {
47 | "cell_type": "code",
48 | "execution_count": null,
49 | "metadata": {},
50 | "outputs": [],
51 | "source": [
52 | "print( # complete here )\n",
53 | "print( # complete here )"
54 | ]
55 | },
56 | {
57 | "cell_type": "markdown",
58 | "metadata": {},
59 | "source": [
60 | "### Make a vector $x=\\begin{bmatrix}1\\\\1 \\end{bmatrix}$ and feed it to your linear module. What is the output?"
61 | ]
62 | },
63 | {
64 | "cell_type": "code",
65 | "execution_count": null,
66 | "metadata": {},
67 | "outputs": [],
68 | "source": [
69 | "x= # complete here \n",
70 | "print(x)\n",
71 | "y= # complete here \n",
72 | "print(y)"
73 | ]
74 | },
75 | {
76 | "cell_type": "markdown",
77 | "metadata": {},
78 | "source": [
79 | "### Change the internal parameters of your module so that the weights become $\n",
80 | "W=\\begin{bmatrix}\n",
81 | "1&2 \\\\ 3&4 \\\\ 5&6\n",
82 | "\\end{bmatrix}\n",
83 | "$. You need to write 6 lines, one per entry to be changed."
84 | ]
85 | },
86 | {
87 | "cell_type": "code",
88 | "execution_count": null,
89 | "metadata": {},
90 | "outputs": [],
91 | "source": [
92 | " # complete here \n",
93 | " # complete here \n",
94 | " # complete here \n",
95 | " # complete here \n",
96 | " # complete here \n",
97 | " # complete here \n",
98 | "print(mod.weight)"
99 | ]
100 | },
101 | {
102 | "cell_type": "markdown",
103 | "metadata": {},
104 | "source": [
105 | "### Feed the vector x to your module with updated parameters. What is the output? "
106 | ]
107 | },
108 | {
109 | "cell_type": "code",
110 | "execution_count": null,
111 | "metadata": {},
112 | "outputs": [],
113 | "source": [
114 | "y= # complete here \n",
115 | "print(y)"
116 | ]
117 | },
118 | {
119 | "cell_type": "markdown",
120 | "metadata": {},
121 | "source": [
122 | "### Compute by hand $y=Wx$ and check that pytorch got it correctly. \n",
123 | "\n",
124 | "$$\n",
125 | "\\begin{bmatrix}\n",
126 | "1&2 \\\\ 3&4 \\\\ 5&6\n",
127 | "\\end{bmatrix}\n",
128 | "\\begin{bmatrix}1\\\\1 \\end{bmatrix}=\n",
129 | "\\begin{bmatrix}?\\\\? \\\\ ? \\end{bmatrix}\n",
130 | "$$"
131 | ]
132 | },
133 | {
134 | "cell_type": "code",
135 | "execution_count": null,
136 | "metadata": {},
137 | "outputs": [],
138 | "source": []
139 | }
140 | ],
141 | "metadata": {
142 | "kernelspec": {
143 | "display_name": "Python 3",
144 | "language": "python",
145 | "name": "python3"
146 | },
147 | "language_info": {
148 | "codemirror_mode": {
149 | "name": "ipython",
150 | "version": 3
151 | },
152 | "file_extension": ".py",
153 | "mimetype": "text/x-python",
154 | "name": "python",
155 | "nbconvert_exporter": "python",
156 | "pygments_lexer": "ipython3",
157 | "version": "3.6.8"
158 | }
159 | },
160 | "nbformat": 4,
161 | "nbformat_minor": 2
162 | }
163 |
--------------------------------------------------------------------------------
/codes/labs_lecture08/lab02_pool_layer/pool_layer_demo.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Lab 02 : Pooling layer - demo"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": 1,
13 | "metadata": {},
14 | "outputs": [],
15 | "source": [
16 | "import torch\n",
17 | "import torch.nn as nn"
18 | ]
19 | },
20 | {
21 | "cell_type": "markdown",
22 | "metadata": {},
23 | "source": [
24 | "### Make a pooling module\n",
25 | "* inputs: activation maps of size n x n\n",
26 | "* output: activation maps of size n/p x n/p\n",
27 | "* p: pooling size"
28 | ]
29 | },
30 | {
31 | "cell_type": "code",
32 | "execution_count": 2,
33 | "metadata": {},
34 | "outputs": [],
35 | "source": [
36 | "mod = nn.MaxPool2d(2,2)"
37 | ]
38 | },
39 | {
40 | "cell_type": "markdown",
41 | "metadata": {},
42 | "source": [
43 | "### Make an input 2 x 6 x 6 (two channels, each one has 6 x 6 pixels )"
44 | ]
45 | },
46 | {
47 | "cell_type": "code",
48 | "execution_count": 8,
49 | "metadata": {},
50 | "outputs": [
51 | {
52 | "name": "stdout",
53 | "output_type": "stream",
54 | "text": [
55 | "tensor([[[[0.6796, 0.0749, 0.5749, 0.5729, 0.3750, 0.9115],\n",
56 | " [0.0087, 0.2287, 0.1944, 0.5500, 0.9770, 0.0556],\n",
57 | " [0.2348, 0.4120, 0.8975, 0.8404, 0.1589, 0.7432],\n",
58 | " [0.7057, 0.1542, 0.8852, 0.1988, 0.2206, 0.2997],\n",
59 | " [0.6186, 0.6267, 0.1895, 0.5063, 0.0490, 0.9198],\n",
60 | " [0.1485, 0.1590, 0.0135, 0.9169, 0.1770, 0.9111]],\n",
61 | "\n",
62 | " [[0.9578, 0.7230, 0.8536, 0.0845, 0.8924, 0.4143],\n",
63 | " [0.8960, 0.1241, 0.8723, 0.3279, 0.6405, 0.8009],\n",
64 | " [0.4433, 0.3989, 0.8508, 0.8352, 0.5912, 0.4906],\n",
65 | " [0.7956, 0.3493, 0.9055, 0.5975, 0.6417, 0.7784],\n",
66 | " [0.8065, 0.9513, 0.0501, 0.2112, 0.8114, 0.8546],\n",
67 | " [0.3578, 0.3014, 0.4694, 0.0160, 0.3717, 0.1823]]]])\n",
68 | "torch.Size([1, 2, 6, 6])\n"
69 | ]
70 | }
71 | ],
72 | "source": [
73 | "bs=1\n",
74 | "\n",
75 | "x=torch.rand(bs,2,6,6)\n",
76 | "\n",
77 | "\n",
78 | "print(x)\n",
79 | "print(x.size())"
80 | ]
81 | },
82 | {
83 | "cell_type": "markdown",
84 | "metadata": {},
85 | "source": [
86 | "### Feed it to the pooling layer: the output size should be divided by 2 "
87 | ]
88 | },
89 | {
90 | "cell_type": "code",
91 | "execution_count": 9,
92 | "metadata": {},
93 | "outputs": [
94 | {
95 | "name": "stdout",
96 | "output_type": "stream",
97 | "text": [
98 | "tensor([[[[0.6796, 0.5749, 0.9770],\n",
99 | " [0.7057, 0.8975, 0.7432],\n",
100 | " [0.6267, 0.9169, 0.9198]],\n",
101 | "\n",
102 | " [[0.9578, 0.8723, 0.8924],\n",
103 | " [0.7956, 0.9055, 0.7784],\n",
104 | " [0.9513, 0.4694, 0.8546]]]])\n",
105 | "torch.Size([1, 2, 3, 3])\n"
106 | ]
107 | }
108 | ],
109 | "source": [
110 | "y=mod(x)\n",
111 | "\n",
112 | "print(y)\n",
113 | "print(y.size())"
114 | ]
115 | },
116 | {
117 | "cell_type": "code",
118 | "execution_count": null,
119 | "metadata": {},
120 | "outputs": [],
121 | "source": []
122 | },
123 | {
124 | "cell_type": "code",
125 | "execution_count": null,
126 | "metadata": {},
127 | "outputs": [],
128 | "source": []
129 | }
130 | ],
131 | "metadata": {
132 | "kernelspec": {
133 | "display_name": "Python 3",
134 | "language": "python",
135 | "name": "python3"
136 | },
137 | "language_info": {
138 | "codemirror_mode": {
139 | "name": "ipython",
140 | "version": 3
141 | },
142 | "file_extension": ".py",
143 | "mimetype": "text/x-python",
144 | "name": "python",
145 | "nbconvert_exporter": "python",
146 | "pygments_lexer": "ipython3",
147 | "version": "3.6.10"
148 | }
149 | },
150 | "nbformat": 4,
151 | "nbformat_minor": 2
152 | }
153 |
--------------------------------------------------------------------------------
/codes/labs_lecture03/lab02_softmax/softmax_demo.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Lab 02 : Softmax function -- demo"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": 1,
13 | "metadata": {},
14 | "outputs": [],
15 | "source": [
16 | "import torch\n",
17 | "import torch.nn.functional as F"
18 | ]
19 | },
20 | {
21 | "cell_type": "markdown",
22 | "metadata": {},
23 | "source": [
24 | "### Make a vector"
25 | ]
26 | },
27 | {
28 | "cell_type": "code",
29 | "execution_count": 3,
30 | "metadata": {},
31 | "outputs": [
32 | {
33 | "name": "stdout",
34 | "output_type": "stream",
35 | "text": [
36 | "tensor([-2.0000, -0.5000, 2.0000, 1.5000])\n",
37 | "torch.Size([4])\n"
38 | ]
39 | }
40 | ],
41 | "source": [
42 | "x=torch.Tensor([ -2 , -0.5 , 2.0 , 1.5 ])\n",
43 | "print(x)\n",
44 | "print(x.size())"
45 | ]
46 | },
47 | {
48 | "cell_type": "markdown",
49 | "metadata": {},
50 | "source": [
51 | "### Feed it to the softmax"
52 | ]
53 | },
54 | {
55 | "cell_type": "code",
56 | "execution_count": 6,
57 | "metadata": {},
58 | "outputs": [
59 | {
60 | "name": "stdout",
61 | "output_type": "stream",
62 | "text": [
63 | "tensor([0.0107, 0.0481, 0.5858, 0.3553])\n",
64 | "torch.Size([4])\n"
65 | ]
66 | }
67 | ],
68 | "source": [
69 | "p=F.softmax(x , dim=0)\n",
70 | "print(p)\n",
71 | "print(p.size())"
72 | ]
73 | },
74 | {
75 | "cell_type": "markdown",
76 | "metadata": {},
77 | "source": [
78 | "### Check that it sums to one"
79 | ]
80 | },
81 | {
82 | "cell_type": "code",
83 | "execution_count": 4,
84 | "metadata": {},
85 | "outputs": [
86 | {
87 | "name": "stdout",
88 | "output_type": "stream",
89 | "text": [
90 | "tensor(1.)\n"
91 | ]
92 | }
93 | ],
94 | "source": [
95 | "print( p.sum() )"
96 | ]
97 | },
98 | {
99 | "cell_type": "markdown",
100 | "metadata": {},
101 | "source": [
102 | "### Make a matrix a matrix"
103 | ]
104 | },
105 | {
106 | "cell_type": "code",
107 | "execution_count": 5,
108 | "metadata": {},
109 | "outputs": [
110 | {
111 | "name": "stdout",
112 | "output_type": "stream",
113 | "text": [
114 | "tensor([[-2.0000, -0.5000, 2.0000, 1.5000],\n",
115 | " [-2.0000, -0.5000, 7.0000, 1.5000]])\n"
116 | ]
117 | }
118 | ],
119 | "source": [
120 | "A=torch.Tensor( [ [ -2 , -0.5 , 2.0 , 1.5 ] , [ -2 , -0.5 , 7.0 , 1.5 ] ])\n",
121 | "print(A)"
122 | ]
123 | },
124 | {
125 | "cell_type": "markdown",
126 | "metadata": {},
127 | "source": [
128 | "### Take the softmax over the rows (dim=1)"
129 | ]
130 | },
131 | {
132 | "cell_type": "code",
133 | "execution_count": 6,
134 | "metadata": {},
135 | "outputs": [
136 | {
137 | "name": "stdout",
138 | "output_type": "stream",
139 | "text": [
140 | "tensor([[0.0107, 0.0481, 0.5858, 0.3553],\n",
141 | " [0.0001, 0.0006, 0.9953, 0.0041]])\n"
142 | ]
143 | }
144 | ],
145 | "source": [
146 | "p = F.softmax(A , dim=1)\n",
147 | "print(p)"
148 | ]
149 | },
150 | {
151 | "cell_type": "markdown",
152 | "metadata": {},
153 | "source": [
154 | "### Take the softmax over the columns (dim=0)"
155 | ]
156 | },
157 | {
158 | "cell_type": "code",
159 | "execution_count": 7,
160 | "metadata": {},
161 | "outputs": [
162 | {
163 | "name": "stdout",
164 | "output_type": "stream",
165 | "text": [
166 | "tensor([[0.5000, 0.5000, 0.0067, 0.5000],\n",
167 | " [0.5000, 0.5000, 0.9933, 0.5000]])\n"
168 | ]
169 | }
170 | ],
171 | "source": [
172 | "p = F.softmax(A , dim=0)\n",
173 | "print(p)"
174 | ]
175 | },
176 | {
177 | "cell_type": "code",
178 | "execution_count": null,
179 | "metadata": {},
180 | "outputs": [],
181 | "source": []
182 | }
183 | ],
184 | "metadata": {
185 | "kernelspec": {
186 | "display_name": "Python 3",
187 | "language": "python",
188 | "name": "python3"
189 | },
190 | "language_info": {
191 | "codemirror_mode": {
192 | "name": "ipython",
193 | "version": 3
194 | },
195 | "file_extension": ".py",
196 | "mimetype": "text/x-python",
197 | "name": "python",
198 | "nbconvert_exporter": "python",
199 | "pygments_lexer": "ipython3",
200 | "version": "3.6.10"
201 | }
202 | },
203 | "nbformat": 4,
204 | "nbformat_minor": 2
205 | }
206 |
--------------------------------------------------------------------------------
/codes/labs_lecture03/lab03_vanilla_nn/vanilla_nn_exercise.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Lab 03 : Vanilla neural networks -- exercise\n",
8 | "\n",
9 | "# Creating a one-layer network"
10 | ]
11 | },
12 | {
13 | "cell_type": "code",
14 | "execution_count": null,
15 | "metadata": {},
16 | "outputs": [],
17 | "source": [
18 | "import torch\n",
19 | "import torch.nn as nn\n",
20 | "import torch.nn.functional as F"
21 | ]
22 | },
23 | {
24 | "cell_type": "markdown",
25 | "metadata": {},
26 | "source": [
27 | "### Make a class for a one layer network. Let's call the layer \"mylayer\". And let's give it a bias."
28 | ]
29 | },
30 | {
31 | "cell_type": "code",
32 | "execution_count": null,
33 | "metadata": {},
34 | "outputs": [],
35 | "source": [
36 | "class one_layer_net(nn.Module):\n",
37 | "\n",
38 | " def __init__(self, input_size, output_size ):\n",
39 | " super(one_layer_net , self).__init__()\n",
40 | " \n",
41 | " # complete here\n",
42 | " \n",
43 | " \n",
44 | " def forward(self, x):\n",
45 | " \n",
46 | " # complete here\n",
47 | " # complete here\n",
48 | " \n",
49 | " return p"
50 | ]
51 | },
52 | {
53 | "cell_type": "markdown",
54 | "metadata": {},
55 | "source": [
56 | "### Create an instance of a one layer net that take input of size 2 and return output of size 2"
57 | ]
58 | },
59 | {
60 | "cell_type": "code",
61 | "execution_count": null,
62 | "metadata": {},
63 | "outputs": [],
64 | "source": [
65 | "net= # complete here\n",
66 | "print(net)"
67 | ]
68 | },
69 | {
70 | "cell_type": "markdown",
71 | "metadata": {},
72 | "source": [
73 | "### Make a vector $x=\\begin{bmatrix}1\\\\1 \\end{bmatrix}$ and feed it to the network. What is the output probability? Check that it sums to one."
74 | ]
75 | },
76 | {
77 | "cell_type": "code",
78 | "execution_count": null,
79 | "metadata": {},
80 | "outputs": [],
81 | "source": [
82 | "x= # complete here\n",
83 | "print(x)\n",
84 | "\n",
85 | "p = # complete here\n",
86 | "print(p)\n",
87 | "\n",
88 | "print(p.sum() )"
89 | ]
90 | },
91 | {
92 | "cell_type": "markdown",
93 | "metadata": {},
94 | "source": [
95 | "### Print the weights as well as the bias of the unique layer of this network. Be careful to use the correct name for this unique layer "
96 | ]
97 | },
98 | {
99 | "cell_type": "code",
100 | "execution_count": null,
101 | "metadata": {},
102 | "outputs": [],
103 | "source": [
104 | "print( # complete here)\n",
105 | "print( # complete here)"
106 | ]
107 | },
108 | {
109 | "cell_type": "markdown",
110 | "metadata": {},
111 | "source": [
112 | "### Change the internal parameters of your network so that the weights are now equal to $\n",
113 | "W=\\begin{bmatrix}\n",
114 | "1&2 \\\\ 3&4 \n",
115 | "\\end{bmatrix}\n",
116 | "$ and the bias is equal to $\n",
117 | "b=\\begin{bmatrix}\n",
118 | "1 \\\\ 1 \n",
119 | "\\end{bmatrix}\n",
120 | "$ "
121 | ]
122 | },
123 | {
124 | "cell_type": "code",
125 | "execution_count": null,
126 | "metadata": {},
127 | "outputs": [],
128 | "source": [
129 | "# CHANGE THE WEIGHTS\n",
130 | "# complete here\n",
131 | "# complete here\n",
132 | "# complete here\n",
133 | "# complete here\n",
134 | "\n",
135 | "# CHANGE THE BIAS\n",
136 | "# complete here\n",
137 | "# complete here\n",
138 | "\n",
139 | "print(net.mylayer.weight)\n",
140 | "print(net.mylayer.bias)"
141 | ]
142 | },
143 | {
144 | "cell_type": "markdown",
145 | "metadata": {},
146 | "source": [
147 | "### Feed the vector x to your network with updated parameters. What is the output? (you should get p= [2% 98%])"
148 | ]
149 | },
150 | {
151 | "cell_type": "code",
152 | "execution_count": null,
153 | "metadata": {},
154 | "outputs": [],
155 | "source": [
156 | "p= # complete here\n",
157 | "print(p)"
158 | ]
159 | }
160 | ],
161 | "metadata": {
162 | "kernelspec": {
163 | "display_name": "Python 3",
164 | "language": "python",
165 | "name": "python3"
166 | },
167 | "language_info": {
168 | "codemirror_mode": {
169 | "name": "ipython",
170 | "version": 3
171 | },
172 | "file_extension": ".py",
173 | "mimetype": "text/x-python",
174 | "name": "python",
175 | "nbconvert_exporter": "python",
176 | "pygments_lexer": "ipython3",
177 | "version": "3.6.8"
178 | }
179 | },
180 | "nbformat": 4,
181 | "nbformat_minor": 2
182 | }
183 |
--------------------------------------------------------------------------------
/codes/labs_lecture06/lab02_cifar_multilayer/gpu_demo.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Lab 02 : GPU demo"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": 1,
13 | "metadata": {},
14 | "outputs": [],
15 | "source": [
16 | "import torch\n",
17 | "import torch.nn as nn\n",
18 | "import torch.nn.functional as F"
19 | ]
20 | },
21 | {
22 | "cell_type": "markdown",
23 | "metadata": {},
24 | "source": [
25 | "### Set the device variable to be either cuda or cpu"
26 | ]
27 | },
28 | {
29 | "cell_type": "code",
30 | "execution_count": 9,
31 | "metadata": {},
32 | "outputs": [
33 | {
34 | "name": "stdout",
35 | "output_type": "stream",
36 | "text": [
37 | "cpu\n"
38 | ]
39 | }
40 | ],
41 | "source": [
42 | "device = torch.device(\"cuda\")\n",
43 | "\n",
44 | "#device = torch.device(\"cpu\")\n",
45 | "\n",
46 | "print(device)"
47 | ]
48 | },
49 | {
50 | "cell_type": "markdown",
51 | "metadata": {},
52 | "source": [
53 | "### Make a random tensor (by default the tensor is created on the CPU)"
54 | ]
55 | },
56 | {
57 | "cell_type": "code",
58 | "execution_count": 10,
59 | "metadata": {},
60 | "outputs": [
61 | {
62 | "name": "stdout",
63 | "output_type": "stream",
64 | "text": [
65 | "tensor([[0.2126, 0.8232, 0.9466],\n",
66 | " [0.0370, 0.5176, 0.7032],\n",
67 | " [0.4544, 0.4457, 0.8515]])\n"
68 | ]
69 | }
70 | ],
71 | "source": [
72 | "x=torch.rand(3,3)\n",
73 | "\n",
74 | "print(x)"
75 | ]
76 | },
77 | {
78 | "cell_type": "markdown",
79 | "metadata": {},
80 | "source": [
81 | "### Send the tensor to the device (which is going to be be the GPU if the device was set to be the GPU -- otherwise it will remain on the CPU)"
82 | ]
83 | },
84 | {
85 | "cell_type": "code",
86 | "execution_count": 11,
87 | "metadata": {},
88 | "outputs": [
89 | {
90 | "name": "stdout",
91 | "output_type": "stream",
92 | "text": [
93 | "tensor([[0.2126, 0.8232, 0.9466],\n",
94 | " [0.0370, 0.5176, 0.7032],\n",
95 | " [0.4544, 0.4457, 0.8515]])\n"
96 | ]
97 | }
98 | ],
99 | "source": [
100 | "x=x.to(device)\n",
101 | "\n",
102 | "print(x)"
103 | ]
104 | },
105 | {
106 | "cell_type": "markdown",
107 | "metadata": {},
108 | "source": [
109 | "### Make a one layer net class"
110 | ]
111 | },
112 | {
113 | "cell_type": "code",
114 | "execution_count": 12,
115 | "metadata": {},
116 | "outputs": [],
117 | "source": [
118 | "class one_layer_net(nn.Module):\n",
119 | "\n",
120 | " def __init__(self, input_size, output_size):\n",
121 | " super(one_layer_net , self).__init__()\n",
122 | " \n",
123 | " self.layer1 = nn.Linear( input_size, output_size , bias=False)\n",
124 | " \n",
125 | " def forward(self, x):\n",
126 | " scores = self.layer1(x)\n",
127 | " return scores"
128 | ]
129 | },
130 | {
131 | "cell_type": "markdown",
132 | "metadata": {},
133 | "source": [
134 | "### Build the net"
135 | ]
136 | },
137 | {
138 | "cell_type": "code",
139 | "execution_count": 13,
140 | "metadata": {},
141 | "outputs": [],
142 | "source": [
143 | "net=one_layer_net(5,2)"
144 | ]
145 | },
146 | {
147 | "cell_type": "markdown",
148 | "metadata": {},
149 | "source": [
150 | "### Send all the weights of the network to the GPU"
151 | ]
152 | },
153 | {
154 | "cell_type": "code",
155 | "execution_count": 14,
156 | "metadata": {},
157 | "outputs": [],
158 | "source": [
159 | "net = net.to(device)"
160 | ]
161 | },
162 | {
163 | "cell_type": "markdown",
164 | "metadata": {},
165 | "source": [
166 | "### Ckeck that the matrix is indeed on the GPU"
167 | ]
168 | },
169 | {
170 | "cell_type": "code",
171 | "execution_count": 8,
172 | "metadata": {},
173 | "outputs": [
174 | {
175 | "name": "stdout",
176 | "output_type": "stream",
177 | "text": [
178 | "Parameter containing:\n",
179 | "tensor([[-0.2646, 0.0491, 0.3317, 0.2717, 0.0483],\n",
180 | " [ 0.1272, -0.3401, 0.1400, 0.0687, 0.3442]], requires_grad=True)\n"
181 | ]
182 | }
183 | ],
184 | "source": [
185 | "print(net.layer1.weight)"
186 | ]
187 | },
188 | {
189 | "cell_type": "code",
190 | "execution_count": null,
191 | "metadata": {},
192 | "outputs": [],
193 | "source": []
194 | }
195 | ],
196 | "metadata": {
197 | "kernelspec": {
198 | "display_name": "Python 3",
199 | "language": "python",
200 | "name": "python3"
201 | },
202 | "language_info": {
203 | "codemirror_mode": {
204 | "name": "ipython",
205 | "version": 3
206 | },
207 | "file_extension": ".py",
208 | "mimetype": "text/x-python",
209 | "name": "python",
210 | "nbconvert_exporter": "python",
211 | "pygments_lexer": "ipython3",
212 | "version": "3.6.10"
213 | }
214 | },
215 | "nbformat": 4,
216 | "nbformat_minor": 2
217 | }
218 |
--------------------------------------------------------------------------------
/codes/labs_lecture03/lab01_linear_module/linear_module_solution.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Lab 01 : Linear module -- solution\n",
8 | "\n",
9 | "# Inspecting a linear module"
10 | ]
11 | },
12 | {
13 | "cell_type": "code",
14 | "execution_count": 1,
15 | "metadata": {},
16 | "outputs": [],
17 | "source": [
18 | "import torch\n",
19 | "import torch.nn as nn\n",
20 | "import torch.nn.functional as F"
21 | ]
22 | },
23 | {
24 | "cell_type": "markdown",
25 | "metadata": {},
26 | "source": [
27 | "### Make a linear module WITHOUT bias that takes input of size 2 and return output of size 3. "
28 | ]
29 | },
30 | {
31 | "cell_type": "code",
32 | "execution_count": 2,
33 | "metadata": {},
34 | "outputs": [
35 | {
36 | "name": "stdout",
37 | "output_type": "stream",
38 | "text": [
39 | "Linear(in_features=2, out_features=3, bias=False)\n"
40 | ]
41 | }
42 | ],
43 | "source": [
44 | "mod=nn.Linear(2,3,bias=False)\n",
45 | "print(mod)"
46 | ]
47 | },
48 | {
49 | "cell_type": "markdown",
50 | "metadata": {},
51 | "source": [
52 | "### Print the internal parameters of the module. Try to print the bias and double check that it return you \"None\""
53 | ]
54 | },
55 | {
56 | "cell_type": "code",
57 | "execution_count": 3,
58 | "metadata": {},
59 | "outputs": [
60 | {
61 | "name": "stdout",
62 | "output_type": "stream",
63 | "text": [
64 | "Parameter containing:\n",
65 | "tensor([[-0.5430, -0.3259],\n",
66 | " [-0.5779, 0.5816],\n",
67 | " [-0.6248, -0.3755]], requires_grad=True)\n",
68 | "None\n"
69 | ]
70 | }
71 | ],
72 | "source": [
73 | "print(mod.weight)\n",
74 | "print(mod.bias)"
75 | ]
76 | },
77 | {
78 | "cell_type": "markdown",
79 | "metadata": {},
80 | "source": [
81 | "### Make a vector $x=\\begin{bmatrix}1\\\\1 \\end{bmatrix}$ and feed it to your linear module. What is the output?"
82 | ]
83 | },
84 | {
85 | "cell_type": "code",
86 | "execution_count": 4,
87 | "metadata": {},
88 | "outputs": [
89 | {
90 | "name": "stdout",
91 | "output_type": "stream",
92 | "text": [
93 | "tensor([1., 1.])\n",
94 | "tensor([-0.8690, 0.0037, -1.0003], grad_fn=)\n"
95 | ]
96 | }
97 | ],
98 | "source": [
99 | "x=torch.Tensor([1,1])\n",
100 | "print(x)\n",
101 | "y=mod(x)\n",
102 | "print(y)"
103 | ]
104 | },
105 | {
106 | "cell_type": "markdown",
107 | "metadata": {},
108 | "source": [
109 | "### Change the internal parameters of your module so that the weights become $\n",
110 | "W=\\begin{bmatrix}\n",
111 | "1&2 \\\\ 3&4 \\\\ 5&6\n",
112 | "\\end{bmatrix}\n",
113 | "$. You need to write 6 lines, one per entry to be changed."
114 | ]
115 | },
116 | {
117 | "cell_type": "code",
118 | "execution_count": 5,
119 | "metadata": {},
120 | "outputs": [
121 | {
122 | "name": "stdout",
123 | "output_type": "stream",
124 | "text": [
125 | "Parameter containing:\n",
126 | "tensor([[1., 2.],\n",
127 | " [3., 4.],\n",
128 | " [5., 6.]], grad_fn=)\n"
129 | ]
130 | }
131 | ],
132 | "source": [
133 | "mod.weight[0,0]=1\n",
134 | "mod.weight[0,1]=2\n",
135 | "mod.weight[1,0]=3\n",
136 | "mod.weight[1,1]=4\n",
137 | "mod.weight[2,0]=5\n",
138 | "mod.weight[2,1]=6\n",
139 | "print(mod.weight)"
140 | ]
141 | },
142 | {
143 | "cell_type": "markdown",
144 | "metadata": {},
145 | "source": [
146 | "### Feed the vector x to your module with updated parameters. What is the output? "
147 | ]
148 | },
149 | {
150 | "cell_type": "code",
151 | "execution_count": 6,
152 | "metadata": {},
153 | "outputs": [
154 | {
155 | "name": "stdout",
156 | "output_type": "stream",
157 | "text": [
158 | "tensor([ 3., 7., 11.], grad_fn=)\n"
159 | ]
160 | }
161 | ],
162 | "source": [
163 | "y=mod(x)\n",
164 | "print(y)"
165 | ]
166 | },
167 | {
168 | "cell_type": "markdown",
169 | "metadata": {},
170 | "source": [
171 | "### Compute by hand $y=Wx$ and check that pytorch got it correctly. \n",
172 | "\n",
173 | "$$\n",
174 | "\\begin{bmatrix}\n",
175 | "1&2 \\\\ 3&4 \\\\ 5&6\n",
176 | "\\end{bmatrix}\n",
177 | "\\begin{bmatrix}1\\\\1 \\end{bmatrix}=\n",
178 | "\\begin{bmatrix}?\\\\? \\\\ ? \\end{bmatrix}\n",
179 | "$$"
180 | ]
181 | },
182 | {
183 | "cell_type": "code",
184 | "execution_count": null,
185 | "metadata": {},
186 | "outputs": [],
187 | "source": []
188 | }
189 | ],
190 | "metadata": {
191 | "kernelspec": {
192 | "display_name": "Python 3",
193 | "language": "python",
194 | "name": "python3"
195 | },
196 | "language_info": {
197 | "codemirror_mode": {
198 | "name": "ipython",
199 | "version": 3
200 | },
201 | "file_extension": ".py",
202 | "mimetype": "text/x-python",
203 | "name": "python",
204 | "nbconvert_exporter": "python",
205 | "pygments_lexer": "ipython3",
206 | "version": "3.6.10"
207 | }
208 | },
209 | "nbformat": 4,
210 | "nbformat_minor": 2
211 | }
212 |
--------------------------------------------------------------------------------
/codes/labs_lecture04/lab01_cross_entropy/cross_entropy_exercise.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Lab 01 : Cross-entropy loss -- exercise"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": null,
13 | "metadata": {},
14 | "outputs": [],
15 | "source": [
16 | "# For Google Colaboratory\n",
17 | "import sys, os\n",
18 | "if 'google.colab' in sys.modules:\n",
19 | " # mount google drive\n",
20 | " from google.colab import drive\n",
21 | " drive.mount('/content/gdrive')\n",
22 | " # find automatically the path of the folder containing \"file_name\" :\n",
23 | " file_name = 'cross_entropy_exercise.ipynb'\n",
24 | " import subprocess\n",
25 | " path_to_file = subprocess.check_output('find . -type f -name ' + str(file_name), shell=True).decode(\"utf-8\")\n",
26 | " path_to_file = path_to_file.replace(file_name,\"\").replace('\\n',\"\")\n",
27 | " # if previous search failed or too long, comment the previous line and simply write down manually the path below :\n",
28 | " #path_to_file = '/content/gdrive/My Drive/CE7454_2020_codes/codes/labs_lecture04/lab01_cross_entropy'\n",
29 | " print(path_to_file)\n",
30 | " # change current path to the folder containing \"file_name\"\n",
31 | " os.chdir(path_to_file)\n",
32 | " !pwd"
33 | ]
34 | },
35 | {
36 | "cell_type": "code",
37 | "execution_count": null,
38 | "metadata": {},
39 | "outputs": [],
40 | "source": [
41 | "import torch\n",
42 | "import torch.nn as nn\n",
43 | "import utils"
44 | ]
45 | },
46 | {
47 | "cell_type": "markdown",
48 | "metadata": {},
49 | "source": [
50 | "### Make a Cross Entropy Criterion and call it criterion. The command is nn.CrossEntropyLoss()."
51 | ]
52 | },
53 | {
54 | "cell_type": "code",
55 | "execution_count": null,
56 | "metadata": {},
57 | "outputs": [],
58 | "source": [
59 | "criterion= # COMPLETE HERE\n",
60 | "\n",
61 | "print(criterion)"
62 | ]
63 | },
64 | {
65 | "cell_type": "markdown",
66 | "metadata": {},
67 | "source": [
68 | "### Suppose that there only two classes (class 0 and class 1).\n",
69 | "### Suppose we have a batch of three data points: \n",
70 | "### ${\\bf x^{(0)}}$ belongs to class 0\n",
71 | "### ${\\bf x^{(1)}}$belongs to class 1\n",
72 | "### ${\\bf x^{(2)}}$ belongs to class 1\n",
73 | "### Put the labels of each of these point a LongTensor:"
74 | ]
75 | },
76 | {
77 | "cell_type": "code",
78 | "execution_count": null,
79 | "metadata": {},
80 | "outputs": [],
81 | "source": [
82 | "labels = # COMPLETE HERE\n",
83 | "\n",
84 | "print(labels,labels.type())"
85 | ]
86 | },
87 | {
88 | "cell_type": "markdown",
89 | "metadata": {},
90 | "source": [
91 | "### Make a batch of scores: each row corresponds to the scores associated with a data point. So your batch of scores should look likes something like:\n",
92 | "\n",
93 | "$$\n",
94 | "\\text{scores} \\;\\; = \\;\\; \\begin{bmatrix}\n",
95 | "s_0^{(0)} & s_1^{(0)} & \\\\\n",
96 | "s_0^{(1)} & s_1^{(1)} & \\\\\n",
97 | "s_0^{(2)} & s_1^{(2)} & \\\\\n",
98 | "\\end{bmatrix}\n",
99 | "$$\n",
100 | "\n",
101 | "### You will need to create a tensor of the form torch.Tensor( [ [ ], [ ], [ ] ] ). Don't forget the extra square brackets!\n",
102 | "\n",
103 | "### Choose scores that will leads to a loss very close to zero, let say around or smaller than 0.05 (indicating that the scores are very good with respect to the labels). "
104 | ]
105 | },
106 | {
107 | "cell_type": "code",
108 | "execution_count": null,
109 | "metadata": {},
110 | "outputs": [],
111 | "source": [
112 | "scores = # COMPLETE HERE\n",
113 | "\n",
114 | "print(scores)"
115 | ]
116 | },
117 | {
118 | "cell_type": "markdown",
119 | "metadata": {},
120 | "source": [
121 | "### Display your batch of scores"
122 | ]
123 | },
124 | {
125 | "cell_type": "code",
126 | "execution_count": null,
127 | "metadata": {
128 | "scrolled": true
129 | },
130 | "outputs": [],
131 | "source": [
132 | "utils.display_scores(scores)"
133 | ]
134 | },
135 | {
136 | "cell_type": "markdown",
137 | "metadata": {},
138 | "source": [
139 | "### Use the criterion to compute the average loss on this batch -- it needs to be around or smaller than 0.05"
140 | ]
141 | },
142 | {
143 | "cell_type": "code",
144 | "execution_count": null,
145 | "metadata": {},
146 | "outputs": [],
147 | "source": [
148 | "average_loss = # COMPLETE HERE\n",
149 | "\n",
150 | "print(average_loss.item())"
151 | ]
152 | },
153 | {
154 | "cell_type": "code",
155 | "execution_count": null,
156 | "metadata": {},
157 | "outputs": [],
158 | "source": []
159 | }
160 | ],
161 | "metadata": {
162 | "kernelspec": {
163 | "display_name": "Python 3",
164 | "language": "python",
165 | "name": "python3"
166 | },
167 | "language_info": {
168 | "codemirror_mode": {
169 | "name": "ipython",
170 | "version": 3
171 | },
172 | "file_extension": ".py",
173 | "mimetype": "text/x-python",
174 | "name": "python",
175 | "nbconvert_exporter": "python",
176 | "pygments_lexer": "ipython3",
177 | "version": "3.6.10"
178 | }
179 | },
180 | "nbformat": 4,
181 | "nbformat_minor": 2
182 | }
183 |
--------------------------------------------------------------------------------
/codes/data/ptb/generate_ptb.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Download PTB"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": null,
13 | "metadata": {},
14 | "outputs": [],
15 | "source": [
16 | "# For Google Colaboratory\n",
17 | "import sys, os\n",
18 | "if 'google.colab' in sys.modules:\n",
19 | " from google.colab import drive\n",
20 | " drive.mount('/content/gdrive')\n",
21 | " file_name = 'generate_ptb.ipynb'\n",
22 | " import subprocess\n",
23 | " path_to_file = subprocess.check_output('find . -type f -name ' + str(file_name), shell=True).decode(\"utf-8\")\n",
24 | " print(path_to_file)\n",
25 | " path_to_file = path_to_file.replace(file_name,\"\").replace('\\n',\"\")\n",
26 | " os.chdir(path_to_file)\n",
27 | " !pwd"
28 | ]
29 | },
30 | {
31 | "cell_type": "code",
32 | "execution_count": 1,
33 | "metadata": {},
34 | "outputs": [],
35 | "source": [
36 | "import torch\n",
37 | "import os"
38 | ]
39 | },
40 | {
41 | "cell_type": "code",
42 | "execution_count": 2,
43 | "metadata": {},
44 | "outputs": [],
45 | "source": [
46 | "class Dictionary(object):\n",
47 | " def __init__(self):\n",
48 | " self.word2idx = {}\n",
49 | " self.idx2word = []\n",
50 | "\n",
51 | " def add_word(self, word):\n",
52 | " if word not in self.word2idx:\n",
53 | " self.idx2word.append(word)\n",
54 | " self.word2idx[word] = len(self.idx2word) - 1\n",
55 | " return self.word2idx[word]\n",
56 | "\n",
57 | " def __len__(self):\n",
58 | " return len(self.idx2word)"
59 | ]
60 | },
61 | {
62 | "cell_type": "code",
63 | "execution_count": 3,
64 | "metadata": {},
65 | "outputs": [],
66 | "source": [
67 | "class Corpus(object):\n",
68 | " def __init__(self, path):\n",
69 | " self.dictionary = Dictionary()\n",
70 | " self.train = self.tokenize(os.path.join(path, 'train.txt'))\n",
71 | " self.valid = self.tokenize(os.path.join(path, 'valid.txt'))\n",
72 | " self.test = self.tokenize(os.path.join(path, 'test.txt'))\n",
73 | "\n",
74 | " def tokenize(self, path):\n",
75 | " \"\"\"Tokenizes a text file.\"\"\"\n",
76 | " assert os.path.exists(path)\n",
77 | " # Add words to the dictionary\n",
78 | " with open(path, 'r') as f:\n",
79 | " tokens = 0\n",
80 | " for line in f:\n",
81 | " words = line.split() + ['']\n",
82 | " tokens += len(words)\n",
83 | " for word in words:\n",
84 | " self.dictionary.add_word(word)\n",
85 | "\n",
86 | " # Tokenize file content\n",
87 | " with open(path, 'r') as f:\n",
88 | " ids = torch.LongTensor(tokens)\n",
89 | " token = 0\n",
90 | " for line in f:\n",
91 | " words = line.split() + ['']\n",
92 | " for word in words:\n",
93 | " ids[token] = self.dictionary.word2idx[word]\n",
94 | " token += 1\n",
95 | "\n",
96 | " return ids"
97 | ]
98 | },
99 | {
100 | "cell_type": "code",
101 | "execution_count": 4,
102 | "metadata": {},
103 | "outputs": [],
104 | "source": [
105 | "data_folder='data_raw'\n",
106 | "corpus = Corpus(data_folder)"
107 | ]
108 | },
109 | {
110 | "cell_type": "code",
111 | "execution_count": 5,
112 | "metadata": {},
113 | "outputs": [],
114 | "source": [
115 | "def batchify(data, bsz):\n",
116 | " # Work out how cleanly we can divide the dataset into bsz parts.\n",
117 | " nbatch = data.size(0) // bsz\n",
118 | " # Trim off any extra elements that wouldn't cleanly fit (remainders).\n",
119 | " data = data.narrow(0, 0, nbatch * bsz)\n",
120 | " # Evenly divide the data across the bsz batches.\n",
121 | " data = data.view(bsz, -1).t().contiguous()\n",
122 | " return data"
123 | ]
124 | },
125 | {
126 | "cell_type": "code",
127 | "execution_count": 6,
128 | "metadata": {},
129 | "outputs": [
130 | {
131 | "name": "stdout",
132 | "output_type": "stream",
133 | "text": [
134 | "torch.Size([46479, 20])\n",
135 | "torch.Size([4121, 20])\n"
136 | ]
137 | }
138 | ],
139 | "source": [
140 | "batch_size=20\n",
141 | "\n",
142 | "train_data = batchify(corpus.train, batch_size)\n",
143 | "val_data = batchify(corpus.valid, batch_size)\n",
144 | "test_data = batchify(corpus.test, batch_size)\n",
145 | "\n",
146 | "vocab_size = len(corpus.dictionary)\n",
147 | "\n",
148 | "print(train_data.size())\n",
149 | "print(test_data.size())\n",
150 | "\n",
151 | "train_length = train_data.size(0)\n",
152 | "test_length = test_data.size(0)\n"
153 | ]
154 | },
155 | {
156 | "cell_type": "code",
157 | "execution_count": 7,
158 | "metadata": {},
159 | "outputs": [],
160 | "source": [
161 | "torch.save(train_data,'train_data.pt')\n",
162 | "torch.save(test_data,'test_data.pt')\n",
163 | "torch.save(corpus.dictionary.idx2word,'idx2word.pt')\n",
164 | "torch.save(corpus.dictionary.word2idx,'word2idx.pt')"
165 | ]
166 | },
167 | {
168 | "cell_type": "code",
169 | "execution_count": null,
170 | "metadata": {},
171 | "outputs": [],
172 | "source": []
173 | }
174 | ],
175 | "metadata": {
176 | "kernelspec": {
177 | "display_name": "Python 3",
178 | "language": "python",
179 | "name": "python3"
180 | },
181 | "language_info": {
182 | "codemirror_mode": {
183 | "name": "ipython",
184 | "version": 3
185 | },
186 | "file_extension": ".py",
187 | "mimetype": "text/x-python",
188 | "name": "python",
189 | "nbconvert_exporter": "python",
190 | "pygments_lexer": "ipython3",
191 | "version": "3.6.8"
192 | }
193 | },
194 | "nbformat": 4,
195 | "nbformat_minor": 2
196 | }
197 |
--------------------------------------------------------------------------------
/codes/labs_lecture05/lab02_epoch/epoch_exercise.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Lab 02: Training with epochs -- exercise"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": null,
13 | "metadata": {},
14 | "outputs": [],
15 | "source": [
16 | "# For Google Colaboratory\n",
17 | "import sys, os\n",
18 | "if 'google.colab' in sys.modules:\n",
19 | " # mount google drive\n",
20 | " from google.colab import drive\n",
21 | " drive.mount('/content/gdrive')\n",
22 | " # find automatically the path of the folder containing \"file_name\" :\n",
23 | " file_name = 'epoch_exercise.ipynb'\n",
24 | " import subprocess\n",
25 | " path_to_file = subprocess.check_output('find . -type f -name ' + str(file_name), shell=True).decode(\"utf-8\")\n",
26 | " path_to_file = path_to_file.replace(file_name,\"\").replace('\\n',\"\")\n",
27 | " # if previous search failed or too long, comment the previous line and simply write down manually the path below :\n",
28 | " #path_to_file = '/content/gdrive/My Drive/CE7454_2020_codes/codes/labs_lecture05/lab02_epoch'\n",
29 | " print(path_to_file)\n",
30 | " # change current path to the folder containing \"file_name\"\n",
31 | " os.chdir(path_to_file)\n",
32 | " !pwd"
33 | ]
34 | },
35 | {
36 | "cell_type": "code",
37 | "execution_count": null,
38 | "metadata": {},
39 | "outputs": [],
40 | "source": [
41 | "import torch\n",
42 | "import torch.nn as nn\n",
43 | "import torch.nn.functional as F\n",
44 | "import torch.optim as optim\n",
45 | "from random import randint\n",
46 | "import utils"
47 | ]
48 | },
49 | {
50 | "cell_type": "markdown",
51 | "metadata": {},
52 | "source": [
53 | "### Download the data and print the sizes"
54 | ]
55 | },
56 | {
57 | "cell_type": "code",
58 | "execution_count": null,
59 | "metadata": {},
60 | "outputs": [],
61 | "source": [
62 | "from utils import check_mnist_dataset_exists\n",
63 | "data_path=check_mnist_dataset_exists()\n",
64 | "\n",
65 | "train_data=torch.load(data_path+'mnist/train_data.pt')\n",
66 | "train_label=torch.load(data_path+'mnist/train_label.pt')\n",
67 | "test_data=torch.load(data_path+'mnist/test_data.pt')"
68 | ]
69 | },
70 | {
71 | "cell_type": "markdown",
72 | "metadata": {},
73 | "source": [
74 | "### Make a ONE layer net class. "
75 | ]
76 | },
77 | {
78 | "cell_type": "code",
79 | "execution_count": null,
80 | "metadata": {},
81 | "outputs": [],
82 | "source": [
83 | "class one_layer_net(nn.Module):\n",
84 | "\n",
85 | " def __init__(self, input_size, output_size):\n",
86 | " super(one_layer_net , self).__init__()\n",
87 | " self.linear_layer = nn.Linear( input_size, output_size , bias=False)\n",
88 | " \n",
89 | " def forward(self, x):\n",
90 | " scores = self.linear_layer(x)\n",
91 | " return scores"
92 | ]
93 | },
94 | {
95 | "cell_type": "markdown",
96 | "metadata": {},
97 | "source": [
98 | "### Build the net"
99 | ]
100 | },
101 | {
102 | "cell_type": "code",
103 | "execution_count": null,
104 | "metadata": {},
105 | "outputs": [],
106 | "source": [
107 | "net=one_layer_net(784,10)\n",
108 | "print(net)"
109 | ]
110 | },
111 | {
112 | "cell_type": "markdown",
113 | "metadata": {},
114 | "source": [
115 | "### Choose the criterion, optimizer, batchsize, learning rate"
116 | ]
117 | },
118 | {
119 | "cell_type": "code",
120 | "execution_count": null,
121 | "metadata": {},
122 | "outputs": [],
123 | "source": [
124 | "criterion = nn.CrossEntropyLoss()\n",
125 | "\n",
126 | "optimizer=torch.optim.SGD( net.parameters() , lr=0.01 )\n",
127 | "\n",
128 | "bs=50"
129 | ]
130 | },
131 | {
132 | "cell_type": "markdown",
133 | "metadata": {},
134 | "source": [
135 | "# You only have stuff to do in this cell"
136 | ]
137 | },
138 | {
139 | "cell_type": "markdown",
140 | "metadata": {},
141 | "source": [
142 | "### Do 15 passes through the training set"
143 | ]
144 | },
145 | {
146 | "cell_type": "code",
147 | "execution_count": null,
148 | "metadata": {},
149 | "outputs": [],
150 | "source": [
151 | "for # COMPLETE\n",
152 | " \n",
153 | " # COMPLETE\n",
154 | " \n",
155 | " for # COMPLETE\n",
156 | " \n",
157 | " optimizer.zero_grad()\n",
158 | " \n",
159 | " # COMPLETE\n",
160 | " # COMPLETE\n",
161 | " # COMPLETE\n",
162 | "\n",
163 | " inputs = minibatch_data.view(bs,784)\n",
164 | " \n",
165 | " inputs.requires_grad_()\n",
166 | "\n",
167 | " scores=net( inputs ) \n",
168 | "\n",
169 | " loss = criterion( scores , minibatch_label) \n",
170 | " \n",
171 | " loss.backward()\n",
172 | "\n",
173 | " optimizer.step()"
174 | ]
175 | },
176 | {
177 | "cell_type": "markdown",
178 | "metadata": {},
179 | "source": [
180 | "### Choose image at random from the test set and see how good/bad are the predictions"
181 | ]
182 | },
183 | {
184 | "cell_type": "code",
185 | "execution_count": null,
186 | "metadata": {},
187 | "outputs": [],
188 | "source": [
189 | "# choose a picture at random\n",
190 | "idx=randint(0, 10000-1)\n",
191 | "im=test_data[idx]\n",
192 | "\n",
193 | "# diplay the picture\n",
194 | "utils.show(im)\n",
195 | "\n",
196 | "# feed it to the net and display the confidence scores\n",
197 | "scores = net( im.view(1,784)) \n",
198 | "probs= F.softmax(scores, dim=1)\n",
199 | "utils.show_prob_mnist(probs)"
200 | ]
201 | }
202 | ],
203 | "metadata": {
204 | "kernelspec": {
205 | "display_name": "Python 3",
206 | "language": "python",
207 | "name": "python3"
208 | },
209 | "language_info": {
210 | "codemirror_mode": {
211 | "name": "ipython",
212 | "version": 3
213 | },
214 | "file_extension": ".py",
215 | "mimetype": "text/x-python",
216 | "name": "python",
217 | "nbconvert_exporter": "python",
218 | "pygments_lexer": "ipython3",
219 | "version": "3.6.10"
220 | }
221 | },
222 | "nbformat": 4,
223 | "nbformat_minor": 2
224 | }
225 |
--------------------------------------------------------------------------------
/codes/labs_lecture03/lab03_vanilla_nn/vanilla_nn_solution.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Lab 03 : Vanilla neural networks -- solution\n",
8 | "\n",
9 | "# Creating a one-layer network"
10 | ]
11 | },
12 | {
13 | "cell_type": "code",
14 | "execution_count": 1,
15 | "metadata": {},
16 | "outputs": [],
17 | "source": [
18 | "import torch\n",
19 | "import torch.nn as nn\n",
20 | "import torch.nn.functional as F"
21 | ]
22 | },
23 | {
24 | "cell_type": "markdown",
25 | "metadata": {},
26 | "source": [
27 | "### Make a class for a one layer network. Let's call the layer \"mylayer\". And let's give it a bias."
28 | ]
29 | },
30 | {
31 | "cell_type": "code",
32 | "execution_count": 2,
33 | "metadata": {},
34 | "outputs": [],
35 | "source": [
36 | "class one_layer_net(nn.Module):\n",
37 | "\n",
38 | " def __init__(self, input_size, output_size ):\n",
39 | " super(one_layer_net , self).__init__()\n",
40 | " \n",
41 | " self.mylayer=nn.Linear(input_size,output_size, bias=True)\n",
42 | " \n",
43 | " \n",
44 | " def forward(self, x):\n",
45 | " \n",
46 | " x = self.mylayer(x)\n",
47 | " p = F.softmax(x,dim=0)\n",
48 | " \n",
49 | " return p"
50 | ]
51 | },
52 | {
53 | "cell_type": "markdown",
54 | "metadata": {},
55 | "source": [
56 | "### Create an instance of a one layer net that take input of size 2 and return output of size 2"
57 | ]
58 | },
59 | {
60 | "cell_type": "code",
61 | "execution_count": 3,
62 | "metadata": {},
63 | "outputs": [
64 | {
65 | "name": "stdout",
66 | "output_type": "stream",
67 | "text": [
68 | "one_layer_net(\n",
69 | " (mylayer): Linear(in_features=2, out_features=2, bias=True)\n",
70 | ")\n"
71 | ]
72 | }
73 | ],
74 | "source": [
75 | "net= one_layer_net(2,2)\n",
76 | "print(net)"
77 | ]
78 | },
79 | {
80 | "cell_type": "markdown",
81 | "metadata": {},
82 | "source": [
83 | "### Make a vector $x=\\begin{bmatrix}1\\\\1 \\end{bmatrix}$ and feed it to the network. What is the output probability?"
84 | ]
85 | },
86 | {
87 | "cell_type": "code",
88 | "execution_count": 4,
89 | "metadata": {},
90 | "outputs": [
91 | {
92 | "name": "stdout",
93 | "output_type": "stream",
94 | "text": [
95 | "tensor([1., 1.])\n",
96 | "tensor([0.5447, 0.4553], grad_fn=)\n",
97 | "tensor(1., grad_fn=)\n"
98 | ]
99 | }
100 | ],
101 | "source": [
102 | "x= torch.Tensor([1,1])\n",
103 | "print(x)\n",
104 | "p = net(x)\n",
105 | "print(p)\n",
106 | "print(p.sum() )"
107 | ]
108 | },
109 | {
110 | "cell_type": "markdown",
111 | "metadata": {},
112 | "source": [
113 | "### Print the weights as well as the bias of the unique layer of this network. Be careful to use the correct name for this unique layer "
114 | ]
115 | },
116 | {
117 | "cell_type": "code",
118 | "execution_count": 5,
119 | "metadata": {},
120 | "outputs": [
121 | {
122 | "name": "stdout",
123 | "output_type": "stream",
124 | "text": [
125 | "Parameter containing:\n",
126 | "tensor([[-0.3148, 0.5843],\n",
127 | " [ 0.3549, -0.1694]], requires_grad=True)\n",
128 | "Parameter containing:\n",
129 | "tensor([0.3936, 0.2985], requires_grad=True)\n"
130 | ]
131 | }
132 | ],
133 | "source": [
134 | "print(net.mylayer.weight)\n",
135 | "print(net.mylayer.bias)"
136 | ]
137 | },
138 | {
139 | "cell_type": "markdown",
140 | "metadata": {},
141 | "source": [
142 | "### Change the internal parameters of your network so that the weights are now equal to $\n",
143 | "W=\\begin{bmatrix}\n",
144 | "1&2 \\\\ 3&4 \n",
145 | "\\end{bmatrix}\n",
146 | "$ and the bias is equal to $\n",
147 | "b=\\begin{bmatrix}\n",
148 | "1 \\\\ 1 \n",
149 | "\\end{bmatrix}\n",
150 | "$ "
151 | ]
152 | },
153 | {
154 | "cell_type": "code",
155 | "execution_count": 6,
156 | "metadata": {},
157 | "outputs": [
158 | {
159 | "name": "stdout",
160 | "output_type": "stream",
161 | "text": [
162 | "Parameter containing:\n",
163 | "tensor([[1., 2.],\n",
164 | " [3., 4.]], grad_fn=)\n",
165 | "Parameter containing:\n",
166 | "tensor([1., 1.], grad_fn=)\n"
167 | ]
168 | }
169 | ],
170 | "source": [
171 | "net.mylayer.weight[0,0]=1\n",
172 | "net.mylayer.weight[0,1]=2\n",
173 | "net.mylayer.weight[1,0]=3\n",
174 | "net.mylayer.weight[1,1]=4\n",
175 | "\n",
176 | "net.mylayer.bias[0]=1\n",
177 | "net.mylayer.bias[1]=1\n",
178 | "\n",
179 | "print(net.mylayer.weight)\n",
180 | "print(net.mylayer.bias)"
181 | ]
182 | },
183 | {
184 | "cell_type": "markdown",
185 | "metadata": {},
186 | "source": [
187 | "### Feed the vector x to your network with updated parameters. What is the output? (you should get p= [2% 98%])"
188 | ]
189 | },
190 | {
191 | "cell_type": "code",
192 | "execution_count": 7,
193 | "metadata": {},
194 | "outputs": [
195 | {
196 | "name": "stdout",
197 | "output_type": "stream",
198 | "text": [
199 | "tensor([0.0180, 0.9820], grad_fn=)\n"
200 | ]
201 | }
202 | ],
203 | "source": [
204 | "p=net(x)\n",
205 | "print(p)"
206 | ]
207 | },
208 | {
209 | "cell_type": "code",
210 | "execution_count": null,
211 | "metadata": {},
212 | "outputs": [],
213 | "source": []
214 | },
215 | {
216 | "cell_type": "code",
217 | "execution_count": null,
218 | "metadata": {},
219 | "outputs": [],
220 | "source": []
221 | }
222 | ],
223 | "metadata": {
224 | "kernelspec": {
225 | "display_name": "Python 3",
226 | "language": "python",
227 | "name": "python3"
228 | },
229 | "language_info": {
230 | "codemirror_mode": {
231 | "name": "ipython",
232 | "version": 3
233 | },
234 | "file_extension": ".py",
235 | "mimetype": "text/x-python",
236 | "name": "python",
237 | "nbconvert_exporter": "python",
238 | "pygments_lexer": "ipython3",
239 | "version": "3.6.10"
240 | }
241 | },
242 | "nbformat": 4,
243 | "nbformat_minor": 2
244 | }
245 |
--------------------------------------------------------------------------------
/codes/labs_lecture02/lab02_pytorch_tensor1/pytorch_tensor_part1_solution.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Lab 02: Manipulate PyTorch Tensors -- solution"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": 1,
13 | "metadata": {},
14 | "outputs": [],
15 | "source": [
16 | "import torch"
17 | ]
18 | },
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {},
22 | "source": [
23 | "### Make the matrices A and B below. Add them together to obtain a matrix C. Print these three matrices.\n",
24 | "$$\n",
25 | "A =\\begin{bmatrix}\n",
26 | "1 & 2 \\\\ 3 & 4\n",
27 | "\\end{bmatrix} \n",
28 | "\\qquad \n",
29 | "B =\\begin{bmatrix}\n",
30 | "10 & 20 \\\\ 30 & 40\n",
31 | "\\end{bmatrix} \\qquad C=A+B =?\n",
32 | "$$"
33 | ]
34 | },
35 | {
36 | "cell_type": "code",
37 | "execution_count": 2,
38 | "metadata": {},
39 | "outputs": [
40 | {
41 | "name": "stdout",
42 | "output_type": "stream",
43 | "text": [
44 | "tensor([[1., 2.],\n",
45 | " [3., 4.]])\n",
46 | "\n",
47 | "tensor([[10., 20.],\n",
48 | " [30., 40.]])\n",
49 | "\n",
50 | "tensor([[11., 22.],\n",
51 | " [33., 44.]])\n"
52 | ]
53 | }
54 | ],
55 | "source": [
56 | "A=torch.Tensor( [ [1,2] , [3,4] ] )\n",
57 | "B=torch.Tensor( [ [10,20] , [30,40] ] )\n",
58 | "\n",
59 | "C=A+B\n",
60 | "\n",
61 | "print(A)\n",
62 | "print('')\n",
63 | "print(B)\n",
64 | "print('')\n",
65 | "print(C)"
66 | ]
67 | },
68 | {
69 | "cell_type": "markdown",
70 | "metadata": {},
71 | "source": [
72 | "### Print the sizes, dimension, and type of the matrix A. Remember, the commands are size(), dim() and type()"
73 | ]
74 | },
75 | {
76 | "cell_type": "code",
77 | "execution_count": 3,
78 | "metadata": {},
79 | "outputs": [
80 | {
81 | "name": "stdout",
82 | "output_type": "stream",
83 | "text": [
84 | "torch.Size([2, 2])\n",
85 | "2\n",
86 | "torch.FloatTensor\n"
87 | ]
88 | }
89 | ],
90 | "source": [
91 | "print( A.size() )\n",
92 | "print( A.dim() )\n",
93 | "print( A.type() )"
94 | ]
95 | },
96 | {
97 | "cell_type": "markdown",
98 | "metadata": {},
99 | "source": [
100 | "### Convert the matrix A to be an integer matrix (type LongTensor). Remember, the command is long(). Then print the type to check it was indeed converted."
101 | ]
102 | },
103 | {
104 | "cell_type": "code",
105 | "execution_count": 4,
106 | "metadata": {},
107 | "outputs": [
108 | {
109 | "name": "stdout",
110 | "output_type": "stream",
111 | "text": [
112 | "tensor([[1, 2],\n",
113 | " [3, 4]])\n",
114 | "torch.LongTensor\n"
115 | ]
116 | }
117 | ],
118 | "source": [
119 | "A=A.long()\n",
120 | "print(A)\n",
121 | "print(A.type())"
122 | ]
123 | },
124 | {
125 | "cell_type": "markdown",
126 | "metadata": {},
127 | "source": [
128 | "### Make a random 5 x 2 x 3 Tensor. The command is torch.rand. Then do the following: 1) Print the tensor, 2) Print its type, 3) Print its dimension, 4) Print its size, 5) Print the size of its middle dimension."
129 | ]
130 | },
131 | {
132 | "cell_type": "code",
133 | "execution_count": 5,
134 | "metadata": {},
135 | "outputs": [
136 | {
137 | "name": "stdout",
138 | "output_type": "stream",
139 | "text": [
140 | "tensor([[[0.1654, 0.1702, 0.6423],\n",
141 | " [0.9256, 0.4278, 0.7653]],\n",
142 | "\n",
143 | " [[0.7499, 0.0527, 0.4733],\n",
144 | " [0.0441, 0.1477, 0.4260]],\n",
145 | "\n",
146 | " [[0.8478, 0.5063, 0.0653],\n",
147 | " [0.4906, 0.5045, 0.5422]],\n",
148 | "\n",
149 | " [[0.3924, 0.0418, 0.2341],\n",
150 | " [0.1129, 0.3110, 0.2656]],\n",
151 | "\n",
152 | " [[0.9444, 0.8174, 0.4218],\n",
153 | " [0.2645, 0.3379, 0.3014]]])\n",
154 | "torch.FloatTensor\n",
155 | "3\n",
156 | "torch.Size([5, 2, 3])\n",
157 | "2\n"
158 | ]
159 | }
160 | ],
161 | "source": [
162 | "A=torch.rand(5,2,3)\n",
163 | "print(A)\n",
164 | "print(A.type())\n",
165 | "print(A.dim())\n",
166 | "print(A.size())\n",
167 | "print(A.size(1))"
168 | ]
169 | },
170 | {
171 | "cell_type": "markdown",
172 | "metadata": {},
173 | "source": [
174 | "### Make 2 x 3 x 4 x 5 tensor filled with zeros then print it. (The command is torch.zeros). See if you can make sense of the display."
175 | ]
176 | },
177 | {
178 | "cell_type": "code",
179 | "execution_count": 6,
180 | "metadata": {},
181 | "outputs": [
182 | {
183 | "name": "stdout",
184 | "output_type": "stream",
185 | "text": [
186 | "tensor([[[[0., 0., 0., 0., 0.],\n",
187 | " [0., 0., 0., 0., 0.],\n",
188 | " [0., 0., 0., 0., 0.],\n",
189 | " [0., 0., 0., 0., 0.]],\n",
190 | "\n",
191 | " [[0., 0., 0., 0., 0.],\n",
192 | " [0., 0., 0., 0., 0.],\n",
193 | " [0., 0., 0., 0., 0.],\n",
194 | " [0., 0., 0., 0., 0.]],\n",
195 | "\n",
196 | " [[0., 0., 0., 0., 0.],\n",
197 | " [0., 0., 0., 0., 0.],\n",
198 | " [0., 0., 0., 0., 0.],\n",
199 | " [0., 0., 0., 0., 0.]]],\n",
200 | "\n",
201 | "\n",
202 | " [[[0., 0., 0., 0., 0.],\n",
203 | " [0., 0., 0., 0., 0.],\n",
204 | " [0., 0., 0., 0., 0.],\n",
205 | " [0., 0., 0., 0., 0.]],\n",
206 | "\n",
207 | " [[0., 0., 0., 0., 0.],\n",
208 | " [0., 0., 0., 0., 0.],\n",
209 | " [0., 0., 0., 0., 0.],\n",
210 | " [0., 0., 0., 0., 0.]],\n",
211 | "\n",
212 | " [[0., 0., 0., 0., 0.],\n",
213 | " [0., 0., 0., 0., 0.],\n",
214 | " [0., 0., 0., 0., 0.],\n",
215 | " [0., 0., 0., 0., 0.]]]])\n"
216 | ]
217 | }
218 | ],
219 | "source": [
220 | "A=torch.zeros(2,3,4,5)\n",
221 | "print(A)"
222 | ]
223 | },
224 | {
225 | "cell_type": "code",
226 | "execution_count": null,
227 | "metadata": {},
228 | "outputs": [],
229 | "source": []
230 | }
231 | ],
232 | "metadata": {
233 | "kernelspec": {
234 | "display_name": "Python 3",
235 | "language": "python",
236 | "name": "python3"
237 | },
238 | "language_info": {
239 | "codemirror_mode": {
240 | "name": "ipython",
241 | "version": 3
242 | },
243 | "file_extension": ".py",
244 | "mimetype": "text/x-python",
245 | "name": "python",
246 | "nbconvert_exporter": "python",
247 | "pygments_lexer": "ipython3",
248 | "version": "3.6.10"
249 | }
250 | },
251 | "nbformat": 4,
252 | "nbformat_minor": 2
253 | }
254 |
--------------------------------------------------------------------------------
/codes/labs_lecture03/lab01_linear_module/linear_module_demo.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Lab 01 : Linear module -- demo"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": 1,
13 | "metadata": {},
14 | "outputs": [],
15 | "source": [
16 | "import torch\n",
17 | "import torch.nn as nn"
18 | ]
19 | },
20 | {
21 | "cell_type": "markdown",
22 | "metadata": {},
23 | "source": [
24 | "### Make a _Linear Module_ that takes input of size 5 and return output of size 3"
25 | ]
26 | },
27 | {
28 | "cell_type": "code",
29 | "execution_count": 3,
30 | "metadata": {},
31 | "outputs": [
32 | {
33 | "name": "stdout",
34 | "output_type": "stream",
35 | "text": [
36 | "Linear(in_features=5, out_features=3, bias=True)\n"
37 | ]
38 | }
39 | ],
40 | "source": [
41 | "mod = nn.Linear(5,3)\n",
42 | "print(mod)"
43 | ]
44 | },
45 | {
46 | "cell_type": "markdown",
47 | "metadata": {},
48 | "source": [
49 | "### Let's make a random tensor of size 5:"
50 | ]
51 | },
52 | {
53 | "cell_type": "code",
54 | "execution_count": 4,
55 | "metadata": {},
56 | "outputs": [
57 | {
58 | "name": "stdout",
59 | "output_type": "stream",
60 | "text": [
61 | "tensor([0.5148, 0.6442, 0.5563, 0.4040, 0.9193])\n",
62 | "torch.Size([5])\n"
63 | ]
64 | }
65 | ],
66 | "source": [
67 | "x=torch.rand(5)\n",
68 | "print(x)\n",
69 | "print(x.size())"
70 | ]
71 | },
72 | {
73 | "cell_type": "markdown",
74 | "metadata": {},
75 | "source": [
76 | "### Feed it to the module:"
77 | ]
78 | },
79 | {
80 | "cell_type": "code",
81 | "execution_count": 8,
82 | "metadata": {},
83 | "outputs": [
84 | {
85 | "name": "stdout",
86 | "output_type": "stream",
87 | "text": [
88 | "tensor([ 0.3300, -0.1137, 0.6271], grad_fn=)\n"
89 | ]
90 | }
91 | ],
92 | "source": [
93 | "y=mod(x)\n",
94 | "print(y)"
95 | ]
96 | },
97 | {
98 | "cell_type": "markdown",
99 | "metadata": {},
100 | "source": [
101 | "### The output y is computed according to the formula:\n",
102 | "$$\n",
103 | "\\begin{bmatrix}\n",
104 | "y_1\\\\ y_2 \\\\y_3 \n",
105 | "\\end{bmatrix} =\n",
106 | "\\begin{bmatrix}\n",
107 | "w_{11} & w_{12} & w_{13}& w_{14}& w_{15} \\\\\n",
108 | "w_{21} & w_{22} & w_{23}& w_{24}& w_{25} \\\\\n",
109 | "w_{31} & w_{32} & w_{33}& w_{34}& w_{35} \\\\\n",
110 | "\\end{bmatrix}\n",
111 | "\\begin{bmatrix}\n",
112 | "x_1\\\\ x_2 \\\\x_3 \\\\ x_4 \\\\x_5\n",
113 | "\\end{bmatrix}\n",
114 | "+\n",
115 | "\\begin{bmatrix}\n",
116 | "b_1\\\\ b_2 \\\\b_3 \n",
117 | "\\end{bmatrix}\n",
118 | "$$\n",
119 | "### were the $w_{ij}$'s are the weight parameters and the $b_i$'s are the bias parameters. These internal parameters can be access as follow:"
120 | ]
121 | },
122 | {
123 | "cell_type": "code",
124 | "execution_count": 9,
125 | "metadata": {},
126 | "outputs": [
127 | {
128 | "name": "stdout",
129 | "output_type": "stream",
130 | "text": [
131 | "Parameter containing:\n",
132 | "tensor([[ 0.2652, -0.4428, 0.1893, 0.3214, -0.0462],\n",
133 | " [-0.3800, 0.4037, 0.0042, 0.4156, -0.4236],\n",
134 | " [ 0.2434, 0.2813, 0.1570, -0.4028, 0.0869]], requires_grad=True)\n",
135 | "torch.Size([3, 5])\n"
136 | ]
137 | }
138 | ],
139 | "source": [
140 | "print(mod.weight)\n",
141 | "print(mod.weight.size())"
142 | ]
143 | },
144 | {
145 | "cell_type": "code",
146 | "execution_count": 10,
147 | "metadata": {},
148 | "outputs": [
149 | {
150 | "name": "stdout",
151 | "output_type": "stream",
152 | "text": [
153 | "Parameter containing:\n",
154 | "tensor([0.2860, 0.0411, 0.3160], requires_grad=True)\n",
155 | "torch.Size([3])\n"
156 | ]
157 | }
158 | ],
159 | "source": [
160 | "print(mod.bias)\n",
161 | "print(mod.bias.size())"
162 | ]
163 | },
164 | {
165 | "cell_type": "markdown",
166 | "metadata": {},
167 | "source": [
168 | "### If we want we can change the internal parameters of the module:"
169 | ]
170 | },
171 | {
172 | "cell_type": "code",
173 | "execution_count": 11,
174 | "metadata": {},
175 | "outputs": [
176 | {
177 | "name": "stdout",
178 | "output_type": "stream",
179 | "text": [
180 | "Parameter containing:\n",
181 | "tensor([[ 0.0000, 1.0000, 2.0000, 0.3214, -0.0462],\n",
182 | " [-0.3800, 0.4037, 0.0042, 0.4156, -0.4236],\n",
183 | " [ 0.2434, 0.2813, 0.1570, -0.4028, 0.0869]], grad_fn=)\n"
184 | ]
185 | }
186 | ],
187 | "source": [
188 | "mod.weight[0,0]=0\n",
189 | "mod.weight[0,1]=1\n",
190 | "mod.weight[0,2]=2\n",
191 | "print(mod.weight)"
192 | ]
193 | },
194 | {
195 | "cell_type": "markdown",
196 | "metadata": {},
197 | "source": [
198 | "### We can also make a Linear module without bias:"
199 | ]
200 | },
201 | {
202 | "cell_type": "code",
203 | "execution_count": 12,
204 | "metadata": {},
205 | "outputs": [
206 | {
207 | "name": "stdout",
208 | "output_type": "stream",
209 | "text": [
210 | "Linear(in_features=5, out_features=3, bias=False)\n"
211 | ]
212 | }
213 | ],
214 | "source": [
215 | "mod2 = nn.Linear(5,3,bias=False)\n",
216 | "print(mod2)"
217 | ]
218 | },
219 | {
220 | "cell_type": "code",
221 | "execution_count": 13,
222 | "metadata": {},
223 | "outputs": [
224 | {
225 | "name": "stdout",
226 | "output_type": "stream",
227 | "text": [
228 | "Parameter containing:\n",
229 | "tensor([[ 0.1703, 0.1601, 0.3649, -0.1387, -0.3961],\n",
230 | " [ 0.4339, 0.2803, 0.0350, -0.3152, 0.3601],\n",
231 | " [-0.0434, 0.4186, 0.1819, 0.0771, 0.1898]], requires_grad=True)\n"
232 | ]
233 | }
234 | ],
235 | "source": [
236 | "print(mod2.weight)"
237 | ]
238 | },
239 | {
240 | "cell_type": "code",
241 | "execution_count": 14,
242 | "metadata": {},
243 | "outputs": [
244 | {
245 | "name": "stdout",
246 | "output_type": "stream",
247 | "text": [
248 | "None\n"
249 | ]
250 | }
251 | ],
252 | "source": [
253 | "print(mod2.bias)"
254 | ]
255 | },
256 | {
257 | "cell_type": "code",
258 | "execution_count": null,
259 | "metadata": {},
260 | "outputs": [],
261 | "source": []
262 | }
263 | ],
264 | "metadata": {
265 | "kernelspec": {
266 | "display_name": "Python 3",
267 | "language": "python",
268 | "name": "python3"
269 | },
270 | "language_info": {
271 | "codemirror_mode": {
272 | "name": "ipython",
273 | "version": 3
274 | },
275 | "file_extension": ".py",
276 | "mimetype": "text/x-python",
277 | "name": "python",
278 | "nbconvert_exporter": "python",
279 | "pygments_lexer": "ipython3",
280 | "version": "3.6.10"
281 | }
282 | },
283 | "nbformat": 4,
284 | "nbformat_minor": 2
285 | }
286 |
--------------------------------------------------------------------------------
/codes/labs_lecture03/lab05_minibatch_training/minibatch_training_exercise.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Lab 05 : Train with mini-batches -- exercise"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": null,
13 | "metadata": {},
14 | "outputs": [],
15 | "source": [
16 | "# For Google Colaboratory\n",
17 | "import sys, os\n",
18 | "if 'google.colab' in sys.modules:\n",
19 | " # mount google drive\n",
20 | " from google.colab import drive\n",
21 | " drive.mount('/content/gdrive')\n",
22 | " # find automatically the path of the folder containing \"file_name\" :\n",
23 | " file_name = 'minibatch_training_exercise.ipynb'\n",
24 | " import subprocess\n",
25 | " path_to_file = subprocess.check_output('find . -type f -name ' + str(file_name), shell=True).decode(\"utf-8\")\n",
26 | " path_to_file = path_to_file.replace(file_name,\"\").replace('\\n',\"\")\n",
27 | " # if previous search failed or too long, comment the previous line and simply write down manually the path below :\n",
28 | " #path_to_file = '/content/gdrive/My Drive/CE7454_2020_codes/codes/labs_lecture03/lab05_minibatch_training'\n",
29 | " print(path_to_file)\n",
30 | " # change current path to the folder containing \"file_name\"\n",
31 | " os.chdir(path_to_file)\n",
32 | " !pwd"
33 | ]
34 | },
35 | {
36 | "cell_type": "code",
37 | "execution_count": null,
38 | "metadata": {},
39 | "outputs": [],
40 | "source": [
41 | "import torch\n",
42 | "import torch.nn as nn\n",
43 | "import torch.nn.functional as F\n",
44 | "import torch.optim as optim\n",
45 | "from random import randint\n",
46 | "import utils"
47 | ]
48 | },
49 | {
50 | "cell_type": "markdown",
51 | "metadata": {},
52 | "source": [
53 | "### Download the data and print the sizes"
54 | ]
55 | },
56 | {
57 | "cell_type": "code",
58 | "execution_count": null,
59 | "metadata": {},
60 | "outputs": [],
61 | "source": [
62 | "from utils import check_fashion_mnist_dataset_exists\n",
63 | "data_path=check_fashion_mnist_dataset_exists()"
64 | ]
65 | },
66 | {
67 | "cell_type": "code",
68 | "execution_count": null,
69 | "metadata": {},
70 | "outputs": [],
71 | "source": [
72 | "train_data=torch.load(data_path+'fashion-mnist/train_data.pt')\n",
73 | "\n",
74 | "print(train_data.size())"
75 | ]
76 | },
77 | {
78 | "cell_type": "code",
79 | "execution_count": null,
80 | "metadata": {},
81 | "outputs": [],
82 | "source": [
83 | "train_label=torch.load(data_path+'fashion-mnist/train_label.pt')\n",
84 | "\n",
85 | "print(train_label.size())"
86 | ]
87 | },
88 | {
89 | "cell_type": "code",
90 | "execution_count": null,
91 | "metadata": {},
92 | "outputs": [],
93 | "source": [
94 | "test_data=torch.load(data_path+'fashion-mnist/test_data.pt')\n",
95 | "\n",
96 | "print(test_data.size())"
97 | ]
98 | },
99 | {
100 | "cell_type": "markdown",
101 | "metadata": {},
102 | "source": [
103 | "### Make a one layer net class "
104 | ]
105 | },
106 | {
107 | "cell_type": "code",
108 | "execution_count": null,
109 | "metadata": {},
110 | "outputs": [],
111 | "source": [
112 | "class one_layer_net(nn.Module):\n",
113 | "\n",
114 | " def __init__(self, input_size, output_size):\n",
115 | " super(one_layer_net , self).__init__()\n",
116 | " \n",
117 | " self.linear_layer = # complete here\n",
118 | " \n",
119 | " def forward(self, x):\n",
120 | " \n",
121 | " y = # complete here\n",
122 | " prob = # complete here\n",
123 | " return prob"
124 | ]
125 | },
126 | {
127 | "cell_type": "markdown",
128 | "metadata": {},
129 | "source": [
130 | "### Build the net"
131 | ]
132 | },
133 | {
134 | "cell_type": "code",
135 | "execution_count": null,
136 | "metadata": {},
137 | "outputs": [],
138 | "source": [
139 | "net=one_layer_net(784,10)\n",
140 | "print(net)"
141 | ]
142 | },
143 | {
144 | "cell_type": "markdown",
145 | "metadata": {},
146 | "source": [
147 | "### Choose the size of the mini-batches "
148 | ]
149 | },
150 | {
151 | "cell_type": "code",
152 | "execution_count": null,
153 | "metadata": {},
154 | "outputs": [],
155 | "source": [
156 | "bs= # complete here"
157 | ]
158 | },
159 | {
160 | "cell_type": "markdown",
161 | "metadata": {},
162 | "source": [
163 | "### Train the network (only 5000 iterations) on the train set"
164 | ]
165 | },
166 | {
167 | "cell_type": "code",
168 | "execution_count": null,
169 | "metadata": {},
170 | "outputs": [],
171 | "source": [
172 | "criterion = nn.NLLLoss()\n",
173 | "optimizer=torch.optim.SGD(net.parameters() , lr=0.01 )\n",
174 | "\n",
175 | "for iter in range(1,5000):\n",
176 | " \n",
177 | " # create a minibatch\n",
178 | " indices= # complete here\n",
179 | " minibatch_data = # complete here\n",
180 | " minibatch_label= # complete here \n",
181 | " \n",
182 | " #reshape them to fit the network\n",
183 | " inputs= # complete here\n",
184 | "\n",
185 | " # feed the input to the net \n",
186 | " inputs.requires_grad_()\n",
187 | " prob= # complete here\n",
188 | " \n",
189 | " \n",
190 | " # update the weights (all the magic happens here -- we will discuss it later)\n",
191 | " log_prob=torch.log(prob)\n",
192 | " loss = criterion(log_prob, minibatch_label) \n",
193 | " optimizer.zero_grad() \n",
194 | " loss.backward()\n",
195 | " optimizer.step()"
196 | ]
197 | },
198 | {
199 | "cell_type": "markdown",
200 | "metadata": {},
201 | "source": [
202 | "### Choose image at random from the test set and see how good/bad are the predictions"
203 | ]
204 | },
205 | {
206 | "cell_type": "code",
207 | "execution_count": null,
208 | "metadata": {},
209 | "outputs": [],
210 | "source": [
211 | "# choose a picture at random\n",
212 | "idx=randint(0, 10000-1)\n",
213 | "im=test_data[idx]\n",
214 | "\n",
215 | "# diplay the picture\n",
216 | "utils.show(im)\n",
217 | "\n",
218 | "# feed it to the net and display the confidence scores\n",
219 | "prob = net( im.view(1,784)) \n",
220 | "utils.show_prob_fashion_mnist(prob)"
221 | ]
222 | },
223 | {
224 | "cell_type": "code",
225 | "execution_count": null,
226 | "metadata": {},
227 | "outputs": [],
228 | "source": []
229 | }
230 | ],
231 | "metadata": {
232 | "kernelspec": {
233 | "display_name": "Python 3",
234 | "language": "python",
235 | "name": "python3"
236 | },
237 | "language_info": {
238 | "codemirror_mode": {
239 | "name": "ipython",
240 | "version": 3
241 | },
242 | "file_extension": ".py",
243 | "mimetype": "text/x-python",
244 | "name": "python",
245 | "nbconvert_exporter": "python",
246 | "pygments_lexer": "ipython3",
247 | "version": "3.6.10"
248 | }
249 | },
250 | "nbformat": 4,
251 | "nbformat_minor": 2
252 | }
253 |
--------------------------------------------------------------------------------
/codes/labs_lecture05/lab01_mlp/mlp_exercise.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Lab 01 : MLP -- exercise\n",
8 | "\n",
9 | "# Understanding the training loop "
10 | ]
11 | },
12 | {
13 | "cell_type": "code",
14 | "execution_count": null,
15 | "metadata": {},
16 | "outputs": [],
17 | "source": [
18 | "# For Google Colaboratory\n",
19 | "import sys, os\n",
20 | "if 'google.colab' in sys.modules:\n",
21 | " # mount google drive\n",
22 | " from google.colab import drive\n",
23 | " drive.mount('/content/gdrive')\n",
24 | " # find automatically the path of the folder containing \"file_name\" :\n",
25 | " file_name = 'mlp_exercise.ipynb'\n",
26 | " import subprocess\n",
27 | " path_to_file = subprocess.check_output('find . -type f -name ' + str(file_name), shell=True).decode(\"utf-8\")\n",
28 | " path_to_file = path_to_file.replace(file_name,\"\").replace('\\n',\"\")\n",
29 | " # if previous search failed or too long, comment the previous line and simply write down manually the path below :\n",
30 | " #path_to_file = '/content/gdrive/My Drive/CE7454_2020_codes/codes/labs_lecture05/lab01_mlp'\n",
31 | " print(path_to_file)\n",
32 | " # change current path to the folder containing \"file_name\"\n",
33 | " os.chdir(path_to_file)\n",
34 | " !pwd"
35 | ]
36 | },
37 | {
38 | "cell_type": "code",
39 | "execution_count": null,
40 | "metadata": {},
41 | "outputs": [],
42 | "source": [
43 | "import torch\n",
44 | "import torch.nn as nn\n",
45 | "import torch.nn.functional as F\n",
46 | "import torch.optim as optim\n",
47 | "from random import randint\n",
48 | "import utils"
49 | ]
50 | },
51 | {
52 | "cell_type": "markdown",
53 | "metadata": {},
54 | "source": [
55 | "### Download the data and print the sizes"
56 | ]
57 | },
58 | {
59 | "cell_type": "code",
60 | "execution_count": null,
61 | "metadata": {},
62 | "outputs": [],
63 | "source": [
64 | "from utils import check_fashion_mnist_dataset_exists\n",
65 | "data_path=check_fashion_mnist_dataset_exists()\n"
66 | ]
67 | },
68 | {
69 | "cell_type": "code",
70 | "execution_count": null,
71 | "metadata": {},
72 | "outputs": [],
73 | "source": [
74 | "train_data=torch.load(data_path+'fashion-mnist/train_data.pt')\n",
75 | "\n",
76 | "print(train_data.size())"
77 | ]
78 | },
79 | {
80 | "cell_type": "code",
81 | "execution_count": null,
82 | "metadata": {},
83 | "outputs": [],
84 | "source": [
85 | "train_label=torch.load(data_path+'fashion-mnist/train_label.pt')\n",
86 | "\n",
87 | "print(train_label.size())"
88 | ]
89 | },
90 | {
91 | "cell_type": "code",
92 | "execution_count": null,
93 | "metadata": {},
94 | "outputs": [],
95 | "source": [
96 | "test_data=torch.load(data_path+'fashion-mnist/test_data.pt')\n",
97 | "\n",
98 | "print(test_data.size())"
99 | ]
100 | },
101 | {
102 | "cell_type": "markdown",
103 | "metadata": {},
104 | "source": [
105 | "### Make a ONE layer net class. The network output are the scores! No softmax needed! You have only one line to write in the forward function"
106 | ]
107 | },
108 | {
109 | "cell_type": "code",
110 | "execution_count": null,
111 | "metadata": {},
112 | "outputs": [],
113 | "source": [
114 | "class one_layer_net(nn.Module):\n",
115 | "\n",
116 | " def __init__(self, input_size, output_size):\n",
117 | " super(one_layer_net , self).__init__()\n",
118 | " self.linear_layer = # complete here\n",
119 | " \n",
120 | " def forward(self, x):\n",
121 | " scores = # complete here\n",
122 | " return scores"
123 | ]
124 | },
125 | {
126 | "cell_type": "markdown",
127 | "metadata": {},
128 | "source": [
129 | "### Build the net"
130 | ]
131 | },
132 | {
133 | "cell_type": "code",
134 | "execution_count": null,
135 | "metadata": {},
136 | "outputs": [],
137 | "source": [
138 | "net= # complete here\n",
139 | "print(net)"
140 | ]
141 | },
142 | {
143 | "cell_type": "markdown",
144 | "metadata": {},
145 | "source": [
146 | "### Choose the criterion and the optimizer: use the CHEAT SHEET to see the correct syntax. \n",
147 | "\n",
148 | "### Remember that the optimizer need to have access to the parameters of the network (net.parameters()).\n",
149 | "\n",
150 | "### Set the batchize and learning rate to be:\n",
151 | "### batchize = 50\n",
152 | "### learning rate = 0.01\n",
153 | "\n",
154 | "\n",
155 | "\n",
156 | "\n",
157 | "\n"
158 | ]
159 | },
160 | {
161 | "cell_type": "code",
162 | "execution_count": null,
163 | "metadata": {},
164 | "outputs": [],
165 | "source": [
166 | "# make the criterion\n",
167 | "criterion = # complete here\n",
168 | "\n",
169 | "# make the SGD optimizer. \n",
170 | "optimizer=torch.optim.SGD(net.parameters(),lr=0.01)\n",
171 | "\n",
172 | "# set up the batch size \n",
173 | "bs=50"
174 | ]
175 | },
176 | {
177 | "cell_type": "markdown",
178 | "metadata": {},
179 | "source": [
180 | "### Complete the training loop"
181 | ]
182 | },
183 | {
184 | "cell_type": "code",
185 | "execution_count": null,
186 | "metadata": {},
187 | "outputs": [],
188 | "source": [
189 | "for iter in range(1,5000):\n",
190 | " \n",
191 | " # Set dL/dU, dL/dV, dL/dW to be filled with zeros\n",
192 | " \n",
193 | " \n",
194 | " # create a minibatch\n",
195 | " \n",
196 | " \n",
197 | " # reshape the minibatch\n",
198 | " \n",
199 | " \n",
200 | " # tell Pytorch to start tracking all operations that will be done on \"inputs\"\n",
201 | " \n",
202 | "\n",
203 | " # forward the minibatch through the net \n",
204 | " \n",
205 | " \n",
206 | " # Compute the average of the losses of the data points in the minibatch\n",
207 | " \n",
208 | " \n",
209 | " # backward pass to compute dL/dU, dL/dV and dL/dW \n",
210 | " \n",
211 | " \n",
212 | " # do one step of stochastic gradient descent: U=U-lr(dL/dU), V=V-lr(dL/dU), ...\n",
213 | " \n",
214 | " "
215 | ]
216 | },
217 | {
218 | "cell_type": "markdown",
219 | "metadata": {},
220 | "source": [
221 | "### Choose image at random from the test set and see how good/bad are the predictions"
222 | ]
223 | },
224 | {
225 | "cell_type": "code",
226 | "execution_count": null,
227 | "metadata": {},
228 | "outputs": [],
229 | "source": [
230 | "# choose a picture at random\n",
231 | "idx=randint(0, 10000-1)\n",
232 | "im=test_data[idx]\n",
233 | "\n",
234 | "# diplay the picture\n",
235 | "utils.show(im)\n",
236 | "\n",
237 | "# feed it to the net and display the confidence scores\n",
238 | "scores = net( im.view(1,784)) \n",
239 | "probs= F.softmax(scores, dim=1)\n",
240 | "utils.show_prob_fashion_mnist(probs)"
241 | ]
242 | },
243 | {
244 | "cell_type": "code",
245 | "execution_count": null,
246 | "metadata": {},
247 | "outputs": [],
248 | "source": []
249 | }
250 | ],
251 | "metadata": {
252 | "kernelspec": {
253 | "display_name": "Python 3",
254 | "language": "python",
255 | "name": "python3"
256 | },
257 | "language_info": {
258 | "codemirror_mode": {
259 | "name": "ipython",
260 | "version": 3
261 | },
262 | "file_extension": ".py",
263 | "mimetype": "text/x-python",
264 | "name": "python",
265 | "nbconvert_exporter": "python",
266 | "pygments_lexer": "ipython3",
267 | "version": "3.6.10"
268 | }
269 | },
270 | "nbformat": 4,
271 | "nbformat_minor": 2
272 | }
273 |
--------------------------------------------------------------------------------
/codes/labs_lecture04/lab01_cross_entropy/utils.py:
--------------------------------------------------------------------------------
1 | import matplotlib.pyplot as plt
2 | import matplotlib
3 | import numpy as np
4 |
5 |
6 | import matplotlib.pyplot as plt
7 | import matplotlib
8 | import numpy as np
9 |
10 | def display_scores(sc):
11 |
12 |
13 | ft=10
14 | ft_label=12
15 |
16 | bs=sc.size(0)
17 | nb_class=sc.size(1)
18 |
19 | f, ax = plt.subplots(1, bs)
20 |
21 | if bs ==2:
22 | f.set_size_inches(8, 8)
23 | f.subplots_adjust(left=None, bottom=None, right=None, top=None,
24 | wspace=None, hspace=0.5)
25 | else:
26 | f.set_size_inches(12, 12)
27 | f.subplots_adjust(left=None, bottom=None, right=None, top=None,
28 | wspace=None, hspace=0.25)
29 |
30 | max_score= sc.max().item()
31 | min_score=sc.min().item()
32 |
33 | label_pos= min_score-8
34 | xmin=-5.5
35 | xmax=5.5
36 |
37 |
38 | label=[]
39 | for i in range(nb_class):
40 | str_nb="{0:.0f}".format(i)
41 | mystr='class '+ str_nb
42 | label.append(mystr)
43 |
44 | y_pos = np.arange(nb_class)*1.2
45 |
46 |
47 |
48 | for count in range(bs):
49 |
50 | ax[count].set_title('data point '+ "{0:.0f}".format(count))
51 |
52 | scores=sc[count].numpy()
53 |
54 | width=0.9
55 | col='darkgreen'
56 |
57 | # plt.rcdefaults()
58 |
59 | # line in the middle
60 | ax[count].plot([0,0], [y_pos[0]-1,y_pos[-1]+1], color='k',linewidth=4)
61 |
62 |
63 | # the plot
64 | barlist=ax[count].barh(y_pos, scores, width , align='center', color=col)
65 |
66 | for idx,bar in enumerate(barlist):
67 | if scores[idx]<0:
68 | bar.set_color('r')
69 |
70 | ax[count].set_xlim([xmin, xmax])
71 | ax[count].invert_yaxis()
72 |
73 | # no y label
74 | ax[count].set_yticklabels([])
75 | ax[count].set_yticks([])
76 |
77 | # x label
78 | ax[count].set_xticklabels([])
79 | ax[count].set_xticks([])
80 |
81 |
82 | ax[count].spines['right'].set_visible(False)
83 | ax[count].spines['top'].set_visible(False)
84 | ax[count].spines['bottom'].set_visible(False)
85 | ax[count].spines['left'].set_visible(False)
86 |
87 | ax[count].set_aspect('equal')
88 |
89 |
90 | for i in range(len(scores)):
91 |
92 | str_nb="{0:.1f}".format(scores[i])
93 | if scores[i]>=0:
94 | ax[count].text( scores[i] + 0.05 , y_pos[i] ,str_nb ,
95 | horizontalalignment='left', verticalalignment='center',
96 | transform=ax[count].transData, color= col,fontsize=ft)
97 | else:
98 | ax[count].text( scores[i] - 0.05 , y_pos[i] ,str_nb ,
99 | horizontalalignment='right', verticalalignment='center',
100 | transform=ax[count].transData, color= 'r',fontsize=ft)
101 |
102 | if count ==0:
103 | ax[0].text( label_pos , y_pos[i] , label[i] ,
104 | horizontalalignment='left', verticalalignment='center',
105 | transform=ax[0].transData, color= 'black',fontsize=ft_label)
106 |
107 |
108 | plt.show()
109 |
110 |
111 |
112 | import os.path
113 | def check_mnist_dataset_exists():
114 | flag_train_data = os.path.isfile('../data/mnist/train_data.pt')
115 | flag_train_label = os.path.isfile('../data/mnist/train_label.pt')
116 | flag_test_data = os.path.isfile('../data/mnist/test_data.pt')
117 | flag_test_label = os.path.isfile('../data/mnist/test_label.pt')
118 | if flag_train_data==False or flag_train_label==False or flag_test_data==False or flag_test_label==False:
119 | print('MNIST dataset missing - downloading...')
120 | import torchvision
121 | import torchvision.transforms as transforms
122 | trainset = torchvision.datasets.MNIST(root='../data/mnist/temp', train=True,
123 | download=True, transform=transforms.ToTensor())
124 | testset = torchvision.datasets.MNIST(root='../data/mnist/temp', train=False,
125 | download=True, transform=transforms.ToTensor())
126 | train_data=torch.Tensor(60000,28,28)
127 | train_label=torch.LongTensor(60000)
128 | for idx , example in enumerate(trainset):
129 | train_data[idx]=example[0].squeeze()
130 | train_label[idx]=example[1]
131 | torch.save(train_data,'../data/mnist/train_data.pt')
132 | torch.save(train_label,'../data/mnist/train_label.pt')
133 | test_data=torch.Tensor(10000,28,28)
134 | test_label=torch.LongTensor(10000)
135 | for idx , example in enumerate(testset):
136 | test_data[idx]=example[0].squeeze()
137 | test_label[idx]=example[1]
138 | torch.save(test_data,'../data/mnist/test_data.pt')
139 | torch.save(test_label,'../data/mnist/test_label.pt')
140 |
141 | def check_fashion_mnist_dataset_exists():
142 | flag_train_data = os.path.isfile('../data/fashion-mnist/train_data.pt')
143 | flag_train_label = os.path.isfile('../data/fashion-mnist/train_label.pt')
144 | flag_test_data = os.path.isfile('../data/fashion-mnist/test_data.pt')
145 | flag_test_label = os.path.isfile('../data/fashion-mnist/test_label.pt')
146 | if flag_train_data==False or flag_train_label==False or flag_test_data==False or flag_test_label==False:
147 | print('FASHION-MNIST dataset missing - downloading...')
148 | import torchvision
149 | import torchvision.transforms as transforms
150 | trainset = torchvision.datasets.FashionMNIST(root='../data/fashion-mnist/temp', train=True,
151 | download=True, transform=transforms.ToTensor())
152 | testset = torchvision.datasets.FashionMNIST(root='../data/fashion-mnist/temp', train=False,
153 | download=True, transform=transforms.ToTensor())
154 | train_data=torch.Tensor(60000,28,28)
155 | train_label=torch.LongTensor(60000)
156 | for idx , example in enumerate(trainset):
157 | train_data[idx]=example[0].squeeze()
158 | train_label[idx]=example[1]
159 | torch.save(train_data,'../data/fashion-mnist/train_data.pt')
160 | torch.save(train_label,'../data/fashion-mnist/train_label.pt')
161 | test_data=torch.Tensor(10000,28,28)
162 | test_label=torch.LongTensor(10000)
163 | for idx , example in enumerate(testset):
164 | test_data[idx]=example[0].squeeze()
165 | test_label[idx]=example[1]
166 | torch.save(test_data,'../data/fashion-mnist/test_data.pt')
167 | torch.save(test_label,'../data/fashion-mnist/test_label.pt')
168 |
169 | def check_cifar_dataset_exists():
170 | flag_train_data = os.path.isfile('../data/cifar/train_data.pt')
171 | flag_train_label = os.path.isfile('../data/cifar/train_label.pt')
172 | flag_test_data = os.path.isfile('../data/cifar/test_data.pt')
173 | flag_test_label = os.path.isfile('../data/cifar/test_label.pt')
174 | if flag_train_data==False or flag_train_label==False or flag_test_data==False or flag_test_label==False:
175 | print('CIFAR dataset missing - downloading... (5min)')
176 | import torchvision
177 | import torchvision.transforms as transforms
178 | trainset = torchvision.datasets.CIFAR10(root='../data/cifar/temp', train=True,
179 | download=True, transform=transforms.ToTensor())
180 | testset = torchvision.datasets.CIFAR10(root='../data/cifar/temp', train=False,
181 | download=True, transform=transforms.ToTensor())
182 | train_data=torch.Tensor(50000,3,32,32)
183 | train_label=torch.LongTensor(50000)
184 | for idx , example in enumerate(trainset):
185 | train_data[idx]=example[0]
186 | train_label[idx]=example[1]
187 | torch.save(train_data,'../data/cifar/train_data.pt')
188 | torch.save(train_label,'../data/cifar/train_label.pt')
189 | test_data=torch.Tensor(10000,3,32,32)
190 | test_label=torch.LongTensor(10000)
191 | for idx , example in enumerate(testset):
192 | test_data[idx]=example[0]
193 | test_label[idx]=example[1]
194 | torch.save(test_data,'../data/cifar/test_data.pt')
195 | torch.save(test_label,'../data/cifar/test_label.pt')
--------------------------------------------------------------------------------
/codes/labs_lecture05/lab03_monitoring_loss/monitoring_loss_exercise.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Lab 03 : Loss and error rate -- exercise"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": null,
13 | "metadata": {},
14 | "outputs": [],
15 | "source": [
16 | "# For Google Colaboratory\n",
17 | "import sys, os\n",
18 | "if 'google.colab' in sys.modules:\n",
19 | " # mount google drive\n",
20 | " from google.colab import drive\n",
21 | " drive.mount('/content/gdrive')\n",
22 | " # find automatically the path of the folder containing \"file_name\" :\n",
23 | " file_name = 'monitoring_loss_exercise.ipynb'\n",
24 | " import subprocess\n",
25 | " path_to_file = subprocess.check_output('find . -type f -name ' + str(file_name), shell=True).decode(\"utf-8\")\n",
26 | " path_to_file = path_to_file.replace(file_name,\"\").replace('\\n',\"\")\n",
27 | " # if previous search failed or too long, comment the previous line and simply write down manually the path below :\n",
28 | " #path_to_file = '/content/gdrive/My Drive/CE7454_2020_codes/codes/labs_lecture05/lab03_monitoring_loss'\n",
29 | " print(path_to_file)\n",
30 | " # change current path to the folder containing \"file_name\"\n",
31 | " os.chdir(path_to_file)\n",
32 | " !pwd"
33 | ]
34 | },
35 | {
36 | "cell_type": "code",
37 | "execution_count": 1,
38 | "metadata": {},
39 | "outputs": [],
40 | "source": [
41 | "import torch\n",
42 | "import torch.nn as nn\n",
43 | "import torch.nn.functional as F\n",
44 | "import torch.optim as optim\n",
45 | "from random import randint\n",
46 | "import utils"
47 | ]
48 | },
49 | {
50 | "cell_type": "markdown",
51 | "metadata": {},
52 | "source": [
53 | "### Download the CIFAR dataset -- check the size carefully!"
54 | ]
55 | },
56 | {
57 | "cell_type": "code",
58 | "execution_count": 2,
59 | "metadata": {},
60 | "outputs": [
61 | {
62 | "name": "stdout",
63 | "output_type": "stream",
64 | "text": [
65 | "torch.Size([50000, 3, 32, 32])\n"
66 | ]
67 | }
68 | ],
69 | "source": [
70 | "from utils import check_cifar_dataset_exists\n",
71 | "data_path=check_cifar_dataset_exists()\n",
72 | "\n",
73 | "train_data=torch.load(data_path+'cifar/train_data.pt')\n",
74 | "train_label=torch.load(data_path+'cifar/train_label.pt')\n",
75 | "test_data=torch.load(data_path+'cifar/test_data.pt')\n",
76 | "\n",
77 | "print(train_data.size())"
78 | ]
79 | },
80 | {
81 | "cell_type": "markdown",
82 | "metadata": {},
83 | "source": [
84 | "### Make a ONE layer net class. "
85 | ]
86 | },
87 | {
88 | "cell_type": "code",
89 | "execution_count": null,
90 | "metadata": {},
91 | "outputs": [],
92 | "source": [
93 | "class one_layer_net(nn.Module):\n",
94 | "\n",
95 | " def __init__(self, input_size, output_size):\n",
96 | " super(one_layer_net , self).__init__()\n",
97 | " self.linear_layer = nn.Linear( input_size, output_size , bias=True)\n",
98 | " \n",
99 | " def forward(self, x):\n",
100 | " scores = self.linear_layer(x)\n",
101 | " return scores"
102 | ]
103 | },
104 | {
105 | "cell_type": "markdown",
106 | "metadata": {},
107 | "source": [
108 | "### Build the net. "
109 | ]
110 | },
111 | {
112 | "cell_type": "code",
113 | "execution_count": null,
114 | "metadata": {},
115 | "outputs": [],
116 | "source": [
117 | "net= one_layer_net(3072,10)\n",
118 | "\n",
119 | "print(net)"
120 | ]
121 | },
122 | {
123 | "cell_type": "markdown",
124 | "metadata": {},
125 | "source": [
126 | "### Choose the criterion and optimizer. Also choose:\n",
127 | "\n",
128 | "### batchsize = 20\n",
129 | "\n",
130 | "### learning rate = 0.01"
131 | ]
132 | },
133 | {
134 | "cell_type": "code",
135 | "execution_count": null,
136 | "metadata": {},
137 | "outputs": [],
138 | "source": [
139 | "criterion = nn.CrossEntropyLoss()\n",
140 | "\n",
141 | "optimizer=torch.optim.SGD( net.parameters() , lr=0.01 )\n",
142 | "\n",
143 | "bs=20"
144 | ]
145 | },
146 | {
147 | "cell_type": "markdown",
148 | "metadata": {},
149 | "source": [
150 | "# You only have to complete this cell\n",
151 | "\n",
152 | "### Do 40 passes through the training set (which contains 50,000 images -- not 60,000 like mnist!)"
153 | ]
154 | },
155 | {
156 | "cell_type": "code",
157 | "execution_count": null,
158 | "metadata": {},
159 | "outputs": [],
160 | "source": [
161 | "for epoch in range(40):\n",
162 | " \n",
163 | " running_loss= # COMPLETE HERE\n",
164 | " running_error= # COMPLETE HERE\n",
165 | " num_batches= # COMPLETE HERE\n",
166 | " \n",
167 | " shuffled_indices=torch.randperm(50000)\n",
168 | " \n",
169 | " for # COMPLETE HERE\n",
170 | " \n",
171 | " # Set the gradients to zeros\n",
172 | " optimizer.zero_grad()\n",
173 | " \n",
174 | " # create a minibatch \n",
175 | " indices=shuffled_indices[count:count+bs]\n",
176 | " minibatch_data = train_data[indices]\n",
177 | " minibatch_label= train_label[indices]\n",
178 | "\n",
179 | " # reshape the minibatch\n",
180 | " inputs = minibatch_data.view(bs,3072)\n",
181 | "\n",
182 | " # tell Pytorch to start tracking all operations that will be done on \"inputs\"\n",
183 | " inputs.requires_grad_()\n",
184 | "\n",
185 | " # forward the minibatch through the net \n",
186 | " scores=net( inputs ) \n",
187 | "\n",
188 | " # Compute the average of the losses of the data points in the minibatch\n",
189 | " loss = criterion( scores , minibatch_label) \n",
190 | " \n",
191 | " # backward pass to compute dL/dU, dL/dV and dL/dW \n",
192 | " loss.backward()\n",
193 | "\n",
194 | " # do one step of stochastic gradient descent: U=U-lr(dL/dU), V=V-lr(dL/dU), ...\n",
195 | " optimizer.step()\n",
196 | " \n",
197 | " # START COMPUTING STATS\n",
198 | " \n",
199 | " # add the loss of this batch to the running loss\n",
200 | " running_loss += # COMPLETE HERE\n",
201 | " \n",
202 | " # compute the error made on this batch and add it to the running error \n",
203 | " error = # COMPLETE HERE\n",
204 | " running_error += # COMPLETE HERE\n",
205 | " \n",
206 | " num_batches += # COMPLETE HERE\n",
207 | " \n",
208 | " \n",
209 | " # compute stats for the full training set\n",
210 | " total_loss = # COMPLETE HERE\n",
211 | " total_error = # COMPLETE HERE\n",
212 | " \n",
213 | " print('epoch=',epoch, '\\t loss=', total_loss , '\\t error=', total_error*100 ,'percent')\n",
214 | " \n",
215 | " \n",
216 | " \n",
217 | " "
218 | ]
219 | },
220 | {
221 | "cell_type": "markdown",
222 | "metadata": {},
223 | "source": [
224 | "### Choose image at random from the test set and see how good/bad are the predictions"
225 | ]
226 | },
227 | {
228 | "cell_type": "code",
229 | "execution_count": null,
230 | "metadata": {},
231 | "outputs": [],
232 | "source": [
233 | "# choose a picture at random\n",
234 | "idx=randint(0, 10000-1)\n",
235 | "im=test_data[idx]\n",
236 | "\n",
237 | "# diplay the picture\n",
238 | "utils.show(im)\n",
239 | "\n",
240 | "# feed it to the net and display the confidence scores\n",
241 | "scores = net( im.view(1,3072)) \n",
242 | "probs= F.softmax(scores, dim=1)\n",
243 | "utils.show_prob_cifar(probs)"
244 | ]
245 | },
246 | {
247 | "cell_type": "code",
248 | "execution_count": null,
249 | "metadata": {},
250 | "outputs": [],
251 | "source": []
252 | }
253 | ],
254 | "metadata": {
255 | "kernelspec": {
256 | "display_name": "Python 3",
257 | "language": "python",
258 | "name": "python3"
259 | },
260 | "language_info": {
261 | "codemirror_mode": {
262 | "name": "ipython",
263 | "version": 3
264 | },
265 | "file_extension": ".py",
266 | "mimetype": "text/x-python",
267 | "name": "python",
268 | "nbconvert_exporter": "python",
269 | "pygments_lexer": "ipython3",
270 | "version": "3.6.10"
271 | }
272 | },
273 | "nbformat": 4,
274 | "nbformat_minor": 2
275 | }
276 |
--------------------------------------------------------------------------------
/codes/labs_lecture03/lab05_minibatch_training/utils.py:
--------------------------------------------------------------------------------
1 | import torch
2 | import numpy as np
3 | import matplotlib.pyplot as plt
4 |
5 | def show(X):
6 | if X.dim() == 3 and X.size(0) == 3:
7 | plt.imshow( np.transpose( X.numpy() , (1, 2, 0)) )
8 | plt.show()
9 | elif X.dim() == 2:
10 | plt.imshow( X.numpy() , cmap='gray' )
11 | plt.show()
12 | else:
13 | print('WRONG TENSOR SIZE')
14 |
15 |
16 |
17 |
18 | def show_prob_mnist(p):
19 |
20 | p=p.data.squeeze().numpy()
21 |
22 | ft=15
23 | label = ('zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight','nine')
24 | #p=p.data.squeeze().numpy()
25 | y_pos = np.arange(len(p))*1.2
26 | target=2
27 | width=0.9
28 | col= 'blue'
29 | #col='darkgreen'
30 |
31 | plt.rcdefaults()
32 | fig, ax = plt.subplots()
33 |
34 | # the plot
35 | ax.barh(y_pos, p, width , align='center', color=col)
36 |
37 | ax.set_xlim([0, 1.3])
38 | #ax.set_ylim([-0.8, len(p)*1.2-1+0.8])
39 |
40 | # y label
41 | ax.set_yticks(y_pos)
42 | ax.set_yticklabels(label, fontsize=ft)
43 | ax.invert_yaxis()
44 | #ax.set_xlabel('Performance')
45 | #ax.set_title('How fast do you want to go today?')
46 |
47 | # x label
48 | ax.set_xticklabels([])
49 | ax.set_xticks([])
50 | #x_pos=np.array([0, 0.25 , 0.5 , 0.75 , 1])
51 | #ax.set_xticks(x_pos)
52 | #ax.set_xticklabels( [0, 0.25 , 0.5 , 0.75 , 1] , fontsize=15)
53 |
54 | ax.spines['right'].set_visible(False)
55 | ax.spines['top'].set_visible(False)
56 | ax.spines['bottom'].set_visible(False)
57 | ax.spines['left'].set_linewidth(4)
58 |
59 |
60 | for i in range(len(p)):
61 | str_nb="{0:.2f}".format(p[i])
62 | ax.text( p[i] + 0.05 , y_pos[i] ,str_nb ,
63 | horizontalalignment='left', verticalalignment='center',
64 | transform=ax.transData, color= col,fontsize=ft)
65 |
66 |
67 |
68 | plt.show()
69 | #fig.savefig('pic/prob', dpi=96, bbox_inches="tight")
70 |
71 |
72 |
73 |
74 |
75 |
76 | def show_prob_fashion_mnist(p):
77 |
78 |
79 | p=p.data.squeeze().numpy()
80 |
81 | ft=15
82 | label = ('T-shirt', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag','Boot')
83 | #p=p.data.squeeze().numpy()
84 | y_pos = np.arange(len(p))*1.2
85 | target=2
86 | width=0.9
87 | col= 'blue'
88 | #col='darkgreen'
89 |
90 | plt.rcdefaults()
91 | fig, ax = plt.subplots()
92 |
93 | # the plot
94 | ax.barh(y_pos, p, width , align='center', color=col)
95 |
96 | ax.set_xlim([0, 1.3])
97 | #ax.set_ylim([-0.8, len(p)*1.2-1+0.8])
98 |
99 | # y label
100 | ax.set_yticks(y_pos)
101 | ax.set_yticklabels(label, fontsize=ft)
102 | ax.invert_yaxis()
103 | #ax.set_xlabel('Performance')
104 | #ax.set_title('How fast do you want to go today?')
105 |
106 | # x label
107 | ax.set_xticklabels([])
108 | ax.set_xticks([])
109 | #x_pos=np.array([0, 0.25 , 0.5 , 0.75 , 1])
110 | #ax.set_xticks(x_pos)
111 | #ax.set_xticklabels( [0, 0.25 , 0.5 , 0.75 , 1] , fontsize=15)
112 |
113 | ax.spines['right'].set_visible(False)
114 | ax.spines['top'].set_visible(False)
115 | ax.spines['bottom'].set_visible(False)
116 | ax.spines['left'].set_linewidth(4)
117 |
118 |
119 | for i in range(len(p)):
120 | str_nb="{0:.2f}".format(p[i])
121 | ax.text( p[i] + 0.05 , y_pos[i] ,str_nb ,
122 | horizontalalignment='left', verticalalignment='center',
123 | transform=ax.transData, color= col,fontsize=ft)
124 |
125 |
126 |
127 | plt.show()
128 | #fig.savefig('pic/prob', dpi=96, bbox_inches="tight")
129 |
130 |
131 |
132 | import os.path
133 | def check_mnist_dataset_exists(path_data='../../data/'):
134 | flag_train_data = os.path.isfile(path_data + 'mnist/train_data.pt')
135 | flag_train_label = os.path.isfile(path_data + 'mnist/train_label.pt')
136 | flag_test_data = os.path.isfile(path_data + 'mnist/test_data.pt')
137 | flag_test_label = os.path.isfile(path_data + 'mnist/test_label.pt')
138 | if flag_train_data==False or flag_train_label==False or flag_test_data==False or flag_test_label==False:
139 | print('MNIST dataset missing - downloading...')
140 | import torchvision
141 | import torchvision.transforms as transforms
142 | trainset = torchvision.datasets.MNIST(root=path_data + 'mnist/temp', train=True,
143 | download=True, transform=transforms.ToTensor())
144 | testset = torchvision.datasets.MNIST(root=path_data + 'mnist/temp', train=False,
145 | download=True, transform=transforms.ToTensor())
146 | train_data=torch.Tensor(60000,28,28)
147 | train_label=torch.LongTensor(60000)
148 | for idx , example in enumerate(trainset):
149 | train_data[idx]=example[0].squeeze()
150 | train_label[idx]=example[1]
151 | torch.save(train_data,path_data + 'mnist/train_data.pt')
152 | torch.save(train_label,path_data + 'mnist/train_label.pt')
153 | test_data=torch.Tensor(10000,28,28)
154 | test_label=torch.LongTensor(10000)
155 | for idx , example in enumerate(testset):
156 | test_data[idx]=example[0].squeeze()
157 | test_label[idx]=example[1]
158 | torch.save(test_data,path_data + 'mnist/test_data.pt')
159 | torch.save(test_label,path_data + 'mnist/test_label.pt')
160 | return path_data
161 |
162 | def check_fashion_mnist_dataset_exists(path_data='../../data/'):
163 | flag_train_data = os.path.isfile(path_data + 'fashion-mnist/train_data.pt')
164 | flag_train_label = os.path.isfile(path_data + 'fashion-mnist/train_label.pt')
165 | flag_test_data = os.path.isfile(path_data + 'fashion-mnist/test_data.pt')
166 | flag_test_label = os.path.isfile(path_data + 'fashion-mnist/test_label.pt')
167 | if flag_train_data==False or flag_train_label==False or flag_test_data==False or flag_test_label==False:
168 | print('FASHION-MNIST dataset missing - downloading...')
169 | import torchvision
170 | import torchvision.transforms as transforms
171 | trainset = torchvision.datasets.FashionMNIST(root=path_data + 'fashion-mnist/temp', train=True,
172 | download=True, transform=transforms.ToTensor())
173 | testset = torchvision.datasets.FashionMNIST(root=path_data + 'fashion-mnist/temp', train=False,
174 | download=True, transform=transforms.ToTensor())
175 | train_data=torch.Tensor(60000,28,28)
176 | train_label=torch.LongTensor(60000)
177 | for idx , example in enumerate(trainset):
178 | train_data[idx]=example[0].squeeze()
179 | train_label[idx]=example[1]
180 | torch.save(train_data,path_data + 'fashion-mnist/train_data.pt')
181 | torch.save(train_label,path_data + 'fashion-mnist/train_label.pt')
182 | test_data=torch.Tensor(10000,28,28)
183 | test_label=torch.LongTensor(10000)
184 | for idx , example in enumerate(testset):
185 | test_data[idx]=example[0].squeeze()
186 | test_label[idx]=example[1]
187 | torch.save(test_data,path_data + 'fashion-mnist/test_data.pt')
188 | torch.save(test_label,path_data + 'fashion-mnist/test_label.pt')
189 | return path_data
190 |
191 | def check_cifar_dataset_exists(path_data='../../data/'):
192 | flag_train_data = os.path.isfile(path_data + 'cifar/train_data.pt')
193 | flag_train_label = os.path.isfile(path_data + 'cifar/train_label.pt')
194 | flag_test_data = os.path.isfile(path_data + 'cifar/test_data.pt')
195 | flag_test_label = os.path.isfile(path_data + 'cifar/test_label.pt')
196 | if flag_train_data==False or flag_train_label==False or flag_test_data==False or flag_test_label==False:
197 | print('CIFAR dataset missing - downloading...')
198 | import torchvision
199 | import torchvision.transforms as transforms
200 | trainset = torchvision.datasets.CIFAR10(root=path_data + 'cifar/temp', train=True,
201 | download=True, transform=transforms.ToTensor())
202 | testset = torchvision.datasets.CIFAR10(root=path_data + 'cifar/temp', train=False,
203 | download=True, transform=transforms.ToTensor())
204 | train_data=torch.Tensor(50000,3,32,32)
205 | train_label=torch.LongTensor(50000)
206 | for idx , example in enumerate(trainset):
207 | train_data[idx]=example[0]
208 | train_label[idx]=example[1]
209 | torch.save(train_data,path_data + 'cifar/train_data.pt')
210 | torch.save(train_label,path_data + 'cifar/train_label.pt')
211 | test_data=torch.Tensor(10000,3,32,32)
212 | test_label=torch.LongTensor(10000)
213 | for idx , example in enumerate(testset):
214 | test_data[idx]=example[0]
215 | test_label[idx]=example[1]
216 | torch.save(test_data,path_data + 'cifar/test_data.pt')
217 | torch.save(test_label,path_data + 'cifar/test_label.pt')
218 | return path_data
219 |
220 |
221 |
--------------------------------------------------------------------------------
/codes/labs_lecture02/lab03_pytorch_tensor2/pytorch_tensor_part2_exercise.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Lab 03: Resizing and slicing in PyTorch -- exercise"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": null,
13 | "metadata": {},
14 | "outputs": [],
15 | "source": [
16 | "# For Google Colaboratory\n",
17 | "import sys, os\n",
18 | "if 'google.colab' in sys.modules:\n",
19 | " # mount google drive\n",
20 | " from google.colab import drive\n",
21 | " drive.mount('/content/gdrive')\n",
22 | " # find automatically the path of the folder containing \"file_name\" :\n",
23 | " file_name = 'pytorch_tensor_part2_exercise.ipynb'\n",
24 | " import subprocess\n",
25 | " path_to_file = subprocess.check_output('find . -type f -name ' + str(file_name), shell=True).decode(\"utf-8\")\n",
26 | " path_to_file = path_to_file.replace(file_name,\"\").replace('\\n',\"\")\n",
27 | " # if previous search failed or too long, comment the previous line and simply write down manually the path below :\n",
28 | " #path_to_file = '/content/gdrive/My Drive/CE7454_2020_codes/codes/labs_lecture02/lab03_pytorch_tensor2'\n",
29 | " print(path_to_file)\n",
30 | " # change current path to the folder containing \"file_name\"\n",
31 | " os.chdir(path_to_file)\n",
32 | " !pwd"
33 | ]
34 | },
35 | {
36 | "cell_type": "code",
37 | "execution_count": null,
38 | "metadata": {},
39 | "outputs": [],
40 | "source": [
41 | "import torch\n",
42 | "import utils"
43 | ]
44 | },
45 | {
46 | "cell_type": "markdown",
47 | "metadata": {},
48 | "source": [
49 | "### Make a 10 x 2 matrix random matrix A. Then store its third row (index = 2) in to a vector v. Then store the first 5 rows (index 0 to index 4) into a submatrix B. The important information is that B has a total of five rows. Print A, v and B."
50 | ]
51 | },
52 | {
53 | "cell_type": "code",
54 | "execution_count": null,
55 | "metadata": {},
56 | "outputs": [],
57 | "source": [
58 | "\n",
59 | "# write your code here\n"
60 | ]
61 | },
62 | {
63 | "cell_type": "markdown",
64 | "metadata": {},
65 | "source": [
66 | "### Extract entry (0,0) of the matrix A and store it into a PYTHON NUMBER x"
67 | ]
68 | },
69 | {
70 | "cell_type": "code",
71 | "execution_count": null,
72 | "metadata": {},
73 | "outputs": [],
74 | "source": [
75 | "\n",
76 | "# write your code here\n"
77 | ]
78 | },
79 | {
80 | "cell_type": "markdown",
81 | "metadata": {},
82 | "source": [
83 | "### Let's download 60,000 gray scale pictures as well as their label. Each picture is 28 by 28 pixels."
84 | ]
85 | },
86 | {
87 | "cell_type": "code",
88 | "execution_count": null,
89 | "metadata": {},
90 | "outputs": [],
91 | "source": [
92 | "from utils import check_mnist_dataset_exists\n",
93 | "data_path=check_mnist_dataset_exists()\n",
94 | "\n",
95 | "data=torch.load(data_path+'mnist/train_data.pt')\n",
96 | "label=torch.load(data_path+'mnist/train_label.pt')"
97 | ]
98 | },
99 | {
100 | "cell_type": "markdown",
101 | "metadata": {},
102 | "source": [
103 | "### Find the size of these two tensors"
104 | ]
105 | },
106 | {
107 | "cell_type": "code",
108 | "execution_count": null,
109 | "metadata": {},
110 | "outputs": [],
111 | "source": [
112 | "\n",
113 | "# write your code here\n"
114 | ]
115 | },
116 | {
117 | "cell_type": "markdown",
118 | "metadata": {},
119 | "source": [
120 | "### Print the first picture by slicing the data tensor. You will see the intensity of each pixel (a value between 0 and 1)"
121 | ]
122 | },
123 | {
124 | "cell_type": "code",
125 | "execution_count": null,
126 | "metadata": {
127 | "scrolled": false
128 | },
129 | "outputs": [],
130 | "source": [
131 | "\n",
132 | "# write your code here\n"
133 | ]
134 | },
135 | {
136 | "cell_type": "markdown",
137 | "metadata": {},
138 | "source": [
139 | "### The function show() from the \"utils\" package will display the picture:"
140 | ]
141 | },
142 | {
143 | "cell_type": "code",
144 | "execution_count": null,
145 | "metadata": {},
146 | "outputs": [],
147 | "source": [
148 | "utils.show(data[10])"
149 | ]
150 | },
151 | {
152 | "cell_type": "markdown",
153 | "metadata": {},
154 | "source": [
155 | "### Print the first entry of the label vector. The label is 5 telling you that this is the picture of a five."
156 | ]
157 | },
158 | {
159 | "cell_type": "code",
160 | "execution_count": null,
161 | "metadata": {},
162 | "outputs": [],
163 | "source": [
164 | "\n",
165 | "# write your code here\n"
166 | ]
167 | },
168 | {
169 | "cell_type": "markdown",
170 | "metadata": {},
171 | "source": [
172 | "### Display picture 20 of the dataset and print its label"
173 | ]
174 | },
175 | {
176 | "cell_type": "code",
177 | "execution_count": null,
178 | "metadata": {},
179 | "outputs": [],
180 | "source": [
181 | "\n",
182 | "# write your code here\n"
183 | ]
184 | },
185 | {
186 | "cell_type": "markdown",
187 | "metadata": {},
188 | "source": [
189 | "### Print the label corresponding to picture 10,000 10,001 10,002 10,003 and 10,004. So you need to extract 5 entries starting from entry 10,000."
190 | ]
191 | },
192 | {
193 | "cell_type": "code",
194 | "execution_count": null,
195 | "metadata": {},
196 | "outputs": [],
197 | "source": [
198 | "\n",
199 | "# write your code here\n"
200 | ]
201 | },
202 | {
203 | "cell_type": "markdown",
204 | "metadata": {},
205 | "source": [
206 | "### Display the two pictures that have label 9"
207 | ]
208 | },
209 | {
210 | "cell_type": "code",
211 | "execution_count": null,
212 | "metadata": {},
213 | "outputs": [],
214 | "source": [
215 | "\n",
216 | "# write your code here\n"
217 | ]
218 | },
219 | {
220 | "cell_type": "markdown",
221 | "metadata": {},
222 | "source": [
223 | "### Lets now play with the CIFAR data set. These are RGB pictures"
224 | ]
225 | },
226 | {
227 | "cell_type": "code",
228 | "execution_count": null,
229 | "metadata": {},
230 | "outputs": [],
231 | "source": [
232 | "from utils import check_cifar_dataset_exists\n",
233 | "data_path=check_cifar_dataset_exists()\n",
234 | "\n",
235 | "data=torch.load(data_path+'cifar/train_data.pt')\n",
236 | "label=torch.load(data_path+'cifar/train_label.pt')"
237 | ]
238 | },
239 | {
240 | "cell_type": "markdown",
241 | "metadata": {},
242 | "source": [
243 | "### Find the size of these two tensors. How many pictures? How many pixels? Note that it is a 4-dimensional Tensor. Dimension 0 gives you the index of the picture, dimension 1 gives you the chanel (R, G or B) and the last two dimension gives you the pixel location."
244 | ]
245 | },
246 | {
247 | "cell_type": "code",
248 | "execution_count": null,
249 | "metadata": {},
250 | "outputs": [],
251 | "source": [
252 | "\n",
253 | "# write your code here\n"
254 | ]
255 | },
256 | {
257 | "cell_type": "markdown",
258 | "metadata": {},
259 | "source": [
260 | "### Extract the first picture (a 3 x 32 x 32 Tensor) and check its size."
261 | ]
262 | },
263 | {
264 | "cell_type": "code",
265 | "execution_count": null,
266 | "metadata": {},
267 | "outputs": [],
268 | "source": [
269 | "\n",
270 | "# write your code here\n"
271 | ]
272 | },
273 | {
274 | "cell_type": "markdown",
275 | "metadata": {},
276 | "source": [
277 | "### Display picture 7, 40 and 100 of the data set with utils.show() and print its label. For CIFAR, the label are:\n",
278 | "0) Airplane \n",
279 | "1) Automobile \n",
280 | "2) Bird \n",
281 | "3) Cat \n",
282 | "4) Deer \n",
283 | "5) Dog \n",
284 | "6) Frog \n",
285 | "7) Horse \n",
286 | "8) Ship \n",
287 | "9) Truck\n",
288 | "\n",
289 | "For example, a picture of a dog will have label 5."
290 | ]
291 | },
292 | {
293 | "cell_type": "code",
294 | "execution_count": null,
295 | "metadata": {},
296 | "outputs": [],
297 | "source": [
298 | "\n",
299 | "# write your code here\n"
300 | ]
301 | }
302 | ],
303 | "metadata": {
304 | "kernelspec": {
305 | "display_name": "Python 3",
306 | "language": "python",
307 | "name": "python3"
308 | },
309 | "language_info": {
310 | "codemirror_mode": {
311 | "name": "ipython",
312 | "version": 3
313 | },
314 | "file_extension": ".py",
315 | "mimetype": "text/x-python",
316 | "name": "python",
317 | "nbconvert_exporter": "python",
318 | "pygments_lexer": "ipython3",
319 | "version": "3.6.10"
320 | }
321 | },
322 | "nbformat": 4,
323 | "nbformat_minor": 2
324 | }
325 |
--------------------------------------------------------------------------------
/codes/data/mnist/generate_mnist.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "## Download MNIST"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": null,
13 | "metadata": {},
14 | "outputs": [],
15 | "source": [
16 | "# For Google Colaboratory\n",
17 | "import sys, os\n",
18 | "if 'google.colab' in sys.modules:\n",
19 | " from google.colab import drive\n",
20 | " drive.mount('/content/gdrive')\n",
21 | " file_name = 'generate_mnist.ipynb'\n",
22 | " import subprocess\n",
23 | " path_to_file = subprocess.check_output('find . -type f -name ' + str(file_name), shell=True).decode(\"utf-8\")\n",
24 | " print(path_to_file)\n",
25 | " path_to_file = path_to_file.replace(file_name,\"\").replace('\\n',\"\")\n",
26 | " os.chdir(path_to_file)\n",
27 | " !pwd"
28 | ]
29 | },
30 | {
31 | "cell_type": "code",
32 | "execution_count": 1,
33 | "metadata": {},
34 | "outputs": [],
35 | "source": [
36 | "import torch\n",
37 | "import torchvision\n",
38 | "import torchvision.transforms as transforms\n",
39 | "import numpy as np\n",
40 | "import matplotlib.pyplot as plt"
41 | ]
42 | },
43 | {
44 | "cell_type": "code",
45 | "execution_count": 2,
46 | "metadata": {},
47 | "outputs": [],
48 | "source": [
49 | "trainset = torchvision.datasets.MNIST(root='./temp', train=True,\n",
50 | " download=True, transform=transforms.ToTensor())\n",
51 | "testset = torchvision.datasets.MNIST(root='./temp', train=False,\n",
52 | " download=True, transform=transforms.ToTensor())"
53 | ]
54 | },
55 | {
56 | "cell_type": "code",
57 | "execution_count": 3,
58 | "metadata": {
59 | "scrolled": true
60 | },
61 | "outputs": [
62 | {
63 | "data": {
64 | "image/png": "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\n",
65 | "text/plain": [
66 | ""
67 | ]
68 | },
69 | "metadata": {},
70 | "output_type": "display_data"
71 | },
72 | {
73 | "name": "stdout",
74 | "output_type": "stream",
75 | "text": [
76 | "tensor(9)\n"
77 | ]
78 | }
79 | ],
80 | "source": [
81 | "idx=4\n",
82 | "pic, label =trainset[idx]\n",
83 | "pic=pic.squeeze()\n",
84 | "plt.imshow(pic.numpy(), cmap='gray')\n",
85 | "plt.show()\n",
86 | "print(label)"
87 | ]
88 | },
89 | {
90 | "cell_type": "code",
91 | "execution_count": 4,
92 | "metadata": {},
93 | "outputs": [],
94 | "source": [
95 | "train_data=torch.Tensor(60000,28,28)\n",
96 | "train_label=torch.LongTensor(60000)\n",
97 | "for idx , example in enumerate(trainset):\n",
98 | " train_data[idx]=example[0].squeeze()\n",
99 | " train_label[idx]=example[1]\n",
100 | "torch.save(train_data,'train_data.pt')\n",
101 | "torch.save(train_label,'train_label.pt')"
102 | ]
103 | },
104 | {
105 | "cell_type": "code",
106 | "execution_count": 5,
107 | "metadata": {},
108 | "outputs": [],
109 | "source": [
110 | "test_data=torch.Tensor(10000,28,28)\n",
111 | "test_label=torch.LongTensor(10000)\n",
112 | "for idx , example in enumerate(testset):\n",
113 | " test_data[idx]=example[0].squeeze()\n",
114 | " test_label[idx]=example[1]\n",
115 | "torch.save(test_data,'test_data.pt')\n",
116 | "torch.save(test_label,'test_label.pt')"
117 | ]
118 | },
119 | {
120 | "cell_type": "code",
121 | "execution_count": null,
122 | "metadata": {},
123 | "outputs": [],
124 | "source": []
125 | },
126 | {
127 | "cell_type": "code",
128 | "execution_count": null,
129 | "metadata": {},
130 | "outputs": [],
131 | "source": []
132 | }
133 | ],
134 | "metadata": {
135 | "kernelspec": {
136 | "display_name": "Python 3",
137 | "language": "python",
138 | "name": "python3"
139 | },
140 | "language_info": {
141 | "codemirror_mode": {
142 | "name": "ipython",
143 | "version": 3
144 | },
145 | "file_extension": ".py",
146 | "mimetype": "text/x-python",
147 | "name": "python",
148 | "nbconvert_exporter": "python",
149 | "pygments_lexer": "ipython3",
150 | "version": "3.6.8"
151 | }
152 | },
153 | "nbformat": 4,
154 | "nbformat_minor": 2
155 | }
156 |
--------------------------------------------------------------------------------
/codes/labs_lecture03/lab04_train_vanilla_nn/train_vanilla_nn_exercise.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Lab 04 : Train vanilla neural network -- exercise\n",
8 | "\n",
9 | "\n",
10 | "# Training a one-layer net on FASHION-MNIST"
11 | ]
12 | },
13 | {
14 | "cell_type": "code",
15 | "execution_count": null,
16 | "metadata": {},
17 | "outputs": [],
18 | "source": [
19 | "# For Google Colaboratory\n",
20 | "import sys, os\n",
21 | "if 'google.colab' in sys.modules:\n",
22 | " # mount google drive\n",
23 | " from google.colab import drive\n",
24 | " drive.mount('/content/gdrive')\n",
25 | " # find automatically the path of the folder containing \"file_name\" :\n",
26 | " file_name = 'train_vanilla_nn_exercise.ipynb'\n",
27 | " import subprocess\n",
28 | " path_to_file = subprocess.check_output('find . -type f -name ' + str(file_name), shell=True).decode(\"utf-8\")\n",
29 | " path_to_file = path_to_file.replace(file_name,\"\").replace('\\n',\"\")\n",
30 | " # if previous search failed or too long, comment the previous line and simply write down manually the path below :\n",
31 | " #path_to_file = '/content/gdrive/My Drive/CE7454_2020_codes/codes/labs_lecture03/lab04_train_vanilla_nn'\n",
32 | " print(path_to_file)\n",
33 | " # change current path to the folder containing \"file_name\"\n",
34 | " os.chdir(path_to_file)\n",
35 | " !pwd"
36 | ]
37 | },
38 | {
39 | "cell_type": "code",
40 | "execution_count": null,
41 | "metadata": {},
42 | "outputs": [],
43 | "source": [
44 | "import torch\n",
45 | "import torch.nn as nn\n",
46 | "import torch.nn.functional as F\n",
47 | "import torch.optim as optim\n",
48 | "from random import randint\n",
49 | "import utils"
50 | ]
51 | },
52 | {
53 | "cell_type": "markdown",
54 | "metadata": {},
55 | "source": [
56 | "### Download the TRAINING SET (data+labels)"
57 | ]
58 | },
59 | {
60 | "cell_type": "code",
61 | "execution_count": null,
62 | "metadata": {},
63 | "outputs": [],
64 | "source": [
65 | "from utils import check_fashion_mnist_dataset_exists\n",
66 | "data_path=check_fashion_mnist_dataset_exists()\n",
67 | "\n",
68 | "train_data=torch.load(data_path+'fashion-mnist/train_data.pt')\n",
69 | "train_label=torch.load(data_path+'fashion-mnist/train_label.pt')\n",
70 | "print(train_data.size())\n",
71 | "print(train_label.size())"
72 | ]
73 | },
74 | {
75 | "cell_type": "markdown",
76 | "metadata": {},
77 | "source": [
78 | "### Download the TEST SET (data only)"
79 | ]
80 | },
81 | {
82 | "cell_type": "code",
83 | "execution_count": null,
84 | "metadata": {},
85 | "outputs": [],
86 | "source": [
87 | "test_data=torch.load(data_path+'fashion-mnist/test_data.pt')\n",
88 | "print(test_data.size())"
89 | ]
90 | },
91 | {
92 | "cell_type": "markdown",
93 | "metadata": {},
94 | "source": [
95 | "### Make a one layer net class"
96 | ]
97 | },
98 | {
99 | "cell_type": "code",
100 | "execution_count": null,
101 | "metadata": {},
102 | "outputs": [],
103 | "source": [
104 | "class one_layer_net(nn.Module):\n",
105 | "\n",
106 | " def __init__(self, input_size, output_size):\n",
107 | " super(one_layer_net , self).__init__()\n",
108 | " \n",
109 | " # complete here\n",
110 | " \n",
111 | " def forward(self, x):\n",
112 | " \n",
113 | " x = # complete here\n",
114 | " p = # complete here\n",
115 | " \n",
116 | " return p"
117 | ]
118 | },
119 | {
120 | "cell_type": "markdown",
121 | "metadata": {},
122 | "source": [
123 | "### Build the net"
124 | ]
125 | },
126 | {
127 | "cell_type": "code",
128 | "execution_count": null,
129 | "metadata": {},
130 | "outputs": [],
131 | "source": [
132 | "net=one_layer_net(784,10)\n",
133 | "print(net)"
134 | ]
135 | },
136 | {
137 | "cell_type": "markdown",
138 | "metadata": {},
139 | "source": [
140 | "### Take the 4th image of the test set:"
141 | ]
142 | },
143 | {
144 | "cell_type": "code",
145 | "execution_count": null,
146 | "metadata": {},
147 | "outputs": [],
148 | "source": [
149 | "im= # complete here\n",
150 | "utils.show(im)"
151 | ]
152 | },
153 | {
154 | "cell_type": "markdown",
155 | "metadata": {},
156 | "source": [
157 | "### And feed it to the UNTRAINED network:"
158 | ]
159 | },
160 | {
161 | "cell_type": "code",
162 | "execution_count": null,
163 | "metadata": {},
164 | "outputs": [],
165 | "source": [
166 | "p = # complete here\n",
167 | "print(p)"
168 | ]
169 | },
170 | {
171 | "cell_type": "markdown",
172 | "metadata": {},
173 | "source": [
174 | "### Display visually the confidence scores"
175 | ]
176 | },
177 | {
178 | "cell_type": "code",
179 | "execution_count": null,
180 | "metadata": {},
181 | "outputs": [],
182 | "source": [
183 | "utils.show_prob_fashion_mnist(p)"
184 | ]
185 | },
186 | {
187 | "cell_type": "markdown",
188 | "metadata": {},
189 | "source": [
190 | "### Train the network (only 5000 iterations) on the train set"
191 | ]
192 | },
193 | {
194 | "cell_type": "code",
195 | "execution_count": null,
196 | "metadata": {},
197 | "outputs": [],
198 | "source": [
199 | "criterion = nn.NLLLoss()\n",
200 | "optimizer=torch.optim.SGD(net.parameters() , lr=0.01 )\n",
201 | "\n",
202 | "for iter in range(1,5000):\n",
203 | " \n",
204 | " # choose a random integer between 0 and 59,999 \n",
205 | " # extract the corresponding picture and label\n",
206 | " # and reshape them to fit the network\n",
207 | "\n",
208 | " # complete here\n",
209 | " # complete here\n",
210 | " # complete here\n",
211 | "\n",
212 | "\n",
213 | " # feed the input to the net \n",
214 | " input.requires_grad_() # for backprobagation -- we will discuss it later\n",
215 | " # complete here \n",
216 | " \n",
217 | " # update the weights (all the magic happens here -- we will discuss it later)\n",
218 | " log_prob=torch.log(prob)\n",
219 | " loss = criterion(log_prob, label) \n",
220 | " optimizer.zero_grad() \n",
221 | " loss.backward()\n",
222 | " optimizer.step()"
223 | ]
224 | },
225 | {
226 | "cell_type": "markdown",
227 | "metadata": {},
228 | "source": [
229 | "### Take the 34th image of the test set:"
230 | ]
231 | },
232 | {
233 | "cell_type": "code",
234 | "execution_count": null,
235 | "metadata": {},
236 | "outputs": [],
237 | "source": [
238 | "im= # complete here\n",
239 | "utils.show(im)"
240 | ]
241 | },
242 | {
243 | "cell_type": "markdown",
244 | "metadata": {},
245 | "source": [
246 | "### Feed it to the TRAINED net:"
247 | ]
248 | },
249 | {
250 | "cell_type": "code",
251 | "execution_count": null,
252 | "metadata": {},
253 | "outputs": [],
254 | "source": [
255 | "p = # complete here\n",
256 | "print(p)"
257 | ]
258 | },
259 | {
260 | "cell_type": "markdown",
261 | "metadata": {},
262 | "source": [
263 | "### Display visually the confidence scores"
264 | ]
265 | },
266 | {
267 | "cell_type": "code",
268 | "execution_count": null,
269 | "metadata": {},
270 | "outputs": [],
271 | "source": [
272 | "utils.show_prob_fashion_mnist(prob)"
273 | ]
274 | },
275 | {
276 | "cell_type": "markdown",
277 | "metadata": {},
278 | "source": [
279 | "### Choose image at random from the test set and see how good/bad are the predictions"
280 | ]
281 | },
282 | {
283 | "cell_type": "code",
284 | "execution_count": null,
285 | "metadata": {},
286 | "outputs": [],
287 | "source": [
288 | "# choose a picture at random\n",
289 | "idx=randint(0, 10000-1)\n",
290 | "im=test_data[idx]\n",
291 | "\n",
292 | "# diplay the picture\n",
293 | "utils.show(im)\n",
294 | "\n",
295 | "# feed it to the net and display the confidence scores\n",
296 | "prob = net( im.view(1,784)) \n",
297 | "utils.show_prob_fashion_mnist(prob)"
298 | ]
299 | },
300 | {
301 | "cell_type": "code",
302 | "execution_count": null,
303 | "metadata": {},
304 | "outputs": [],
305 | "source": []
306 | }
307 | ],
308 | "metadata": {
309 | "kernelspec": {
310 | "display_name": "Python 3",
311 | "language": "python",
312 | "name": "python3"
313 | },
314 | "language_info": {
315 | "codemirror_mode": {
316 | "name": "ipython",
317 | "version": 3
318 | },
319 | "file_extension": ".py",
320 | "mimetype": "text/x-python",
321 | "name": "python",
322 | "nbconvert_exporter": "python",
323 | "pygments_lexer": "ipython3",
324 | "version": "3.6.10"
325 | }
326 | },
327 | "nbformat": 4,
328 | "nbformat_minor": 2
329 | }
330 |
--------------------------------------------------------------------------------
/codes/labs_lecture08/lab01_conv_layer/conv_layer_demo.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Lab 01 : Convolutional layer - demo"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": 2,
13 | "metadata": {},
14 | "outputs": [],
15 | "source": [
16 | "import torch\n",
17 | "import torch.nn as nn"
18 | ]
19 | },
20 | {
21 | "cell_type": "markdown",
22 | "metadata": {},
23 | "source": [
24 | "### Make a convolutional module\n",
25 | "* inputs: 2 channels\n",
26 | "* output: 5 activation maps \n",
27 | "* filters are 3x3\n",
28 | "* padding with one layer of zero to not shrink anything"
29 | ]
30 | },
31 | {
32 | "cell_type": "code",
33 | "execution_count": 3,
34 | "metadata": {},
35 | "outputs": [],
36 | "source": [
37 | "mod = nn.Conv2d( 2 , 5 , kernel_size=3, padding=1 )"
38 | ]
39 | },
40 | {
41 | "cell_type": "markdown",
42 | "metadata": {},
43 | "source": [
44 | "### Make an input 2 x 6 x 6 (two channels, each one has 6 x 6 pixels )"
45 | ]
46 | },
47 | {
48 | "cell_type": "code",
49 | "execution_count": 4,
50 | "metadata": {},
51 | "outputs": [
52 | {
53 | "name": "stdout",
54 | "output_type": "stream",
55 | "text": [
56 | "tensor([[[[0.9647, 0.6641, 0.1846, 0.0741, 0.2426, 0.0286],\n",
57 | " [0.2926, 0.2690, 0.8945, 0.7917, 0.6449, 0.4339],\n",
58 | " [0.4199, 0.9387, 0.3030, 0.7605, 0.5784, 0.1366],\n",
59 | " [0.0920, 0.2539, 0.1089, 0.0882, 0.4449, 0.9903],\n",
60 | " [0.0181, 0.6897, 0.2516, 0.8399, 0.7515, 0.3681],\n",
61 | " [0.6694, 0.0813, 0.5470, 0.1661, 0.7311, 0.2856]],\n",
62 | "\n",
63 | " [[0.0572, 0.5530, 0.5090, 0.3118, 0.4822, 0.0546],\n",
64 | " [0.6331, 0.2684, 0.6874, 0.0646, 0.2393, 0.4566],\n",
65 | " [0.4991, 0.1681, 0.3625, 0.3109, 0.8576, 0.1604],\n",
66 | " [0.5816, 0.0894, 0.6019, 0.9650, 0.5826, 0.2410],\n",
67 | " [0.9037, 0.5362, 0.3347, 0.6226, 0.9154, 0.0774],\n",
68 | " [0.7821, 0.1263, 0.7604, 0.9761, 0.5548, 0.0411]]]])\n",
69 | "torch.Size([1, 2, 6, 6])\n"
70 | ]
71 | }
72 | ],
73 | "source": [
74 | "bs=1\n",
75 | "\n",
76 | "x=torch.rand(bs,2,6,6)\n",
77 | "\n",
78 | "print(x)\n",
79 | "\n",
80 | "print(x.size())"
81 | ]
82 | },
83 | {
84 | "cell_type": "markdown",
85 | "metadata": {},
86 | "source": [
87 | "### Feed it to the convolutional layer: the output should have 5 channels (each one is 6x6)"
88 | ]
89 | },
90 | {
91 | "cell_type": "code",
92 | "execution_count": 5,
93 | "metadata": {},
94 | "outputs": [
95 | {
96 | "name": "stdout",
97 | "output_type": "stream",
98 | "text": [
99 | "tensor([[[[-0.1464, -0.5058, -0.4121, -0.3813, -0.1551, -0.2146],\n",
100 | " [-0.1091, -0.1937, -0.4113, -0.4900, -0.4752, -0.3575],\n",
101 | " [-0.1968, -0.2069, -0.5147, -0.5477, -0.3661, -0.1405],\n",
102 | " [-0.3232, -0.2430, -0.4195, -0.3464, -0.4819, -0.2669],\n",
103 | " [-0.2057, -0.3815, -0.4011, -0.6929, -0.5494, -0.2856],\n",
104 | " [ 0.2158, -0.4186, 0.0138, -0.1730, 0.0374, -0.2357]],\n",
105 | "\n",
106 | " [[ 0.0326, 0.3959, 0.2708, 0.3268, 0.2733, 0.2370],\n",
107 | " [-0.0920, 0.4574, 0.1929, 0.4797, 0.2473, 0.2792],\n",
108 | " [ 0.2269, 0.2934, 0.5939, 0.0310, 0.1756, 0.4366],\n",
109 | " [ 0.1510, 0.0887, 0.2965, 0.3478, 0.3414, 0.1654],\n",
110 | " [ 0.3653, 0.3119, 0.6454, 0.2852, 0.3365, 0.3320],\n",
111 | " [ 0.3551, 0.4914, 0.2409, 0.5954, 0.4347, 0.2755]],\n",
112 | "\n",
113 | " [[ 0.0403, 0.0786, 0.4327, 0.0157, 0.0642, 0.1806],\n",
114 | " [ 0.3882, 0.2421, 0.1530, 0.2215, 0.3505, 0.1158],\n",
115 | " [ 0.1697, 0.1244, 0.3598, 0.3732, 0.4586, 0.4285],\n",
116 | " [ 0.4907, 0.3364, 0.4214, 0.2710, 0.2338, 0.0839],\n",
117 | " [ 0.3489, -0.3433, 0.1847, 0.1535, 0.4897, 0.1452],\n",
118 | " [ 0.1153, 0.2302, 0.2669, 0.0721, 0.1907, 0.4729]],\n",
119 | "\n",
120 | " [[-0.0360, 0.1642, 0.3257, 0.4081, 0.1841, 0.3494],\n",
121 | " [ 0.1032, 0.2319, -0.1029, -0.1142, 0.3707, 0.2799],\n",
122 | " [-0.0367, -0.1640, 0.0386, -0.0038, 0.2665, 0.4206],\n",
123 | " [-0.0241, 0.3080, 0.1909, 0.0266, 0.2889, 0.1669],\n",
124 | " [ 0.1545, -0.0705, -0.0506, 0.0191, 0.1389, 0.3643],\n",
125 | " [-0.0852, -0.0449, -0.1369, -0.0463, -0.1860, -0.1205]],\n",
126 | "\n",
127 | " [[-0.1925, -0.2259, -0.1972, -0.0347, 0.0645, 0.0102],\n",
128 | " [ 0.1180, -0.1686, -0.1686, -0.3588, -0.2420, -0.2505],\n",
129 | " [-0.0371, -0.1183, -0.0901, -0.2361, -0.2311, -0.2995],\n",
130 | " [ 0.2258, -0.2436, 0.2367, 0.0642, -0.2452, -0.4158],\n",
131 | " [-0.0117, -0.0458, -0.0610, -0.0914, -0.4276, -0.2875],\n",
132 | " [ 0.2252, -0.1837, 0.1722, 0.1150, -0.1843, -0.3350]]]],\n",
133 | " grad_fn=)\n",
134 | "torch.Size([1, 5, 6, 6])\n"
135 | ]
136 | }
137 | ],
138 | "source": [
139 | "y=mod(x)\n",
140 | "\n",
141 | "print(y)\n",
142 | "\n",
143 | "print(y.size())"
144 | ]
145 | },
146 | {
147 | "cell_type": "markdown",
148 | "metadata": {},
149 | "source": [
150 | "### Lets look at the 5 filters.\n",
151 | "* Our filters are 2x3x3\n",
152 | "* Each of the filter has 2 channels because the inputs have two channels"
153 | ]
154 | },
155 | {
156 | "cell_type": "code",
157 | "execution_count": 6,
158 | "metadata": {},
159 | "outputs": [
160 | {
161 | "name": "stdout",
162 | "output_type": "stream",
163 | "text": [
164 | "Parameter containing:\n",
165 | "tensor([[[[ 0.0769, 0.0854, -0.0457],\n",
166 | " [-0.2278, 0.1797, -0.2205],\n",
167 | " [-0.1373, 0.1309, -0.1921]],\n",
168 | "\n",
169 | " [[-0.2324, 0.0508, 0.1103],\n",
170 | " [-0.0557, 0.0228, -0.0676],\n",
171 | " [-0.1515, -0.2187, -0.0543]]],\n",
172 | "\n",
173 | "\n",
174 | " [[[-0.0162, -0.1215, -0.0105],\n",
175 | " [ 0.2319, -0.1407, 0.0846],\n",
176 | " [-0.0128, -0.0090, -0.1557]],\n",
177 | "\n",
178 | " [[-0.1215, 0.2144, -0.0722],\n",
179 | " [ 0.2164, 0.1504, 0.0624],\n",
180 | " [-0.0099, -0.1855, 0.2224]]],\n",
181 | "\n",
182 | "\n",
183 | " [[[ 0.2247, -0.0227, 0.1882],\n",
184 | " [ 0.1728, -0.2182, -0.1315],\n",
185 | " [-0.1470, 0.2327, 0.1637]],\n",
186 | "\n",
187 | " [[ 0.2089, 0.1279, -0.0395],\n",
188 | " [-0.2204, -0.1502, -0.0791],\n",
189 | " [-0.1176, 0.2314, -0.1635]]],\n",
190 | "\n",
191 | "\n",
192 | " [[[-0.0567, 0.1631, -0.0448],\n",
193 | " [ 0.1037, -0.1097, -0.1254],\n",
194 | " [ 0.2014, 0.1393, -0.0822]],\n",
195 | "\n",
196 | " [[-0.2334, 0.0062, -0.0660],\n",
197 | " [-0.0554, -0.0243, 0.1007],\n",
198 | " [ 0.2226, 0.1079, -0.1888]]],\n",
199 | "\n",
200 | "\n",
201 | " [[[-0.0275, -0.0402, 0.1873],\n",
202 | " [-0.1531, -0.1483, -0.1436],\n",
203 | " [ 0.1848, -0.1573, -0.0067]],\n",
204 | "\n",
205 | " [[-0.2103, 0.0290, 0.1645],\n",
206 | " [-0.0098, 0.0435, 0.0835],\n",
207 | " [-0.1490, -0.0220, 0.0387]]]], requires_grad=True)\n",
208 | "torch.Size([5, 2, 3, 3])\n"
209 | ]
210 | }
211 | ],
212 | "source": [
213 | "print(mod.weight)\n",
214 | "\n",
215 | "print(mod.weight.size())"
216 | ]
217 | },
218 | {
219 | "cell_type": "code",
220 | "execution_count": null,
221 | "metadata": {},
222 | "outputs": [],
223 | "source": []
224 | },
225 | {
226 | "cell_type": "code",
227 | "execution_count": null,
228 | "metadata": {},
229 | "outputs": [],
230 | "source": []
231 | }
232 | ],
233 | "metadata": {
234 | "kernelspec": {
235 | "display_name": "Python 3",
236 | "language": "python",
237 | "name": "python3"
238 | },
239 | "language_info": {
240 | "codemirror_mode": {
241 | "name": "ipython",
242 | "version": 3
243 | },
244 | "file_extension": ".py",
245 | "mimetype": "text/x-python",
246 | "name": "python",
247 | "nbconvert_exporter": "python",
248 | "pygments_lexer": "ipython3",
249 | "version": "3.6.10"
250 | }
251 | },
252 | "nbformat": 4,
253 | "nbformat_minor": 2
254 | }
255 |
--------------------------------------------------------------------------------
/codes/data/generate_all_data.py:
--------------------------------------------------------------------------------
1 |
2 |
3 | # run the code :
4 | # python generate_all_data.py
5 |
6 |
7 | import torch
8 | import os.path
9 | import os
10 |
11 |
12 | #############################
13 | # MNIST
14 | #############################
15 | def check_mnist_dataset_exists(path_data='./'):
16 | flag_train_data = os.path.isfile(path_data + 'mnist/train_data.pt')
17 | flag_train_label = os.path.isfile(path_data + 'mnist/train_label.pt')
18 | flag_test_data = os.path.isfile(path_data + 'mnist/test_data.pt')
19 | flag_test_label = os.path.isfile(path_data + 'mnist/test_label.pt')
20 | if flag_train_data==False or flag_train_label==False or flag_test_data==False or flag_test_label==False:
21 | print('MNIST dataset missing - downloading...')
22 | import torchvision
23 | import torchvision.transforms as transforms
24 | trainset = torchvision.datasets.MNIST(root=path_data + 'mnist/temp', train=True,
25 | download=True, transform=transforms.ToTensor())
26 | testset = torchvision.datasets.MNIST(root=path_data + 'mnist/temp', train=False,
27 | download=True, transform=transforms.ToTensor())
28 | train_data=torch.Tensor(60000,28,28)
29 | train_label=torch.LongTensor(60000)
30 | for idx , example in enumerate(trainset):
31 | train_data[idx]=example[0].squeeze()
32 | train_label[idx]=example[1]
33 | torch.save(train_data,path_data + 'mnist/train_data.pt')
34 | torch.save(train_label,path_data + 'mnist/train_label.pt')
35 | test_data=torch.Tensor(10000,28,28)
36 | test_label=torch.LongTensor(10000)
37 | for idx , example in enumerate(testset):
38 | test_data[idx]=example[0].squeeze()
39 | test_label[idx]=example[1]
40 | torch.save(test_data,path_data + 'mnist/test_data.pt')
41 | torch.save(test_label,path_data + 'mnist/test_label.pt')
42 | return path_data
43 |
44 | #############################
45 | # FASHION-MNIST
46 | #############################
47 | def check_fashion_mnist_dataset_exists(path_data='./'):
48 | flag_train_data = os.path.isfile(path_data + 'fashion-mnist/train_data.pt')
49 | flag_train_label = os.path.isfile(path_data + 'fashion-mnist/train_label.pt')
50 | flag_test_data = os.path.isfile(path_data + 'fashion-mnist/test_data.pt')
51 | flag_test_label = os.path.isfile(path_data + 'fashion-mnist/test_label.pt')
52 | if flag_train_data==False or flag_train_label==False or flag_test_data==False or flag_test_label==False:
53 | print('FASHION-MNIST dataset missing - downloading...')
54 | import torchvision
55 | import torchvision.transforms as transforms
56 | trainset = torchvision.datasets.FashionMNIST(root=path_data + 'fashion-mnist/temp', train=True,
57 | download=True, transform=transforms.ToTensor())
58 | testset = torchvision.datasets.FashionMNIST(root=path_data + 'fashion-mnist/temp', train=False,
59 | download=True, transform=transforms.ToTensor())
60 | train_data=torch.Tensor(60000,28,28)
61 | train_label=torch.LongTensor(60000)
62 | for idx , example in enumerate(trainset):
63 | train_data[idx]=example[0].squeeze()
64 | train_label[idx]=example[1]
65 | torch.save(train_data,path_data + 'fashion-mnist/train_data.pt')
66 | torch.save(train_label,path_data + 'fashion-mnist/train_label.pt')
67 | test_data=torch.Tensor(10000,28,28)
68 | test_label=torch.LongTensor(10000)
69 | for idx , example in enumerate(testset):
70 | test_data[idx]=example[0].squeeze()
71 | test_label[idx]=example[1]
72 | torch.save(test_data,path_data + 'fashion-mnist/test_data.pt')
73 | torch.save(test_label,path_data + 'fashion-mnist/test_label.pt')
74 | return path_data
75 |
76 | #############################
77 | # CIFAR
78 | #############################
79 | def check_cifar_dataset_exists(path_data='./'):
80 | flag_train_data = os.path.isfile(path_data + 'cifar/train_data.pt')
81 | flag_train_label = os.path.isfile(path_data + 'cifar/train_label.pt')
82 | flag_test_data = os.path.isfile(path_data + 'cifar/test_data.pt')
83 | flag_test_label = os.path.isfile(path_data + 'cifar/test_label.pt')
84 | if flag_train_data==False or flag_train_label==False or flag_test_data==False or flag_test_label==False:
85 | print('CIFAR dataset missing - downloading...')
86 | import torchvision
87 | import torchvision.transforms as transforms
88 | trainset = torchvision.datasets.CIFAR10(root=path_data + 'cifar/temp', train=True,
89 | download=True, transform=transforms.ToTensor())
90 | testset = torchvision.datasets.CIFAR10(root=path_data + 'cifar/temp', train=False,
91 | download=True, transform=transforms.ToTensor())
92 | train_data=torch.Tensor(50000,3,32,32)
93 | train_label=torch.LongTensor(50000)
94 | for idx , example in enumerate(trainset):
95 | train_data[idx]=example[0]
96 | train_label[idx]=example[1]
97 | torch.save(train_data,path_data + 'cifar/train_data.pt')
98 | torch.save(train_label,path_data + 'cifar/train_label.pt')
99 | test_data=torch.Tensor(10000,3,32,32)
100 | test_label=torch.LongTensor(10000)
101 | for idx , example in enumerate(testset):
102 | test_data[idx]=example[0]
103 | test_label[idx]=example[1]
104 | torch.save(test_data,path_data + 'cifar/test_data.pt')
105 | torch.save(test_label,path_data + 'cifar/test_label.pt')
106 | return path_data
107 |
108 | #############################
109 | # PTB
110 | #############################
111 | class Dictionary(object):
112 | def __init__(self):
113 | self.word2idx = {}
114 | self.idx2word = []
115 |
116 | def add_word(self, word):
117 | if word not in self.word2idx:
118 | self.idx2word.append(word)
119 | self.word2idx[word] = len(self.idx2word) - 1
120 | return self.word2idx[word]
121 |
122 | def __len__(self):
123 | return len(self.idx2word)
124 |
125 | class Corpus(object):
126 | def __init__(self, path):
127 | self.dictionary = Dictionary()
128 | self.train = self.tokenize(os.path.join(path, 'train.txt'))
129 | self.valid = self.tokenize(os.path.join(path, 'valid.txt'))
130 | self.test = self.tokenize(os.path.join(path, 'test.txt'))
131 |
132 | def tokenize(self, path):
133 | """Tokenizes a text file."""
134 | assert os.path.exists(path)
135 | # Add words to the dictionary
136 | with open(path, 'r') as f:
137 | tokens = 0
138 | for line in f:
139 | words = line.split() + ['']
140 | tokens += len(words)
141 | for word in words:
142 | self.dictionary.add_word(word)
143 |
144 | # Tokenize file content
145 | with open(path, 'r') as f:
146 | ids = torch.LongTensor(tokens)
147 | token = 0
148 | for line in f:
149 | words = line.split() + ['']
150 | for word in words:
151 | ids[token] = self.dictionary.word2idx[word]
152 | token += 1
153 |
154 | return ids
155 |
156 | def batchify(data, bsz):
157 | # Work out how cleanly we can divide the dataset into bsz parts.
158 | nbatch = data.size(0) // bsz
159 | # Trim off any extra elements that wouldn't cleanly fit (remainders).
160 | data = data.narrow(0, 0, nbatch * bsz)
161 | # Evenly divide the data across the bsz batches.
162 | data = data.view(bsz, -1).t().contiguous()
163 | return data
164 |
165 | def check_ptb_dataset_exists(path_data='./'):
166 | flag_idx2word = os.path.isfile(path_data + 'ptb/idx2word.pt')
167 | flag_test_data = os.path.isfile(path_data + 'ptb/test_data.pt')
168 | flag_train_data = os.path.isfile(path_data + 'ptb/train_data.pt')
169 | flag_word2idx = os.path.isfile(path_data + 'ptb/word2idx.pt')
170 | if flag_idx2word==False or flag_test_data==False or flag_train_data==False or flag_word2idx==False:
171 | print('PTB dataset missing - generating...')
172 | data_folder = 'ptb/data_raw'
173 | corpus = Corpus(path_data+data_folder)
174 | batch_size=20
175 | train_data = batchify(corpus.train, batch_size)
176 | val_data = batchify(corpus.valid, batch_size)
177 | test_data = batchify(corpus.test, batch_size)
178 | vocab_size = len(corpus.dictionary)
179 | torch.save(train_data,path_data + 'ptb/train_data.pt')
180 | torch.save(test_data,path_data + 'ptb/test_data.pt')
181 | torch.save(corpus.dictionary.idx2word,path_data + 'ptb/idx2word.pt')
182 | torch.save(corpus.dictionary.word2idx,path_data + 'ptb/word2idx.pt')
183 | return path_data
184 |
185 |
186 |
187 |
188 |
189 | _ = check_mnist_dataset_exists()
190 | _ = check_fashion_mnist_dataset_exists()
191 | _ = check_cifar_dataset_exists()
192 | _ = check_ptb_dataset_exists()
193 |
194 |
195 |
--------------------------------------------------------------------------------
/codes/labs_lecture05/lab02_epoch/utils.py:
--------------------------------------------------------------------------------
1 | import torch
2 | import numpy as np
3 | import matplotlib.pyplot as plt
4 |
5 |
6 |
7 | def num_correct_prediction( scores , labels ):
8 |
9 | predicted_labels = scores.argmax(dim=1)
10 | indicator = (predicted_labels == labels)
11 | num_matches=indicator.sum()
12 | num_matches_python= num_matches.item()
13 |
14 | return num_matches_python
15 |
16 |
17 | def show(X):
18 | if X.dim() == 3 and X.size(0) == 3:
19 | plt.imshow( np.transpose( X.numpy() , (1, 2, 0)) )
20 | plt.show()
21 | elif X.dim() == 2:
22 | plt.imshow( X.numpy() , cmap='gray' )
23 | plt.show()
24 | else:
25 | print('WRONG TENSOR SIZE')
26 |
27 |
28 |
29 |
30 | def show_prob_mnist(p):
31 |
32 | p=p.data.squeeze().numpy()
33 |
34 | ft=15
35 | label = ('zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight','nine')
36 | #p=p.data.squeeze().numpy()
37 | y_pos = np.arange(len(p))*1.2
38 | target=2
39 | width=0.9
40 | col= 'blue'
41 | #col='darkgreen'
42 |
43 | plt.rcdefaults()
44 | fig, ax = plt.subplots()
45 |
46 | # the plot
47 | ax.barh(y_pos, p, width , align='center', color=col)
48 |
49 | ax.set_xlim([0, 1.3])
50 | #ax.set_ylim([-0.8, len(p)*1.2-1+0.8])
51 |
52 | # y label
53 | ax.set_yticks(y_pos)
54 | ax.set_yticklabels(label, fontsize=ft)
55 | ax.invert_yaxis()
56 | #ax.set_xlabel('Performance')
57 | #ax.set_title('How fast do you want to go today?')
58 |
59 | # x label
60 | ax.set_xticklabels([])
61 | ax.set_xticks([])
62 | #x_pos=np.array([0, 0.25 , 0.5 , 0.75 , 1])
63 | #ax.set_xticks(x_pos)
64 | #ax.set_xticklabels( [0, 0.25 , 0.5 , 0.75 , 1] , fontsize=15)
65 |
66 | ax.spines['right'].set_visible(False)
67 | ax.spines['top'].set_visible(False)
68 | ax.spines['bottom'].set_visible(False)
69 | ax.spines['left'].set_linewidth(4)
70 |
71 |
72 | for i in range(len(p)):
73 | str_nb="{0:.2f}".format(p[i])
74 | ax.text( p[i] + 0.05 , y_pos[i] ,str_nb ,
75 | horizontalalignment='left', verticalalignment='center',
76 | transform=ax.transData, color= col,fontsize=ft)
77 |
78 |
79 |
80 | plt.show()
81 | #fig.savefig('pic/prob', dpi=96, bbox_inches="tight")
82 |
83 |
84 |
85 |
86 |
87 |
88 | def show_prob_fashion_mnist(p):
89 |
90 |
91 | p=p.data.squeeze().numpy()
92 |
93 | ft=15
94 | label = ('T-shirt', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag','Boot')
95 | #p=p.data.squeeze().numpy()
96 | y_pos = np.arange(len(p))*1.2
97 | target=2
98 | width=0.9
99 | col= 'blue'
100 | #col='darkgreen'
101 |
102 | plt.rcdefaults()
103 | fig, ax = plt.subplots()
104 |
105 | # the plot
106 | ax.barh(y_pos, p, width , align='center', color=col)
107 |
108 | ax.set_xlim([0, 1.3])
109 | #ax.set_ylim([-0.8, len(p)*1.2-1+0.8])
110 |
111 | # y label
112 | ax.set_yticks(y_pos)
113 | ax.set_yticklabels(label, fontsize=ft)
114 | ax.invert_yaxis()
115 | #ax.set_xlabel('Performance')
116 | #ax.set_title('How fast do you want to go today?')
117 |
118 | # x label
119 | ax.set_xticklabels([])
120 | ax.set_xticks([])
121 | #x_pos=np.array([0, 0.25 , 0.5 , 0.75 , 1])
122 | #ax.set_xticks(x_pos)
123 | #ax.set_xticklabels( [0, 0.25 , 0.5 , 0.75 , 1] , fontsize=15)
124 |
125 | ax.spines['right'].set_visible(False)
126 | ax.spines['top'].set_visible(False)
127 | ax.spines['bottom'].set_visible(False)
128 | ax.spines['left'].set_linewidth(4)
129 |
130 |
131 | for i in range(len(p)):
132 | str_nb="{0:.2f}".format(p[i])
133 | ax.text( p[i] + 0.05 , y_pos[i] ,str_nb ,
134 | horizontalalignment='left', verticalalignment='center',
135 | transform=ax.transData, color= col,fontsize=ft)
136 |
137 |
138 |
139 | plt.show()
140 | #fig.savefig('pic/prob', dpi=96, bbox_inches="tight")
141 |
142 |
143 |
144 | import os.path
145 | def check_mnist_dataset_exists(path_data='../../data/'):
146 | flag_train_data = os.path.isfile(path_data + 'mnist/train_data.pt')
147 | flag_train_label = os.path.isfile(path_data + 'mnist/train_label.pt')
148 | flag_test_data = os.path.isfile(path_data + 'mnist/test_data.pt')
149 | flag_test_label = os.path.isfile(path_data + 'mnist/test_label.pt')
150 | if flag_train_data==False or flag_train_label==False or flag_test_data==False or flag_test_label==False:
151 | print('MNIST dataset missing - downloading...')
152 | import torchvision
153 | import torchvision.transforms as transforms
154 | trainset = torchvision.datasets.MNIST(root=path_data + 'mnist/temp', train=True,
155 | download=True, transform=transforms.ToTensor())
156 | testset = torchvision.datasets.MNIST(root=path_data + 'mnist/temp', train=False,
157 | download=True, transform=transforms.ToTensor())
158 | train_data=torch.Tensor(60000,28,28)
159 | train_label=torch.LongTensor(60000)
160 | for idx , example in enumerate(trainset):
161 | train_data[idx]=example[0].squeeze()
162 | train_label[idx]=example[1]
163 | torch.save(train_data,path_data + 'mnist/train_data.pt')
164 | torch.save(train_label,path_data + 'mnist/train_label.pt')
165 | test_data=torch.Tensor(10000,28,28)
166 | test_label=torch.LongTensor(10000)
167 | for idx , example in enumerate(testset):
168 | test_data[idx]=example[0].squeeze()
169 | test_label[idx]=example[1]
170 | torch.save(test_data,path_data + 'mnist/test_data.pt')
171 | torch.save(test_label,path_data + 'mnist/test_label.pt')
172 | return path_data
173 |
174 | def check_fashion_mnist_dataset_exists(path_data='../../data/'):
175 | flag_train_data = os.path.isfile(path_data + 'fashion-mnist/train_data.pt')
176 | flag_train_label = os.path.isfile(path_data + 'fashion-mnist/train_label.pt')
177 | flag_test_data = os.path.isfile(path_data + 'fashion-mnist/test_data.pt')
178 | flag_test_label = os.path.isfile(path_data + 'fashion-mnist/test_label.pt')
179 | if flag_train_data==False or flag_train_label==False or flag_test_data==False or flag_test_label==False:
180 | print('FASHION-MNIST dataset missing - downloading...')
181 | import torchvision
182 | import torchvision.transforms as transforms
183 | trainset = torchvision.datasets.FashionMNIST(root=path_data + 'fashion-mnist/temp', train=True,
184 | download=True, transform=transforms.ToTensor())
185 | testset = torchvision.datasets.FashionMNIST(root=path_data + 'fashion-mnist/temp', train=False,
186 | download=True, transform=transforms.ToTensor())
187 | train_data=torch.Tensor(60000,28,28)
188 | train_label=torch.LongTensor(60000)
189 | for idx , example in enumerate(trainset):
190 | train_data[idx]=example[0].squeeze()
191 | train_label[idx]=example[1]
192 | torch.save(train_data,path_data + 'fashion-mnist/train_data.pt')
193 | torch.save(train_label,path_data + 'fashion-mnist/train_label.pt')
194 | test_data=torch.Tensor(10000,28,28)
195 | test_label=torch.LongTensor(10000)
196 | for idx , example in enumerate(testset):
197 | test_data[idx]=example[0].squeeze()
198 | test_label[idx]=example[1]
199 | torch.save(test_data,path_data + 'fashion-mnist/test_data.pt')
200 | torch.save(test_label,path_data + 'fashion-mnist/test_label.pt')
201 | return path_data
202 |
203 | def check_cifar_dataset_exists(path_data='../../data/'):
204 | flag_train_data = os.path.isfile(path_data + 'cifar/train_data.pt')
205 | flag_train_label = os.path.isfile(path_data + 'cifar/train_label.pt')
206 | flag_test_data = os.path.isfile(path_data + 'cifar/test_data.pt')
207 | flag_test_label = os.path.isfile(path_data + 'cifar/test_label.pt')
208 | if flag_train_data==False or flag_train_label==False or flag_test_data==False or flag_test_label==False:
209 | print('CIFAR dataset missing - downloading...')
210 | import torchvision
211 | import torchvision.transforms as transforms
212 | trainset = torchvision.datasets.CIFAR10(root=path_data + 'cifar/temp', train=True,
213 | download=True, transform=transforms.ToTensor())
214 | testset = torchvision.datasets.CIFAR10(root=path_data + 'cifar/temp', train=False,
215 | download=True, transform=transforms.ToTensor())
216 | train_data=torch.Tensor(50000,3,32,32)
217 | train_label=torch.LongTensor(50000)
218 | for idx , example in enumerate(trainset):
219 | train_data[idx]=example[0]
220 | train_label[idx]=example[1]
221 | torch.save(train_data,path_data + 'cifar/train_data.pt')
222 | torch.save(train_label,path_data + 'cifar/train_label.pt')
223 | test_data=torch.Tensor(10000,3,32,32)
224 | test_label=torch.LongTensor(10000)
225 | for idx , example in enumerate(testset):
226 | test_data[idx]=example[0]
227 | test_label[idx]=example[1]
228 | torch.save(test_data,path_data + 'cifar/test_data.pt')
229 | torch.save(test_label,path_data + 'cifar/test_label.pt')
230 | return path_data
231 |
232 |
233 |
--------------------------------------------------------------------------------
/codes/labs_lecture03/lab04_train_vanilla_nn/utils.py:
--------------------------------------------------------------------------------
1 | import torch
2 | import numpy as np
3 | import matplotlib.pyplot as plt
4 | import seaborn as sns
5 |
6 | cmap = sns.diverging_palette(262, 10, sep=1, n=16, s=99, l=50, center="dark", as_cmap=True) # best
7 |
8 |
9 | def show(X):
10 | if X.dim() == 3 and X.size(0) == 3:
11 | plt.imshow( np.transpose( X.numpy() , (1, 2, 0)) )
12 | plt.show()
13 | elif X.dim() == 2:
14 | plt.imshow( X.numpy() , cmap='gray' )
15 | plt.show()
16 | else:
17 | print('WRONG TENSOR SIZE')
18 |
19 |
20 | def show_template(template):
21 | vm=0.3
22 | height=4
23 | width=6
24 | f, ax1 = plt.subplots()
25 | temp=template.detach().numpy()
26 | sns.heatmap(temp, cmap=cmap, ax=ax1, cbar=True,square=True,vmin=-vm, vmax=vm )
27 | plt.show()
28 |
29 |
30 |
31 | def show_prob_mnist(p):
32 |
33 | p=p.data.squeeze().numpy()
34 |
35 | ft=15
36 | label = ('zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight','nine')
37 | #p=p.data.squeeze().numpy()
38 | y_pos = np.arange(len(p))*1.2
39 | target=2
40 | width=0.9
41 | col= 'blue'
42 | #col='darkgreen'
43 |
44 | plt.rcdefaults()
45 | fig, ax = plt.subplots()
46 |
47 | # the plot
48 | ax.barh(y_pos, p, width , align='center', color=col)
49 |
50 | ax.set_xlim([0, 1.3])
51 | #ax.set_ylim([-0.8, len(p)*1.2-1+0.8])
52 |
53 | # y label
54 | ax.set_yticks(y_pos)
55 | ax.set_yticklabels(label, fontsize=ft)
56 | ax.invert_yaxis()
57 | #ax.set_xlabel('Performance')
58 | #ax.set_title('How fast do you want to go today?')
59 |
60 | # x label
61 | ax.set_xticklabels([])
62 | ax.set_xticks([])
63 | #x_pos=np.array([0, 0.25 , 0.5 , 0.75 , 1])
64 | #ax.set_xticks(x_pos)
65 | #ax.set_xticklabels( [0, 0.25 , 0.5 , 0.75 , 1] , fontsize=15)
66 |
67 | ax.spines['right'].set_visible(False)
68 | ax.spines['top'].set_visible(False)
69 | ax.spines['bottom'].set_visible(False)
70 | ax.spines['left'].set_linewidth(4)
71 |
72 |
73 | for i in range(len(p)):
74 | str_nb="{0:.2f}".format(p[i])
75 | ax.text( p[i] + 0.05 , y_pos[i] ,str_nb ,
76 | horizontalalignment='left', verticalalignment='center',
77 | transform=ax.transData, color= col,fontsize=ft)
78 |
79 |
80 |
81 | plt.show()
82 | #fig.savefig('pic/prob', dpi=96, bbox_inches="tight")
83 |
84 |
85 |
86 |
87 |
88 |
89 | def show_prob_fashion_mnist(p):
90 |
91 |
92 | p=p.data.squeeze().numpy()
93 |
94 | ft=15
95 | label = ('T-shirt', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag','Boot')
96 | #p=p.data.squeeze().numpy()
97 | y_pos = np.arange(len(p))*1.2
98 | target=2
99 | width=0.9
100 | col= 'blue'
101 | #col='darkgreen'
102 |
103 | plt.rcdefaults()
104 | fig, ax = plt.subplots()
105 |
106 | # the plot
107 | ax.barh(y_pos, p, width , align='center', color=col)
108 |
109 | ax.set_xlim([0, 1.3])
110 | #ax.set_ylim([-0.8, len(p)*1.2-1+0.8])
111 |
112 | # y label
113 | ax.set_yticks(y_pos)
114 | ax.set_yticklabels(label, fontsize=ft)
115 | ax.invert_yaxis()
116 | #ax.set_xlabel('Performance')
117 | #ax.set_title('How fast do you want to go today?')
118 |
119 | # x label
120 | ax.set_xticklabels([])
121 | ax.set_xticks([])
122 | #x_pos=np.array([0, 0.25 , 0.5 , 0.75 , 1])
123 | #ax.set_xticks(x_pos)
124 | #ax.set_xticklabels( [0, 0.25 , 0.5 , 0.75 , 1] , fontsize=15)
125 |
126 | ax.spines['right'].set_visible(False)
127 | ax.spines['top'].set_visible(False)
128 | ax.spines['bottom'].set_visible(False)
129 | ax.spines['left'].set_linewidth(4)
130 |
131 |
132 | for i in range(len(p)):
133 | str_nb="{0:.2f}".format(p[i])
134 | ax.text( p[i] + 0.05 , y_pos[i] ,str_nb ,
135 | horizontalalignment='left', verticalalignment='center',
136 | transform=ax.transData, color= col,fontsize=ft)
137 |
138 |
139 |
140 | plt.show()
141 | #fig.savefig('pic/prob', dpi=96, bbox_inches="tight")
142 |
143 |
144 |
145 | import os.path
146 | def check_mnist_dataset_exists(path_data='../../data/'):
147 | flag_train_data = os.path.isfile(path_data + 'mnist/train_data.pt')
148 | flag_train_label = os.path.isfile(path_data + 'mnist/train_label.pt')
149 | flag_test_data = os.path.isfile(path_data + 'mnist/test_data.pt')
150 | flag_test_label = os.path.isfile(path_data + 'mnist/test_label.pt')
151 | if flag_train_data==False or flag_train_label==False or flag_test_data==False or flag_test_label==False:
152 | print('MNIST dataset missing - downloading...')
153 | import torchvision
154 | import torchvision.transforms as transforms
155 | trainset = torchvision.datasets.MNIST(root=path_data + 'mnist/temp', train=True,
156 | download=True, transform=transforms.ToTensor())
157 | testset = torchvision.datasets.MNIST(root=path_data + 'mnist/temp', train=False,
158 | download=True, transform=transforms.ToTensor())
159 | train_data=torch.Tensor(60000,28,28)
160 | train_label=torch.LongTensor(60000)
161 | for idx , example in enumerate(trainset):
162 | train_data[idx]=example[0].squeeze()
163 | train_label[idx]=example[1]
164 | torch.save(train_data,path_data + 'mnist/train_data.pt')
165 | torch.save(train_label,path_data + 'mnist/train_label.pt')
166 | test_data=torch.Tensor(10000,28,28)
167 | test_label=torch.LongTensor(10000)
168 | for idx , example in enumerate(testset):
169 | test_data[idx]=example[0].squeeze()
170 | test_label[idx]=example[1]
171 | torch.save(test_data,path_data + 'mnist/test_data.pt')
172 | torch.save(test_label,path_data + 'mnist/test_label.pt')
173 | return path_data
174 |
175 | def check_fashion_mnist_dataset_exists(path_data='../../data/'):
176 | flag_train_data = os.path.isfile(path_data + 'fashion-mnist/train_data.pt')
177 | flag_train_label = os.path.isfile(path_data + 'fashion-mnist/train_label.pt')
178 | flag_test_data = os.path.isfile(path_data + 'fashion-mnist/test_data.pt')
179 | flag_test_label = os.path.isfile(path_data + 'fashion-mnist/test_label.pt')
180 | if flag_train_data==False or flag_train_label==False or flag_test_data==False or flag_test_label==False:
181 | print('FASHION-MNIST dataset missing - downloading...')
182 | import torchvision
183 | import torchvision.transforms as transforms
184 | trainset = torchvision.datasets.FashionMNIST(root=path_data + 'fashion-mnist/temp', train=True,
185 | download=True, transform=transforms.ToTensor())
186 | testset = torchvision.datasets.FashionMNIST(root=path_data + 'fashion-mnist/temp', train=False,
187 | download=True, transform=transforms.ToTensor())
188 | train_data=torch.Tensor(60000,28,28)
189 | train_label=torch.LongTensor(60000)
190 | for idx , example in enumerate(trainset):
191 | train_data[idx]=example[0].squeeze()
192 | train_label[idx]=example[1]
193 | torch.save(train_data,path_data + 'fashion-mnist/train_data.pt')
194 | torch.save(train_label,path_data + 'fashion-mnist/train_label.pt')
195 | test_data=torch.Tensor(10000,28,28)
196 | test_label=torch.LongTensor(10000)
197 | for idx , example in enumerate(testset):
198 | test_data[idx]=example[0].squeeze()
199 | test_label[idx]=example[1]
200 | torch.save(test_data,path_data + 'fashion-mnist/test_data.pt')
201 | torch.save(test_label,path_data + 'fashion-mnist/test_label.pt')
202 | return path_data
203 |
204 | def check_cifar_dataset_exists(path_data='../../data/'):
205 | flag_train_data = os.path.isfile(path_data + 'cifar/train_data.pt')
206 | flag_train_label = os.path.isfile(path_data + 'cifar/train_label.pt')
207 | flag_test_data = os.path.isfile(path_data + 'cifar/test_data.pt')
208 | flag_test_label = os.path.isfile(path_data + 'cifar/test_label.pt')
209 | if flag_train_data==False or flag_train_label==False or flag_test_data==False or flag_test_label==False:
210 | print('CIFAR dataset missing - downloading...')
211 | import torchvision
212 | import torchvision.transforms as transforms
213 | trainset = torchvision.datasets.CIFAR10(root=path_data + 'cifar/temp', train=True,
214 | download=True, transform=transforms.ToTensor())
215 | testset = torchvision.datasets.CIFAR10(root=path_data + 'cifar/temp', train=False,
216 | download=True, transform=transforms.ToTensor())
217 | train_data=torch.Tensor(50000,3,32,32)
218 | train_label=torch.LongTensor(50000)
219 | for idx , example in enumerate(trainset):
220 | train_data[idx]=example[0]
221 | train_label[idx]=example[1]
222 | torch.save(train_data,path_data + 'cifar/train_data.pt')
223 | torch.save(train_label,path_data + 'cifar/train_label.pt')
224 | test_data=torch.Tensor(10000,3,32,32)
225 | test_label=torch.LongTensor(10000)
226 | for idx , example in enumerate(testset):
227 | test_data[idx]=example[0]
228 | test_label[idx]=example[1]
229 | torch.save(test_data,path_data + 'cifar/test_data.pt')
230 | torch.save(test_label,path_data + 'cifar/test_label.pt')
231 | return path_data
232 |
233 |
234 |
--------------------------------------------------------------------------------
/codes/labs_lecture05/lab04_test_set/test_set_exercise.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Lab 04 : Test set evaluation -- exercise"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": null,
13 | "metadata": {},
14 | "outputs": [],
15 | "source": [
16 | "# For Google Colaboratory\n",
17 | "import sys, os\n",
18 | "if 'google.colab' in sys.modules:\n",
19 | " # mount google drive\n",
20 | " from google.colab import drive\n",
21 | " drive.mount('/content/gdrive')\n",
22 | " # find automatically the path of the folder containing \"file_name\" :\n",
23 | " file_name = 'test_set_exercise.ipynb'\n",
24 | " import subprocess\n",
25 | " path_to_file = subprocess.check_output('find . -type f -name ' + str(file_name), shell=True).decode(\"utf-8\")\n",
26 | " path_to_file = path_to_file.replace(file_name,\"\").replace('\\n',\"\")\n",
27 | " # if previous search failed or too long, comment the previous line and simply write down manually the path below :\n",
28 | " #path_to_file = '/content/gdrive/My Drive/CE7454_2020_codes/codes/labs_lecture05/lab04_test_set'\n",
29 | " print(path_to_file)\n",
30 | " # change current path to the folder containing \"file_name\"\n",
31 | " os.chdir(path_to_file)\n",
32 | " !pwd"
33 | ]
34 | },
35 | {
36 | "cell_type": "code",
37 | "execution_count": 1,
38 | "metadata": {},
39 | "outputs": [],
40 | "source": [
41 | "import torch\n",
42 | "import torch.nn as nn\n",
43 | "import torch.nn.functional as F\n",
44 | "import torch.optim as optim\n",
45 | "from random import randint\n",
46 | "import utils"
47 | ]
48 | },
49 | {
50 | "cell_type": "markdown",
51 | "metadata": {},
52 | "source": [
53 | "### Download the CIFAR dataset -- check the size carefully!"
54 | ]
55 | },
56 | {
57 | "cell_type": "code",
58 | "execution_count": 2,
59 | "metadata": {},
60 | "outputs": [
61 | {
62 | "name": "stdout",
63 | "output_type": "stream",
64 | "text": [
65 | "torch.Size([50000, 3, 32, 32])\n"
66 | ]
67 | }
68 | ],
69 | "source": [
70 | "from utils import check_cifar_dataset_exists\n",
71 | "data_path=check_cifar_dataset_exists()\n",
72 | "\n",
73 | "train_data=torch.load(data_path+'cifar/train_data.pt')\n",
74 | "train_label=torch.load(data_path+'cifar/train_label.pt')\n",
75 | "test_data=torch.load(data_path+'cifar/test_data.pt')\n",
76 | "test_label=torch.load(data_path+'cifar/test_label.pt')\n",
77 | "\n",
78 | "print(train_data.size())"
79 | ]
80 | },
81 | {
82 | "cell_type": "markdown",
83 | "metadata": {},
84 | "source": [
85 | "### Make a ONE layer net class. "
86 | ]
87 | },
88 | {
89 | "cell_type": "code",
90 | "execution_count": null,
91 | "metadata": {},
92 | "outputs": [],
93 | "source": [
94 | "class one_layer_net(nn.Module):\n",
95 | "\n",
96 | " def __init__(self, input_size, output_size):\n",
97 | " super(one_layer_net , self).__init__()\n",
98 | " self.linear_layer = nn.Linear( input_size, output_size , bias=True)\n",
99 | " \n",
100 | " def forward(self, x):\n",
101 | " scores = self.linear_layer(x)\n",
102 | " return scores"
103 | ]
104 | },
105 | {
106 | "cell_type": "markdown",
107 | "metadata": {},
108 | "source": [
109 | "### Build the net. "
110 | ]
111 | },
112 | {
113 | "cell_type": "code",
114 | "execution_count": null,
115 | "metadata": {},
116 | "outputs": [],
117 | "source": [
118 | "net=one_layer_net(3072,10)\n",
119 | "print(net)"
120 | ]
121 | },
122 | {
123 | "cell_type": "markdown",
124 | "metadata": {},
125 | "source": [
126 | "### Choose the criterion and optimizer. Also choose:\n"
127 | ]
128 | },
129 | {
130 | "cell_type": "code",
131 | "execution_count": null,
132 | "metadata": {},
133 | "outputs": [],
134 | "source": [
135 | "criterion = nn.CrossEntropyLoss()\n",
136 | "optimizer=torch.optim.SGD( net.parameters() , lr=0.01 )\n",
137 | "bs=20"
138 | ]
139 | },
140 | {
141 | "cell_type": "markdown",
142 | "metadata": {},
143 | "source": [
144 | "### Do 50 passes through the training set."
145 | ]
146 | },
147 | {
148 | "cell_type": "code",
149 | "execution_count": null,
150 | "metadata": {},
151 | "outputs": [],
152 | "source": [
153 | "for epoch in range(50):\n",
154 | " \n",
155 | " running_loss=0\n",
156 | " running_error=0\n",
157 | " num_batches=0\n",
158 | " \n",
159 | " shuffled_indices=torch.randperm(50000)\n",
160 | " \n",
161 | " for count in range(0,50000,bs):\n",
162 | " \n",
163 | " # Set the gradients to zeros\n",
164 | " optimizer.zero_grad()\n",
165 | " \n",
166 | " # create a minibatch \n",
167 | " indices=shuffled_indices[count:count+bs]\n",
168 | " minibatch_data = train_data[indices]\n",
169 | " minibatch_label= train_label[indices]\n",
170 | "\n",
171 | " # reshape the minibatch\n",
172 | " inputs = minibatch_data.view(bs,3072)\n",
173 | "\n",
174 | " # tell Pytorch to start tracking all operations that will be done on \"inputs\"\n",
175 | " inputs.requires_grad_()\n",
176 | "\n",
177 | " # forward the minibatch through the net \n",
178 | " scores=net( inputs ) \n",
179 | "\n",
180 | " # Compute the average of the losses of the data points in the minibatch\n",
181 | " loss = criterion( scores , minibatch_label) \n",
182 | " \n",
183 | " # backward pass to compute dL/dU, dL/dV and dL/dW \n",
184 | " loss.backward()\n",
185 | "\n",
186 | " # do one step of stochastic gradient descent: U=U-lr(dL/dU), V=V-lr(dL/dU), ...\n",
187 | " optimizer.step()\n",
188 | " \n",
189 | " # START COMPUTING STATS\n",
190 | " \n",
191 | " # add the loss of this batch to the running loss\n",
192 | " running_loss += loss.detach().item()\n",
193 | " \n",
194 | " # compute the error made on this batch and add it to the running error \n",
195 | " error = utils.get_error( scores.detach() , minibatch_label)\n",
196 | " running_error += error.item()\n",
197 | " \n",
198 | " num_batches+=1\n",
199 | " \n",
200 | " \n",
201 | " # compute stats for the full training set\n",
202 | " total_loss = running_loss/num_batches\n",
203 | " total_error = running_error/num_batches\n",
204 | " \n",
205 | " print('epoch=',epoch, '\\t loss=', total_loss , '\\t error=', total_error*100 ,'percent')\n",
206 | " \n",
207 | " "
208 | ]
209 | },
210 | {
211 | "cell_type": "markdown",
212 | "metadata": {},
213 | "source": [
214 | "# You only have to complete this cell"
215 | ]
216 | },
217 | {
218 | "cell_type": "markdown",
219 | "metadata": {},
220 | "source": [
221 | "### So we are doing around 59% error on the training set. BUT how much are we doing on test set?"
222 | ]
223 | },
224 | {
225 | "cell_type": "code",
226 | "execution_count": null,
227 | "metadata": {},
228 | "outputs": [],
229 | "source": [
230 | "running_error= # COMPLETE HERE\n",
231 | "num_batches= # COMPLETE HERE\n",
232 | "\n",
233 | "\n",
234 | "for # COMPLETE HERE\n",
235 | "\n",
236 | " # extract the minibatch\n",
237 | " minibatch_data = # COMPLETE HERE\n",
238 | " minibatch_label= # COMPLETE HERE\n",
239 | "\n",
240 | " # reshape the minibatch\n",
241 | " inputs = # COMPLETE HERE\n",
242 | "\n",
243 | " # feed it to the network\n",
244 | " scores= # COMPLETE HERE\n",
245 | "\n",
246 | " # compute the error made on this batch\n",
247 | " error = # COMPLETE HERE\n",
248 | " \n",
249 | " # add it to the running error\n",
250 | " running_error += # COMPLETE HERE\n",
251 | "\n",
252 | " num_batches += # COMPLETE HERE\n",
253 | "\n",
254 | "\n",
255 | "# compute error rate on the full test set\n",
256 | "total_error = # COMPLETE HERE\n",
257 | "\n",
258 | "print( 'error rate on test set =', total_error*100 ,'percent')"
259 | ]
260 | },
261 | {
262 | "cell_type": "markdown",
263 | "metadata": {},
264 | "source": [
265 | "### Choose image at random from the test set and see how good/bad are the predictions"
266 | ]
267 | },
268 | {
269 | "cell_type": "code",
270 | "execution_count": null,
271 | "metadata": {},
272 | "outputs": [],
273 | "source": [
274 | "# choose a picture at random\n",
275 | "idx=randint(0, 10000-1)\n",
276 | "im=test_data[idx]\n",
277 | "\n",
278 | "# diplay the picture\n",
279 | "utils.show(im)\n",
280 | "\n",
281 | "# feed it to the net and display the confidence scores\n",
282 | "scores = net( im.view(1,3072)) \n",
283 | "probs= F.softmax(scores, dim=1)\n",
284 | "utils.show_prob_cifar(probs)"
285 | ]
286 | },
287 | {
288 | "cell_type": "code",
289 | "execution_count": null,
290 | "metadata": {},
291 | "outputs": [],
292 | "source": []
293 | }
294 | ],
295 | "metadata": {
296 | "kernelspec": {
297 | "display_name": "Python 3",
298 | "language": "python",
299 | "name": "python3"
300 | },
301 | "language_info": {
302 | "codemirror_mode": {
303 | "name": "ipython",
304 | "version": 3
305 | },
306 | "file_extension": ".py",
307 | "mimetype": "text/x-python",
308 | "name": "python",
309 | "nbconvert_exporter": "python",
310 | "pygments_lexer": "ipython3",
311 | "version": "3.6.10"
312 | }
313 | },
314 | "nbformat": 4,
315 | "nbformat_minor": 2
316 | }
317 |
--------------------------------------------------------------------------------
/codes/data/fashion-mnist/generate_fashion_mnist.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Download FASHION-MNIST"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": null,
13 | "metadata": {},
14 | "outputs": [],
15 | "source": [
16 | "# For Google Colaboratory\n",
17 | "import sys, os\n",
18 | "if 'google.colab' in sys.modules:\n",
19 | " from google.colab import drive\n",
20 | " drive.mount('/content/gdrive')\n",
21 | " file_name = 'generate_fashion_mnist.ipynb'\n",
22 | " import subprocess\n",
23 | " path_to_file = subprocess.check_output('find . -type f -name ' + str(file_name), shell=True).decode(\"utf-8\")\n",
24 | " print(path_to_file)\n",
25 | " path_to_file = path_to_file.replace(file_name,\"\").replace('\\n',\"\")\n",
26 | " os.chdir(path_to_file)\n",
27 | " !pwd"
28 | ]
29 | },
30 | {
31 | "cell_type": "code",
32 | "execution_count": 1,
33 | "metadata": {},
34 | "outputs": [],
35 | "source": [
36 | "import torch\n",
37 | "import torchvision\n",
38 | "import torchvision.transforms as transforms\n",
39 | "import numpy as np\n",
40 | "import matplotlib.pyplot as plt\n",
41 | "from random import randint"
42 | ]
43 | },
44 | {
45 | "cell_type": "code",
46 | "execution_count": 2,
47 | "metadata": {},
48 | "outputs": [
49 | {
50 | "name": "stdout",
51 | "output_type": "stream",
52 | "text": [
53 | "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-images-idx3-ubyte.gz\n",
54 | "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-labels-idx1-ubyte.gz\n",
55 | "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-images-idx3-ubyte.gz\n",
56 | "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-labels-idx1-ubyte.gz\n",
57 | "Processing...\n",
58 | "Done!\n"
59 | ]
60 | }
61 | ],
62 | "source": [
63 | "trainset = torchvision.datasets.FashionMNIST(root='./temp', train=True,\n",
64 | " download=True, transform=transforms.ToTensor())\n",
65 | "testset = torchvision.datasets.FashionMNIST(root='./temp', train=False,\n",
66 | " download=True, transform=transforms.ToTensor())"
67 | ]
68 | },
69 | {
70 | "cell_type": "code",
71 | "execution_count": 3,
72 | "metadata": {
73 | "scrolled": true
74 | },
75 | "outputs": [
76 | {
77 | "data": {
78 | "image/png": "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\n",
79 | "text/plain": [
80 | ""
81 | ]
82 | },
83 | "metadata": {},
84 | "output_type": "display_data"
85 | },
86 | {
87 | "name": "stdout",
88 | "output_type": "stream",
89 | "text": [
90 | "tensor(0)\n"
91 | ]
92 | }
93 | ],
94 | "source": [
95 | "idx=4\n",
96 | "pic, label =trainset[idx]\n",
97 | "pic=pic.squeeze()\n",
98 | "plt.imshow(pic.numpy(), cmap='gray')\n",
99 | "plt.show()\n",
100 | "print(label)"
101 | ]
102 | },
103 | {
104 | "cell_type": "code",
105 | "execution_count": 4,
106 | "metadata": {},
107 | "outputs": [],
108 | "source": [
109 | "train_data=torch.Tensor(60000,28,28)\n",
110 | "train_label=torch.LongTensor(60000)\n",
111 | "for idx , example in enumerate(trainset):\n",
112 | " train_data[idx]=example[0].squeeze()\n",
113 | " train_label[idx]=example[1]\n",
114 | "torch.save(train_data,'train_data.pt')\n",
115 | "torch.save(train_label,'train_label.pt')"
116 | ]
117 | },
118 | {
119 | "cell_type": "code",
120 | "execution_count": 5,
121 | "metadata": {},
122 | "outputs": [],
123 | "source": [
124 | "test_data=torch.Tensor(10000,28,28)\n",
125 | "test_label=torch.LongTensor(10000)\n",
126 | "for idx , example in enumerate(testset):\n",
127 | " test_data[idx]=example[0].squeeze()\n",
128 | " test_label[idx]=example[1]\n",
129 | "torch.save(test_data,'test_data.pt')\n",
130 | "torch.save(test_label,'test_label.pt')"
131 | ]
132 | },
133 | {
134 | "cell_type": "code",
135 | "execution_count": null,
136 | "metadata": {},
137 | "outputs": [],
138 | "source": []
139 | }
140 | ],
141 | "metadata": {
142 | "kernelspec": {
143 | "display_name": "Python 3",
144 | "language": "python",
145 | "name": "python3"
146 | },
147 | "language_info": {
148 | "codemirror_mode": {
149 | "name": "ipython",
150 | "version": 3
151 | },
152 | "file_extension": ".py",
153 | "mimetype": "text/x-python",
154 | "name": "python",
155 | "nbconvert_exporter": "python",
156 | "pygments_lexer": "ipython3",
157 | "version": "3.6.8"
158 | }
159 | },
160 | "nbformat": 4,
161 | "nbformat_minor": 2
162 | }
163 |
--------------------------------------------------------------------------------
/codes/labs_lecture10/lab01_vrnn/utils.py:
--------------------------------------------------------------------------------
1 | import torch
2 | import torch.nn.functional as F
3 | import numpy as np
4 | import math
5 | import os.path
6 |
7 |
8 | #############################
9 | # PTB
10 | #############################
11 | def check_ptb_dataset_exists(path_data='../../data/'):
12 | flag_idx2word = os.path.isfile(path_data + 'ptb/idx2word.pt')
13 | flag_test_data = os.path.isfile(path_data + 'ptb/test_data.pt')
14 | flag_train_data = os.path.isfile(path_data + 'ptb/train_data.pt')
15 | flag_word2idx = os.path.isfile(path_data + 'ptb/word2idx.pt')
16 | if flag_idx2word==False or flag_test_data==False or flag_train_data==False or flag_word2idx==False:
17 | print('PTB dataset missing - generating...')
18 | data_folder = 'ptb/data_raw'
19 | corpus = Corpus(path_data+data_folder)
20 | batch_size=20
21 | train_data = batchify(corpus.train, batch_size)
22 | val_data = batchify(corpus.valid, batch_size)
23 | test_data = batchify(corpus.test, batch_size)
24 | vocab_size = len(corpus.dictionary)
25 | torch.save(train_data,path_data + 'ptb/train_data.pt')
26 | torch.save(test_data,path_data + 'ptb/test_data.pt')
27 | torch.save(corpus.dictionary.idx2word,path_data + 'ptb/idx2word.pt')
28 | torch.save(corpus.dictionary.word2idx,path_data + 'ptb/word2idx.pt')
29 | return path_data
30 |
31 | class Dictionary(object):
32 | def __init__(self):
33 | self.word2idx = {}
34 | self.idx2word = []
35 |
36 | def add_word(self, word):
37 | if word not in self.word2idx:
38 | self.idx2word.append(word)
39 | self.word2idx[word] = len(self.idx2word) - 1
40 | return self.word2idx[word]
41 |
42 | def __len__(self):
43 | return len(self.idx2word)
44 |
45 | class Corpus(object):
46 | def __init__(self, path):
47 | self.dictionary = Dictionary()
48 | self.train = self.tokenize(os.path.join(path, 'train.txt'))
49 | self.valid = self.tokenize(os.path.join(path, 'valid.txt'))
50 | self.test = self.tokenize(os.path.join(path, 'test.txt'))
51 |
52 | def tokenize(self, path):
53 | """Tokenizes a text file."""
54 | assert os.path.exists(path)
55 | # Add words to the dictionary
56 | with open(path, 'r') as f:
57 | tokens = 0
58 | for line in f:
59 | words = line.split() + ['']
60 | tokens += len(words)
61 | for word in words:
62 | self.dictionary.add_word(word)
63 |
64 | # Tokenize file content
65 | with open(path, 'r') as f:
66 | ids = torch.LongTensor(tokens)
67 | token = 0
68 | for line in f:
69 | words = line.split() + ['']
70 | for word in words:
71 | ids[token] = self.dictionary.word2idx[word]
72 | token += 1
73 |
74 | return ids
75 |
76 | def batchify(data, bsz):
77 | # Work out how cleanly we can divide the dataset into bsz parts.
78 | nbatch = data.size(0) // bsz
79 | # Trim off any extra elements that wouldn't cleanly fit (remainders).
80 | data = data.narrow(0, 0, nbatch * bsz)
81 | # Evenly divide the data across the bsz batches.
82 | data = data.view(bsz, -1).t().contiguous()
83 | return data
84 |
85 |
86 | path_data = '../../data/'
87 | _ = check_ptb_dataset_exists(path_data)
88 | word2idx = torch.load(path_data + 'ptb/word2idx.pt')
89 | idx2word = torch.load(path_data + 'ptb/idx2word.pt')
90 |
91 |
92 |
93 |
94 |
95 |
96 | def normalize_gradient(net):
97 |
98 | grad_norm_sq=0
99 |
100 | for p in net.parameters():
101 | grad_norm_sq += p.grad.data.norm()**2
102 |
103 | grad_norm=math.sqrt(grad_norm_sq)
104 |
105 | if grad_norm<1e-4:
106 | net.zero_grad()
107 | print('grad norm close to zero')
108 | else:
109 | for p in net.parameters():
110 | p.grad.data.div_(grad_norm)
111 |
112 | return grad_norm
113 |
114 |
115 | def display_num_param(net):
116 | nb_param = 0
117 | for param in net.parameters():
118 | nb_param += param.numel()
119 | print('There are {} ({:.2f} million) parameters in this neural network'.format(
120 | nb_param, nb_param/1e6)
121 | )
122 |
123 | def sentence2vector(sentence):
124 | words = sentence.split()
125 | x = torch.LongTensor(len(words),1)
126 | for idx, word in enumerate(words):
127 |
128 | if word not in word2idx:
129 | print('You entered a word which is not in the vocabulary.')
130 | print('Make sure that you do not have any capital letters')
131 | else:
132 | x[idx,0]=word2idx[word]
133 | return x
134 |
135 |
136 | def show_next_word(scores):
137 | num_word_display=30
138 | prob=F.softmax(scores,dim=2)
139 | p=prob[-1].squeeze()
140 | p,word_idx = torch.topk(p,num_word_display)
141 |
142 | for i,idx in enumerate(word_idx):
143 | percentage= p[i].item()*100
144 | word= idx2word[idx.item()]
145 | print( "{:.1f}%\t".format(percentage), word )
146 |
147 |
148 |
149 |
150 | def check_mnist_dataset_exists(path_data='../../data/'):
151 | flag_train_data = os.path.isfile(path_data + 'mnist/train_data.pt')
152 | flag_train_label = os.path.isfile(path_data + 'mnist/train_label.pt')
153 | flag_test_data = os.path.isfile(path_data + 'mnist/test_data.pt')
154 | flag_test_label = os.path.isfile(path_data + 'mnist/test_label.pt')
155 | if flag_train_data==False or flag_train_label==False or flag_test_data==False or flag_test_label==False:
156 | print('MNIST dataset missing - downloading...')
157 | import torchvision
158 | import torchvision.transforms as transforms
159 | trainset = torchvision.datasets.MNIST(root=path_data + 'mnist/temp', train=True,
160 | download=True, transform=transforms.ToTensor())
161 | testset = torchvision.datasets.MNIST(root=path_data + 'mnist/temp', train=False,
162 | download=True, transform=transforms.ToTensor())
163 | train_data=torch.Tensor(60000,28,28)
164 | train_label=torch.LongTensor(60000)
165 | for idx , example in enumerate(trainset):
166 | train_data[idx]=example[0].squeeze()
167 | train_label[idx]=example[1]
168 | torch.save(train_data,path_data + 'mnist/train_data.pt')
169 | torch.save(train_label,path_data + 'mnist/train_label.pt')
170 | test_data=torch.Tensor(10000,28,28)
171 | test_label=torch.LongTensor(10000)
172 | for idx , example in enumerate(testset):
173 | test_data[idx]=example[0].squeeze()
174 | test_label[idx]=example[1]
175 | torch.save(test_data,path_data + 'mnist/test_data.pt')
176 | torch.save(test_label,path_data + 'mnist/test_label.pt')
177 | return path_data
178 |
179 | def check_fashion_mnist_dataset_exists(path_data='../../data/'):
180 | flag_train_data = os.path.isfile(path_data + 'fashion-mnist/train_data.pt')
181 | flag_train_label = os.path.isfile(path_data + 'fashion-mnist/train_label.pt')
182 | flag_test_data = os.path.isfile(path_data + 'fashion-mnist/test_data.pt')
183 | flag_test_label = os.path.isfile(path_data + 'fashion-mnist/test_label.pt')
184 | if flag_train_data==False or flag_train_label==False or flag_test_data==False or flag_test_label==False:
185 | print('FASHION-MNIST dataset missing - downloading...')
186 | import torchvision
187 | import torchvision.transforms as transforms
188 | trainset = torchvision.datasets.FashionMNIST(root=path_data + 'fashion-mnist/temp', train=True,
189 | download=True, transform=transforms.ToTensor())
190 | testset = torchvision.datasets.FashionMNIST(root=path_data + 'fashion-mnist/temp', train=False,
191 | download=True, transform=transforms.ToTensor())
192 | train_data=torch.Tensor(60000,28,28)
193 | train_label=torch.LongTensor(60000)
194 | for idx , example in enumerate(trainset):
195 | train_data[idx]=example[0].squeeze()
196 | train_label[idx]=example[1]
197 | torch.save(train_data,path_data + 'fashion-mnist/train_data.pt')
198 | torch.save(train_label,path_data + 'fashion-mnist/train_label.pt')
199 | test_data=torch.Tensor(10000,28,28)
200 | test_label=torch.LongTensor(10000)
201 | for idx , example in enumerate(testset):
202 | test_data[idx]=example[0].squeeze()
203 | test_label[idx]=example[1]
204 | torch.save(test_data,path_data + 'fashion-mnist/test_data.pt')
205 | torch.save(test_label,path_data + 'fashion-mnist/test_label.pt')
206 | return path_data
207 |
208 | def check_cifar_dataset_exists(path_data='../../data/'):
209 | flag_train_data = os.path.isfile(path_data + 'cifar/train_data.pt')
210 | flag_train_label = os.path.isfile(path_data + 'cifar/train_label.pt')
211 | flag_test_data = os.path.isfile(path_data + 'cifar/test_data.pt')
212 | flag_test_label = os.path.isfile(path_data + 'cifar/test_label.pt')
213 | if flag_train_data==False or flag_train_label==False or flag_test_data==False or flag_test_label==False:
214 | print('CIFAR dataset missing - downloading...')
215 | import torchvision
216 | import torchvision.transforms as transforms
217 | trainset = torchvision.datasets.CIFAR10(root=path_data + 'cifar/temp', train=True,
218 | download=True, transform=transforms.ToTensor())
219 | testset = torchvision.datasets.CIFAR10(root=path_data + 'cifar/temp', train=False,
220 | download=True, transform=transforms.ToTensor())
221 | train_data=torch.Tensor(50000,3,32,32)
222 | train_label=torch.LongTensor(50000)
223 | for idx , example in enumerate(trainset):
224 | train_data[idx]=example[0]
225 | train_label[idx]=example[1]
226 | torch.save(train_data,path_data + 'cifar/train_data.pt')
227 | torch.save(train_label,path_data + 'cifar/train_label.pt')
228 | test_data=torch.Tensor(10000,3,32,32)
229 | test_label=torch.LongTensor(10000)
230 | for idx , example in enumerate(testset):
231 | test_data[idx]=example[0]
232 | test_label[idx]=example[1]
233 | torch.save(test_data,path_data + 'cifar/test_data.pt')
234 | torch.save(test_label,path_data + 'cifar/test_label.pt')
235 | return path_data
236 |
237 |
238 |
--------------------------------------------------------------------------------