├── .DS_Store
├── Labs
├── Lab_1A.ipynb
├── Lab_1B.ipynb
├── Lab_2A.ipynb
├── Lab_2B.ipynb
├── Lab_3A.ipynb
├── Lab_3B.ipynb
├── Lab_4A.ipynb
├── Lab_4B.ipynb
├── Lab_5_6A.ipynb
├── Lab_6B.ipynb
└── Lab_7.ipynb
├── Meta_Learning_Notebooks
├── .DS_Store
├── 0_Basic_NNs
│ ├── .DS_Store
│ ├── .ipynb_checkpoints
│ │ ├── models-checkpoint.ipynb
│ │ ├── nb0-checkpoint.ipynb
│ │ └── utils-checkpoint.ipynb
│ ├── __pycache__
│ │ ├── models.cpython-37.pyc
│ │ └── utils.cpython-37.pyc
│ ├── models.ipynb
│ ├── nb0.ipynb
│ └── utils.ipynb
├── 1_MAML
│ ├── .DS_Store
│ ├── .ipynb_checkpoints
│ │ ├── l2lutils-checkpoint.ipynb
│ │ ├── models-checkpoint.ipynb
│ │ ├── nb1-checkpoint.ipynb
│ │ └── utils-checkpoint.ipynb
│ ├── data
│ ├── l2lutils.ipynb
│ ├── models.ipynb
│ ├── nb1.ipynb
│ └── utils.ipynb
├── 2_Model_MetaL
│ ├── .ipynb_checkpoints
│ │ ├── models-checkpoint.ipynb
│ │ ├── nb2-CNP-checkpoint.ipynb
│ │ └── nb2-checkpoint.ipynb
│ ├── data
│ ├── l2lutils.ipynb
│ ├── models.ipynb
│ ├── nb2-CNP.ipynb
│ ├── nb2-RNN.ipynb
│ ├── nb2.ipynb
│ └── utils.ipynb
├── 3_Metric_MetaL
│ ├── .ipynb_checkpoints
│ │ └── nb3-checkpoint.ipynb
│ ├── l2lutils.ipynb
│ ├── models.ipynb
│ ├── nb3.ipynb
│ └── utils.ipynb
├── 4_GLN_MetaL
│ ├── .DS_Store
│ ├── .ipynb_checkpoints
│ │ ├── models-checkpoint.ipynb
│ │ └── nb4-checkpoint.ipynb
│ ├── l2lutils.ipynb
│ ├── models.ipynb
│ ├── nb4.ipynb
│ ├── pygln
│ │ ├── .DS_Store
│ │ ├── __init__.py
│ │ ├── __pycache__
│ │ │ ├── __init__.cpython-37.pyc
│ │ │ ├── base.cpython-37.pyc
│ │ │ ├── gln.cpython-37.pyc
│ │ │ └── utils.cpython-37.pyc
│ │ ├── base.py
│ │ ├── baselines.py
│ │ ├── gln.bak
│ │ ├── gln.py
│ │ ├── jax
│ │ │ ├── __init__.py
│ │ │ └── gln.py
│ │ ├── numpy
│ │ │ ├── __init__.py
│ │ │ ├── __pycache__
│ │ │ │ ├── __init__.cpython-37.pyc
│ │ │ │ └── gln.cpython-37.pyc
│ │ │ └── gln.py
│ │ ├── pytorch
│ │ │ ├── __init__.py
│ │ │ ├── __pycache__
│ │ │ │ ├── __init__.cpython-37.pyc
│ │ │ │ └── gln.cpython-37.pyc
│ │ │ └── gln.py
│ │ ├── tf
│ │ │ ├── __init__.py
│ │ │ └── gln.py
│ │ └── utils.py
│ └── utils.ipynb
├── atcold_selected
│ ├── .DS_Store
│ ├── .ipynb_checkpoints
│ │ └── 05-regression-checkpoint.ipynb
│ ├── 04-spiral_classification.ipynb
│ ├── 05-regression.ipynb
│ ├── __pycache__
│ │ └── plot_lib.cpython-37.pyc
│ ├── plot_lib.py
│ └── sequential_tasks.py
└── data-gen-explorations-for-course
│ ├── .DS_Store
│ ├── .ipynb_checkpoints
│ ├── comp-checkpoint.ipynb
│ ├── l2l_data-checkpoint.ipynb
│ ├── models-checkpoint.ipynb
│ ├── sinedata-checkpoint.ipynb
│ └── test_features-checkpoint.ipynb
│ ├── comp.csv
│ ├── comp.ipynb
│ ├── comp.py
│ ├── l2l_data.ipynb
│ ├── models.ipynb
│ ├── sinedata.ipynb
│ └── test_features.ipynb
├── README.md
└── Tutorials
├── .DS_Store
├── DL-Quiz
└── dl_quiz.pdf
├── Week1
├── main.pdf
└── week1_summary.pdf
├── Week2
├── main.pdf
└── week2_summary.pdf
├── Week3
├── CNN Examples_TD.pdf
└── week3_summary.pdf
├── Week4
├── cnn_backprop.pdf
└── week4_summary.pdf
├── Week5
└── week5_summary.pdf
└── Week6
└── week6_summary.pdf
/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tirtharajdash/BITS-G513_MetaLearning/81d62d08a420fb94059e04c2f2e9c7d0af10e151/.DS_Store
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tirtharajdash/BITS-G513_MetaLearning/81d62d08a420fb94059e04c2f2e9c7d0af10e151/Meta_Learning_Notebooks/.DS_Store
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/0_Basic_NNs/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tirtharajdash/BITS-G513_MetaLearning/81d62d08a420fb94059e04c2f2e9c7d0af10e151/Meta_Learning_Notebooks/0_Basic_NNs/.DS_Store
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/0_Basic_NNs/.ipynb_checkpoints/models-checkpoint.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Deep Learning Models etc"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": 4,
13 | "metadata": {},
14 | "outputs": [
15 | {
16 | "name": "stdout",
17 | "output_type": "stream",
18 | "text": [
19 | "importing Jupyter notebook from utils.ipynb\n"
20 | ]
21 | },
22 | {
23 | "data": {
24 | "text/html": [
25 | "\n",
26 | " \n",
33 | "\n",
34 | " Imports 1 (show/hide)\n",
35 | " "
36 | ],
37 | "text/plain": [
38 | ""
39 | ]
40 | },
41 | "execution_count": 4,
42 | "metadata": {},
43 | "output_type": "execute_result"
44 | }
45 | ],
46 | "source": [
47 | "from IPython.display import HTML\n",
48 | "import import_ipynb\n",
49 | "import random\n",
50 | "import utils\n",
51 | "utils.hide_toggle('Imports 1')"
52 | ]
53 | },
54 | {
55 | "cell_type": "code",
56 | "execution_count": 5,
57 | "metadata": {},
58 | "outputs": [
59 | {
60 | "data": {
61 | "text/html": [
62 | "\n",
63 | " \n",
70 | "\n",
71 | " Imports 2 (show/hide)\n",
72 | " "
73 | ],
74 | "text/plain": [
75 | ""
76 | ]
77 | },
78 | "execution_count": 5,
79 | "metadata": {},
80 | "output_type": "execute_result"
81 | }
82 | ],
83 | "source": [
84 | "import numpy as np\n",
85 | "import torch\n",
86 | "from torch import nn\n",
87 | "from torch import optim\n",
88 | "from IPython import display\n",
89 | "utils.hide_toggle('Imports 2')"
90 | ]
91 | },
92 | {
93 | "cell_type": "markdown",
94 | "metadata": {},
95 | "source": [
96 | "Compute accuracy of predictions."
97 | ]
98 | },
99 | {
100 | "cell_type": "code",
101 | "execution_count": 40,
102 | "metadata": {},
103 | "outputs": [
104 | {
105 | "data": {
106 | "text/html": [
107 | "\n",
108 | " \n",
115 | "\n",
116 | " Function: accuracy (show/hide)\n",
117 | " "
118 | ],
119 | "text/plain": [
120 | ""
121 | ]
122 | },
123 | "execution_count": 40,
124 | "metadata": {},
125 | "output_type": "execute_result"
126 | }
127 | ],
128 | "source": [
129 | "def accuracy(Net,X_test,y_test,verbose=True):\n",
130 | " Net.eval()\n",
131 | " m = X_test.shape[0]\n",
132 | " y_pred = Net(X_test)\n",
133 | " predicted = torch.max(y_pred, 1)[1]\n",
134 | " correct = (predicted == y_test).float().sum().item()\n",
135 | " if verbose: print(correct,m)\n",
136 | " accuracy = correct/m\n",
137 | " Net.train()\n",
138 | " return accuracy\n",
139 | "utils.hide_toggle('Function: accuracy')"
140 | ]
141 | },
142 | {
143 | "cell_type": "markdown",
144 | "metadata": {},
145 | "source": [
146 | "Generic training loop"
147 | ]
148 | },
149 | {
150 | "cell_type": "code",
151 | "execution_count": 7,
152 | "metadata": {},
153 | "outputs": [
154 | {
155 | "data": {
156 | "text/html": [
157 | "\n",
158 | " \n",
165 | "\n",
166 | " Function Train (show/hide)\n",
167 | " "
168 | ],
169 | "text/plain": [
170 | ""
171 | ]
172 | },
173 | "execution_count": 7,
174 | "metadata": {},
175 | "output_type": "execute_result"
176 | }
177 | ],
178 | "source": [
179 | "def Train(Net,data,epochs=20,lr=5e-2,Loss=nn.NLLLoss(),verbose=False):\n",
180 | " #optimizer = optim.Adam(Net.parameters(),lr=lr)\n",
181 | " losses = []\n",
182 | " accs = []\n",
183 | " for e in range(epochs):\n",
184 | " step=0\n",
185 | " tot_loss=0.0\n",
186 | " acc=0.0\n",
187 | " for (X,y) in data:\n",
188 | " y_pred = Net(X)\n",
189 | " loss = Loss(y_pred,y)\n",
190 | " Net.optimizer.zero_grad()\n",
191 | " loss.backward()\n",
192 | " Net.optimizer.step()\n",
193 | " step+=1\n",
194 | " tot_loss+=loss\n",
195 | " acc+=accuracy(Net,X,y,verbose=False)\n",
196 | " l = tot_loss.item()/step\n",
197 | " a = acc/step\n",
198 | " losses += [l]\n",
199 | " accs += [a]\n",
200 | " if verbose: \n",
201 | " print('Epoch % 2d Loss: %2.5e Accuracy: %2.5f'%(e,l,a))\n",
202 | " display.clear_output(wait=True)\n",
203 | " return Net,losses,accs\n",
204 | "utils.hide_toggle('Function Train')"
205 | ]
206 | },
207 | {
208 | "cell_type": "markdown",
209 | "metadata": {},
210 | "source": [
211 | "Multi-layer perceptron with ReLU non-lineartities; for classification or regression."
212 | ]
213 | },
214 | {
215 | "cell_type": "code",
216 | "execution_count": 11,
217 | "metadata": {},
218 | "outputs": [
219 | {
220 | "data": {
221 | "text/html": [
222 | "\n",
223 | " \n",
230 | "\n",
231 | " Class MLP (show/hide)\n",
232 | " "
233 | ],
234 | "text/plain": [
235 | ""
236 | ]
237 | },
238 | "execution_count": 11,
239 | "metadata": {},
240 | "output_type": "execute_result"
241 | }
242 | ],
243 | "source": [
244 | "class MLP(nn.Module):\n",
245 | " def __init__(self,dims=[5,3,2],task='classification',lr=1e-3):\n",
246 | " super(MLP,self).__init__()\n",
247 | " self.dims=dims\n",
248 | " self.n = len(self.dims)-1\n",
249 | " self.task=task\n",
250 | " self.layers=nn.ModuleList()\n",
251 | " for i in range(self.n-1):\n",
252 | " self.layers.append(nn.Linear(dims[i],dims[i+1]))\n",
253 | " self.layers.append(nn.ReLU())\n",
254 | " if task=='classification': \n",
255 | " self.layers.append(nn.Linear(dims[i+1],dims[i+2]))\n",
256 | " self.layers.append(nn.LogSoftmax(dim=1))\n",
257 | " elif task=='regression': \n",
258 | " self.layers.append(nn.Linear(dims[i+1],dims[i+2]))\n",
259 | " self.layers.append(nn.Linear(dims[i+2],1))\n",
260 | " else: self.layers.append(nn.Linear(dims[i+1],dims[i+2]))\n",
261 | " self.optimizer = optim.Adam(self.parameters(),lr=lr)\n",
262 | " def forward(self,x):\n",
263 | " for l in self.layers:\n",
264 | " x = l(x)\n",
265 | " return(x)\n",
266 | "utils.hide_toggle('Class MLP')"
267 | ]
268 | },
269 | {
270 | "cell_type": "markdown",
271 | "metadata": {},
272 | "source": [
273 | "Recurrent network using GRU"
274 | ]
275 | },
276 | {
277 | "cell_type": "code",
278 | "execution_count": 9,
279 | "metadata": {},
280 | "outputs": [
281 | {
282 | "data": {
283 | "text/html": [
284 | "\n",
285 | " \n",
292 | "\n",
293 | " Class RNN (show/hide)\n",
294 | " "
295 | ],
296 | "text/plain": [
297 | ""
298 | ]
299 | },
300 | "execution_count": 9,
301 | "metadata": {},
302 | "output_type": "execute_result"
303 | }
304 | ],
305 | "source": [
306 | "class RNN(nn.Module):\n",
307 | " def __init__(self,n_features=1,dim=5,n_layers=3,n_classes=2,lr=1e-3):\n",
308 | " super(RNN,self).__init__()\n",
309 | " self.n_layers = n_layers\n",
310 | " self.dim = dim\n",
311 | " self.n_features = n_features\n",
312 | " self.rnn = nn.GRU(n_features, dim, n_layers,batch_first=True)\n",
313 | " self.linear = nn.Linear(dim,n_classes)\n",
314 | " self.logsoft = nn.LogSoftmax(dim=-1)\n",
315 | " self.optimizer = optim.Adam(self.parameters(),lr=lr)\n",
316 | " def forward(self,X):\n",
317 | " if self.n_features==1: X=X.unsqueeze(-1)\n",
318 | " h = torch.randn(self.n_layers,X.shape[0],self.dim)\n",
319 | " H = self.rnn(X,h)[1]\n",
320 | " G = self.logsoft(self.linear(H[-1,:,:]))\n",
321 | " return G\n",
322 | "utils.hide_toggle('Class RNN')"
323 | ]
324 | },
325 | {
326 | "cell_type": "code",
327 | "execution_count": null,
328 | "metadata": {},
329 | "outputs": [],
330 | "source": []
331 | }
332 | ],
333 | "metadata": {
334 | "kernelspec": {
335 | "display_name": "Python 3",
336 | "language": "python",
337 | "name": "python3"
338 | },
339 | "language_info": {
340 | "codemirror_mode": {
341 | "name": "ipython",
342 | "version": 3
343 | },
344 | "file_extension": ".py",
345 | "mimetype": "text/x-python",
346 | "name": "python",
347 | "nbconvert_exporter": "python",
348 | "pygments_lexer": "ipython3",
349 | "version": "3.7.9"
350 | }
351 | },
352 | "nbformat": 4,
353 | "nbformat_minor": 4
354 | }
355 |
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/0_Basic_NNs/.ipynb_checkpoints/nb0-checkpoint.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# TRAINING DEEP NEURAL NETWORKS"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": null,
13 | "metadata": {},
14 | "outputs": [],
15 | "source": [
16 | "#from google.colab import drive\n",
17 | "#drive.mount('/content/drive')\n",
18 | "#%cd drive/MyDrive/'Colab Notebooks'\n",
19 | "#%cd meta-learning-course-notebooks/0_Basic_NNs/\n",
20 | "#!ls"
21 | ]
22 | },
23 | {
24 | "cell_type": "code",
25 | "execution_count": null,
26 | "metadata": {},
27 | "outputs": [],
28 | "source": [
29 | "#!pip install import_ipynb --quiet"
30 | ]
31 | },
32 | {
33 | "cell_type": "code",
34 | "execution_count": null,
35 | "metadata": {
36 | "scrolled": false
37 | },
38 | "outputs": [],
39 | "source": [
40 | "import import_ipynb\n",
41 | "import utils\n",
42 | "import models\n",
43 | "from IPython import display\n",
44 | "import torch\n",
45 | "from sklearn.manifold import TSNE\n",
46 | "from matplotlib import pyplot as plt"
47 | ]
48 | },
49 | {
50 | "cell_type": "markdown",
51 | "metadata": {},
52 | "source": [
53 | "Random data in $\\mathcal{R}^N$ in $C$ clusters/classes."
54 | ]
55 | },
56 | {
57 | "cell_type": "code",
58 | "execution_count": null,
59 | "metadata": {
60 | "scrolled": false
61 | },
62 | "outputs": [],
63 | "source": [
64 | "#Generate data - euclidean\n",
65 | "train_ds, test_ds, dloader = utils.euclideanDataset(n_samples=10000,n_features=20,n_classes=10,batch_size=32)"
66 | ]
67 | },
68 | {
69 | "cell_type": "markdown",
70 | "metadata": {},
71 | "source": [
72 | "Project data down to a 2-D space for visualiation."
73 | ]
74 | },
75 | {
76 | "cell_type": "code",
77 | "execution_count": null,
78 | "metadata": {},
79 | "outputs": [],
80 | "source": [
81 | "tsne = TSNE()\n",
82 | "X_2d = tsne.fit_transform(train_ds.samples[0:100])\n",
83 | "plt.scatter(X_2d[:,0],X_2d[:,1])\n",
84 | "plt.show()"
85 | ]
86 | },
87 | {
88 | "cell_type": "code",
89 | "execution_count": null,
90 | "metadata": {
91 | "scrolled": false
92 | },
93 | "outputs": [],
94 | "source": [
95 | "# See some batches of data.\n",
96 | "for b in dloader:\n",
97 | " print(b)\n",
98 | " samp = b\n",
99 | " break"
100 | ]
101 | },
102 | {
103 | "cell_type": "code",
104 | "execution_count": null,
105 | "metadata": {
106 | "scrolled": false
107 | },
108 | "outputs": [],
109 | "source": [
110 | "# Define an MLP network. Note that input dimension has to be data dimension. For classification\n",
111 | "# final dimension has to be number of classes; for regression one.\n",
112 | "#torch.manual_seed(10)\n",
113 | "net = models.MLP(dims=[20,32,10])"
114 | ]
115 | },
116 | {
117 | "cell_type": "markdown",
118 | "metadata": {},
119 | "source": [
120 | "Train the network"
121 | ]
122 | },
123 | {
124 | "cell_type": "code",
125 | "execution_count": null,
126 | "metadata": {},
127 | "outputs": [],
128 | "source": [
129 | "# Train the network; note that network is trained in place so repeated calls further train it.\n",
130 | "net,losses,accs=models.Train(net,dloader,lr=1e-4,epochs=100,verbose=True)"
131 | ]
132 | },
133 | {
134 | "cell_type": "markdown",
135 | "metadata": {},
136 | "source": [
137 | "Accuracy on training set."
138 | ]
139 | },
140 | {
141 | "cell_type": "code",
142 | "execution_count": null,
143 | "metadata": {
144 | "scrolled": false
145 | },
146 | "outputs": [],
147 | "source": [
148 | "#Training accuracy.\n",
149 | "models.accuracy(net,train_ds.samples,train_ds.labels,verbose=True)"
150 | ]
151 | },
152 | {
153 | "cell_type": "markdown",
154 | "metadata": {},
155 | "source": [
156 | "Accuracy on test set."
157 | ]
158 | },
159 | {
160 | "cell_type": "code",
161 | "execution_count": null,
162 | "metadata": {
163 | "scrolled": false
164 | },
165 | "outputs": [],
166 | "source": [
167 | "# Test accuracy.\n",
168 | "models.accuracy(net,test_ds.samples,test_ds.labels)"
169 | ]
170 | }
171 | ],
172 | "metadata": {
173 | "kernelspec": {
174 | "display_name": "Python 3",
175 | "language": "python",
176 | "name": "python3"
177 | },
178 | "language_info": {
179 | "codemirror_mode": {
180 | "name": "ipython",
181 | "version": 3
182 | },
183 | "file_extension": ".py",
184 | "mimetype": "text/x-python",
185 | "name": "python",
186 | "nbconvert_exporter": "python",
187 | "pygments_lexer": "ipython3",
188 | "version": "3.7.9"
189 | }
190 | },
191 | "nbformat": 4,
192 | "nbformat_minor": 4
193 | }
194 |
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/0_Basic_NNs/.ipynb_checkpoints/utils-checkpoint.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Dataset Generation"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": 1,
13 | "metadata": {},
14 | "outputs": [],
15 | "source": [
16 | "import torch\n",
17 | "import numpy as np\n",
18 | "from torch.utils.data import Dataset\n",
19 | "import sklearn.datasets as skds\n",
20 | "from sklearn.model_selection import train_test_split\n",
21 | "from torch.utils.data import DataLoader\n",
22 | "import math\n",
23 | "from numpy import sin, pi\n",
24 | "from matplotlib import pyplot as plt\n",
25 | "from torchvision.datasets import MNIST"
26 | ]
27 | },
28 | {
29 | "cell_type": "markdown",
30 | "metadata": {},
31 | "source": [
32 | "Data generation routines will return a MyDS dataset class of type torch.utils.Dataset"
33 | ]
34 | },
35 | {
36 | "cell_type": "code",
37 | "execution_count": 21,
38 | "metadata": {},
39 | "outputs": [],
40 | "source": [
41 | "class MyDS(Dataset):\n",
42 | " def __init__(self, X,y):\n",
43 | " self.samples = torch.DoubleTensor(X)\n",
44 | " self.labels = torch.LongTensor(y)\n",
45 | " \n",
46 | " def __len__(self):\n",
47 | " return len(self.samples)\n",
48 | "\n",
49 | " def __getitem__(self, idx):\n",
50 | " return (self.samples[idx],self.labels[idx])"
51 | ]
52 | },
53 | {
54 | "cell_type": "markdown",
55 | "metadata": {},
56 | "source": [
57 | "Generate data in $\\mathcal{R}^n$ with $n$ features, a number of classes, prescribed class separation and clusters per class. Return datasets for training and testing data and data-loader for training data using prescribed batch size."
58 | ]
59 | },
60 | {
61 | "cell_type": "code",
62 | "execution_count": 3,
63 | "metadata": {},
64 | "outputs": [],
65 | "source": [
66 | "def euclideanDataset(n_samples=1000,n_classes=2,class_sep=2.0,n_features=5,batch_size=1):\n",
67 | " X,y = skds.make_classification(n_samples=n_samples,n_classes=n_classes,\n",
68 | " class_sep=class_sep,n_features=n_features, \n",
69 | " n_informative=n_features,n_redundant=0,\n",
70 | " n_repeated=0,n_clusters_per_class=1)\n",
71 | " X_train,X_test,y_train,y_test = train_test_split(X,y)\n",
72 | " ds = MyDS(X_train,y_train)\n",
73 | " ds_hold = MyDS(X_test,y_test)\n",
74 | " dsloader = torch.utils.data.DataLoader(dataset=ds,batch_size=batch_size,shuffle=True)\n",
75 | " return ds,ds_hold,dsloader"
76 | ]
77 | },
78 | {
79 | "cell_type": "markdown",
80 | "metadata": {},
81 | "source": [
82 | "Generate samples from a sinwave $a$ sin$(f\\pi t)$ of scale $a$, frequency $f$ at intervals $t = i \\delta t$ "
83 | ]
84 | },
85 | {
86 | "cell_type": "code",
87 | "execution_count": 4,
88 | "metadata": {},
89 | "outputs": [],
90 | "source": [
91 | "class mysin():\n",
92 | " def __init__(self,ampl = 1.0, freq=0.5, delta=0.1,phase=0.0):\n",
93 | " self.f,self.d,self.a,self.p = freq, delta, ampl, phase\n",
94 | " self.t = 0.0\n",
95 | " return\n",
96 | " def reset(self,t=0.0):\n",
97 | " self.t = 0.0\n",
98 | " return self.func()\n",
99 | " def next(self):\n",
100 | " val = self.func() \n",
101 | " self.t += self.d\n",
102 | " return val\n",
103 | " def __call__(self,t):\n",
104 | " old_t = self.t\n",
105 | " self.t = t\n",
106 | " val = self.func()\n",
107 | " self.t = old_t\n",
108 | " return val\n",
109 | " def func(self):\n",
110 | " return self.a * sin(pi*(self.f*self.t+self.p))\n",
111 | " def series(self,n=10):\n",
112 | " return np.array([self(t*self.d) for t in range(n)])\n",
113 | " def set_phase(self,phase=0.0):\n",
114 | " self.p = phase"
115 | ]
116 | },
117 | {
118 | "cell_type": "code",
119 | "execution_count": 5,
120 | "metadata": {},
121 | "outputs": [],
122 | "source": [
123 | "def sinmix(X,m):\n",
124 | " w = []\n",
125 | " for i in range(X.shape[0]):\n",
126 | " s = np.zeros(m)\n",
127 | " for j in [2*k for k in range(int(X.shape[1]/2))]:\n",
128 | " if (j/2)%2 == 0: s+=mysin(freq=X[i,j],phase=1.5,ampl=X[i,j+1]).series(m)\n",
129 | " elif (j/2)%2 == 1: s+=np.tanh(mysin(freq=X[i,j],phase=1.5,ampl=X[i,j+1]).series(m))\n",
130 | " w+=[s]\n",
131 | " return np.array(w)"
132 | ]
133 | },
134 | {
135 | "cell_type": "code",
136 | "execution_count": 6,
137 | "metadata": {},
138 | "outputs": [],
139 | "source": [
140 | "#X = np.array([[2.5,1.0,3.5,1.0]])\n",
141 | "#plt.plot(sinmix(X,20)[0])\n",
142 | "#plt.show()"
143 | ]
144 | },
145 | {
146 | "cell_type": "markdown",
147 | "metadata": {},
148 | "source": [
149 | "Random time-series = random frequency (f) and amplitude (a); random instance of a class = random phase (p) and length (l). We will generate n random clusters in 4D = {$f_1$,$a_1$,$f_2$,$a_2$}. For starters we will choose phase and length as constants and sum the two sine waves for each 4D vector to get a time series of fixed length."
150 | ]
151 | },
152 | {
153 | "cell_type": "code",
154 | "execution_count": 7,
155 | "metadata": {},
156 | "outputs": [],
157 | "source": [
158 | "def sinDataset(n_samples=10,n_classes=10,length=10,batch_size=1):\n",
159 | " n_features = 4\n",
160 | " X,y = skds.make_classification(n_samples=n_samples,n_classes=n_classes,\n",
161 | " class_sep=4.0,n_features=n_features, \n",
162 | " n_informative=n_features,n_redundant=0,\n",
163 | " n_repeated=0,n_clusters_per_class=1)\n",
164 | " S = sinmix(X,length)\n",
165 | " X_train,X_test,y_train,y_test = train_test_split(S,y)\n",
166 | " ds = MyDS(X_train,y_train)\n",
167 | " ds_hold = MyDS(X_test,y_test)\n",
168 | " dsloader = torch.utils.data.DataLoader(dataset=ds,batch_size=1,shuffle=True)\n",
169 | " return ds,ds_hold,dsloader"
170 | ]
171 | },
172 | {
173 | "cell_type": "code",
174 | "execution_count": 8,
175 | "metadata": {},
176 | "outputs": [],
177 | "source": [
178 | "S,y,dl = sinDataset(n_classes=5,n_samples=30,length=50)"
179 | ]
180 | },
181 | {
182 | "cell_type": "markdown",
183 | "metadata": {},
184 | "source": [
185 | "MNIST Dataset"
186 | ]
187 | },
188 | {
189 | "cell_type": "code",
190 | "execution_count": 19,
191 | "metadata": {},
192 | "outputs": [],
193 | "source": [
194 | "def mnist_data(batch_size=32,frac=1.0):\n",
195 | " trainset = MNIST('./data', train=True, download=False)\n",
196 | " X_train = trainset.data.numpy().reshape(60000, -1).astype(np.float) / 255\n",
197 | " n_train = int(X_train.shape[0]*frac)\n",
198 | " X_train = X_train[0:n_train]\n",
199 | " y_train = trainset.targets\n",
200 | " y_train = y_train[0:n_train]\n",
201 | " testset = MNIST('./data', train=False, download=False)\n",
202 | " X_test = testset.data.numpy().reshape(10000, -1).astype(np.float) / 255\n",
203 | " y_test = testset.targets\n",
204 | " ds = MyDS(X_train,y_train)\n",
205 | " ds_hold = MyDS(X_test,y_test)\n",
206 | " dsloader = torch.utils.data.DataLoader(dataset=ds,batch_size=batch_size,shuffle=True)\n",
207 | " return ds,ds_hold,dsloader"
208 | ]
209 | },
210 | {
211 | "cell_type": "code",
212 | "execution_count": 22,
213 | "metadata": {},
214 | "outputs": [],
215 | "source": [
216 | "ds,dsh,dsldr = mnist_data()"
217 | ]
218 | },
219 | {
220 | "cell_type": "code",
221 | "execution_count": 26,
222 | "metadata": {},
223 | "outputs": [
224 | {
225 | "data": {
226 | "text/plain": [
227 | "torch.Size([60000])"
228 | ]
229 | },
230 | "execution_count": 26,
231 | "metadata": {},
232 | "output_type": "execute_result"
233 | }
234 | ],
235 | "source": [
236 | "ds.labels.shape"
237 | ]
238 | },
239 | {
240 | "cell_type": "code",
241 | "execution_count": null,
242 | "metadata": {},
243 | "outputs": [],
244 | "source": []
245 | }
246 | ],
247 | "metadata": {
248 | "kernelspec": {
249 | "display_name": "Python 3",
250 | "language": "python",
251 | "name": "python3"
252 | },
253 | "language_info": {
254 | "codemirror_mode": {
255 | "name": "ipython",
256 | "version": 3
257 | },
258 | "file_extension": ".py",
259 | "mimetype": "text/x-python",
260 | "name": "python",
261 | "nbconvert_exporter": "python",
262 | "pygments_lexer": "ipython3",
263 | "version": "3.7.9"
264 | }
265 | },
266 | "nbformat": 4,
267 | "nbformat_minor": 4
268 | }
269 |
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/0_Basic_NNs/__pycache__/models.cpython-37.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tirtharajdash/BITS-G513_MetaLearning/81d62d08a420fb94059e04c2f2e9c7d0af10e151/Meta_Learning_Notebooks/0_Basic_NNs/__pycache__/models.cpython-37.pyc
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/0_Basic_NNs/__pycache__/utils.cpython-37.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tirtharajdash/BITS-G513_MetaLearning/81d62d08a420fb94059e04c2f2e9c7d0af10e151/Meta_Learning_Notebooks/0_Basic_NNs/__pycache__/utils.cpython-37.pyc
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/0_Basic_NNs/models.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Deep Learning Models etc"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": 4,
13 | "metadata": {},
14 | "outputs": [
15 | {
16 | "name": "stdout",
17 | "output_type": "stream",
18 | "text": [
19 | "importing Jupyter notebook from utils.ipynb\n"
20 | ]
21 | },
22 | {
23 | "data": {
24 | "text/html": [
25 | "\n",
26 | " \n",
33 | "\n",
34 | " Imports 1 (show/hide)\n",
35 | " "
36 | ],
37 | "text/plain": [
38 | ""
39 | ]
40 | },
41 | "execution_count": 4,
42 | "metadata": {},
43 | "output_type": "execute_result"
44 | }
45 | ],
46 | "source": [
47 | "from IPython.display import HTML\n",
48 | "import import_ipynb\n",
49 | "import random\n",
50 | "import utils\n",
51 | "utils.hide_toggle('Imports 1')"
52 | ]
53 | },
54 | {
55 | "cell_type": "code",
56 | "execution_count": 5,
57 | "metadata": {},
58 | "outputs": [
59 | {
60 | "data": {
61 | "text/html": [
62 | "\n",
63 | " \n",
70 | "\n",
71 | " Imports 2 (show/hide)\n",
72 | " "
73 | ],
74 | "text/plain": [
75 | ""
76 | ]
77 | },
78 | "execution_count": 5,
79 | "metadata": {},
80 | "output_type": "execute_result"
81 | }
82 | ],
83 | "source": [
84 | "import numpy as np\n",
85 | "import torch\n",
86 | "from torch import nn\n",
87 | "from torch import optim\n",
88 | "from IPython import display\n",
89 | "utils.hide_toggle('Imports 2')"
90 | ]
91 | },
92 | {
93 | "cell_type": "markdown",
94 | "metadata": {},
95 | "source": [
96 | "Compute accuracy of predictions."
97 | ]
98 | },
99 | {
100 | "cell_type": "code",
101 | "execution_count": 40,
102 | "metadata": {},
103 | "outputs": [
104 | {
105 | "data": {
106 | "text/html": [
107 | "\n",
108 | " \n",
115 | "\n",
116 | " Function: accuracy (show/hide)\n",
117 | " "
118 | ],
119 | "text/plain": [
120 | ""
121 | ]
122 | },
123 | "execution_count": 40,
124 | "metadata": {},
125 | "output_type": "execute_result"
126 | }
127 | ],
128 | "source": [
129 | "def accuracy(Net,X_test,y_test,verbose=True):\n",
130 | " Net.eval()\n",
131 | " m = X_test.shape[0]\n",
132 | " y_pred = Net(X_test)\n",
133 | " predicted = torch.max(y_pred, 1)[1]\n",
134 | " correct = (predicted == y_test).float().sum().item()\n",
135 | " if verbose: print(correct,m)\n",
136 | " accuracy = correct/m\n",
137 | " Net.train()\n",
138 | " return accuracy\n",
139 | "utils.hide_toggle('Function: accuracy')"
140 | ]
141 | },
142 | {
143 | "cell_type": "markdown",
144 | "metadata": {},
145 | "source": [
146 | "Generic training loop"
147 | ]
148 | },
149 | {
150 | "cell_type": "code",
151 | "execution_count": 7,
152 | "metadata": {},
153 | "outputs": [
154 | {
155 | "data": {
156 | "text/html": [
157 | "\n",
158 | " \n",
165 | "\n",
166 | " Function Train (show/hide)\n",
167 | " "
168 | ],
169 | "text/plain": [
170 | ""
171 | ]
172 | },
173 | "execution_count": 7,
174 | "metadata": {},
175 | "output_type": "execute_result"
176 | }
177 | ],
178 | "source": [
179 | "def Train(Net,data,epochs=20,lr=5e-2,Loss=nn.NLLLoss(),verbose=False):\n",
180 | " #optimizer = optim.Adam(Net.parameters(),lr=lr)\n",
181 | " losses = []\n",
182 | " accs = []\n",
183 | " for e in range(epochs):\n",
184 | " step=0\n",
185 | " tot_loss=0.0\n",
186 | " acc=0.0\n",
187 | " for (X,y) in data:\n",
188 | " y_pred = Net(X)\n",
189 | " loss = Loss(y_pred,y)\n",
190 | " Net.optimizer.zero_grad()\n",
191 | " loss.backward()\n",
192 | " Net.optimizer.step()\n",
193 | " step+=1\n",
194 | " tot_loss+=loss\n",
195 | " acc+=accuracy(Net,X,y,verbose=False)\n",
196 | " l = tot_loss.item()/step\n",
197 | " a = acc/step\n",
198 | " losses += [l]\n",
199 | " accs += [a]\n",
200 | " if verbose: \n",
201 | " print('Epoch % 2d Loss: %2.5e Accuracy: %2.5f'%(e,l,a))\n",
202 | " display.clear_output(wait=True)\n",
203 | " return Net,losses,accs\n",
204 | "utils.hide_toggle('Function Train')"
205 | ]
206 | },
207 | {
208 | "cell_type": "markdown",
209 | "metadata": {},
210 | "source": [
211 | "Multi-layer perceptron with ReLU non-lineartities; for classification or regression."
212 | ]
213 | },
214 | {
215 | "cell_type": "code",
216 | "execution_count": 11,
217 | "metadata": {},
218 | "outputs": [
219 | {
220 | "data": {
221 | "text/html": [
222 | "\n",
223 | " \n",
230 | "\n",
231 | " Class MLP (show/hide)\n",
232 | " "
233 | ],
234 | "text/plain": [
235 | ""
236 | ]
237 | },
238 | "execution_count": 11,
239 | "metadata": {},
240 | "output_type": "execute_result"
241 | }
242 | ],
243 | "source": [
244 | "class MLP(nn.Module):\n",
245 | " def __init__(self,dims=[5,3,2],task='classification',lr=1e-3):\n",
246 | " super(MLP,self).__init__()\n",
247 | " self.dims=dims\n",
248 | " self.n = len(self.dims)-1\n",
249 | " self.task=task\n",
250 | " self.layers=nn.ModuleList()\n",
251 | " for i in range(self.n-1):\n",
252 | " self.layers.append(nn.Linear(dims[i],dims[i+1]))\n",
253 | " self.layers.append(nn.ReLU())\n",
254 | " if task=='classification': \n",
255 | " self.layers.append(nn.Linear(dims[i+1],dims[i+2]))\n",
256 | " self.layers.append(nn.LogSoftmax(dim=1))\n",
257 | " elif task=='regression': \n",
258 | " self.layers.append(nn.Linear(dims[i+1],dims[i+2]))\n",
259 | " self.layers.append(nn.Linear(dims[i+2],1))\n",
260 | " else: self.layers.append(nn.Linear(dims[i+1],dims[i+2]))\n",
261 | " self.optimizer = optim.Adam(self.parameters(),lr=lr)\n",
262 | " def forward(self,x):\n",
263 | " for l in self.layers:\n",
264 | " x = l(x)\n",
265 | " return(x)\n",
266 | "utils.hide_toggle('Class MLP')"
267 | ]
268 | },
269 | {
270 | "cell_type": "markdown",
271 | "metadata": {},
272 | "source": [
273 | "Recurrent network using GRU"
274 | ]
275 | },
276 | {
277 | "cell_type": "code",
278 | "execution_count": 9,
279 | "metadata": {},
280 | "outputs": [
281 | {
282 | "data": {
283 | "text/html": [
284 | "\n",
285 | " \n",
292 | "\n",
293 | " Class RNN (show/hide)\n",
294 | " "
295 | ],
296 | "text/plain": [
297 | ""
298 | ]
299 | },
300 | "execution_count": 9,
301 | "metadata": {},
302 | "output_type": "execute_result"
303 | }
304 | ],
305 | "source": [
306 | "class RNN(nn.Module):\n",
307 | " def __init__(self,n_features=1,dim=5,n_layers=3,n_classes=2,lr=1e-3):\n",
308 | " super(RNN,self).__init__()\n",
309 | " self.n_layers = n_layers\n",
310 | " self.dim = dim\n",
311 | " self.n_features = n_features\n",
312 | " self.rnn = nn.GRU(n_features, dim, n_layers,batch_first=True)\n",
313 | " self.linear = nn.Linear(dim,n_classes)\n",
314 | " self.logsoft = nn.LogSoftmax(dim=-1)\n",
315 | " self.optimizer = optim.Adam(self.parameters(),lr=lr)\n",
316 | " def forward(self,X):\n",
317 | " if self.n_features==1: X=X.unsqueeze(-1)\n",
318 | " h = torch.randn(self.n_layers,X.shape[0],self.dim)\n",
319 | " H = self.rnn(X,h)[1]\n",
320 | " G = self.logsoft(self.linear(H[-1,:,:]))\n",
321 | " return G\n",
322 | "utils.hide_toggle('Class RNN')"
323 | ]
324 | },
325 | {
326 | "cell_type": "code",
327 | "execution_count": null,
328 | "metadata": {},
329 | "outputs": [],
330 | "source": []
331 | }
332 | ],
333 | "metadata": {
334 | "kernelspec": {
335 | "display_name": "Python 3",
336 | "language": "python",
337 | "name": "python3"
338 | },
339 | "language_info": {
340 | "codemirror_mode": {
341 | "name": "ipython",
342 | "version": 3
343 | },
344 | "file_extension": ".py",
345 | "mimetype": "text/x-python",
346 | "name": "python",
347 | "nbconvert_exporter": "python",
348 | "pygments_lexer": "ipython3",
349 | "version": "3.7.9"
350 | }
351 | },
352 | "nbformat": 4,
353 | "nbformat_minor": 4
354 | }
355 |
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/0_Basic_NNs/nb0.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# TRAINING DEEP NEURAL NETWORKS"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": null,
13 | "metadata": {},
14 | "outputs": [],
15 | "source": [
16 | "#from google.colab import drive\n",
17 | "#drive.mount('/content/drive')\n",
18 | "#%cd drive/MyDrive/'Colab Notebooks'\n",
19 | "#%cd meta-learning-course-notebooks/0_Basic_NNs/\n",
20 | "#!ls"
21 | ]
22 | },
23 | {
24 | "cell_type": "code",
25 | "execution_count": null,
26 | "metadata": {},
27 | "outputs": [],
28 | "source": [
29 | "#!pip install import_ipynb --quiet"
30 | ]
31 | },
32 | {
33 | "cell_type": "code",
34 | "execution_count": null,
35 | "metadata": {
36 | "scrolled": false
37 | },
38 | "outputs": [],
39 | "source": [
40 | "import import_ipynb\n",
41 | "import utils\n",
42 | "import models\n",
43 | "from IPython import display\n",
44 | "import torch\n",
45 | "from sklearn.manifold import TSNE\n",
46 | "from matplotlib import pyplot as plt"
47 | ]
48 | },
49 | {
50 | "cell_type": "markdown",
51 | "metadata": {},
52 | "source": [
53 | "Random data in $\\mathcal{R}^N$ in $C$ clusters/classes."
54 | ]
55 | },
56 | {
57 | "cell_type": "code",
58 | "execution_count": null,
59 | "metadata": {
60 | "scrolled": false
61 | },
62 | "outputs": [],
63 | "source": [
64 | "#Generate data - euclidean\n",
65 | "train_ds, test_ds, dloader = utils.euclideanDataset(n_samples=10000,n_features=20,n_classes=10,batch_size=32)"
66 | ]
67 | },
68 | {
69 | "cell_type": "markdown",
70 | "metadata": {},
71 | "source": [
72 | "Project data down to a 2-D space for visualiation."
73 | ]
74 | },
75 | {
76 | "cell_type": "code",
77 | "execution_count": null,
78 | "metadata": {},
79 | "outputs": [],
80 | "source": [
81 | "tsne = TSNE()\n",
82 | "X_2d = tsne.fit_transform(train_ds.samples[0:100])\n",
83 | "plt.scatter(X_2d[:,0],X_2d[:,1])\n",
84 | "plt.show()"
85 | ]
86 | },
87 | {
88 | "cell_type": "code",
89 | "execution_count": null,
90 | "metadata": {
91 | "scrolled": false
92 | },
93 | "outputs": [],
94 | "source": [
95 | "# See some batches of data.\n",
96 | "for b in dloader:\n",
97 | " print(b)\n",
98 | " samp = b\n",
99 | " break"
100 | ]
101 | },
102 | {
103 | "cell_type": "code",
104 | "execution_count": null,
105 | "metadata": {
106 | "scrolled": false
107 | },
108 | "outputs": [],
109 | "source": [
110 | "# Define an MLP network. Note that input dimension has to be data dimension. For classification\n",
111 | "# final dimension has to be number of classes; for regression one.\n",
112 | "#torch.manual_seed(10)\n",
113 | "net = models.MLP(dims=[20,32,10])"
114 | ]
115 | },
116 | {
117 | "cell_type": "markdown",
118 | "metadata": {},
119 | "source": [
120 | "Train the network"
121 | ]
122 | },
123 | {
124 | "cell_type": "code",
125 | "execution_count": null,
126 | "metadata": {},
127 | "outputs": [],
128 | "source": [
129 | "# Train the network; note that network is trained in place so repeated calls further train it.\n",
130 | "net,losses,accs=models.Train(net,dloader,lr=1e-4,epochs=100,verbose=True)"
131 | ]
132 | },
133 | {
134 | "cell_type": "markdown",
135 | "metadata": {},
136 | "source": [
137 | "Accuracy on training set."
138 | ]
139 | },
140 | {
141 | "cell_type": "code",
142 | "execution_count": null,
143 | "metadata": {
144 | "scrolled": false
145 | },
146 | "outputs": [],
147 | "source": [
148 | "#Training accuracy.\n",
149 | "models.accuracy(net,train_ds.samples,train_ds.labels,verbose=True)"
150 | ]
151 | },
152 | {
153 | "cell_type": "markdown",
154 | "metadata": {},
155 | "source": [
156 | "Accuracy on test set."
157 | ]
158 | },
159 | {
160 | "cell_type": "code",
161 | "execution_count": null,
162 | "metadata": {
163 | "scrolled": false
164 | },
165 | "outputs": [],
166 | "source": [
167 | "# Test accuracy.\n",
168 | "models.accuracy(net,test_ds.samples,test_ds.labels)"
169 | ]
170 | }
171 | ],
172 | "metadata": {
173 | "kernelspec": {
174 | "display_name": "Python 3",
175 | "language": "python",
176 | "name": "python3"
177 | },
178 | "language_info": {
179 | "codemirror_mode": {
180 | "name": "ipython",
181 | "version": 3
182 | },
183 | "file_extension": ".py",
184 | "mimetype": "text/x-python",
185 | "name": "python",
186 | "nbconvert_exporter": "python",
187 | "pygments_lexer": "ipython3",
188 | "version": "3.7.9"
189 | }
190 | },
191 | "nbformat": 4,
192 | "nbformat_minor": 4
193 | }
194 |
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/1_MAML/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tirtharajdash/BITS-G513_MetaLearning/81d62d08a420fb94059e04c2f2e9c7d0af10e151/Meta_Learning_Notebooks/1_MAML/.DS_Store
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/1_MAML/.ipynb_checkpoints/l2lutils-checkpoint.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 19,
6 | "metadata": {},
7 | "outputs": [],
8 | "source": [
9 | "import torch\n",
10 | "import numpy as np\n",
11 | "import learn2learn as l2l\n",
12 | "from learn2learn.data import *\n",
13 | "import import_ipynb\n",
14 | "import utils"
15 | ]
16 | },
17 | {
18 | "cell_type": "code",
19 | "execution_count": 49,
20 | "metadata": {},
21 | "outputs": [],
22 | "source": [
23 | "class KShotLoader():\n",
24 | " def __init__(self,myds,num_tasks=1000,shots=2,ways=2,classes=None):\n",
25 | " self.shots = shots\n",
26 | " self.ways = ways\n",
27 | " self.myMds = l2l.data.MetaDataset(myds)\n",
28 | " if classes == None:\n",
29 | " n_classes = len(set(myds.labels))\n",
30 | " classes = [i for i in range(n_classes)]\n",
31 | " self.my_tasks = l2l.data.TaskDataset(self.myMds, task_transforms=[\n",
32 | " l2l.data.transforms.FilterLabels(self.myMds,classes),\n",
33 | " l2l.data.transforms.NWays(self.myMds,ways),\n",
34 | " l2l.data.transforms.KShots(self.myMds,2*shots),\n",
35 | " l2l.data.transforms.LoadData(self.myMds),\n",
36 | " l2l.data.transforms.RemapLabels(self.myMds),\n",
37 | " l2l.data.transforms.ConsecutiveLabels(self.myMds)\n",
38 | " ],num_tasks=num_tasks)\n",
39 | " def get_task(self):\n",
40 | " data,labels = self.my_tasks.sample()\n",
41 | " adaptation_indices = np.zeros(data.size(0), dtype=bool)\n",
42 | " adaptation_indices[np.arange(self.shots*self.ways) * 2] = True\n",
43 | " evaluation_indices = torch.from_numpy(~adaptation_indices)\n",
44 | " adaptation_indices = torch.from_numpy(adaptation_indices)\n",
45 | " adaptation_data, adaptation_labels = data[adaptation_indices], labels[adaptation_indices]\n",
46 | " evaluation_data, evaluation_labels = data[evaluation_indices], labels[evaluation_indices]\n",
47 | " d_train = (adaptation_data,adaptation_labels)\n",
48 | " d_test = (evaluation_data,evaluation_labels)\n",
49 | " return d_train, d_test"
50 | ]
51 | },
52 | {
53 | "cell_type": "code",
54 | "execution_count": 50,
55 | "metadata": {},
56 | "outputs": [],
57 | "source": [
58 | "meta_train_ds, meta_test_ds, _ = utils.euclideanDataset(n_samples=100,n_features=20,n_classes=10,batch_size=32)"
59 | ]
60 | },
61 | {
62 | "cell_type": "code",
63 | "execution_count": 51,
64 | "metadata": {},
65 | "outputs": [
66 | {
67 | "data": {
68 | "text/plain": [
69 | "([0, 1, 2, 3, 4], [5, 6, 7, 8, 9])"
70 | ]
71 | },
72 | "execution_count": 51,
73 | "metadata": {},
74 | "output_type": "execute_result"
75 | }
76 | ],
77 | "source": [
78 | "classes_train = [i for i in range(5)]\n",
79 | "classes_test = [i+5 for i in range(5)]\n",
80 | "classes_train, classes_test"
81 | ]
82 | },
83 | {
84 | "cell_type": "code",
85 | "execution_count": 56,
86 | "metadata": {},
87 | "outputs": [],
88 | "source": [
89 | "kloader=KShotLoader(meta_train_ds,ways=10)"
90 | ]
91 | },
92 | {
93 | "cell_type": "code",
94 | "execution_count": 57,
95 | "metadata": {},
96 | "outputs": [],
97 | "source": [
98 | "d_train,d_test=kloader.get_task()"
99 | ]
100 | },
101 | {
102 | "cell_type": "code",
103 | "execution_count": 58,
104 | "metadata": {},
105 | "outputs": [
106 | {
107 | "data": {
108 | "text/plain": [
109 | "(tensor([[-2.2269e+00, -3.2695e+00, 5.4442e+00, -4.8867e+00, 4.2291e+00,\n",
110 | " 3.5576e+00, 2.5956e+00, -2.0264e+00, 4.8915e-01, 1.2715e+00,\n",
111 | " 5.1616e+00, 3.5798e+00, -9.4333e-01, 1.2588e+00, 1.4621e+00,\n",
112 | " -1.2572e+00, -4.5453e+00, -3.3953e+00, 8.0548e+00, 3.9462e+00],\n",
113 | " [-5.9133e+00, -4.5788e+00, 3.7464e+00, 8.7112e+00, 1.8168e+00,\n",
114 | " 8.5292e-02, 3.3876e+00, 1.7877e+00, 2.8035e+00, 5.6987e+00,\n",
115 | " -4.9392e+00, -3.6449e-01, -1.0126e+00, -3.3236e+00, -1.1476e+00,\n",
116 | " 1.9044e+00, -2.6974e+00, 1.4532e+00, -1.0257e+00, 5.9243e+00],\n",
117 | " [ 7.3079e-01, 3.6682e+00, -4.1600e+00, 1.5629e+00, -4.9982e+00,\n",
118 | " -4.8190e+00, 1.0289e+00, 2.0922e+00, 4.0064e+00, -5.1631e+00,\n",
119 | " -6.5307e+00, 2.1510e+00, 4.1438e-01, -4.7272e+00, 1.4923e-01,\n",
120 | " -4.8651e+00, 1.4452e-01, -6.5634e-01, 8.1033e-01, -1.9831e+00],\n",
121 | " [ 4.3463e+00, 4.3864e+00, -5.6252e+00, 4.3318e+00, -7.6635e-01,\n",
122 | " -2.6735e+00, 3.2048e+00, -9.0111e-01, 2.8629e-02, -4.6157e+00,\n",
123 | " 3.6303e+00, 2.7464e+00, 5.4187e-01, -2.6940e+00, 4.3800e+00,\n",
124 | " -1.8433e+00, -3.6847e-01, -3.5298e+00, 4.9547e+00, 2.8569e-01],\n",
125 | " [ 4.1147e+00, -5.8418e-01, 1.3630e+00, 3.6684e+00, 2.5691e+00,\n",
126 | " -3.3291e+00, 4.3714e+00, -1.9379e+00, -4.2100e+00, -2.3724e+00,\n",
127 | " -5.1832e-01, -2.4486e+00, -5.9383e-01, 5.4475e-01, 4.6592e-01,\n",
128 | " 2.1946e+00, -5.5729e+00, -2.0402e+00, -1.4138e+00, -2.1541e+00],\n",
129 | " [ 1.9774e+00, 7.3130e-01, -2.2713e+00, 1.1763e+00, 1.6943e+00,\n",
130 | " -9.5949e-01, 4.2298e+00, -2.3525e+00, -1.8844e+00, -6.4841e+00,\n",
131 | " -1.1723e+00, -1.8794e+00, 5.6158e-01, -4.0907e-01, 7.4737e-01,\n",
132 | " -2.3348e+00, -2.5160e+00, -4.7975e+00, 6.2609e-01, -3.8379e-01],\n",
133 | " [ 3.8392e+00, -1.0061e+00, -2.0550e+00, -6.7015e+00, 1.9526e+00,\n",
134 | " -3.1422e+00, -1.7629e+00, -3.1425e+00, 1.9887e+00, -3.6550e-01,\n",
135 | " -5.5158e-01, -1.1851e+00, -9.0496e-01, 8.0973e-01, -3.3213e+00,\n",
136 | " -2.4684e+00, -1.3632e+00, -4.7255e+00, -2.2272e+00, 3.3308e+00],\n",
137 | " [ 2.6690e+00, -1.0850e-01, -5.1167e-01, -9.6850e+00, 4.3982e+00,\n",
138 | " -7.5746e-01, -9.8493e+00, -1.1709e+00, -4.3790e+00, -4.1250e+00,\n",
139 | " -2.2037e+00, -3.3930e+00, 5.8988e-01, 5.8929e-01, -1.1153e+00,\n",
140 | " -3.3788e+00, 4.2896e+00, -7.7364e-01, 4.4568e-01, 2.0532e+00],\n",
141 | " [ 2.6593e+00, 9.1491e-01, -9.2550e-01, -2.8969e+00, -7.3885e+00,\n",
142 | " 1.9502e+00, -1.5401e+00, -2.8838e+00, -1.5919e+00, 4.3639e+00,\n",
143 | " -7.1466e+00, -3.1020e-01, 9.8707e-01, -1.6789e+00, -2.0021e+00,\n",
144 | " -2.6057e+00, -4.0620e+00, -2.8061e+00, -9.1016e-01, -3.2192e-01],\n",
145 | " [ 5.5933e+00, 2.0517e+00, -4.9328e-01, 1.3719e+00, -2.1863e+00,\n",
146 | " 1.7520e+00, -7.6038e+00, -5.3395e+00, -2.0731e+00, 3.2440e+00,\n",
147 | " -5.0263e+00, 3.3928e+00, -3.0744e+00, 1.6162e+00, -3.4616e+00,\n",
148 | " -2.1301e+00, -2.5426e+00, -3.4552e+00, -3.0237e+00, 4.4189e-01],\n",
149 | " [ 3.4417e+00, -1.5765e+00, 6.0367e+00, -6.7478e+00, 3.4999e+00,\n",
150 | " -2.0446e-01, -1.3513e+00, 1.2951e+00, 3.1270e+00, -1.0417e+00,\n",
151 | " 5.3056e+00, -2.6117e-01, -4.5455e+00, -5.1930e+00, -5.5577e-01,\n",
152 | " 9.1826e-01, -5.4130e-01, 2.9301e+00, -2.7295e+00, 1.8777e+00],\n",
153 | " [ 7.1241e+00, -3.2710e+00, -2.1455e+00, 2.1997e-01, -5.0980e+00,\n",
154 | " 4.9687e-01, 7.7729e-01, -6.0353e+00, -3.0846e+00, -1.5226e+00,\n",
155 | " 3.5975e+00, 4.0680e+00, -5.0229e+00, 1.3456e+00, 1.7484e+00,\n",
156 | " -3.8724e+00, 3.9670e+00, 2.0390e+00, -1.6984e-01, 3.6139e+00],\n",
157 | " [-1.0306e+00, -3.9669e+00, 8.0757e-03, -7.2059e-01, 1.3549e-01,\n",
158 | " 7.2127e+00, 5.8698e-02, -1.7749e+00, -6.3006e+00, -3.2256e+00,\n",
159 | " -3.8428e-01, 3.5050e+00, -7.9910e-01, -1.5871e+00, -4.3679e-01,\n",
160 | " 5.9838e-02, 2.3698e+00, 4.2053e+00, -2.4062e+00, -2.4723e+00],\n",
161 | " [ 1.4872e+00, -3.0025e+00, -3.2500e+00, 1.3665e+00, 1.7077e+00,\n",
162 | " 5.8097e+00, 4.0806e+00, -4.3767e+00, -4.0485e+00, -5.2227e+00,\n",
163 | " 9.3946e-01, 5.1380e+00, 2.0545e+00, 1.6730e+00, -4.1845e+00,\n",
164 | " -6.3160e-01, 4.2295e+00, -3.8230e-01, -1.8269e+00, 2.3999e+00],\n",
165 | " [ 4.5167e-01, -8.5301e-01, 1.1365e+00, 1.6636e+00, 2.6024e+00,\n",
166 | " -1.1376e+00, 8.0975e+00, -1.6008e+00, 9.7496e-01, -3.0984e+00,\n",
167 | " -3.5695e+00, 4.4206e+00, -3.7914e+00, 7.7940e-01, -1.1682e+00,\n",
168 | " -1.4943e+00, -1.2542e-01, 5.1024e+00, 3.0937e+00, 3.2379e+00],\n",
169 | " [-1.7432e-01, -3.4995e+00, 2.2380e-01, 3.5483e+00, 2.2081e+00,\n",
170 | " -1.5572e+00, 3.5698e+00, 6.8495e-01, 1.8234e+00, -1.9483e+00,\n",
171 | " -5.4730e-01, 4.0651e+00, -2.8460e+00, -3.5513e+00, 1.8409e+00,\n",
172 | " -8.9406e-01, -9.4173e-02, 5.2429e+00, -1.4051e+00, 8.8920e-01],\n",
173 | " [-1.8081e+00, 2.3670e+00, 2.9845e+00, -7.2137e-01, -1.7561e+00,\n",
174 | " -2.3222e-01, -4.4968e+00, -3.0355e+00, -1.4890e+00, 2.4993e+00,\n",
175 | " -4.9809e+00, 1.9229e+00, 6.6299e-01, -1.0311e+00, 3.4498e+00,\n",
176 | " 3.7998e-01, 2.2223e+00, -4.0118e+00, 1.4638e+00, 1.0754e-01],\n",
177 | " [ 1.5058e+00, -6.9874e-01, 1.7713e+00, -4.9047e+00, 1.9866e-02,\n",
178 | " -7.9816e-01, 3.0304e+00, -1.9952e+00, 2.1795e-01, -1.1960e+00,\n",
179 | " -2.0037e+00, 5.9589e+00, -3.2473e+00, -2.3790e+00, 1.3595e+00,\n",
180 | " -2.3937e+00, 1.5056e+00, -5.6902e+00, 3.9320e+00, -3.1806e+00],\n",
181 | " [ 7.9813e-03, 3.2129e+00, 2.0145e+00, 4.7004e+00, 2.2127e+00,\n",
182 | " 1.9116e-01, 4.1624e+00, -4.4571e+00, -3.9139e+00, -2.3842e+00,\n",
183 | " 2.5942e+00, -1.7124e+00, 1.0031e+00, -2.7667e+00, -6.8536e+00,\n",
184 | " 3.1029e+00, -2.4199e+00, -5.7588e-01, 1.0431e+00, 3.0947e+00],\n",
185 | " [ 4.1552e+00, 4.4767e+00, 1.9866e+00, -3.2598e+00, 4.3165e+00,\n",
186 | " -3.5739e+00, 2.9771e+00, -2.3801e+00, -5.3078e+00, 1.4114e+00,\n",
187 | " 6.0424e+00, -1.1509e+00, -2.8970e-01, -8.2261e-01, -3.2405e+00,\n",
188 | " -2.6111e+00, 5.6930e+00, -3.1017e+00, 2.4199e+00, -4.5887e+00]]),\n",
189 | " tensor([3, 3, 6, 6, 0, 0, 9, 9, 2, 2, 8, 8, 7, 7, 1, 1, 4, 4, 5, 5]))"
190 | ]
191 | },
192 | "execution_count": 58,
193 | "metadata": {},
194 | "output_type": "execute_result"
195 | }
196 | ],
197 | "source": [
198 | "d_train"
199 | ]
200 | },
201 | {
202 | "cell_type": "code",
203 | "execution_count": 59,
204 | "metadata": {},
205 | "outputs": [],
206 | "source": [
207 | "d_train,d_test=kloader.get_task()"
208 | ]
209 | },
210 | {
211 | "cell_type": "code",
212 | "execution_count": 17,
213 | "metadata": {},
214 | "outputs": [
215 | {
216 | "data": {
217 | "text/plain": [
218 | "(tensor([[ 3.6960, 3.6126, 1.0981, -0.9689, 0.7627, 6.3903, 0.9497, -5.2207,\n",
219 | " -1.1006, -1.5891, 2.8220, -0.1910, -1.8938, -1.2033, -4.1035, 3.5602,\n",
220 | " 2.1527, -0.2454, -3.7900, 2.2128],\n",
221 | " [ 2.4989, 1.0827, 3.5661, 1.1853, 1.2567, 1.1357, 7.4111, -0.7032,\n",
222 | " 0.9706, -3.9028, -1.4625, 4.8641, 7.0013, -2.6186, -0.7804, -0.9068,\n",
223 | " -0.7404, -6.4392, -2.4066, 1.6961],\n",
224 | " [-2.5684, 0.5440, -2.7641, 1.0810, -1.3895, 2.2740, -2.4515, 1.1156,\n",
225 | " 1.1550, -0.2270, -1.5329, 7.2667, -1.6682, -0.8797, -4.6239, -1.6831,\n",
226 | " -1.4690, 1.2760, -1.6313, 0.0796],\n",
227 | " [-3.3656, 2.2880, -2.2261, 3.9417, 2.2327, 5.2604, -1.2766, 1.7004,\n",
228 | " 4.9334, 4.1778, 3.3986, 2.3361, 1.3132, -0.3988, -3.8092, 3.8010,\n",
229 | " -5.0442, 0.8580, -3.2479, 3.5913]]),\n",
230 | " tensor([1, 1, 0, 0]))"
231 | ]
232 | },
233 | "execution_count": 17,
234 | "metadata": {},
235 | "output_type": "execute_result"
236 | }
237 | ],
238 | "source": [
239 | "d_train"
240 | ]
241 | },
242 | {
243 | "cell_type": "code",
244 | "execution_count": null,
245 | "metadata": {},
246 | "outputs": [],
247 | "source": []
248 | }
249 | ],
250 | "metadata": {
251 | "kernelspec": {
252 | "display_name": "Python 3",
253 | "language": "python",
254 | "name": "python3"
255 | },
256 | "language_info": {
257 | "codemirror_mode": {
258 | "name": "ipython",
259 | "version": 3
260 | },
261 | "file_extension": ".py",
262 | "mimetype": "text/x-python",
263 | "name": "python",
264 | "nbconvert_exporter": "python",
265 | "pygments_lexer": "ipython3",
266 | "version": "3.7.9"
267 | }
268 | },
269 | "nbformat": 4,
270 | "nbformat_minor": 4
271 | }
272 |
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/1_MAML/.ipynb_checkpoints/models-checkpoint.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Deep Learning Models etc"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": 1,
13 | "metadata": {},
14 | "outputs": [],
15 | "source": [
16 | "import numpy as np\n",
17 | "import torch\n",
18 | "from torch import nn\n",
19 | "from torch import optim\n",
20 | "from IPython import display"
21 | ]
22 | },
23 | {
24 | "cell_type": "markdown",
25 | "metadata": {},
26 | "source": [
27 | "Generic training loop"
28 | ]
29 | },
30 | {
31 | "cell_type": "code",
32 | "execution_count": 2,
33 | "metadata": {},
34 | "outputs": [],
35 | "source": [
36 | "def accuracy(Net,X_test,y_test,verbose=True):\n",
37 | " Net.eval()\n",
38 | " m = X_test.shape[0]\n",
39 | " y_pred = Net(X_test)\n",
40 | " _, predicted = torch.max(y_pred, 1)\n",
41 | " correct = (predicted == y_test).float().sum().item()\n",
42 | " if verbose: print(correct,m)\n",
43 | " accuracy = correct/m\n",
44 | " Net.train()\n",
45 | " return accuracy"
46 | ]
47 | },
48 | {
49 | "cell_type": "code",
50 | "execution_count": 3,
51 | "metadata": {},
52 | "outputs": [],
53 | "source": [
54 | "def Train(Net,data,epochs=20,lr=5e-2,Loss=nn.NLLLoss(),verbose=False):\n",
55 | " #optimizer = optim.Adam(Net.parameters(),lr=lr)\n",
56 | " for e in range(epochs):\n",
57 | " step=0\n",
58 | " tot_loss=0.0\n",
59 | " acc=0.0\n",
60 | " for (X,y) in data:\n",
61 | " y_pred = Net(X)\n",
62 | " loss = Loss(y_pred,y)\n",
63 | " if verbose: print('Step loss: ',loss)\n",
64 | " Net.optimizer.zero_grad()\n",
65 | " loss.backward()\n",
66 | " Net.optimizer.step()\n",
67 | " step+=1\n",
68 | " tot_loss+=loss\n",
69 | " acc+=accuracy(Net,X,y,verbose=False) \n",
70 | " print('Epoch % 2d Loss: %2.5e Accuracy: %2.5f'%(e,tot_loss.item()/step,acc/step))\n",
71 | " display.clear_output(wait=True)\n",
72 | " return Net,loss"
73 | ]
74 | },
75 | {
76 | "cell_type": "markdown",
77 | "metadata": {},
78 | "source": [
79 | "Multi-layer perceptron with ReLU non-lineartities; for classification or regression."
80 | ]
81 | },
82 | {
83 | "cell_type": "code",
84 | "execution_count": 4,
85 | "metadata": {},
86 | "outputs": [],
87 | "source": [
88 | "class MLP(nn.Module):\n",
89 | " def __init__(self,dims=[5,3,2],task='classification',lr=1e-3):\n",
90 | " super(MLP,self).__init__()\n",
91 | " self.dims=dims\n",
92 | " self.n = len(self.dims)-1\n",
93 | " self.task=task\n",
94 | " self.layers=nn.ModuleList()\n",
95 | " for i in range(self.n-1):\n",
96 | " self.layers.append(nn.Linear(dims[i],dims[i+1]))\n",
97 | " self.layers.append(nn.ReLU())\n",
98 | " if task=='classification': \n",
99 | " self.layers.append(nn.Linear(dims[i+1],dims[i+2]))\n",
100 | " self.layers.append(nn.LogSoftmax(dim=1))\n",
101 | " elif task=='regression': self.layers.append(nn.Linear(dims[i+1],1))\n",
102 | " self.optimizer = optim.Adam(self.parameters(),lr=lr)\n",
103 | " def forward(self,x):\n",
104 | " for l in self.layers:\n",
105 | " x = l(x)\n",
106 | " return(x)"
107 | ]
108 | },
109 | {
110 | "cell_type": "code",
111 | "execution_count": 9,
112 | "metadata": {},
113 | "outputs": [],
114 | "source": [
115 | "class RNN(nn.Module):\n",
116 | " def __init__(self,n_features=1,dim=5,n_layers=3,n_classes=2,lr=1e-3):\n",
117 | " super(RNN,self).__init__()\n",
118 | " self.n_layers = n_layers\n",
119 | " self.dim = dim\n",
120 | " self.n_features = n_features\n",
121 | " self.rnn = nn.GRU(n_features, dim, n_layers,batch_first=True)\n",
122 | " self.linear = nn.Linear(dim,n_classes)\n",
123 | " self.logsoft = nn.LogSoftmax(dim=-1)\n",
124 | " self.optimizer = optim.Adam(self.parameters(),lr=lr)\n",
125 | " def forward(self,X):\n",
126 | " if self.n_features==1: X=X.unsqueeze(-1)\n",
127 | " h = torch.randn(self.n_layers,X.shape[0],self.dim)\n",
128 | " H = self.rnn(X,h)[1]\n",
129 | " G = self.logsoft(self.linear(H[-1,:,:]))\n",
130 | " return G"
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.7.9"
158 | }
159 | },
160 | "nbformat": 4,
161 | "nbformat_minor": 4
162 | }
163 |
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/1_MAML/data:
--------------------------------------------------------------------------------
1 | /Users/gautamshroff2019/Code/meta-learning-course-notebooks/0_Basic_NNs/data
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/1_MAML/l2lutils.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 19,
6 | "metadata": {},
7 | "outputs": [],
8 | "source": [
9 | "import torch\n",
10 | "import numpy as np\n",
11 | "import learn2learn as l2l\n",
12 | "from learn2learn.data import *\n",
13 | "import import_ipynb\n",
14 | "import utils"
15 | ]
16 | },
17 | {
18 | "cell_type": "code",
19 | "execution_count": 49,
20 | "metadata": {},
21 | "outputs": [],
22 | "source": [
23 | "class KShotLoader():\n",
24 | " def __init__(self,myds,num_tasks=1000,shots=2,ways=2,classes=None):\n",
25 | " self.shots = shots\n",
26 | " self.ways = ways\n",
27 | " self.myMds = l2l.data.MetaDataset(myds)\n",
28 | " if classes == None:\n",
29 | " n_classes = len(set(myds.labels))\n",
30 | " classes = [i for i in range(n_classes)]\n",
31 | " self.my_tasks = l2l.data.TaskDataset(self.myMds, task_transforms=[\n",
32 | " l2l.data.transforms.FilterLabels(self.myMds,classes),\n",
33 | " l2l.data.transforms.NWays(self.myMds,ways),\n",
34 | " l2l.data.transforms.KShots(self.myMds,2*shots),\n",
35 | " l2l.data.transforms.LoadData(self.myMds),\n",
36 | " l2l.data.transforms.RemapLabels(self.myMds),\n",
37 | " l2l.data.transforms.ConsecutiveLabels(self.myMds)\n",
38 | " ],num_tasks=num_tasks)\n",
39 | " def get_task(self):\n",
40 | " data,labels = self.my_tasks.sample()\n",
41 | " adaptation_indices = np.zeros(data.size(0), dtype=bool)\n",
42 | " adaptation_indices[np.arange(self.shots*self.ways) * 2] = True\n",
43 | " evaluation_indices = torch.from_numpy(~adaptation_indices)\n",
44 | " adaptation_indices = torch.from_numpy(adaptation_indices)\n",
45 | " adaptation_data, adaptation_labels = data[adaptation_indices], labels[adaptation_indices]\n",
46 | " evaluation_data, evaluation_labels = data[evaluation_indices], labels[evaluation_indices]\n",
47 | " d_train = (adaptation_data,adaptation_labels)\n",
48 | " d_test = (evaluation_data,evaluation_labels)\n",
49 | " return d_train, d_test"
50 | ]
51 | },
52 | {
53 | "cell_type": "code",
54 | "execution_count": 50,
55 | "metadata": {},
56 | "outputs": [],
57 | "source": [
58 | "meta_train_ds, meta_test_ds, _ = utils.euclideanDataset(n_samples=100,n_features=20,n_classes=10,batch_size=32)"
59 | ]
60 | },
61 | {
62 | "cell_type": "code",
63 | "execution_count": 51,
64 | "metadata": {},
65 | "outputs": [
66 | {
67 | "data": {
68 | "text/plain": [
69 | "([0, 1, 2, 3, 4], [5, 6, 7, 8, 9])"
70 | ]
71 | },
72 | "execution_count": 51,
73 | "metadata": {},
74 | "output_type": "execute_result"
75 | }
76 | ],
77 | "source": [
78 | "classes_train = [i for i in range(5)]\n",
79 | "classes_test = [i+5 for i in range(5)]\n",
80 | "classes_train, classes_test"
81 | ]
82 | },
83 | {
84 | "cell_type": "code",
85 | "execution_count": 56,
86 | "metadata": {},
87 | "outputs": [],
88 | "source": [
89 | "kloader=KShotLoader(meta_train_ds,ways=10)"
90 | ]
91 | },
92 | {
93 | "cell_type": "code",
94 | "execution_count": 57,
95 | "metadata": {},
96 | "outputs": [],
97 | "source": [
98 | "d_train,d_test=kloader.get_task()"
99 | ]
100 | },
101 | {
102 | "cell_type": "code",
103 | "execution_count": 58,
104 | "metadata": {},
105 | "outputs": [
106 | {
107 | "data": {
108 | "text/plain": [
109 | "(tensor([[-2.2269e+00, -3.2695e+00, 5.4442e+00, -4.8867e+00, 4.2291e+00,\n",
110 | " 3.5576e+00, 2.5956e+00, -2.0264e+00, 4.8915e-01, 1.2715e+00,\n",
111 | " 5.1616e+00, 3.5798e+00, -9.4333e-01, 1.2588e+00, 1.4621e+00,\n",
112 | " -1.2572e+00, -4.5453e+00, -3.3953e+00, 8.0548e+00, 3.9462e+00],\n",
113 | " [-5.9133e+00, -4.5788e+00, 3.7464e+00, 8.7112e+00, 1.8168e+00,\n",
114 | " 8.5292e-02, 3.3876e+00, 1.7877e+00, 2.8035e+00, 5.6987e+00,\n",
115 | " -4.9392e+00, -3.6449e-01, -1.0126e+00, -3.3236e+00, -1.1476e+00,\n",
116 | " 1.9044e+00, -2.6974e+00, 1.4532e+00, -1.0257e+00, 5.9243e+00],\n",
117 | " [ 7.3079e-01, 3.6682e+00, -4.1600e+00, 1.5629e+00, -4.9982e+00,\n",
118 | " -4.8190e+00, 1.0289e+00, 2.0922e+00, 4.0064e+00, -5.1631e+00,\n",
119 | " -6.5307e+00, 2.1510e+00, 4.1438e-01, -4.7272e+00, 1.4923e-01,\n",
120 | " -4.8651e+00, 1.4452e-01, -6.5634e-01, 8.1033e-01, -1.9831e+00],\n",
121 | " [ 4.3463e+00, 4.3864e+00, -5.6252e+00, 4.3318e+00, -7.6635e-01,\n",
122 | " -2.6735e+00, 3.2048e+00, -9.0111e-01, 2.8629e-02, -4.6157e+00,\n",
123 | " 3.6303e+00, 2.7464e+00, 5.4187e-01, -2.6940e+00, 4.3800e+00,\n",
124 | " -1.8433e+00, -3.6847e-01, -3.5298e+00, 4.9547e+00, 2.8569e-01],\n",
125 | " [ 4.1147e+00, -5.8418e-01, 1.3630e+00, 3.6684e+00, 2.5691e+00,\n",
126 | " -3.3291e+00, 4.3714e+00, -1.9379e+00, -4.2100e+00, -2.3724e+00,\n",
127 | " -5.1832e-01, -2.4486e+00, -5.9383e-01, 5.4475e-01, 4.6592e-01,\n",
128 | " 2.1946e+00, -5.5729e+00, -2.0402e+00, -1.4138e+00, -2.1541e+00],\n",
129 | " [ 1.9774e+00, 7.3130e-01, -2.2713e+00, 1.1763e+00, 1.6943e+00,\n",
130 | " -9.5949e-01, 4.2298e+00, -2.3525e+00, -1.8844e+00, -6.4841e+00,\n",
131 | " -1.1723e+00, -1.8794e+00, 5.6158e-01, -4.0907e-01, 7.4737e-01,\n",
132 | " -2.3348e+00, -2.5160e+00, -4.7975e+00, 6.2609e-01, -3.8379e-01],\n",
133 | " [ 3.8392e+00, -1.0061e+00, -2.0550e+00, -6.7015e+00, 1.9526e+00,\n",
134 | " -3.1422e+00, -1.7629e+00, -3.1425e+00, 1.9887e+00, -3.6550e-01,\n",
135 | " -5.5158e-01, -1.1851e+00, -9.0496e-01, 8.0973e-01, -3.3213e+00,\n",
136 | " -2.4684e+00, -1.3632e+00, -4.7255e+00, -2.2272e+00, 3.3308e+00],\n",
137 | " [ 2.6690e+00, -1.0850e-01, -5.1167e-01, -9.6850e+00, 4.3982e+00,\n",
138 | " -7.5746e-01, -9.8493e+00, -1.1709e+00, -4.3790e+00, -4.1250e+00,\n",
139 | " -2.2037e+00, -3.3930e+00, 5.8988e-01, 5.8929e-01, -1.1153e+00,\n",
140 | " -3.3788e+00, 4.2896e+00, -7.7364e-01, 4.4568e-01, 2.0532e+00],\n",
141 | " [ 2.6593e+00, 9.1491e-01, -9.2550e-01, -2.8969e+00, -7.3885e+00,\n",
142 | " 1.9502e+00, -1.5401e+00, -2.8838e+00, -1.5919e+00, 4.3639e+00,\n",
143 | " -7.1466e+00, -3.1020e-01, 9.8707e-01, -1.6789e+00, -2.0021e+00,\n",
144 | " -2.6057e+00, -4.0620e+00, -2.8061e+00, -9.1016e-01, -3.2192e-01],\n",
145 | " [ 5.5933e+00, 2.0517e+00, -4.9328e-01, 1.3719e+00, -2.1863e+00,\n",
146 | " 1.7520e+00, -7.6038e+00, -5.3395e+00, -2.0731e+00, 3.2440e+00,\n",
147 | " -5.0263e+00, 3.3928e+00, -3.0744e+00, 1.6162e+00, -3.4616e+00,\n",
148 | " -2.1301e+00, -2.5426e+00, -3.4552e+00, -3.0237e+00, 4.4189e-01],\n",
149 | " [ 3.4417e+00, -1.5765e+00, 6.0367e+00, -6.7478e+00, 3.4999e+00,\n",
150 | " -2.0446e-01, -1.3513e+00, 1.2951e+00, 3.1270e+00, -1.0417e+00,\n",
151 | " 5.3056e+00, -2.6117e-01, -4.5455e+00, -5.1930e+00, -5.5577e-01,\n",
152 | " 9.1826e-01, -5.4130e-01, 2.9301e+00, -2.7295e+00, 1.8777e+00],\n",
153 | " [ 7.1241e+00, -3.2710e+00, -2.1455e+00, 2.1997e-01, -5.0980e+00,\n",
154 | " 4.9687e-01, 7.7729e-01, -6.0353e+00, -3.0846e+00, -1.5226e+00,\n",
155 | " 3.5975e+00, 4.0680e+00, -5.0229e+00, 1.3456e+00, 1.7484e+00,\n",
156 | " -3.8724e+00, 3.9670e+00, 2.0390e+00, -1.6984e-01, 3.6139e+00],\n",
157 | " [-1.0306e+00, -3.9669e+00, 8.0757e-03, -7.2059e-01, 1.3549e-01,\n",
158 | " 7.2127e+00, 5.8698e-02, -1.7749e+00, -6.3006e+00, -3.2256e+00,\n",
159 | " -3.8428e-01, 3.5050e+00, -7.9910e-01, -1.5871e+00, -4.3679e-01,\n",
160 | " 5.9838e-02, 2.3698e+00, 4.2053e+00, -2.4062e+00, -2.4723e+00],\n",
161 | " [ 1.4872e+00, -3.0025e+00, -3.2500e+00, 1.3665e+00, 1.7077e+00,\n",
162 | " 5.8097e+00, 4.0806e+00, -4.3767e+00, -4.0485e+00, -5.2227e+00,\n",
163 | " 9.3946e-01, 5.1380e+00, 2.0545e+00, 1.6730e+00, -4.1845e+00,\n",
164 | " -6.3160e-01, 4.2295e+00, -3.8230e-01, -1.8269e+00, 2.3999e+00],\n",
165 | " [ 4.5167e-01, -8.5301e-01, 1.1365e+00, 1.6636e+00, 2.6024e+00,\n",
166 | " -1.1376e+00, 8.0975e+00, -1.6008e+00, 9.7496e-01, -3.0984e+00,\n",
167 | " -3.5695e+00, 4.4206e+00, -3.7914e+00, 7.7940e-01, -1.1682e+00,\n",
168 | " -1.4943e+00, -1.2542e-01, 5.1024e+00, 3.0937e+00, 3.2379e+00],\n",
169 | " [-1.7432e-01, -3.4995e+00, 2.2380e-01, 3.5483e+00, 2.2081e+00,\n",
170 | " -1.5572e+00, 3.5698e+00, 6.8495e-01, 1.8234e+00, -1.9483e+00,\n",
171 | " -5.4730e-01, 4.0651e+00, -2.8460e+00, -3.5513e+00, 1.8409e+00,\n",
172 | " -8.9406e-01, -9.4173e-02, 5.2429e+00, -1.4051e+00, 8.8920e-01],\n",
173 | " [-1.8081e+00, 2.3670e+00, 2.9845e+00, -7.2137e-01, -1.7561e+00,\n",
174 | " -2.3222e-01, -4.4968e+00, -3.0355e+00, -1.4890e+00, 2.4993e+00,\n",
175 | " -4.9809e+00, 1.9229e+00, 6.6299e-01, -1.0311e+00, 3.4498e+00,\n",
176 | " 3.7998e-01, 2.2223e+00, -4.0118e+00, 1.4638e+00, 1.0754e-01],\n",
177 | " [ 1.5058e+00, -6.9874e-01, 1.7713e+00, -4.9047e+00, 1.9866e-02,\n",
178 | " -7.9816e-01, 3.0304e+00, -1.9952e+00, 2.1795e-01, -1.1960e+00,\n",
179 | " -2.0037e+00, 5.9589e+00, -3.2473e+00, -2.3790e+00, 1.3595e+00,\n",
180 | " -2.3937e+00, 1.5056e+00, -5.6902e+00, 3.9320e+00, -3.1806e+00],\n",
181 | " [ 7.9813e-03, 3.2129e+00, 2.0145e+00, 4.7004e+00, 2.2127e+00,\n",
182 | " 1.9116e-01, 4.1624e+00, -4.4571e+00, -3.9139e+00, -2.3842e+00,\n",
183 | " 2.5942e+00, -1.7124e+00, 1.0031e+00, -2.7667e+00, -6.8536e+00,\n",
184 | " 3.1029e+00, -2.4199e+00, -5.7588e-01, 1.0431e+00, 3.0947e+00],\n",
185 | " [ 4.1552e+00, 4.4767e+00, 1.9866e+00, -3.2598e+00, 4.3165e+00,\n",
186 | " -3.5739e+00, 2.9771e+00, -2.3801e+00, -5.3078e+00, 1.4114e+00,\n",
187 | " 6.0424e+00, -1.1509e+00, -2.8970e-01, -8.2261e-01, -3.2405e+00,\n",
188 | " -2.6111e+00, 5.6930e+00, -3.1017e+00, 2.4199e+00, -4.5887e+00]]),\n",
189 | " tensor([3, 3, 6, 6, 0, 0, 9, 9, 2, 2, 8, 8, 7, 7, 1, 1, 4, 4, 5, 5]))"
190 | ]
191 | },
192 | "execution_count": 58,
193 | "metadata": {},
194 | "output_type": "execute_result"
195 | }
196 | ],
197 | "source": [
198 | "d_train"
199 | ]
200 | },
201 | {
202 | "cell_type": "code",
203 | "execution_count": 59,
204 | "metadata": {},
205 | "outputs": [],
206 | "source": [
207 | "d_train,d_test=kloader.get_task()"
208 | ]
209 | },
210 | {
211 | "cell_type": "code",
212 | "execution_count": 17,
213 | "metadata": {},
214 | "outputs": [
215 | {
216 | "data": {
217 | "text/plain": [
218 | "(tensor([[ 3.6960, 3.6126, 1.0981, -0.9689, 0.7627, 6.3903, 0.9497, -5.2207,\n",
219 | " -1.1006, -1.5891, 2.8220, -0.1910, -1.8938, -1.2033, -4.1035, 3.5602,\n",
220 | " 2.1527, -0.2454, -3.7900, 2.2128],\n",
221 | " [ 2.4989, 1.0827, 3.5661, 1.1853, 1.2567, 1.1357, 7.4111, -0.7032,\n",
222 | " 0.9706, -3.9028, -1.4625, 4.8641, 7.0013, -2.6186, -0.7804, -0.9068,\n",
223 | " -0.7404, -6.4392, -2.4066, 1.6961],\n",
224 | " [-2.5684, 0.5440, -2.7641, 1.0810, -1.3895, 2.2740, -2.4515, 1.1156,\n",
225 | " 1.1550, -0.2270, -1.5329, 7.2667, -1.6682, -0.8797, -4.6239, -1.6831,\n",
226 | " -1.4690, 1.2760, -1.6313, 0.0796],\n",
227 | " [-3.3656, 2.2880, -2.2261, 3.9417, 2.2327, 5.2604, -1.2766, 1.7004,\n",
228 | " 4.9334, 4.1778, 3.3986, 2.3361, 1.3132, -0.3988, -3.8092, 3.8010,\n",
229 | " -5.0442, 0.8580, -3.2479, 3.5913]]),\n",
230 | " tensor([1, 1, 0, 0]))"
231 | ]
232 | },
233 | "execution_count": 17,
234 | "metadata": {},
235 | "output_type": "execute_result"
236 | }
237 | ],
238 | "source": [
239 | "d_train"
240 | ]
241 | },
242 | {
243 | "cell_type": "code",
244 | "execution_count": null,
245 | "metadata": {},
246 | "outputs": [],
247 | "source": []
248 | }
249 | ],
250 | "metadata": {
251 | "kernelspec": {
252 | "display_name": "Python 3",
253 | "language": "python",
254 | "name": "python3"
255 | },
256 | "language_info": {
257 | "codemirror_mode": {
258 | "name": "ipython",
259 | "version": 3
260 | },
261 | "file_extension": ".py",
262 | "mimetype": "text/x-python",
263 | "name": "python",
264 | "nbconvert_exporter": "python",
265 | "pygments_lexer": "ipython3",
266 | "version": "3.7.9"
267 | }
268 | },
269 | "nbformat": 4,
270 | "nbformat_minor": 4
271 | }
272 |
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/1_MAML/models.ipynb:
--------------------------------------------------------------------------------
1 | /Users/gautamshroff2019/Code/meta-learning-course-notebooks/0_Basic_NNs/models.ipynb
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/1_MAML/utils.ipynb:
--------------------------------------------------------------------------------
1 | /Users/gautamshroff2019/Code/meta-learning-course-notebooks/0_Basic_NNs/utils.ipynb
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/2_Model_MetaL/.ipynb_checkpoints/models-checkpoint.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Deep Learning Models etc"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": 4,
13 | "metadata": {},
14 | "outputs": [
15 | {
16 | "name": "stdout",
17 | "output_type": "stream",
18 | "text": [
19 | "importing Jupyter notebook from utils.ipynb\n"
20 | ]
21 | },
22 | {
23 | "data": {
24 | "text/html": [
25 | "\n",
26 | " \n",
33 | "\n",
34 | " Imports 1 (show/hide)\n",
35 | " "
36 | ],
37 | "text/plain": [
38 | ""
39 | ]
40 | },
41 | "execution_count": 4,
42 | "metadata": {},
43 | "output_type": "execute_result"
44 | }
45 | ],
46 | "source": [
47 | "from IPython.display import HTML\n",
48 | "import import_ipynb\n",
49 | "import random\n",
50 | "import utils\n",
51 | "utils.hide_toggle('Imports 1')"
52 | ]
53 | },
54 | {
55 | "cell_type": "code",
56 | "execution_count": 5,
57 | "metadata": {},
58 | "outputs": [
59 | {
60 | "data": {
61 | "text/html": [
62 | "\n",
63 | " \n",
70 | "\n",
71 | " Imports 2 (show/hide)\n",
72 | " "
73 | ],
74 | "text/plain": [
75 | ""
76 | ]
77 | },
78 | "execution_count": 5,
79 | "metadata": {},
80 | "output_type": "execute_result"
81 | }
82 | ],
83 | "source": [
84 | "import numpy as np\n",
85 | "import torch\n",
86 | "from torch import nn\n",
87 | "from torch import optim\n",
88 | "from IPython import display\n",
89 | "utils.hide_toggle('Imports 2')"
90 | ]
91 | },
92 | {
93 | "cell_type": "markdown",
94 | "metadata": {},
95 | "source": [
96 | "Compute accuracy of predictions."
97 | ]
98 | },
99 | {
100 | "cell_type": "code",
101 | "execution_count": 6,
102 | "metadata": {},
103 | "outputs": [
104 | {
105 | "data": {
106 | "text/html": [
107 | "\n",
108 | " \n",
115 | "\n",
116 | " Function: accuracy (show/hide)\n",
117 | " "
118 | ],
119 | "text/plain": [
120 | ""
121 | ]
122 | },
123 | "execution_count": 6,
124 | "metadata": {},
125 | "output_type": "execute_result"
126 | }
127 | ],
128 | "source": [
129 | "def accuracy(Net,X_test,y_test,verbose=True):\n",
130 | " Net.eval()\n",
131 | " m = X_test.shape[0]\n",
132 | " y_pred = Net(X_test)\n",
133 | " _, predicted = torch.max(y_pred, 1)\n",
134 | " correct = (predicted == y_test).float().sum().item()\n",
135 | " if verbose: print(correct,m)\n",
136 | " accuracy = correct/m\n",
137 | " Net.train()\n",
138 | " return accuracy\n",
139 | "utils.hide_toggle('Function: accuracy')"
140 | ]
141 | },
142 | {
143 | "cell_type": "markdown",
144 | "metadata": {},
145 | "source": [
146 | "Generic training loop"
147 | ]
148 | },
149 | {
150 | "cell_type": "code",
151 | "execution_count": 7,
152 | "metadata": {},
153 | "outputs": [
154 | {
155 | "data": {
156 | "text/html": [
157 | "\n",
158 | " \n",
165 | "\n",
166 | " Function Train (show/hide)\n",
167 | " "
168 | ],
169 | "text/plain": [
170 | ""
171 | ]
172 | },
173 | "execution_count": 7,
174 | "metadata": {},
175 | "output_type": "execute_result"
176 | }
177 | ],
178 | "source": [
179 | "def Train(Net,data,epochs=20,lr=5e-2,Loss=nn.NLLLoss(),verbose=False):\n",
180 | " #optimizer = optim.Adam(Net.parameters(),lr=lr)\n",
181 | " losses = []\n",
182 | " accs = []\n",
183 | " for e in range(epochs):\n",
184 | " step=0\n",
185 | " tot_loss=0.0\n",
186 | " acc=0.0\n",
187 | " for (X,y) in data:\n",
188 | " y_pred = Net(X)\n",
189 | " loss = Loss(y_pred,y)\n",
190 | " Net.optimizer.zero_grad()\n",
191 | " loss.backward()\n",
192 | " Net.optimizer.step()\n",
193 | " step+=1\n",
194 | " tot_loss+=loss\n",
195 | " acc+=accuracy(Net,X,y,verbose=False)\n",
196 | " l = tot_loss.item()/step\n",
197 | " a = acc/step\n",
198 | " losses += [l]\n",
199 | " accs += [a]\n",
200 | " if verbose: \n",
201 | " print('Epoch % 2d Loss: %2.5e Accuracy: %2.5f'%(e,l,a))\n",
202 | " display.clear_output(wait=True)\n",
203 | " return Net,losses,accs\n",
204 | "utils.hide_toggle('Function Train')"
205 | ]
206 | },
207 | {
208 | "cell_type": "markdown",
209 | "metadata": {},
210 | "source": [
211 | "Multi-layer perceptron with ReLU non-lineartities; for classification or regression."
212 | ]
213 | },
214 | {
215 | "cell_type": "code",
216 | "execution_count": 8,
217 | "metadata": {},
218 | "outputs": [
219 | {
220 | "data": {
221 | "text/html": [
222 | "\n",
223 | " \n",
230 | "\n",
231 | " Class MLP (show/hide)\n",
232 | " "
233 | ],
234 | "text/plain": [
235 | ""
236 | ]
237 | },
238 | "execution_count": 8,
239 | "metadata": {},
240 | "output_type": "execute_result"
241 | }
242 | ],
243 | "source": [
244 | "class MLP(nn.Module):\n",
245 | " def __init__(self,dims=[5,3,2],task='classification',lr=1e-3):\n",
246 | " super(MLP,self).__init__()\n",
247 | " self.dims=dims\n",
248 | " self.n = len(self.dims)-1\n",
249 | " self.task=task\n",
250 | " self.layers=nn.ModuleList()\n",
251 | " for i in range(self.n-1):\n",
252 | " self.layers.append(nn.Linear(dims[i],dims[i+1]))\n",
253 | " self.layers.append(nn.ReLU())\n",
254 | " if task=='classification': \n",
255 | " self.layers.append(nn.Linear(dims[i+1],dims[i+2]))\n",
256 | " self.layers.append(nn.LogSoftmax(dim=1))\n",
257 | " elif task=='regression': \n",
258 | " self.layers.append(nn.Linear(dims[i+1],dims[i+2]))\n",
259 | " self.layers.append(nn.Linear(dims[i+2],1))\n",
260 | " else: self.layers.append(nn.Linear(dims[i+1],dims[i+2]))\n",
261 | " self.optimizer = optim.Adam(self.parameters(),lr=lr)\n",
262 | " def forward(self,x):\n",
263 | " for l in self.layers:\n",
264 | " x = l(x)\n",
265 | " return(x)\n",
266 | "utils.hide_toggle('Class MLP')"
267 | ]
268 | },
269 | {
270 | "cell_type": "markdown",
271 | "metadata": {},
272 | "source": [
273 | "Recurrent network using GRU"
274 | ]
275 | },
276 | {
277 | "cell_type": "code",
278 | "execution_count": 9,
279 | "metadata": {},
280 | "outputs": [
281 | {
282 | "data": {
283 | "text/html": [
284 | "\n",
285 | " \n",
292 | "\n",
293 | " Class RNN (show/hide)\n",
294 | " "
295 | ],
296 | "text/plain": [
297 | ""
298 | ]
299 | },
300 | "execution_count": 9,
301 | "metadata": {},
302 | "output_type": "execute_result"
303 | }
304 | ],
305 | "source": [
306 | "class RNN(nn.Module):\n",
307 | " def __init__(self,n_features=1,dim=5,n_layers=3,n_classes=2,lr=1e-3):\n",
308 | " super(RNN,self).__init__()\n",
309 | " self.n_layers = n_layers\n",
310 | " self.dim = dim\n",
311 | " self.n_features = n_features\n",
312 | " self.rnn = nn.GRU(n_features, dim, n_layers,batch_first=True)\n",
313 | " self.linear = nn.Linear(dim,n_classes)\n",
314 | " self.logsoft = nn.LogSoftmax(dim=-1)\n",
315 | " self.optimizer = optim.Adam(self.parameters(),lr=lr)\n",
316 | " def forward(self,X):\n",
317 | " if self.n_features==1: X=X.unsqueeze(-1)\n",
318 | " h = torch.randn(self.n_layers,X.shape[0],self.dim)\n",
319 | " H = self.rnn(X,h)[1]\n",
320 | " G = self.logsoft(self.linear(H[-1,:,:]))\n",
321 | " return G\n",
322 | "utils.hide_toggle('Class RNN')"
323 | ]
324 | },
325 | {
326 | "cell_type": "code",
327 | "execution_count": null,
328 | "metadata": {},
329 | "outputs": [],
330 | "source": []
331 | }
332 | ],
333 | "metadata": {
334 | "kernelspec": {
335 | "display_name": "Python 3",
336 | "language": "python",
337 | "name": "python3"
338 | },
339 | "language_info": {
340 | "codemirror_mode": {
341 | "name": "ipython",
342 | "version": 3
343 | },
344 | "file_extension": ".py",
345 | "mimetype": "text/x-python",
346 | "name": "python",
347 | "nbconvert_exporter": "python",
348 | "pygments_lexer": "ipython3",
349 | "version": "3.7.9"
350 | }
351 | },
352 | "nbformat": 4,
353 | "nbformat_minor": 4
354 | }
355 |
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/2_Model_MetaL/data:
--------------------------------------------------------------------------------
1 | /Users/gautamshroff2019/Code/meta-learning-course-notebooks/0_Basic_NNs/data
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/2_Model_MetaL/l2lutils.ipynb:
--------------------------------------------------------------------------------
1 | /Users/gautamshroff2019/Code/meta-learning-course-notebooks/1_MAML/l2lutils.ipynb
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/2_Model_MetaL/models.ipynb:
--------------------------------------------------------------------------------
1 | /Users/gautamshroff2019/Code/meta-learning-course-notebooks/0_Basic_NNs/models.ipynb
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/2_Model_MetaL/utils.ipynb:
--------------------------------------------------------------------------------
1 | /Users/gautamshroff2019/Code/meta-learning-course-notebooks/0_Basic_NNs/utils.ipynb
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/3_Metric_MetaL/.ipynb_checkpoints/nb3-checkpoint.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# METRIC-BASED META-LEARNING using Matching Networks"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": null,
13 | "metadata": {},
14 | "outputs": [],
15 | "source": [
16 | "#from google.colab import drive\n",
17 | "#drive.mount('/content/drive')\n",
18 | "#%cd drive/MyDrive/'Colab Notebooks'\n",
19 | "#%cd meta-learning-course-notebooks/1_MAML/\n",
20 | "#!ls"
21 | ]
22 | },
23 | {
24 | "cell_type": "code",
25 | "execution_count": null,
26 | "metadata": {},
27 | "outputs": [],
28 | "source": [
29 | "#!pip install import_ipynb --quiet"
30 | ]
31 | },
32 | {
33 | "cell_type": "code",
34 | "execution_count": null,
35 | "metadata": {},
36 | "outputs": [],
37 | "source": [
38 | "#!pip install learn2learn --quiet"
39 | ]
40 | },
41 | {
42 | "cell_type": "code",
43 | "execution_count": null,
44 | "metadata": {},
45 | "outputs": [],
46 | "source": [
47 | "import import_ipynb\n",
48 | "import utils\n",
49 | "import models\n",
50 | "utils.hide_toggle('Imports 1')"
51 | ]
52 | },
53 | {
54 | "cell_type": "code",
55 | "execution_count": null,
56 | "metadata": {
57 | "scrolled": false
58 | },
59 | "outputs": [],
60 | "source": [
61 | "from IPython import display\n",
62 | "import torch\n",
63 | "from sklearn.manifold import TSNE\n",
64 | "from matplotlib import pyplot as plt\n",
65 | "from l2lutils import KShotLoader\n",
66 | "from IPython import display\n",
67 | "utils.hide_toggle('Imports 2')"
68 | ]
69 | },
70 | {
71 | "cell_type": "markdown",
72 | "metadata": {},
73 | "source": [
74 | "# Data Generation and Loading"
75 | ]
76 | },
77 | {
78 | "cell_type": "code",
79 | "execution_count": null,
80 | "metadata": {},
81 | "outputs": [],
82 | "source": [
83 | "#Generate data - euclidean\n",
84 | "meta_train_ds, meta_test_ds, full_loader = utils.euclideanDataset(n_samples=10000,n_features=20,n_classes=10,batch_size=32)"
85 | ]
86 | },
87 | {
88 | "cell_type": "code",
89 | "execution_count": null,
90 | "metadata": {},
91 | "outputs": [],
92 | "source": [
93 | "# Define an MLP network. Note that input dimension has to be data dimension. For classification\n",
94 | "# final dimension has to be number of classes; for regression one.\n",
95 | "#torch.manual_seed(10)\n",
96 | "net = models.MLP(dims=[20,32,32,10])"
97 | ]
98 | },
99 | {
100 | "cell_type": "code",
101 | "execution_count": null,
102 | "metadata": {},
103 | "outputs": [],
104 | "source": [
105 | "# Train the network; note that network is trained in place so repeated calls further train it.\n",
106 | "net,loss,accs=models.Train(net,full_loader,lr=1e-2,epochs=50,verbose=True)"
107 | ]
108 | },
109 | {
110 | "cell_type": "code",
111 | "execution_count": null,
112 | "metadata": {},
113 | "outputs": [],
114 | "source": [
115 | "#Training accuracy.\n",
116 | "models.accuracy(net,meta_train_ds.samples,meta_train_ds.labels,verbose=True)"
117 | ]
118 | },
119 | {
120 | "cell_type": "code",
121 | "execution_count": null,
122 | "metadata": {},
123 | "outputs": [],
124 | "source": [
125 | "# Test accuracy.\n",
126 | "models.accuracy(net,meta_test_ds.samples,meta_test_ds.labels)"
127 | ]
128 | },
129 | {
130 | "cell_type": "markdown",
131 | "metadata": {},
132 | "source": [
133 | "# Meta-Learning: Tasks"
134 | ]
135 | },
136 | {
137 | "cell_type": "markdown",
138 | "metadata": {},
139 | "source": [
140 | "Generate a k-shot n-way loader using the meta-training dataset"
141 | ]
142 | },
143 | {
144 | "cell_type": "code",
145 | "execution_count": null,
146 | "metadata": {},
147 | "outputs": [],
148 | "source": [
149 | "meta_train_kloader=KShotLoader(meta_train_ds,shots=5,ways=5)"
150 | ]
151 | },
152 | {
153 | "cell_type": "markdown",
154 | "metadata": {},
155 | "source": [
156 | "Sample a task - each task has a k-shot n-way training set and a similar test set"
157 | ]
158 | },
159 | {
160 | "cell_type": "code",
161 | "execution_count": null,
162 | "metadata": {},
163 | "outputs": [],
164 | "source": [
165 | "d_train,d_test=meta_train_kloader.get_task()"
166 | ]
167 | },
168 | {
169 | "cell_type": "markdown",
170 | "metadata": {},
171 | "source": [
172 | "Let's try directly learning using the task training set albeit its small size: create a dataset and loader and train it with the earlier network and Train function."
173 | ]
174 | },
175 | {
176 | "cell_type": "code",
177 | "execution_count": null,
178 | "metadata": {},
179 | "outputs": [],
180 | "source": [
181 | "taskds = utils.MyDS(d_train[0],d_train[1])"
182 | ]
183 | },
184 | {
185 | "cell_type": "code",
186 | "execution_count": null,
187 | "metadata": {},
188 | "outputs": [],
189 | "source": [
190 | "d_train_loader = torch.utils.data.DataLoader(dataset=taskds,batch_size=1,shuffle=True)"
191 | ]
192 | },
193 | {
194 | "cell_type": "code",
195 | "execution_count": null,
196 | "metadata": {},
197 | "outputs": [],
198 | "source": [
199 | "net,loss,accs=models.Train(net,d_train_loader,lr=1e-1,epochs=10,verbose=False)"
200 | ]
201 | },
202 | {
203 | "cell_type": "markdown",
204 | "metadata": {},
205 | "source": [
206 | "How does it do on the test set of the sampled task?"
207 | ]
208 | },
209 | {
210 | "cell_type": "code",
211 | "execution_count": null,
212 | "metadata": {},
213 | "outputs": [],
214 | "source": [
215 | "models.accuracy(net,d_test[0],d_test[1])"
216 | ]
217 | },
218 | {
219 | "cell_type": "markdown",
220 | "metadata": {},
221 | "source": [
222 | "# Matching Networks"
223 | ]
224 | },
225 | {
226 | "cell_type": "code",
227 | "execution_count": null,
228 | "metadata": {},
229 | "outputs": [],
230 | "source": [
231 | "import learn2learn as l2l\n",
232 | "import torch.optim as optim\n",
233 | "import torch.nn as nn\n",
234 | "import torch.nn.functional as F"
235 | ]
236 | },
237 | {
238 | "cell_type": "markdown",
239 | "metadata": {},
240 | "source": [
241 | "Sampling a training task: Note that each of d_train and d_test is a tuple comprising of a training set, and labels."
242 | ]
243 | },
244 | {
245 | "cell_type": "code",
246 | "execution_count": null,
247 | "metadata": {},
248 | "outputs": [],
249 | "source": [
250 | "d_train,d_test=meta_train_kloader.get_task()"
251 | ]
252 | },
253 | {
254 | "cell_type": "code",
255 | "execution_count": null,
256 | "metadata": {},
257 | "outputs": [],
258 | "source": [
259 | "lossfn = torch.nn.NLLLoss()"
260 | ]
261 | },
262 | {
263 | "cell_type": "markdown",
264 | "metadata": {},
265 | "source": [
266 | "Cos computes cosine similarities between a batch of targets and a given support set"
267 | ]
268 | },
269 | {
270 | "cell_type": "code",
271 | "execution_count": null,
272 | "metadata": {},
273 | "outputs": [],
274 | "source": [
275 | "class Cos(nn.Module):\n",
276 | " def __init__(self,dims=[20,32,32]):\n",
277 | " super(Cos,self).__init__()\n",
278 | " def forward(self,target,ss):\n",
279 | " # compute cosine distances between \n",
280 | " # target (batch,embedding_dim) and support set ss (ss_size,embedding_dim)\n",
281 | " # return (batch,ss_size)\n",
282 | " target_normed = F.normalize(target,p=2,dim=1)\n",
283 | " # shape of target_normed will be (batch,1,embedding_dim)\n",
284 | " ss_normed = F.normalize(ss,p=2,dim=1).permute(1,0)\n",
285 | " similarities = torch.mm(target_normed,ss_normed)\n",
286 | " # result will be (batch,ss_size)\n",
287 | " return similarities"
288 | ]
289 | },
290 | {
291 | "cell_type": "markdown",
292 | "metadata": {},
293 | "source": [
294 | "Matching Network (simple - without Full-context embeddings)"
295 | ]
296 | },
297 | {
298 | "cell_type": "code",
299 | "execution_count": null,
300 | "metadata": {},
301 | "outputs": [],
302 | "source": [
303 | "class MAN(nn.Module):\n",
304 | " def __init__(self,dims=[20,32,32],n_classes=2,lr=1e-3):\n",
305 | " super(MAN,self).__init__()\n",
306 | " self.n_classes = n_classes\n",
307 | " self.mlp = models.MLP(dims=dims,task='embedding')\n",
308 | " self.cos = Cos()\n",
309 | " self.attn = nn.Softmax(dim=1)\n",
310 | " self.optimizer = optim.Adam(self.parameters(),lr=lr)\n",
311 | " def forward(self,X,d_train):\n",
312 | " # X = (batch,n_features)\n",
313 | " (x_tr,y_tr) = d_train\n",
314 | " # x_tr = (ss_size,n_features), y_tr = (ss_size)\n",
315 | " ss_e = self.mlp(x_tr)\n",
316 | " X_e = self.mlp(X)\n",
317 | " sims = self.cos(X_e,ss_e)\n",
318 | " # size (batch,ss_size)\n",
319 | " attn_wts = self.attn(sims)\n",
320 | " y_h = torch.eye(self.n_classes)[y_tr]\n",
321 | " # y_h = one-hot version of y_tr = (ss_size,n_classes)\n",
322 | " preds = attn_wts@y_h\n",
323 | " return preds"
324 | ]
325 | },
326 | {
327 | "cell_type": "code",
328 | "execution_count": null,
329 | "metadata": {},
330 | "outputs": [],
331 | "source": [
332 | "X = torch.Tensor([[1,1,1],[-1,-1,-1],[1,2,3],[-1,-2,-3]])\n",
333 | "y_tr = torch.LongTensor([0,1])\n",
334 | "x_tr = X[[0,1],:]\n",
335 | "d_tr = (x_tr,y_tr)"
336 | ]
337 | },
338 | {
339 | "cell_type": "code",
340 | "execution_count": null,
341 | "metadata": {},
342 | "outputs": [],
343 | "source": [
344 | "man = MAN(dims=[3,8,8])"
345 | ]
346 | },
347 | {
348 | "cell_type": "markdown",
349 | "metadata": {},
350 | "source": [
351 | "# Putting it all together: Training a Matching Network\n",
352 | "Now let's put all of the above in a loop - training Matching Network algorithm:"
353 | ]
354 | },
355 | {
356 | "cell_type": "code",
357 | "execution_count": null,
358 | "metadata": {},
359 | "outputs": [],
360 | "source": [
361 | "# Redifning accuracy function so that it takes h - dataset context - as input since net requires it.\n",
362 | "def accuracy(Net,X_test,y_test,h,verbose=True):\n",
363 | " #Net.eval()\n",
364 | " m = X_test.shape[0]\n",
365 | " y_pred = Net(X_test,h)\n",
366 | " _, predicted = torch.max(y_pred, 1)\n",
367 | " correct = (predicted == y_test).float().sum().item()\n",
368 | " if verbose: print(correct,m)\n",
369 | " accuracy = correct/m\n",
370 | " #Net.train()\n",
371 | " return accuracy"
372 | ]
373 | },
374 | {
375 | "cell_type": "code",
376 | "execution_count": null,
377 | "metadata": {},
378 | "outputs": [],
379 | "source": [
380 | "classes_train = [i for i in range(5)]\n",
381 | "classes_test = [i+5 for i in range(5)]\n",
382 | "classes_train, classes_test"
383 | ]
384 | },
385 | {
386 | "cell_type": "code",
387 | "execution_count": null,
388 | "metadata": {},
389 | "outputs": [],
390 | "source": [
391 | "import learn2learn as l2l\n",
392 | "import torch.optim as optim\n",
393 | "shots,ways = 5,5\n",
394 | "net = MAN(n_classes=ways,dims=[20,64,32],lr=1e-4)\n",
395 | "lossfn = torch.nn.NLLLoss()\n",
396 | "meta_train_kloader=KShotLoader(meta_train_ds,shots=shots,ways=ways,num_tasks=1000,classes=classes_train)"
397 | ]
398 | },
399 | {
400 | "cell_type": "code",
401 | "execution_count": null,
402 | "metadata": {},
403 | "outputs": [],
404 | "source": [
405 | "epoch=0\n",
406 | "n_epochs=100\n",
407 | "task_count=50\n",
408 | "while epoch\n",
27 | " function code_toggle_15564854882154834972() {\n",
28 | " $('div.cell.code_cell.rendered.selected').find('div.input').toggle();\n",
29 | " }\n",
30 | "\n",
31 | " \n",
32 | " \n",
33 | "\n",
34 | " Imports 1 (show/hide)\n",
35 | " "
36 | ],
37 | "text/plain": [
38 | ""
39 | ]
40 | },
41 | "execution_count": 4,
42 | "metadata": {},
43 | "output_type": "execute_result"
44 | }
45 | ],
46 | "source": [
47 | "from IPython.display import HTML\n",
48 | "import import_ipynb\n",
49 | "import random\n",
50 | "import utils\n",
51 | "utils.hide_toggle('Imports 1')"
52 | ]
53 | },
54 | {
55 | "cell_type": "code",
56 | "execution_count": 5,
57 | "metadata": {},
58 | "outputs": [
59 | {
60 | "data": {
61 | "text/html": [
62 | "\n",
63 | " \n",
70 | "\n",
71 | " Imports 2 (show/hide)\n",
72 | " "
73 | ],
74 | "text/plain": [
75 | ""
76 | ]
77 | },
78 | "execution_count": 5,
79 | "metadata": {},
80 | "output_type": "execute_result"
81 | }
82 | ],
83 | "source": [
84 | "import numpy as np\n",
85 | "import torch\n",
86 | "from torch import nn\n",
87 | "from torch import optim\n",
88 | "from IPython import display\n",
89 | "utils.hide_toggle('Imports 2')"
90 | ]
91 | },
92 | {
93 | "cell_type": "markdown",
94 | "metadata": {},
95 | "source": [
96 | "Compute accuracy of predictions."
97 | ]
98 | },
99 | {
100 | "cell_type": "code",
101 | "execution_count": 6,
102 | "metadata": {},
103 | "outputs": [
104 | {
105 | "data": {
106 | "text/html": [
107 | "\n",
108 | " \n",
115 | "\n",
116 | " Function: accuracy (show/hide)\n",
117 | " "
118 | ],
119 | "text/plain": [
120 | ""
121 | ]
122 | },
123 | "execution_count": 6,
124 | "metadata": {},
125 | "output_type": "execute_result"
126 | }
127 | ],
128 | "source": [
129 | "def accuracy(Net,X_test,y_test,verbose=True):\n",
130 | " Net.eval()\n",
131 | " m = X_test.shape[0]\n",
132 | " y_pred = Net(X_test)\n",
133 | " predicted = torch.max(y_pred, 1)\n",
134 | " correct = (predicted == y_test).sum().item()\n",
135 | " if verbose: print(correct,m)\n",
136 | " accuracy = correct/m\n",
137 | " Net.train()\n",
138 | " return accuracy\n",
139 | "utils.hide_toggle('Function: accuracy')"
140 | ]
141 | },
142 | {
143 | "cell_type": "markdown",
144 | "metadata": {},
145 | "source": [
146 | "Generic training loop"
147 | ]
148 | },
149 | {
150 | "cell_type": "code",
151 | "execution_count": 7,
152 | "metadata": {},
153 | "outputs": [
154 | {
155 | "data": {
156 | "text/html": [
157 | "\n",
158 | " \n",
165 | "\n",
166 | " Function Train (show/hide)\n",
167 | " "
168 | ],
169 | "text/plain": [
170 | ""
171 | ]
172 | },
173 | "execution_count": 7,
174 | "metadata": {},
175 | "output_type": "execute_result"
176 | }
177 | ],
178 | "source": [
179 | "def Train(Net,data,epochs=20,lr=5e-2,Loss=nn.NLLLoss(),verbose=False):\n",
180 | " #optimizer = optim.Adam(Net.parameters(),lr=lr)\n",
181 | " losses = []\n",
182 | " accs = []\n",
183 | " for e in range(epochs):\n",
184 | " step=0\n",
185 | " tot_loss=0.0\n",
186 | " acc=0.0\n",
187 | " for (X,y) in data:\n",
188 | " y_pred = Net(X)\n",
189 | " loss = Loss(y_pred,y)\n",
190 | " Net.optimizer.zero_grad()\n",
191 | " loss.backward()\n",
192 | " Net.optimizer.step()\n",
193 | " step+=1\n",
194 | " tot_loss+=loss\n",
195 | " acc+=accuracy(Net,X,y,verbose=False)\n",
196 | " l = tot_loss.item()/step\n",
197 | " a = acc/step\n",
198 | " losses += [l]\n",
199 | " accs += [a]\n",
200 | " if verbose: \n",
201 | " print('Epoch % 2d Loss: %2.5e Accuracy: %2.5f'%(e,l,a))\n",
202 | " display.clear_output(wait=True)\n",
203 | " return Net,losses,accs\n",
204 | "utils.hide_toggle('Function Train')"
205 | ]
206 | },
207 | {
208 | "cell_type": "markdown",
209 | "metadata": {},
210 | "source": [
211 | "Multi-layer perceptron with ReLU non-lineartities; for classification or regression."
212 | ]
213 | },
214 | {
215 | "cell_type": "code",
216 | "execution_count": 8,
217 | "metadata": {},
218 | "outputs": [
219 | {
220 | "data": {
221 | "text/html": [
222 | "\n",
223 | " \n",
230 | "\n",
231 | " Class MLP (show/hide)\n",
232 | " "
233 | ],
234 | "text/plain": [
235 | ""
236 | ]
237 | },
238 | "execution_count": 8,
239 | "metadata": {},
240 | "output_type": "execute_result"
241 | }
242 | ],
243 | "source": [
244 | "class MLP(nn.Module):\n",
245 | " def __init__(self,dims=[5,3,2],task='classification',lr=1e-3):\n",
246 | " super(MLP,self).__init__()\n",
247 | " self.dims=dims\n",
248 | " self.n = len(self.dims)-1\n",
249 | " self.task=task\n",
250 | " self.layers=nn.ModuleList()\n",
251 | " for i in range(self.n-1):\n",
252 | " self.layers.append(nn.Linear(dims[i],dims[i+1]))\n",
253 | " self.layers.append(nn.ReLU())\n",
254 | " if task=='classification': \n",
255 | " self.layers.append(nn.Linear(dims[i+1],dims[i+2]))\n",
256 | " self.layers.append(nn.LogSoftmax(dim=1))\n",
257 | " elif task=='regression': \n",
258 | " self.layers.append(nn.Linear(dims[i+1],dims[i+2]))\n",
259 | " self.layers.append(nn.Linear(dims[i+2],1))\n",
260 | " else: self.layers.append(nn.Linear(dims[i+1],dims[i+2]))\n",
261 | " self.optimizer = optim.Adam(self.parameters(),lr=lr)\n",
262 | " def forward(self,x):\n",
263 | " for l in self.layers:\n",
264 | " x = l(x)\n",
265 | " return(x)\n",
266 | "utils.hide_toggle('Class MLP')"
267 | ]
268 | },
269 | {
270 | "cell_type": "markdown",
271 | "metadata": {},
272 | "source": [
273 | "Recurrent network using GRU"
274 | ]
275 | },
276 | {
277 | "cell_type": "code",
278 | "execution_count": 9,
279 | "metadata": {},
280 | "outputs": [
281 | {
282 | "data": {
283 | "text/html": [
284 | "\n",
285 | " \n",
292 | "\n",
293 | " Class RNN (show/hide)\n",
294 | " "
295 | ],
296 | "text/plain": [
297 | ""
298 | ]
299 | },
300 | "execution_count": 9,
301 | "metadata": {},
302 | "output_type": "execute_result"
303 | }
304 | ],
305 | "source": [
306 | "class RNN(nn.Module):\n",
307 | " def __init__(self,n_features=1,dim=5,n_layers=3,n_classes=2,lr=1e-3):\n",
308 | " super(RNN,self).__init__()\n",
309 | " self.n_layers = n_layers\n",
310 | " self.dim = dim\n",
311 | " self.n_features = n_features\n",
312 | " self.rnn = nn.GRU(n_features, dim, n_layers,batch_first=True)\n",
313 | " self.linear = nn.Linear(dim,n_classes)\n",
314 | " self.logsoft = nn.LogSoftmax(dim=-1)\n",
315 | " self.optimizer = optim.Adam(self.parameters(),lr=lr)\n",
316 | " def forward(self,X):\n",
317 | " if self.n_features==1: X=X.unsqueeze(-1)\n",
318 | " h = torch.randn(self.n_layers,X.shape[0],self.dim)\n",
319 | " H = self.rnn(X,h)[1]\n",
320 | " G = self.logsoft(self.linear(H[-1,:,:]))\n",
321 | " return G\n",
322 | "utils.hide_toggle('Class RNN')"
323 | ]
324 | },
325 | {
326 | "cell_type": "code",
327 | "execution_count": null,
328 | "metadata": {},
329 | "outputs": [],
330 | "source": []
331 | }
332 | ],
333 | "metadata": {
334 | "kernelspec": {
335 | "display_name": "Python 3",
336 | "language": "python",
337 | "name": "python3"
338 | },
339 | "language_info": {
340 | "codemirror_mode": {
341 | "name": "ipython",
342 | "version": 3
343 | },
344 | "file_extension": ".py",
345 | "mimetype": "text/x-python",
346 | "name": "python",
347 | "nbconvert_exporter": "python",
348 | "pygments_lexer": "ipython3",
349 | "version": "3.7.9"
350 | }
351 | },
352 | "nbformat": 4,
353 | "nbformat_minor": 4
354 | }
355 |
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/4_GLN_MetaL/l2lutils.ipynb:
--------------------------------------------------------------------------------
1 | /Users/gautamshroff2019/Code/meta-learning-course-notebooks/1_MAML/l2lutils.ipynb
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/4_GLN_MetaL/models.ipynb:
--------------------------------------------------------------------------------
1 | /Users/gautamshroff2019/Code/meta-learning-course-notebooks/0_Basic_NNs/models.ipynb
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/4_GLN_MetaL/pygln/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tirtharajdash/BITS-G513_MetaLearning/81d62d08a420fb94059e04c2f2e9c7d0af10e151/Meta_Learning_Notebooks/4_GLN_MetaL/pygln/.DS_Store
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/4_GLN_MetaL/pygln/__init__.py:
--------------------------------------------------------------------------------
1 | from pygln.gln import GLN
2 |
3 |
4 | __all__ = ['GLN']
5 |
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/4_GLN_MetaL/pygln/__pycache__/__init__.cpython-37.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tirtharajdash/BITS-G513_MetaLearning/81d62d08a420fb94059e04c2f2e9c7d0af10e151/Meta_Learning_Notebooks/4_GLN_MetaL/pygln/__pycache__/__init__.cpython-37.pyc
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/4_GLN_MetaL/pygln/__pycache__/base.cpython-37.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tirtharajdash/BITS-G513_MetaLearning/81d62d08a420fb94059e04c2f2e9c7d0af10e151/Meta_Learning_Notebooks/4_GLN_MetaL/pygln/__pycache__/base.cpython-37.pyc
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/4_GLN_MetaL/pygln/__pycache__/gln.cpython-37.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tirtharajdash/BITS-G513_MetaLearning/81d62d08a420fb94059e04c2f2e9c7d0af10e151/Meta_Learning_Notebooks/4_GLN_MetaL/pygln/__pycache__/gln.cpython-37.pyc
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/4_GLN_MetaL/pygln/__pycache__/utils.cpython-37.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tirtharajdash/BITS-G513_MetaLearning/81d62d08a420fb94059e04c2f2e9c7d0af10e151/Meta_Learning_Notebooks/4_GLN_MetaL/pygln/__pycache__/utils.cpython-37.pyc
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/4_GLN_MetaL/pygln/base.py:
--------------------------------------------------------------------------------
1 | from abc import ABC, abstractmethod
2 | import numpy as np
3 | from typing import Callable, Optional, Sequence
4 |
5 |
6 | class OnlineUpdateModel(ABC):
7 | """Base class for online-update models, shared by all backend implementations."""
8 |
9 | @abstractmethod
10 | def predict(self,
11 | input: np.ndarray,
12 | target: Optional[np.ndarray] = None,
13 | return_probs: bool = False) -> np.ndarray:
14 | """
15 | Predict the class for the given inputs, and optionally update the weights.
16 |
17 | Args:
18 | input (np.array[B, N]): Batch of B N-dim float input vectors.
19 | target (np.array[B]): Optional batch of B bool/int target class labels which, if given,
20 | triggers an online update if given.
21 | return_probs (bool): Whether to return the classification probability (for each
22 | one-vs-all classifier if num_classes given) instead of the class.
23 |
24 | Returns:
25 | Predicted class per input instance, or classification probabilities if return_probs set.
26 | """
27 | raise NotImplementedError()
28 |
29 |
30 | class GLNBase(OnlineUpdateModel):
31 | """
32 | Base class for Gated Linear Network implementations (https://arxiv.org/abs/1910.01526).
33 |
34 | Args:
35 | layer_sizes (list[int >= 1]): List of layer output sizes, excluding last classification
36 | layer which is added implicitly.
37 | input_size (int >= 1): Input vector size.
38 | num_classes (int >= 2): For values >2, turns GLN into a multi-class classifier by internally
39 | creating a one-vs-all binary GLN classifier per class and return the argmax as output.
40 | context_map_size (int >= 1): Context dimension, i.e. number of context halfspaces.
41 | bias (bool): Whether to add a bias prediction in each layer.
42 | context_bias (bool): Whether to use a random non-zero bias for context halfspace gating.
43 | base_predictor (np.array[N] -> np.array[K]): If given, maps the N-dim input vector to a
44 | corresponding K-dim vector of base predictions (could be a constant prior), instead of
45 | simply using the clipped input vector itself.
46 | learning_rate (float > 0.0): Update learning rate.
47 | pred_clipping (0.0 < float < 0.5): Clip predictions into [p, 1 - p] at each layer.
48 | weight_clipping (float > 0.0): Clip weights into [-w, w] after each update.
49 | """
50 | def __init__(self,
51 | layer_sizes: Sequence[int],
52 | input_size: int,
53 | num_classes: int = 2,
54 | context_map_size: int = 4,
55 | bias: bool = True,
56 | context_bias: bool = False,
57 | base_predictor: Optional[Callable[[np.ndarray], np.ndarray]] = None,
58 | learning_rate: float = 1e-3,
59 | pred_clipping: float = 1e-3,
60 | weight_clipping: float = 5.0):
61 | super().__init__()
62 |
63 | assert all(size > 0 for size in layer_sizes)
64 | self.layer_sizes = tuple(layer_sizes)
65 |
66 | assert input_size > 0
67 | self.input_size = int(input_size)
68 |
69 | assert num_classes >= 2
70 | self.num_classes = int(num_classes)
71 |
72 | assert context_map_size >= 0
73 | self.context_map_size = int(context_map_size)
74 |
75 | self.bias = bool(bias)
76 |
77 | self.context_bias = bool(context_bias)
78 |
79 | if base_predictor is None:
80 | self.base_predictor = (
81 | lambda x: (x - x.min(axis=1, keepdims=True)) /
82 | (x.max(axis=1, keepdims=True) - x.min(axis=1, keepdims=True))
83 | )
84 | self.base_pred_size = self.input_size
85 | else:
86 | self.base_predictor = base_predictor
87 | dummy_input = np.zeros(shape=(1, self.input_size))
88 | dummy_pred = self.base_predictor(dummy_input)
89 | assert dummy_pred.dtype in (np.float32, np.float64)
90 | assert dummy_pred.ndim == 2 and dummy_pred.shape[0] == 1
91 | self.base_pred_size = dummy_pred.shape[1]
92 |
93 | assert not isinstance(learning_rate, float) or learning_rate > 0.0
94 | self.learning_rate = learning_rate
95 |
96 | assert 0.0 < pred_clipping < 1.0
97 | self.pred_clipping = float(pred_clipping)
98 |
99 | assert weight_clipping > 0.0
100 | self.weight_clipping = float(weight_clipping)
101 |
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/4_GLN_MetaL/pygln/baselines.py:
--------------------------------------------------------------------------------
1 | import numpy as np
2 | import torch
3 | from typing import Optional, Sequence
4 |
5 |
6 | class MLP(torch.nn.Module):
7 |
8 | def __init__(self,
9 | layer_sizes: Sequence[int],
10 | input_size: int,
11 | num_classes: int = 2,
12 | learning_rate: float = 2.5e-4):
13 | super().__init__()
14 |
15 | self.layers = torch.nn.ModuleList()
16 | previous_size = input_size
17 | for size in layer_sizes:
18 | self.layers.append(torch.nn.Linear(in_features=previous_size, out_features=size))
19 | self.layers.append(torch.nn.Tanh())
20 | previous_size = size
21 | self.layers.append(torch.nn.Linear(in_features=previous_size, out_features=num_classes))
22 |
23 | self.loss = torch.nn.CrossEntropyLoss()
24 | self.optimizer = torch.optim.Adam(self.parameters(), lr=learning_rate)
25 |
26 | if torch.cuda.is_available():
27 | self.cuda()
28 |
29 | def predict(self, input: np.ndarray, target: Optional[np.ndarray] = None) -> np.ndarray:
30 | input = torch.tensor(input, dtype=torch.float32)
31 | if target is not None:
32 | target = torch.tensor(target)
33 | if torch.cuda.is_available():
34 | input = input.cuda()
35 | if target is not None:
36 | target = target.cuda()
37 |
38 | # Prediction
39 | for layer in self.layers:
40 | input = layer(input)
41 | output = input.argmax(dim=1)
42 |
43 | # Update
44 | if target is not None:
45 | self.optimizer.zero_grad()
46 | loss = self.loss(input, target)
47 | loss.backward()
48 | self.optimizer.step()
49 |
50 | if torch.cuda.is_available():
51 | output = output.cpu()
52 | return output.numpy()
53 |
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/4_GLN_MetaL/pygln/gln.bak:
--------------------------------------------------------------------------------
1 | import numpy as np
2 | from typing import Callable, Optional, Sequence
3 |
4 |
5 | def GLN(backend: str,
6 | layer_sizes: Sequence[int],
7 | input_size: int,
8 | num_classes: int = 2,
9 | context_map_size: int = 4,
10 | bias: bool = True,
11 | context_bias: bool = False,
12 | base_predictor: Optional[Callable[[np.ndarray], np.ndarray]] = None,
13 | learning_rate: float = 1e-3,
14 | pred_clipping: float = 1e-3,
15 | weight_clipping: float = 5.0):
16 | """
17 | Backend wrapper for Gated Linear Network implementations (https://arxiv.org/abs/1910.01526).
18 |
19 | Args:
20 | backend ("jax", "numpy", "pytorch", "tf"): Which backend implementation to use.
21 | layer_sizes (list[int >= 1]): List of layer output sizes, excluding last classification
22 | layer which is added implicitly.
23 | input_size (int >= 1): Input vector size.
24 | num_classes (int >= 2): For values >2, turns GLN into a multi-class classifier by internally
25 | creating a one-vs-all binary GLN classifier per class and return the argmax as output.
26 | context_map_size (int >= 1): Context dimension, i.e. number of context halfspaces.
27 | bias (bool): Whether to add a bias prediction in each layer.
28 | context_bias (bool): Whether to use a random non-zero bias for context halfspace gating.
29 | base_predictor (np.array[N] -> np.array[K]): If given, maps the N-dim input vector to a
30 | corresponding K-dim vector of base predictions (could be a constant prior), instead of
31 | simply using the clipped input vector itself.
32 | learning_rate (float > 0.0): Update learning rate.
33 | pred_clipping (0.0 < float < 0.5): Clip predictions into [p, 1 - p] at each layer.
34 | weight_clipping (float > 0.0): Clip weights into [-w, w] after each update.
35 | """
36 |
37 | if backend == 'jax':
38 | from pygln.jax import GLN
39 | elif backend in ['numpy', 'np']:
40 | from pygln.numpy import GLN
41 | elif backend in ['pytorch', 'torch']:
42 | from pygln.pytorch import GLN
43 | elif backend in ['tensorflow', 'tf']:
44 | from pygln.tf import GLN
45 | else:
46 | raise NotImplementedError(f"No implementation for backend {backend}.")
47 |
48 | return GLN(layer_sizes, input_size, num_classes, context_map_size, bias,
49 | context_bias, base_predictor, learning_rate, pred_clipping,
50 | weight_clipping)
51 |
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/4_GLN_MetaL/pygln/gln.py:
--------------------------------------------------------------------------------
1 | import numpy as np
2 | from typing import Callable, Optional, Sequence
3 |
4 |
5 | def GLN(backend: str,
6 | layer_sizes: Sequence[int],
7 | input_size: int,
8 | num_classes: int = 2,
9 | context_map_size: int = 4,
10 | bias: bool = True,
11 | context_bias: bool = False,
12 | base_predictor: Optional[Callable[[np.ndarray], np.ndarray]] = None,
13 | learning_rate: float = 1e-3,
14 | pred_clipping: float = 1e-3,
15 | weight_clipping: float = 5.0):
16 | """
17 | Backend wrapper for Gated Linear Network implementations (https://arxiv.org/abs/1910.01526).
18 |
19 | Args:
20 | backend ("jax", "numpy", "pytorch", "tf"): Which backend implementation to use.
21 | layer_sizes (list[int >= 1]): List of layer output sizes, excluding last classification
22 | layer which is added implicitly.
23 | input_size (int >= 1): Input vector size.
24 | num_classes (int >= 2): For values >2, turns GLN into a multi-class classifier by internally
25 | creating a one-vs-all binary GLN classifier per class and return the argmax as output.
26 | context_map_size (int >= 1): Context dimension, i.e. number of context halfspaces.
27 | bias (bool): Whether to add a bias prediction in each layer.
28 | context_bias (bool): Whether to use a random non-zero bias for context halfspace gating.
29 | base_predictor (np.array[N] -> np.array[K]): If given, maps the N-dim input vector to a
30 | corresponding K-dim vector of base predictions (could be a constant prior), instead of
31 | simply using the clipped input vector itself.
32 | learning_rate (float > 0.0): Update learning rate.
33 | pred_clipping (0.0 < float < 0.5): Clip predictions into [p, 1 - p] at each layer.
34 | weight_clipping (float > 0.0): Clip weights into [-w, w] after each update.
35 | """
36 |
37 | if backend == 'jax':
38 | from pygln.jax import GLN
39 | elif backend in ['numpy', 'np']:
40 | from pygln.numpy import GLN
41 | elif backend in ['pytorch', 'torch']:
42 | from pygln.pytorch import GLN
43 | elif backend in ['tensorflow', 'tf']:
44 | from pygln.tf import GLN
45 | else:
46 | raise NotImplementedError(f"No implementation for backend {backend}.")
47 |
48 | return GLN(layer_sizes, input_size, num_classes, context_map_size, bias,
49 | context_bias, base_predictor, learning_rate, pred_clipping,
50 | weight_clipping)
51 |
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/4_GLN_MetaL/pygln/jax/__init__.py:
--------------------------------------------------------------------------------
1 | from pygln.jax.gln import GLN
2 |
3 |
4 | __all__ = ['GLN']
5 |
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/4_GLN_MetaL/pygln/numpy/__init__.py:
--------------------------------------------------------------------------------
1 | from pygln.numpy.gln import GLN, PaperLearningRate
2 |
3 | __all__ = ['GLN', 'PaperLearningRate']
4 |
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/4_GLN_MetaL/pygln/numpy/__pycache__/__init__.cpython-37.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tirtharajdash/BITS-G513_MetaLearning/81d62d08a420fb94059e04c2f2e9c7d0af10e151/Meta_Learning_Notebooks/4_GLN_MetaL/pygln/numpy/__pycache__/__init__.cpython-37.pyc
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/4_GLN_MetaL/pygln/numpy/__pycache__/gln.cpython-37.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tirtharajdash/BITS-G513_MetaLearning/81d62d08a420fb94059e04c2f2e9c7d0af10e151/Meta_Learning_Notebooks/4_GLN_MetaL/pygln/numpy/__pycache__/gln.cpython-37.pyc
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/4_GLN_MetaL/pygln/pytorch/__init__.py:
--------------------------------------------------------------------------------
1 | from pygln.pytorch.gln import GLN, PaperLearningRate
2 |
3 | __all__ = ['GLN', 'PaperLearningRate']
4 |
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/4_GLN_MetaL/pygln/pytorch/__pycache__/__init__.cpython-37.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tirtharajdash/BITS-G513_MetaLearning/81d62d08a420fb94059e04c2f2e9c7d0af10e151/Meta_Learning_Notebooks/4_GLN_MetaL/pygln/pytorch/__pycache__/__init__.cpython-37.pyc
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/4_GLN_MetaL/pygln/pytorch/__pycache__/gln.cpython-37.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tirtharajdash/BITS-G513_MetaLearning/81d62d08a420fb94059e04c2f2e9c7d0af10e151/Meta_Learning_Notebooks/4_GLN_MetaL/pygln/pytorch/__pycache__/gln.cpython-37.pyc
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/4_GLN_MetaL/pygln/pytorch/gln.py:
--------------------------------------------------------------------------------
1 | import numpy as np
2 | import torch
3 | from scipy.special import logit as slogit
4 | from torch import nn
5 | from typing import Callable, Optional, Sequence, Union
6 |
7 | from ..base import GLNBase
8 |
9 |
10 | class DynamicParameter(nn.Module):
11 | def __init__(self, name: Optional[str] = None):
12 | super().__init__()
13 | self.step = 0
14 | self.name = name
15 |
16 | @property
17 | def value(self):
18 | self.step += 1
19 | return self.step
20 |
21 |
22 | class ConstantParameter(DynamicParameter):
23 | def __init__(self, constant_value: float, name: Optional[str] = None):
24 | super().__init__(name)
25 |
26 | assert isinstance(constant_value, float)
27 | self.constant_value = constant_value
28 |
29 | @property
30 | def value(self):
31 | return self.constant_value
32 |
33 |
34 | class PaperLearningRate(DynamicParameter):
35 | def __init__(self):
36 | DynamicParameter.__init__(self, 'paper_learning_rate')
37 |
38 | @property
39 | def value(self):
40 | return min(100 / super().value, 0.01)
41 |
42 |
43 | class Linear(nn.Module):
44 | def __init__(self,
45 | size: int,
46 | input_size: int,
47 | context_size: int,
48 | context_map_size: int,
49 | num_classes: int,
50 | learning_rate: DynamicParameter,
51 | pred_clipping: float,
52 | weight_clipping: float,
53 | bias: bool,
54 | context_bias: bool):
55 | super().__init__()
56 |
57 | assert size > 0 and input_size > 0 and context_size > 0
58 | assert context_map_size >= 0
59 | assert num_classes >= 2
60 |
61 | self.context_map_size = context_map_size
62 | self.num_classes = num_classes if num_classes > 2 else 1
63 | self.learning_rate = learning_rate
64 | # clipping value for predictions
65 | self.pred_clipping = pred_clipping
66 | # clipping value for weights of layer
67 | self.weight_clipping = weight_clipping
68 |
69 | if bias and size > 1:
70 | self.bias = torch.empty(
71 | (1, 1,
72 | self.num_classes)).uniform_(slogit(self.pred_clipping),
73 | slogit(1 - self.pred_clipping))
74 | self.size = size - 1
75 | else:
76 | self.bias = None
77 | self.size = size
78 |
79 | if context_map_size > 0:
80 | self._context_maps = torch.as_tensor(
81 | np.random.normal(size=(self.num_classes, self.size,
82 | context_map_size, context_size)),
83 | dtype=torch.float32)
84 |
85 | # constant values for halfspace gating
86 | if context_map_size == 0:
87 | pass
88 | elif context_bias:
89 | context_bias_shape = (self.num_classes, self.size,
90 | context_map_size, 1)
91 | self._context_bias = torch.tensor(
92 | np.random.normal(size=context_bias_shape), dtype=torch.float32)
93 | self._context_maps /= torch.norm(self._context_maps,
94 | dim=-1,
95 | keepdim=True)
96 | else:
97 | self._context_bias = torch.tensor(0.0)
98 |
99 | self.bias = nn.Parameter(self.bias, requires_grad=False)
100 |
101 | if context_map_size > 0:
102 | self._context_maps = nn.Parameter(self._context_maps,
103 | requires_grad=False)
104 | self._context_bias = nn.Parameter(self._context_bias,
105 | requires_grad=False)
106 |
107 | # array to convert mapped_context_binary context to index
108 | self._boolean_converter = nn.Parameter(torch.as_tensor(
109 | np.array([[2**i] for i in range(context_map_size)])),
110 | requires_grad=False)
111 |
112 | # weights for the whole layer
113 | weights_shape = (self.num_classes, self.size, 2**context_map_size,
114 | input_size)
115 | self._weights = nn.Parameter(torch.full(size=weights_shape,
116 | fill_value=1 / input_size,
117 | dtype=torch.float32),
118 | requires_grad=False)
119 |
120 | def predict(self, logit, context, target=None):
121 | if self.context_map_size > 0:
122 | # project side information and determine context index
123 | distances = torch.matmul(self._context_maps, context.T)
124 | mapped_context_binary = (distances > self._context_bias).int()
125 | current_context_indices = torch.sum(mapped_context_binary *
126 | self._boolean_converter,
127 | dim=-2)
128 | else:
129 | current_context_indices = torch.zeros(
130 | self.num_classes, self.size, 1, dtype=torch.int64
131 | )
132 |
133 | # select all context across all neurons in layer
134 | current_selected_weights = self._weights[
135 | torch.arange(self.num_classes).reshape(-1, 1, 1),
136 | torch.arange(self.size).reshape(1, -1, 1
137 | ), current_context_indices, :]
138 |
139 | if logit.ndim == 2:
140 | logit = torch.unsqueeze(logit, dim=-1)
141 |
142 | output_logits = torch.clamp(torch.matmul(
143 | current_selected_weights,
144 | torch.unsqueeze(logit.T, dim=-3)).diagonal(dim1=-2, dim2=-1),
145 | min=slogit(self.pred_clipping),
146 | max=slogit(1 - self.pred_clipping)).T
147 |
148 | if target is not None:
149 | sigmoids = torch.sigmoid(output_logits)
150 | # compute update
151 | diff = sigmoids - torch.unsqueeze(target, dim=1)
152 | update_values = self.learning_rate.value * torch.unsqueeze(
153 | diff, dim=-1) * torch.unsqueeze(logit.permute(0, 2, 1), dim=1)
154 | self._weights[torch.arange(self.num_classes).reshape(-1, 1, 1),
155 | torch.arange(self.size).reshape(1, -1, 1),
156 | current_context_indices, :] = torch.clamp(
157 | current_selected_weights -
158 | update_values.permute(2, 1, 0, 3),
159 | -self.weight_clipping, self.weight_clipping)
160 |
161 | if self.bias is not None:
162 | bias_append = torch.cat([self.bias] * output_logits.shape[0],
163 | dim=0)
164 | output_logits = torch.cat([bias_append, output_logits], dim=1)
165 |
166 | return output_logits
167 |
168 | def extra_repr(self):
169 | return 'input_size={}, neurons={}, context_map_size={}, bias={}'.format(
170 | self._weights.size(3), self._weights.size(1),
171 | self._context_maps.size(2), self.bias)
172 |
173 |
174 | class GLN(nn.Module, GLNBase):
175 | """
176 | PyTorch implementation of Gated Linear Networks (https://arxiv.org/abs/1910.01526).
177 |
178 | Args:
179 | layer_sizes (list[int >= 1]): List of layer output sizes, excluding last classification
180 | layer which is added implicitly.
181 | input_size (int >= 1): Input vector size.
182 | num_classes (int >= 2): For values >2, turns GLN into a multi-class classifier by internally
183 | creating a one-vs-all binary GLN classifier per class and return the argmax as output.
184 | context_map_size (int >= 1): Context dimension, i.e. number of context halfspaces.
185 | bias (bool): Whether to add a bias prediction in each layer.
186 | context_bias (bool): Whether to use a random non-zero bias for context halfspace gating.
187 | base_predictor (np.array[N] -> np.array[K]): If given, maps the N-dim input vector to a
188 | corresponding K-dim vector of base predictions (could be a constant prior), instead of
189 | simply using the clipped input vector itself.
190 | learning_rate (float > 0.0): Update learning rate.
191 | pred_clipping (0.0 < float < 0.5): Clip predictions into [p, 1 - p] at each layer.
192 | weight_clipping (float > 0.0): Clip weights into [-w, w] after each update.
193 | """
194 | def __init__(self,
195 | layer_sizes: Sequence[int],
196 | input_size: int,
197 | num_classes: int = 2,
198 | context_map_size: int = 4,
199 | bias: bool = True,
200 | context_bias: bool = False,
201 | base_predictor: Optional[
202 | Callable[[np.ndarray], np.ndarray]] = None,
203 | learning_rate: Union[float, DynamicParameter] = 1e-3,
204 | pred_clipping: float = 1e-3,
205 | weight_clipping: float = 5.0):
206 |
207 | nn.Module.__init__(self)
208 | GLNBase.__init__(self, layer_sizes, input_size, num_classes,
209 | context_map_size, bias, context_bias, base_predictor,
210 | learning_rate, pred_clipping, weight_clipping)
211 |
212 | # Initialize layers
213 | self.layers = nn.ModuleList()
214 | previous_size = self.base_pred_size
215 |
216 | if isinstance(learning_rate, float):
217 | self.learning_rate = ConstantParameter(learning_rate,
218 | 'learning_rate')
219 | elif isinstance(learning_rate, DynamicParameter):
220 | self.learning_rate = learning_rate
221 | else:
222 | raise ValueError('Invalid learning rate')
223 |
224 | if bias:
225 | self.base_bias = np.random.uniform(low=slogit(pred_clipping),
226 | high=slogit(1 - pred_clipping))
227 | for size in (self.layer_sizes + (1,)):
228 | layer = Linear(size, previous_size, self.input_size,
229 | self.context_map_size, self.num_classes,
230 | self.learning_rate, self.pred_clipping,
231 | self.weight_clipping, self.bias, self.context_bias)
232 | self.layers.append(layer)
233 | previous_size = size
234 |
235 | if torch.cuda.is_available():
236 | self.cuda()
237 |
238 | def predict(self,
239 | input: np.ndarray,
240 | target: Optional[np.ndarray] = None,
241 | return_probs: bool = False) -> np.ndarray:
242 | """
243 | Predict the class for the given inputs, and optionally update the weights.
244 |
245 | Args:
246 | input (np.array[B, N]): Batch of B N-dim float input vectors.
247 | target (np.array[B]): Optional batch of B target class labels (bool, or int if
248 | num_classes given) which, if given, triggers an online update if given.
249 | return_probs (bool): Whether to return the classification probability (for each
250 | one-vs-all classifier if num_classes given) instead of the class.
251 |
252 | Returns:
253 | Predicted class per input instance (bool, or int if num_classes given),
254 | or classification probabilities if return_probs set.
255 | """
256 | if input.ndim == 1:
257 | input = torch.unsqueeze(input, dim=0)
258 |
259 | # Base predictions
260 | base_preds = self.base_predictor(input)
261 |
262 | # Default data transform
263 | input = torch.tensor(input, dtype=torch.float32)
264 | base_preds = torch.tensor(base_preds, dtype=torch.float32)
265 | if target is not None:
266 | target = torch.tensor(target)
267 | if torch.cuda.is_available():
268 | input = input.cuda()
269 | base_preds = base_preds.cuda()
270 | if target is not None:
271 | target = target.cuda()
272 |
273 | # Context
274 | context = input
275 |
276 | # Target
277 | if target is not None:
278 | target = nn.functional.one_hot(target.long(), self.num_classes)
279 | if self.num_classes == 2:
280 | target = target[:, 1:]
281 |
282 | # Base logits
283 | base_preds = torch.clamp(base_preds,
284 | min=self.pred_clipping,
285 | max=(1.0 - self.pred_clipping))
286 | logits = torch.log(base_preds / (1.0 - base_preds))
287 | if self.bias:
288 | logits[:, 0] = self.base_bias
289 |
290 | # Layers
291 | for layer in self.layers:
292 | logits = layer.predict(logit=logits,
293 | context=context,
294 | target=target)
295 |
296 | logits = torch.squeeze(logits, dim=1)
297 | if self.num_classes == 2:
298 | logits = logits.squeeze(dim=1)
299 |
300 | if return_probs:
301 | output = torch.sigmoid(logits)
302 | elif self.num_classes == 2:
303 | output = logits > 0
304 | else:
305 | output = torch.argmax(logits, dim=1)
306 |
307 | if torch.cuda.is_available():
308 | output = output.cpu()
309 | return output.numpy()
310 |
311 | def extra_repr(self):
312 | return 'num_classes={}, num_layers={}'.format(self.num_classes,
313 | len(self.layers))
314 |
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/4_GLN_MetaL/pygln/tf/__init__.py:
--------------------------------------------------------------------------------
1 | from pygln.tf.gln import GLN
2 |
3 |
4 | __all__ = ['GLN']
5 |
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/4_GLN_MetaL/pygln/utils.py:
--------------------------------------------------------------------------------
1 | import numpy as np
2 | from scipy.ndimage import interpolation
3 |
4 |
5 | ###################################################
6 | # adopted from https://fsix.github.io/mnist/
7 | def moments(image):
8 | c0, c1 = np.mgrid[:image.shape[0], :
9 | image.shape[1]] # A trick in numPy to create a mesh grid
10 | totalImage = np.sum(image) # sum of pixels
11 | m0 = np.sum(c0 * image) / totalImage # mu_x
12 | m1 = np.sum(c1 * image) / totalImage # mu_y
13 | m00 = np.sum((c0 - m0)**2 * image) / totalImage # var(x)
14 | m11 = np.sum((c1 - m1)**2 * image) / totalImage # var(y)
15 | m01 = np.sum((c0 - m0) * (c1 - m1) * image) / totalImage # covariance(x,y)
16 | mu_vector = np.array([m0, m1
17 | ]) # Notice that these are \mu_x, \mu_y respectively
18 | covariance_matrix = np.array(
19 | [[m00, m01],
20 | [m01, m11]]) # Do you see a similarity between the covariance matrix
21 | return mu_vector, covariance_matrix
22 |
23 |
24 | def deskew(image):
25 | c, v = moments(image)
26 | alpha = v[0, 1] / v[0, 0]
27 | affine = np.array([[1, 0], [alpha, 1]])
28 | ocenter = np.array(image.shape) / 2.0
29 | offset = c - np.dot(affine, ocenter)
30 | return interpolation.affine_transform(image, affine, offset=offset)
31 |
32 |
33 | def deskewAll(X):
34 | currents = []
35 | for i in range(len(X)):
36 | currents.append(deskew(X[i].reshape(28, 28)).flatten())
37 | return np.array(currents)
38 |
39 |
40 | ###################################################
41 |
42 |
43 | def get_mnist(deskewed=True):
44 | from torchvision.datasets import MNIST
45 |
46 | trainset = MNIST('./data', train=True, download=True)
47 | X_train = trainset.data.numpy().reshape(60000, -1).astype(np.float) / 255
48 | if deskewed:
49 | X_train = deskewAll(X_train)
50 | y_train = trainset.targets.numpy()
51 |
52 | testset = MNIST('./data', train=False, download=True)
53 | X_test = testset.data.numpy().reshape(10000, -1).astype(np.float) / 255
54 | if deskewed:
55 | X_test = deskewAll(X_test)
56 | y_test = testset.targets.numpy()
57 |
58 | return X_train, y_train, X_test, y_test
59 |
60 |
61 | def shuffle_data(X, y):
62 | assert X.shape[0] == y.shape[0]
63 | rng = np.random.default_rng()
64 | permutation = rng.permutation(X.shape[0])
65 | return X[permutation, :], y[permutation]
66 |
67 |
68 | def evaluate_mnist(model,
69 | deskewed=True,
70 | batch_size=1,
71 | num_epochs=1,
72 | mnist_class=None):
73 | from tqdm import tqdm
74 |
75 | # get MNIST data as numpy arrays
76 | X_train, y_train, X_test, y_test = get_mnist(deskewed)
77 | # randomly shuffle data
78 | X_train, y_train = shuffle_data(X_train, y_train)
79 | X_test, y_test = shuffle_data(X_test, y_test)
80 |
81 | if mnist_class is not None:
82 | y_train = (y_train == mnist_class).astype(np.int)
83 | y_test = (y_test == mnist_class).astype(np.int)
84 |
85 | accuracy_after_each_epoch = []
86 |
87 | for e in range(num_epochs):
88 | num_batches = int(np.ceil(len(X_train) / batch_size))
89 | for i in tqdm(range(num_batches)):
90 | # get batch
91 | batch_start = i * batch_size
92 | batch_end = batch_start + batch_size
93 | X_batch = X_train[batch_start:batch_end]
94 | y_batch = y_train[batch_start:batch_end]
95 |
96 | # run forward with data
97 | _ = model.predict(X_batch, y_batch)
98 |
99 | # perform inference on test set
100 | num_batches = int(np.ceil(len(X_test) / batch_size))
101 | outputs = []
102 | for i in tqdm(range(num_batches)):
103 | # get batch
104 | batch_start = i * batch_size
105 | batch_end = batch_start + batch_size
106 | X_batch = X_test[batch_start:batch_end]
107 |
108 | # run forward with data
109 | pred = model.predict(X_batch)
110 | outputs.append(pred)
111 |
112 | outputs = np.vstack(outputs)
113 |
114 | # define metrics
115 | outputs = outputs.flatten()
116 | accuracy = 100 * sum(y_test == outputs) / len(y_test)
117 | accuracy_after_each_epoch.append(accuracy)
118 |
119 | if len(accuracy_after_each_epoch) > 1:
120 | return accuracy_after_each_epoch
121 | else:
122 | return accuracy_after_each_epoch[0]
123 |
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/4_GLN_MetaL/utils.ipynb:
--------------------------------------------------------------------------------
1 | /Users/gautamshroff2019/Code/meta-learning-course-notebooks/0_Basic_NNs/utils.ipynb
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/atcold_selected/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tirtharajdash/BITS-G513_MetaLearning/81d62d08a420fb94059e04c2f2e9c7d0af10e151/Meta_Learning_Notebooks/atcold_selected/.DS_Store
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/atcold_selected/__pycache__/plot_lib.cpython-37.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tirtharajdash/BITS-G513_MetaLearning/81d62d08a420fb94059e04c2f2e9c7d0af10e151/Meta_Learning_Notebooks/atcold_selected/__pycache__/plot_lib.cpython-37.pyc
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/atcold_selected/plot_lib.py:
--------------------------------------------------------------------------------
1 | from matplotlib import pyplot as plt
2 | import numpy as np
3 | import torch
4 | from IPython.display import HTML, display
5 |
6 |
7 | def set_default(figsize=(10, 10), dpi=100):
8 | plt.style.use(['dark_background', 'bmh'])
9 | plt.rc('axes', facecolor='k')
10 | plt.rc('figure', facecolor='k')
11 | plt.rc('figure', figsize=figsize, dpi=dpi)
12 |
13 |
14 | def plot_data(X, y, d=0, auto=False, zoom=1):
15 | X = X.cpu()
16 | y = y.cpu()
17 | plt.scatter(X.numpy()[:, 0], X.numpy()[:, 1], c=y, s=20, cmap=plt.cm.Spectral)
18 | plt.axis('square')
19 | plt.axis(np.array((-1.1, 1.1, -1.1, 1.1)) * zoom)
20 | if auto is True: plt.axis('equal')
21 | plt.axis('off')
22 |
23 | _m, _c = 0, '.15'
24 | plt.axvline(0, ymin=_m, color=_c, lw=1, zorder=0)
25 | plt.axhline(0, xmin=_m, color=_c, lw=1, zorder=0)
26 |
27 |
28 | def plot_model(X, y, model):
29 | model.cpu()
30 | mesh = np.arange(-1.1, 1.1, 0.01)
31 | xx, yy = np.meshgrid(mesh, mesh)
32 | with torch.no_grad():
33 | data = torch.from_numpy(np.vstack((xx.reshape(-1), yy.reshape(-1))).T).float()
34 | Z = model(data).detach()
35 | Z = np.argmax(Z, axis=1).reshape(xx.shape)
36 | plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral, alpha=0.3)
37 | plot_data(X, y)
38 |
39 |
40 | def show_scatterplot(X, colors, title=''):
41 | colors = colors.cpu().numpy()
42 | X = X.cpu().numpy()
43 | plt.figure()
44 | plt.axis('equal')
45 | plt.scatter(X[:, 0], X[:, 1], c=colors, s=30)
46 | # plt.grid(True)
47 | plt.title(title)
48 | plt.axis('off')
49 |
50 |
51 | def plot_bases(bases, width=0.04):
52 | bases = bases.cpu()
53 | bases[2:] -= bases[:2]
54 | plt.arrow(*bases[0], *bases[2], width=width, color=(1,0,0), zorder=10, alpha=1., length_includes_head=True)
55 | plt.arrow(*bases[1], *bases[3], width=width, color=(0,1,0), zorder=10, alpha=1., length_includes_head=True)
56 |
57 |
58 | def show_mat(mat, vect, prod, threshold=-1):
59 | # Subplot grid definition
60 | fig, (ax1, ax2, ax3) = plt.subplots(1, 3, sharex=False, sharey=True,
61 | gridspec_kw={'width_ratios':[5,1,1]})
62 | # Plot matrices
63 | cax1 = ax1.matshow(mat.numpy(), clim=(-1, 1))
64 | ax2.matshow(vect.numpy(), clim=(-1, 1))
65 | cax3 = ax3.matshow(prod.numpy(), clim=(threshold, 1))
66 |
67 | # Set titles
68 | ax1.set_title(f'A: {mat.size(0)} \u00D7 {mat.size(1)}')
69 | ax2.set_title(f'a^(i): {vect.numel()}')
70 | ax3.set_title(f'p: {prod.numel()}')
71 |
72 | # Remove xticks for vectors
73 | ax2.set_xticks(tuple())
74 | ax3.set_xticks(tuple())
75 |
76 | # Plot colourbars
77 | fig.colorbar(cax1, ax=ax2)
78 | fig.colorbar(cax3, ax=ax3)
79 |
80 | # Fix y-axis limits
81 | ax1.set_ylim(bottom=max(len(prod), len(vect)) - 0.5)
82 |
83 |
84 | colors = dict(
85 | aqua='#8dd3c7',
86 | yellow='#ffffb3',
87 | lavender='#bebada',
88 | red='#fb8072',
89 | blue='#80b1d3',
90 | orange='#fdb462',
91 | green='#b3de69',
92 | pink='#fccde5',
93 | grey='#d9d9d9',
94 | violet='#bc80bd',
95 | unk1='#ccebc5',
96 | unk2='#ffed6f',
97 | )
98 |
99 |
100 | def _cstr(s, color='black'):
101 | if s == ' ':
102 | return f' '
103 | else:
104 | return f'{s} '
105 |
106 | # print html
107 | def _print_color(t):
108 | display(HTML(''.join([_cstr(ti, color=ci) for ti, ci in t])))
109 |
110 | # get appropriate color for value
111 | def _get_clr(value):
112 | colors = ('#85c2e1', '#89c4e2', '#95cae5', '#99cce6', '#a1d0e8',
113 | '#b2d9ec', '#baddee', '#c2e1f0', '#eff7fb', '#f9e8e8',
114 | '#f9e8e8', '#f9d4d4', '#f9bdbd', '#f8a8a8', '#f68f8f',
115 | '#f47676', '#f45f5f', '#f34343', '#f33b3b', '#f42e2e')
116 | value = int((value * 100) / 5)
117 | if value == len(colors): value -= 1 # fixing bugs...
118 | return colors[value]
119 |
120 | def _visualise_values(output_values, result_list):
121 | text_colours = []
122 | for i in range(len(output_values)):
123 | text = (result_list[i], _get_clr(output_values[i]))
124 | text_colours.append(text)
125 | _print_color(text_colours)
126 |
127 | def print_colourbar():
128 | color_range = torch.linspace(-2.5, 2.5, 20)
129 | to_print = [(f'{x:.2f}', _get_clr((x+2.5)/5)) for x in color_range]
130 | _print_color(to_print)
131 |
132 |
133 | # Let's only focus on the last time step for now
134 | # First, the cell state (Long term memory)
135 | def plot_state(data, state, b, decoder):
136 | actual_data = decoder(data[b, :, :].numpy())
137 | seq_len = len(actual_data)
138 | seq_len_w_pad = len(state)
139 | for s in range(state.size(2)):
140 | states = torch.sigmoid(state[:, b, s])
141 | _visualise_values(states[seq_len_w_pad - seq_len:], list(actual_data))
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/atcold_selected/sequential_tasks.py:
--------------------------------------------------------------------------------
1 | import numpy as np
2 | import six
3 |
4 | def pad_sequences(sequences, maxlen=None, dtype='int32',
5 | padding='pre', truncating='pre', value=0.):
6 | if not hasattr(sequences, '__len__'):
7 | raise ValueError('`sequences` must be iterable.')
8 | lengths = []
9 | for x in sequences:
10 | if not hasattr(x, '__len__'):
11 | raise ValueError('`sequences` must be a list of iterables. '
12 | 'Found non-iterable: ' + str(x))
13 | lengths.append(len(x))
14 |
15 | num_samples = len(sequences)
16 | if maxlen is None:
17 | maxlen = np.max(lengths)
18 |
19 | # take the sample shape from the first non empty sequence
20 | # checking for consistency in the main loop below.
21 | sample_shape = tuple()
22 | for s in sequences:
23 | if len(s) > 0:
24 | sample_shape = np.asarray(s).shape[1:]
25 | break
26 |
27 | is_dtype_str = np.issubdtype(dtype, np.str_) or np.issubdtype(dtype, np.unicode_)
28 | if isinstance(value, six.string_types) and dtype != object and not is_dtype_str:
29 | raise ValueError("`dtype` {} is not compatible with `value`'s type: {}\n"
30 | "You should set `dtype=object` for variable length strings."
31 | .format(dtype, type(value)))
32 |
33 | x = np.full((num_samples, maxlen) + sample_shape, value, dtype=dtype)
34 | for idx, s in enumerate(sequences):
35 | if not len(s):
36 | continue # empty list/array was found
37 | if truncating == 'pre':
38 | trunc = s[-maxlen:]
39 | elif truncating == 'post':
40 | trunc = s[:maxlen]
41 | else:
42 | raise ValueError('Truncating type "%s" '
43 | 'not understood' % truncating)
44 |
45 | # check `trunc` has expected shape
46 | trunc = np.asarray(trunc, dtype=dtype)
47 | if trunc.shape[1:] != sample_shape:
48 | raise ValueError('Shape of sample %s of sequence at position %s '
49 | 'is different from expected shape %s' %
50 | (trunc.shape[1:], idx, sample_shape))
51 |
52 | if padding == 'post':
53 | x[idx, :len(trunc)] = trunc
54 | elif padding == 'pre':
55 | x[idx, -len(trunc):] = trunc
56 | else:
57 | raise ValueError('Padding type "%s" not understood' % padding)
58 | return x
59 |
60 | def to_categorical(y, num_classes=None, dtype='float32'):
61 | y = np.array(y, dtype='int')
62 | input_shape = y.shape
63 | if input_shape and input_shape[-1] == 1 and len(input_shape) > 1:
64 | input_shape = tuple(input_shape[:-1])
65 | y = y.ravel()
66 | if not num_classes:
67 | num_classes = np.max(y) + 1
68 | n = y.shape[0]
69 | categorical = np.zeros((n, num_classes), dtype=dtype)
70 | categorical[np.arange(n), y] = 1
71 | output_shape = input_shape + (num_classes,)
72 | categorical = np.reshape(categorical, output_shape)
73 | return categorical
74 |
75 | class EchoData():
76 |
77 | def __init__(self, series_length=40000, batch_size=32,
78 | echo_step=3, truncated_length=10, seed=None):
79 |
80 | self.series_length = series_length
81 | self.truncated_length = truncated_length
82 | self.n_batches = series_length//truncated_length
83 |
84 | self.echo_step = echo_step
85 | self.batch_size = batch_size
86 | if seed is not None:
87 | np.random.seed(seed)
88 | self.x_batch = None
89 | self.y_batch = None
90 | self.x_chunks = []
91 | self.y_chunks = []
92 | self.generate_new_series()
93 | self.prepare_batches()
94 |
95 | def __getitem__(self, index):
96 | if index == 0:
97 | self.generate_new_series()
98 | self.prepare_batches()
99 | return self.x_chunks[index], self.y_chunks[index]
100 |
101 | def __len__(self):
102 | return self.n_batches
103 |
104 | def generate_new_series(self):
105 | x = np.random.choice(
106 | 2,
107 | size=(self.batch_size, self.series_length),
108 | p=[0.5, 0.5])
109 | y = np.roll(x, self.echo_step, axis=1)
110 | y[:, 0:self.echo_step] = 0
111 | self.x_batch = x
112 | self.y_batch = y
113 |
114 | def prepare_batches(self):
115 | x = np.expand_dims(self.x_batch, axis=-1)
116 | y = np.expand_dims(self.y_batch, axis=-1)
117 | self.x_chunks = np.split(x, self.n_batches, axis=1)
118 | self.y_chunks = np.split(y, self.n_batches, axis=1)
119 |
120 | class TemporalOrderExp6aSequence():
121 | """
122 | From Hochreiter&Schmidhuber(1997):
123 |
124 | The goal is to classify sequences. Elements and targets are represented locally
125 | (input vectors with only one non-zero bit). The sequence starts with an E, ends
126 | with a B (the "trigger symbol") and otherwise consists of randomly chosen symbols
127 | from the set {a, b, c, d} except for two elements at positions t1 and t2 that are
128 | either X or Y . The sequence length is randomly chosen between 100 and 110, t1 is
129 | randomly chosen between 10 and 20, and t2 is randomly chosen between 50 and 60.
130 | There are 4 sequence classes Q, R, S, U which depend on the temporal order of X and Y.
131 | The rules are:
132 | X, X -> Q,
133 | X, Y -> R,
134 | Y , X -> S,
135 | Y , Y -> U.
136 |
137 | """
138 |
139 | def __init__(self, length_range=(100, 111), t1_range=(10, 21), t2_range=(50, 61),
140 | batch_size=32, seed=None):
141 |
142 | self.classes = ['Q', 'R', 'S', 'U']
143 | self.n_classes = len(self.classes)
144 |
145 | self.relevant_symbols = ['X', 'Y']
146 | self.distraction_symbols = ['a', 'b', 'c', 'd']
147 | self.start_symbol = 'B'
148 | self.end_symbol = 'E'
149 |
150 | self.length_range = length_range
151 | self.t1_range = t1_range
152 | self.t2_range = t2_range
153 | self.batch_size = batch_size
154 |
155 | if seed is not None:
156 | np.random.seed(seed)
157 |
158 | all_symbols = self.relevant_symbols + self.distraction_symbols + \
159 | [self.start_symbol] + [self.end_symbol]
160 | self.n_symbols = len(all_symbols)
161 | self.s_to_idx = {s: n for n, s in enumerate(all_symbols)}
162 | self.idx_to_s = {n: s for n, s in enumerate(all_symbols)}
163 |
164 | self.c_to_idx = {c: n for n, c in enumerate(self.classes)}
165 | self.idx_to_c = {n: c for n, c in enumerate(self.classes)}
166 |
167 | def generate_pair(self):
168 | length = np.random.randint(self.length_range[0], self.length_range[1])
169 | t1 = np.random.randint(self.t1_range[0], self.t1_range[1])
170 | t2 = np.random.randint(self.t2_range[0], self.t2_range[1])
171 |
172 | x = np.random.choice(self.distraction_symbols, length)
173 | x[0] = self.start_symbol
174 | x[-1] = self.end_symbol
175 |
176 | y = np.random.choice(self.classes)
177 | if y == 'Q':
178 | x[t1], x[t2] = self.relevant_symbols[0], self.relevant_symbols[0]
179 | elif y == 'R':
180 | x[t1], x[t2] = self.relevant_symbols[0], self.relevant_symbols[1]
181 | elif y == 'S':
182 | x[t1], x[t2] = self.relevant_symbols[1], self.relevant_symbols[0]
183 | else:
184 | x[t1], x[t2] = self.relevant_symbols[1], self.relevant_symbols[1]
185 |
186 | return ''.join(x), y
187 |
188 | # encoding/decoding single instance version
189 |
190 | def encode_x(self, x):
191 | idx_x = [self.s_to_idx[s] for s in x]
192 | return to_categorical(idx_x, num_classes=self.n_symbols)
193 |
194 | def encode_y(self, y):
195 | idx_y = self.c_to_idx[y]
196 | return to_categorical(idx_y, num_classes=self.n_classes)
197 |
198 | def decode_x(self, x):
199 | x = x[np.sum(x, axis=1) > 0] # remove padding
200 | return ''.join([self.idx_to_s[pos] for pos in np.argmax(x, axis=1)])
201 |
202 | def decode_y(self, y):
203 | return self.idx_to_c[np.argmax(y)]
204 |
205 | # encoding/decoding batch versions
206 |
207 | def encode_x_batch(self, x_batch):
208 | return pad_sequences([self.encode_x(x) for x in x_batch],
209 | maxlen=self.length_range[1])
210 |
211 | def encode_y_batch(self, y_batch):
212 | return np.array([self.encode_y(y) for y in y_batch])
213 |
214 | def decode_x_batch(self, x_batch):
215 | return [self.decode_x(x) for x in x_batch]
216 |
217 | def decode_y_batch(self, y_batch):
218 | return [self.idx_to_c[pos] for pos in np.argmax(y_batch, axis=1)]
219 |
220 | def __len__(self):
221 | """ Let's assume 1000 sequences as the size of data. """
222 | return int(1000. / self.batch_size)
223 |
224 | def __getitem__(self, index):
225 | batch_x, batch_y = [], []
226 | for _ in range(self.batch_size):
227 | x, y = self.generate_pair()
228 | batch_x.append(x)
229 | batch_y.append(y)
230 | return self.encode_x_batch(batch_x), self.encode_y_batch(batch_y)
231 |
232 | class DifficultyLevel:
233 | """ On HARD, settings are identical to the original settings from the '97 paper."""
234 | EASY, NORMAL, MODERATE, HARD, NIGHTMARE = range(5)
235 |
236 | @staticmethod
237 | def get_predefined_generator(difficulty_level, batch_size=32, seed=8382):
238 | EASY = TemporalOrderExp6aSequence.DifficultyLevel.EASY
239 | NORMAL = TemporalOrderExp6aSequence.DifficultyLevel.NORMAL
240 | MODERATE = TemporalOrderExp6aSequence.DifficultyLevel.MODERATE
241 | HARD = TemporalOrderExp6aSequence.DifficultyLevel.HARD
242 |
243 | if difficulty_level == EASY:
244 | length_range = (7, 9)
245 | t1_range = (1, 3)
246 | t2_range = (4, 6)
247 | elif difficulty_level == NORMAL:
248 | length_range = (30, 41)
249 | t1_range = (2, 6)
250 | t2_range = (20, 28)
251 | elif difficulty_level == MODERATE:
252 | length_range = (60, 81)
253 | t1_range = (10, 21)
254 | t2_range = (45, 55)
255 | elif difficulty_level == HARD:
256 | length_range = (100, 111)
257 | t1_range = (10, 21)
258 | t2_range = (50, 61)
259 | else:
260 | length_range = (300, 501)
261 | t1_range = (10, 81)
262 | t2_range = (250, 291)
263 | return TemporalOrderExp6aSequence(length_range, t1_range, t2_range,
264 | batch_size, seed)
265 |
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/data-gen-explorations-for-course/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tirtharajdash/BITS-G513_MetaLearning/81d62d08a420fb94059e04c2f2e9c7d0af10e151/Meta_Learning_Notebooks/data-gen-explorations-for-course/.DS_Store
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/data-gen-explorations-for-course/.ipynb_checkpoints/comp-checkpoint.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [],
3 | "metadata": {},
4 | "nbformat": 4,
5 | "nbformat_minor": 4
6 | }
7 |
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/data-gen-explorations-for-course/.ipynb_checkpoints/models-checkpoint.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 3,
6 | "metadata": {},
7 | "outputs": [],
8 | "source": [
9 | "import torch.nn as nn\n",
10 | "import torch"
11 | ]
12 | },
13 | {
14 | "cell_type": "code",
15 | "execution_count": 30,
16 | "metadata": {},
17 | "outputs": [],
18 | "source": [
19 | "class MyGRU1(nn.Module):\n",
20 | " def __init__(self):\n",
21 | " super(MyGRU1,self).__init__()\n",
22 | " self.rnn = nn.GRU(1, 5, 3,batch_first=True)\n",
23 | " self.linear = nn.Linear(5,3)\n",
24 | " self.logsoft = nn.LogSoftmax(dim=-1)\n",
25 | " super\n",
26 | " def forward(self,X):\n",
27 | " h = torch.rand(3,X.shape[0],X.shape[1])\n",
28 | " H = self.rnn(X,h)[1]\n",
29 | " G = self.logsoft(self.linear(H[-1,:,:]))\n",
30 | " return G"
31 | ]
32 | },
33 | {
34 | "cell_type": "code",
35 | "execution_count": 32,
36 | "metadata": {},
37 | "outputs": [
38 | {
39 | "ename": "RuntimeError",
40 | "evalue": "Expected hidden size (3, 2, 5), got [3, 10, 5]",
41 | "output_type": "error",
42 | "traceback": [
43 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
44 | "\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)",
45 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mrnn\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnn\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mGRU\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mbatch_first\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mmygru\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mMyGRU1\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mmygru\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mX\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
46 | "\u001b[0;32m//anaconda3/envs/py3gen/lib/python3.7/site-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_call_impl\u001b[0;34m(self, *input, **kwargs)\u001b[0m\n\u001b[1;32m 725\u001b[0m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_slow_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 726\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 727\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 728\u001b[0m for hook in itertools.chain(\n\u001b[1;32m 729\u001b[0m \u001b[0m_global_forward_hooks\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
47 | "\u001b[0;32m\u001b[0m in \u001b[0;36mforward\u001b[0;34m(self, X)\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mX\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0mh\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrand\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mX\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 10\u001b[0;31m \u001b[0mH\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrnn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mX\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mh\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 11\u001b[0m \u001b[0mG\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlogsoft\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlinear\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mH\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mG\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
48 | "\u001b[0;32m//anaconda3/envs/py3gen/lib/python3.7/site-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_call_impl\u001b[0;34m(self, *input, **kwargs)\u001b[0m\n\u001b[1;32m 725\u001b[0m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_slow_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 726\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 727\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 728\u001b[0m for hook in itertools.chain(\n\u001b[1;32m 729\u001b[0m \u001b[0m_global_forward_hooks\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
49 | "\u001b[0;32m//anaconda3/envs/py3gen/lib/python3.7/site-packages/torch/nn/modules/rnn.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(self, input, hx)\u001b[0m\n\u001b[1;32m 735\u001b[0m \u001b[0mhx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpermute_hidden\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mhx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msorted_indices\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 736\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 737\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcheck_forward_args\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbatch_sizes\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 738\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mbatch_sizes\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 739\u001b[0m result = _VF.gru(input, hx, self._flat_weights, self.bias, self.num_layers,\n",
50 | "\u001b[0;32m//anaconda3/envs/py3gen/lib/python3.7/site-packages/torch/nn/modules/rnn.py\u001b[0m in \u001b[0;36mcheck_forward_args\u001b[0;34m(self, input, hidden, batch_sizes)\u001b[0m\n\u001b[1;32m 200\u001b[0m \u001b[0mexpected_hidden_size\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_expected_hidden_size\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbatch_sizes\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 201\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 202\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcheck_hidden_size\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mhidden\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mexpected_hidden_size\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 203\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 204\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mpermute_hidden\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhx\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mTensor\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mpermutation\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mOptional\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mTensor\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
51 | "\u001b[0;32m//anaconda3/envs/py3gen/lib/python3.7/site-packages/torch/nn/modules/rnn.py\u001b[0m in \u001b[0;36mcheck_hidden_size\u001b[0;34m(self, hx, expected_hidden_size, msg)\u001b[0m\n\u001b[1;32m 194\u001b[0m msg: str = 'Expected hidden size {}, got {}') -> None:\n\u001b[1;32m 195\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mhx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msize\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0mexpected_hidden_size\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 196\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mRuntimeError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmsg\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mexpected_hidden_size\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlist\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mhx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msize\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 197\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 198\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mcheck_forward_args\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mTensor\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhidden\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mTensor\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbatch_sizes\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mOptional\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mTensor\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
52 | "\u001b[0;31mRuntimeError\u001b[0m: Expected hidden size (3, 2, 5), got [3, 10, 5]"
53 | ]
54 | }
55 | ],
56 | "source": [
57 | "X = torch.rand(2,10,1)\n",
58 | "rnn = nn.GRU(1, 5, 3,batch_first=True)\n",
59 | "mygru = MyGRU1()\n",
60 | "mygru(X)"
61 | ]
62 | },
63 | {
64 | "cell_type": "code",
65 | "execution_count": null,
66 | "metadata": {},
67 | "outputs": [],
68 | "source": []
69 | },
70 | {
71 | "cell_type": "code",
72 | "execution_count": null,
73 | "metadata": {},
74 | "outputs": [],
75 | "source": []
76 | }
77 | ],
78 | "metadata": {
79 | "kernelspec": {
80 | "display_name": "Python 3",
81 | "language": "python",
82 | "name": "python3"
83 | },
84 | "language_info": {
85 | "codemirror_mode": {
86 | "name": "ipython",
87 | "version": 3
88 | },
89 | "file_extension": ".py",
90 | "mimetype": "text/x-python",
91 | "name": "python",
92 | "nbconvert_exporter": "python",
93 | "pygments_lexer": "ipython3",
94 | "version": "3.7.9"
95 | }
96 | },
97 | "nbformat": 4,
98 | "nbformat_minor": 2
99 | }
100 |
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/data-gen-explorations-for-course/.ipynb_checkpoints/test_features-checkpoint.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 7,
6 | "metadata": {},
7 | "outputs": [],
8 | "source": [
9 | "from IPython.display import HTML\n",
10 | "import random"
11 | ]
12 | },
13 | {
14 | "cell_type": "code",
15 | "execution_count": 8,
16 | "metadata": {},
17 | "outputs": [
18 | {
19 | "data": {
20 | "text/html": [
21 | "\n",
33 | "Click to show/hide here code."
34 | ],
35 | "text/plain": [
36 | ""
37 | ]
38 | },
39 | "execution_count": 8,
40 | "metadata": {},
41 | "output_type": "execute_result"
42 | }
43 | ],
44 | "source": [
45 | "def addToggle():\n",
46 | " str = '''\n",
58 | "Click to show/hide here code.'''\n",
59 | " return str\n",
60 | "HTML(addToggle())"
61 | ]
62 | },
63 | {
64 | "cell_type": "code",
65 | "execution_count": 30,
66 | "metadata": {},
67 | "outputs": [
68 | {
69 | "ename": "SyntaxError",
70 | "evalue": "EOL while scanning string literal (, line 5)",
71 | "output_type": "error",
72 | "traceback": [
73 | "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m5\u001b[0m\n\u001b[0;31m toggle_text = '' + message + ' + ' (show/hide)' # text shown on toggle link\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m EOL while scanning string literal\n"
74 | ]
75 | }
76 | ],
77 | "source": [
78 | "def hide_toggle(for_next=False, message=''):\n",
79 | " this_cell = \"\"\"$('div.cell.code_cell.rendered.selected')\"\"\"\n",
80 | " next_cell = this_cell + '.next()'\n",
81 | "\n",
82 | " toggle_text = '' + message + '' + ' (show/hide)' # text shown on toggle link\n",
83 | " target_cell = this_cell # target cell to control with toggle\n",
84 | " js_hide_current = '' # bit of JS to permanently hide code in current cell (only when toggling next cell)\n",
85 | "\n",
86 | " if for_next:\n",
87 | " target_cell = next_cell\n",
88 | " toggle_text += ' next cell'\n",
89 | " js_hide_current = this_cell + '.find(\"div.input\").hide();'\n",
90 | "\n",
91 | " js_f_name = 'code_toggle_{}'.format(str(random.randint(1,2**64)))\n",
92 | "\n",
93 | " html = \"\"\"\n",
94 | " \n",
101 | "\n",
102 | " {toggle_text}\n",
103 | " \"\"\".format(\n",
104 | " f_name=js_f_name,\n",
105 | " cell_selector=target_cell,\n",
106 | " js_hide_current=js_hide_current, \n",
107 | " toggle_text=toggle_text\n",
108 | " )\n",
109 | "\n",
110 | " return HTML(html)"
111 | ]
112 | },
113 | {
114 | "cell_type": "markdown",
115 | "metadata": {},
116 | "source": [
117 | "Hello"
118 | ]
119 | },
120 | {
121 | "cell_type": "code",
122 | "execution_count": 28,
123 | "metadata": {},
124 | "outputs": [
125 | {
126 | "data": {
127 | "text/html": [
128 | "\n",
129 | " \n",
136 | "\n",
137 | " Code for printing 1000 (show/hide)\n",
138 | " "
139 | ],
140 | "text/plain": [
141 | ""
142 | ]
143 | },
144 | "execution_count": 28,
145 | "metadata": {},
146 | "output_type": "execute_result"
147 | }
148 | ],
149 | "source": [
150 | "#print(1000)\n",
151 | "a = 10\n",
152 | "hide_toggle(message=\"Code for printing 1000\")"
153 | ]
154 | },
155 | {
156 | "cell_type": "code",
157 | "execution_count": 10,
158 | "metadata": {},
159 | "outputs": [
160 | {
161 | "data": {
162 | "text/html": [
163 | "\n",
175 | "Click to show/hide here code."
176 | ],
177 | "text/plain": [
178 | ""
179 | ]
180 | },
181 | "execution_count": 10,
182 | "metadata": {},
183 | "output_type": "execute_result"
184 | }
185 | ],
186 | "source": [
187 | "HTML(addToggle())"
188 | ]
189 | },
190 | {
191 | "cell_type": "code",
192 | "execution_count": null,
193 | "metadata": {},
194 | "outputs": [],
195 | "source": []
196 | }
197 | ],
198 | "metadata": {
199 | "kernelspec": {
200 | "display_name": "Python 3",
201 | "language": "python",
202 | "name": "python3"
203 | },
204 | "language_info": {
205 | "codemirror_mode": {
206 | "name": "ipython",
207 | "version": 3
208 | },
209 | "file_extension": ".py",
210 | "mimetype": "text/x-python",
211 | "name": "python",
212 | "nbconvert_exporter": "python",
213 | "pygments_lexer": "ipython3",
214 | "version": "3.7.9"
215 | }
216 | },
217 | "nbformat": 4,
218 | "nbformat_minor": 4
219 | }
220 |
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/data-gen-explorations-for-course/comp.csv:
--------------------------------------------------------------------------------
1 | ,0,1,2
2 | 0,1,2,3
3 | 1,4,5,6
4 |
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/data-gen-explorations-for-course/comp.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 1,
6 | "metadata": {},
7 | "outputs": [],
8 | "source": [
9 | "import pandas as pd"
10 | ]
11 | },
12 | {
13 | "cell_type": "code",
14 | "execution_count": 2,
15 | "metadata": {},
16 | "outputs": [],
17 | "source": [
18 | "df = pd.DataFrame([[1,2,3],[4,5,6]])"
19 | ]
20 | },
21 | {
22 | "cell_type": "code",
23 | "execution_count": 3,
24 | "metadata": {},
25 | "outputs": [],
26 | "source": [
27 | "df.to_csv('./comp.csv')"
28 | ]
29 | },
30 | {
31 | "cell_type": "code",
32 | "execution_count": null,
33 | "metadata": {},
34 | "outputs": [],
35 | "source": []
36 | }
37 | ],
38 | "metadata": {
39 | "kernelspec": {
40 | "display_name": "Python 3",
41 | "language": "python",
42 | "name": "python3"
43 | },
44 | "language_info": {
45 | "codemirror_mode": {
46 | "name": "ipython",
47 | "version": 3
48 | },
49 | "file_extension": ".py",
50 | "mimetype": "text/x-python",
51 | "name": "python",
52 | "nbconvert_exporter": "python",
53 | "pygments_lexer": "ipython3",
54 | "version": "3.7.9"
55 | }
56 | },
57 | "nbformat": 4,
58 | "nbformat_minor": 4
59 | }
60 |
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/data-gen-explorations-for-course/comp.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 | # coding: utf-8
3 |
4 | # In[1]:
5 |
6 |
7 | import pandas as pd
8 |
9 |
10 | # In[2]:
11 |
12 |
13 | df = pd.DataFrame([[1,2,3],[4,5,6]])
14 |
15 |
16 | # In[3]:
17 |
18 |
19 | df.to_csv('./comp.csv')
20 |
21 |
22 | # In[ ]:
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/data-gen-explorations-for-course/models.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 3,
6 | "metadata": {},
7 | "outputs": [],
8 | "source": [
9 | "import torch.nn as nn\n",
10 | "import torch"
11 | ]
12 | },
13 | {
14 | "cell_type": "code",
15 | "execution_count": 40,
16 | "metadata": {},
17 | "outputs": [],
18 | "source": [
19 | "class MyGRU1(nn.Module):\n",
20 | " def __init__(self,n_features=1,dim=5,n_layers=3,n_classes=2):\n",
21 | " super(MyGRU1,self).__init__()\n",
22 | " self.n_layers = n_layers\n",
23 | " self.dim = dim\n",
24 | " self.rnn = nn.GRU(n_features, dim, n_layers,batch_first=True)\n",
25 | " self.linear = nn.Linear(dim,n_classes)\n",
26 | " self.logsoft = nn.LogSoftmax(dim=-1)\n",
27 | " super\n",
28 | " def forward(self,X):\n",
29 | " h = torch.randn(self.n_layers,X.shape[0],self.dim)\n",
30 | " H = self.rnn(X,h)[1]\n",
31 | " G = self.logsoft(self.linear(H[-1,:,:]))\n",
32 | " return G"
33 | ]
34 | },
35 | {
36 | "cell_type": "code",
37 | "execution_count": 42,
38 | "metadata": {},
39 | "outputs": [
40 | {
41 | "data": {
42 | "text/plain": [
43 | "tensor([[-0.7647, -0.6263],\n",
44 | " [-0.7669, -0.6245]], grad_fn=)"
45 | ]
46 | },
47 | "execution_count": 42,
48 | "metadata": {},
49 | "output_type": "execute_result"
50 | }
51 | ],
52 | "source": [
53 | "X = torch.randn(2,10,1)\n",
54 | "rnn = nn.GRU(1, 5, 3,batch_first=True)\n",
55 | "mygru = MyGRU1(1,10,35,2)\n",
56 | "mygru(X)"
57 | ]
58 | },
59 | {
60 | "cell_type": "code",
61 | "execution_count": null,
62 | "metadata": {},
63 | "outputs": [],
64 | "source": []
65 | },
66 | {
67 | "cell_type": "code",
68 | "execution_count": null,
69 | "metadata": {},
70 | "outputs": [],
71 | "source": []
72 | }
73 | ],
74 | "metadata": {
75 | "kernelspec": {
76 | "display_name": "Python 3",
77 | "language": "python",
78 | "name": "python3"
79 | },
80 | "language_info": {
81 | "codemirror_mode": {
82 | "name": "ipython",
83 | "version": 3
84 | },
85 | "file_extension": ".py",
86 | "mimetype": "text/x-python",
87 | "name": "python",
88 | "nbconvert_exporter": "python",
89 | "pygments_lexer": "ipython3",
90 | "version": "3.7.9"
91 | }
92 | },
93 | "nbformat": 4,
94 | "nbformat_minor": 2
95 | }
96 |
--------------------------------------------------------------------------------
/Meta_Learning_Notebooks/data-gen-explorations-for-course/test_features.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 7,
6 | "metadata": {},
7 | "outputs": [],
8 | "source": [
9 | "from IPython.display import HTML\n",
10 | "import random"
11 | ]
12 | },
13 | {
14 | "cell_type": "code",
15 | "execution_count": 8,
16 | "metadata": {},
17 | "outputs": [
18 | {
19 | "data": {
20 | "text/html": [
21 | "\n",
33 | "Click to show/hide here code."
34 | ],
35 | "text/plain": [
36 | ""
37 | ]
38 | },
39 | "execution_count": 8,
40 | "metadata": {},
41 | "output_type": "execute_result"
42 | }
43 | ],
44 | "source": [
45 | "def addToggle():\n",
46 | " str = '''\n",
58 | "Click to show/hide here code.'''\n",
59 | " return str\n",
60 | "HTML(addToggle())"
61 | ]
62 | },
63 | {
64 | "cell_type": "code",
65 | "execution_count": 33,
66 | "metadata": {},
67 | "outputs": [
68 | {
69 | "data": {
70 | "text/html": [
71 | "\n",
72 | " \n",
79 | "\n",
80 | " Code for toggling cell visibility (show/hide)\n",
81 | " "
82 | ],
83 | "text/plain": [
84 | ""
85 | ]
86 | },
87 | "execution_count": 33,
88 | "metadata": {},
89 | "output_type": "execute_result"
90 | }
91 | ],
92 | "source": [
93 | "def hide_toggle(for_next=False, message=''):\n",
94 | " this_cell = \"\"\"$('div.cell.code_cell.rendered.selected')\"\"\"\n",
95 | " next_cell = this_cell + '.next()'\n",
96 | "\n",
97 | " toggle_text = '' + message + '' + ' (show/hide)' # text shown on toggle link\n",
98 | " target_cell = this_cell # target cell to control with toggle\n",
99 | " js_hide_current = '' # bit of JS to permanently hide code in current cell (only when toggling next cell)\n",
100 | "\n",
101 | " if for_next:\n",
102 | " target_cell = next_cell\n",
103 | " toggle_text += ' next cell'\n",
104 | " js_hide_current = this_cell + '.find(\"div.input\").hide();'\n",
105 | "\n",
106 | " js_f_name = 'code_toggle_{}'.format(str(random.randint(1,2**64)))\n",
107 | "\n",
108 | " html = \"\"\"\n",
109 | " \n",
116 | "\n",
117 | " {toggle_text}\n",
118 | " \"\"\".format(\n",
119 | " f_name=js_f_name,\n",
120 | " cell_selector=target_cell,\n",
121 | " js_hide_current=js_hide_current, \n",
122 | " toggle_text=toggle_text\n",
123 | " )\n",
124 | "\n",
125 | " return HTML(html)\n",
126 | "hide_toggle(message='Code for toggling cell visibility')"
127 | ]
128 | },
129 | {
130 | "cell_type": "markdown",
131 | "metadata": {},
132 | "source": [
133 | "Hello"
134 | ]
135 | },
136 | {
137 | "cell_type": "code",
138 | "execution_count": 32,
139 | "metadata": {},
140 | "outputs": [
141 | {
142 | "data": {
143 | "text/html": [
144 | "\n",
145 | " \n",
152 | "\n",
153 | " Code for printing 1000 (show/hide)\n",
154 | " "
155 | ],
156 | "text/plain": [
157 | ""
158 | ]
159 | },
160 | "execution_count": 32,
161 | "metadata": {},
162 | "output_type": "execute_result"
163 | }
164 | ],
165 | "source": [
166 | "#print(1000)\n",
167 | "a = 10\n",
168 | "hide_toggle(message=\"Code for printing 1000\")"
169 | ]
170 | },
171 | {
172 | "cell_type": "code",
173 | "execution_count": 10,
174 | "metadata": {},
175 | "outputs": [
176 | {
177 | "data": {
178 | "text/html": [
179 | "\n",
191 | "Click to show/hide here code."
192 | ],
193 | "text/plain": [
194 | ""
195 | ]
196 | },
197 | "execution_count": 10,
198 | "metadata": {},
199 | "output_type": "execute_result"
200 | }
201 | ],
202 | "source": [
203 | "HTML(addToggle())"
204 | ]
205 | },
206 | {
207 | "cell_type": "code",
208 | "execution_count": null,
209 | "metadata": {},
210 | "outputs": [],
211 | "source": []
212 | }
213 | ],
214 | "metadata": {
215 | "kernelspec": {
216 | "display_name": "Python 3",
217 | "language": "python",
218 | "name": "python3"
219 | },
220 | "language_info": {
221 | "codemirror_mode": {
222 | "name": "ipython",
223 | "version": 3
224 | },
225 | "file_extension": ".py",
226 | "mimetype": "text/x-python",
227 | "name": "python",
228 | "nbconvert_exporter": "python",
229 | "pygments_lexer": "ipython3",
230 | "version": "3.7.9"
231 | }
232 | },
233 | "nbformat": 4,
234 | "nbformat_minor": 4
235 | }
236 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Meta Learning (BITS G513)
2 |
3 | Meta Learning Course Material (Spring 2021, at BITS-Goa)
4 |
5 | ## Week 1
6 | ### Tutorial
7 | - [Slides](https://github.com/tirtharajdash/BITS-G513_MetaLearning/blob/main/Tutorials/Week1/main.pdf)
8 | - [Summary](https://github.com/tirtharajdash/BITS-G513_MetaLearning/blob/main/Tutorials/Week1/week1_summary.pdf)
9 |
10 | ### Lab
11 | - [1A: Basic Gradient Computation. Leaf and Non-Leaf Variables](https://github.com/tirtharajdash/BITS-G513_MetaLearning/blob/main/Labs/Lab_1A.ipynb) [](https://colab.research.google.com/github/tirtharajdash/BITS-G513_MetaLearning/blob/main/Labs/Lab_1A.ipynb)
12 |
13 | - [1B: Simple Classification and Regression](https://github.com/tirtharajdash/BITS-G513_MetaLearning/blob/main/Labs/Lab_1B.ipynb) [](https://colab.research.google.com/github/tirtharajdash/BITS-G513_MetaLearning/blob/main/Labs/Lab_1B.ipynb)
14 |
15 |
16 | ## Week 2
17 | ### Tutorial
18 | - [Slides](https://github.com/tirtharajdash/BITS-G513_MetaLearning/blob/main/Tutorials/Week2/main.pdf)
19 | - [Summary](https://github.com/tirtharajdash/BITS-G513_MetaLearning/blob/main/Tutorials/Week2/week2_summary.pdf)
20 |
21 | ### Lab
22 | - [2A: Deep Learning - Gradient Descent and Backpropagation](https://github.com/tirtharajdash/BITS-G513_MetaLearning/blob/main/Labs/Lab_2A.ipynb) [](https://colab.research.google.com/github/tirtharajdash/BITS-G513_MetaLearning/blob/main/Labs/Lab_2A.ipynb)
23 |
24 | - [2B: Deep Learning - With Pytorch ](https://github.com/tirtharajdash/BITS-G513_MetaLearning/blob/main/Labs/Lab_2B.ipynb) [](https://colab.research.google.com/github/tirtharajdash/BITS-G513_MetaLearning/blob/main/Labs/Lab_2B.ipynb)
25 |
26 | ## Week 3
27 | ### Tutorial
28 | - [Slides](https://github.com/tirtharajdash/BITS-G513_MetaLearning/blob/main/Tutorials/Week3/CNN%20Examples_TD.pdf)
29 | - [Summary](https://github.com/tirtharajdash/BITS-G513_MetaLearning/blob/main/Tutorials/Week3/week3_summary.pdf)
30 |
31 | ### Lab
32 | - [3A: Introduction to Convolutions and 1D Convolutions](https://github.com/tirtharajdash/BITS-G513_MetaLearning/blob/main/Labs/Lab_3A.ipynb) [](https://colab.research.google.com/github/tirtharajdash/BITS-G513_MetaLearning/blob/main/Labs/Lab_3A.ipynb)
33 |
34 | - [3B: CNNs and 2D Convolutions ](https://github.com/tirtharajdash/BITS-G513_MetaLearning/blob/main/Labs/Lab_3B.ipynb) [](https://colab.research.google.com/github/tirtharajdash/BITS-G513_MetaLearning/blob/main/Labs/Lab_3B.ipynb)
35 |
36 | ## Week 4
37 | ### Tutorial
38 | - [Slides](https://github.com/tirtharajdash/BITS-G513_MetaLearning/blob/main/Tutorials/Week4/cnn_backprop.pdf)
39 | - [Summary](https://github.com/tirtharajdash/BITS-G513_MetaLearning/blob/main/Tutorials/Week4/week4_summary.pdf)
40 |
41 | ### Lab
42 | - [4: Convolution on Real Audio, 1D CNN based Emotion Classification](https://github.com/tirtharajdash/BITS-G513_MetaLearning/blob/main/Labs/Lab_4A.ipynb) [](https://colab.research.google.com/github/tirtharajdash/BITS-G513_MetaLearning/blob/main/Labs/Lab_4A.ipynb)
43 |
44 |
45 | ## Week 5
46 | ### Tutorial
47 | - [Summary](https://github.com/tirtharajdash/BITS-G513_MetaLearning/blob/main/Tutorials/Week5/week5_summary.pdf)
48 | ### Lab
49 | - [5: Transfer Learning and FineTuning Pre-Trained models](https://github.com/tirtharajdash/BITS-G513_MetaLearning/blob/main/Labs/Lab_5_6A.ipynb) [](https://colab.research.google.com/github/tirtharajdash/BITS-G513_MetaLearning/blob/main/Labs/Lab_5_6A.ipynb)
50 |
51 |
52 | ## Week 6
53 |
54 | ### Lab
55 | - [6A: SGD + Momentum and BatchNorm experiments](https://github.com/tirtharajdash/BITS-G513_MetaLearning/blob/main/Labs/Lab_5_6A.ipynb) [](https://colab.research.google.com/github/tirtharajdash/BITS-G513_MetaLearning/blob/main/Labs/Lab_5_6A.ipynb)
56 | - [6B: AdaGrad, RMSprop, Adam, Optimizer Benchmarking](https://colab.research.google.com/github/tirtharajdash/BITS-G513_MetaLearning/blob/main/Labs/Lab_6B.ipynb) [](https://colab.research.google.com/github/tirtharajdash/BITS-G513_MetaLearning/blob/main/Labs/Lab_6B.ipynb)
57 |
58 | ## Week 7
59 |
60 | ### Lab
61 | - [7: Sequence Classification using RNNs and LSTMs](https://github.com/tirtharajdash/BITS-G513_MetaLearning/blob/main/Labs/Lab_7.ipynb) [](https://colab.research.google.com/github/tirtharajdash/BITS-G513_MetaLearning/blob/main/Labs/Lab_7.ipynb)
62 |
63 | ------
64 |
65 | ### Staff
66 | #### Instructors
67 |
68 | - [Gautam Shroff](https://www.linkedin.com/in/gautam-shroff-066901/)
69 | - [Tirtharaj Dash](https://tirtharajdash.github.io/)
70 |
71 | #### Teaching Assistants
72 |
73 | - [Sharad Chitlangia](https://www.sharadchitlang.ai/)
74 | - [Aditya Ahuja](https://adiah80.github.io/)
75 |
76 | The initial part of the coursework (on Deep Learning) has been adapted from Yann LeCun and Alfredo Canziani's course on [Deep Learning at NYU](https://atcold.github.io/pytorch-Deep-Learning/)
77 |
78 |
79 | #### Google group
80 |
81 | A non-public group to communicate some course specific messages is created: [Join/View](https://groups.google.com/u/1/g/meta-learning-2021)
82 |
--------------------------------------------------------------------------------
/Tutorials/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tirtharajdash/BITS-G513_MetaLearning/81d62d08a420fb94059e04c2f2e9c7d0af10e151/Tutorials/.DS_Store
--------------------------------------------------------------------------------
/Tutorials/DL-Quiz/dl_quiz.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tirtharajdash/BITS-G513_MetaLearning/81d62d08a420fb94059e04c2f2e9c7d0af10e151/Tutorials/DL-Quiz/dl_quiz.pdf
--------------------------------------------------------------------------------
/Tutorials/Week1/main.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tirtharajdash/BITS-G513_MetaLearning/81d62d08a420fb94059e04c2f2e9c7d0af10e151/Tutorials/Week1/main.pdf
--------------------------------------------------------------------------------
/Tutorials/Week1/week1_summary.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tirtharajdash/BITS-G513_MetaLearning/81d62d08a420fb94059e04c2f2e9c7d0af10e151/Tutorials/Week1/week1_summary.pdf
--------------------------------------------------------------------------------
/Tutorials/Week2/main.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tirtharajdash/BITS-G513_MetaLearning/81d62d08a420fb94059e04c2f2e9c7d0af10e151/Tutorials/Week2/main.pdf
--------------------------------------------------------------------------------
/Tutorials/Week2/week2_summary.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tirtharajdash/BITS-G513_MetaLearning/81d62d08a420fb94059e04c2f2e9c7d0af10e151/Tutorials/Week2/week2_summary.pdf
--------------------------------------------------------------------------------
/Tutorials/Week3/CNN Examples_TD.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tirtharajdash/BITS-G513_MetaLearning/81d62d08a420fb94059e04c2f2e9c7d0af10e151/Tutorials/Week3/CNN Examples_TD.pdf
--------------------------------------------------------------------------------
/Tutorials/Week3/week3_summary.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tirtharajdash/BITS-G513_MetaLearning/81d62d08a420fb94059e04c2f2e9c7d0af10e151/Tutorials/Week3/week3_summary.pdf
--------------------------------------------------------------------------------
/Tutorials/Week4/cnn_backprop.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tirtharajdash/BITS-G513_MetaLearning/81d62d08a420fb94059e04c2f2e9c7d0af10e151/Tutorials/Week4/cnn_backprop.pdf
--------------------------------------------------------------------------------
/Tutorials/Week4/week4_summary.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tirtharajdash/BITS-G513_MetaLearning/81d62d08a420fb94059e04c2f2e9c7d0af10e151/Tutorials/Week4/week4_summary.pdf
--------------------------------------------------------------------------------
/Tutorials/Week5/week5_summary.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tirtharajdash/BITS-G513_MetaLearning/81d62d08a420fb94059e04c2f2e9c7d0af10e151/Tutorials/Week5/week5_summary.pdf
--------------------------------------------------------------------------------
/Tutorials/Week6/week6_summary.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tirtharajdash/BITS-G513_MetaLearning/81d62d08a420fb94059e04c2f2e9c7d0af10e151/Tutorials/Week6/week6_summary.pdf
--------------------------------------------------------------------------------