Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,8 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
/labs_dl/lab03/data
labs_dl/lab03/lab03_16/t10k-images-idx3-ubyte.gz
labs_dl/lab03/lab03_16/t10k-labels-idx1-ubyte.gz
labs_dl/lab03/lab03_16/train-images-idx3-ubyte.gz
labs_dl/lab03/lab03_16/train-labels-idx1-ubyte.gz
1,062 changes: 1,062 additions & 0 deletions labs_dl/lab01/lab01_16/lab01_16.ipynb

Large diffs are not rendered by default.

Binary file added labs_dl/lab01/lab01_16/t10k-images-idx3-ubyte.gz
Binary file not shown.
Binary file added labs_dl/lab01/lab01_16/t10k-labels-idx1-ubyte.gz
Binary file not shown.
Binary file added labs_dl/lab01/lab01_16/train-images-idx3-ubyte.gz
Binary file not shown.
Binary file added labs_dl/lab01/lab01_16/train-labels-idx1-ubyte.gz
Binary file not shown.
1,545 changes: 1,545 additions & 0 deletions labs_dl/lab02/lab02_16.ipynb

Large diffs are not rendered by default.

Binary file removed labs_dl/lab02/progress.png
Binary file not shown.
1,321 changes: 1,321 additions & 0 deletions labs_dl/lab03/lab03_16/lab03_16.ipynb

Large diffs are not rendered by default.

100 changes: 100 additions & 0 deletions labs_dl/lab03/lab03_16/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import sys
import os
import time

import numpy as np
from tqdm.auto import tqdm
import matplotlib.pyplot as plt


def load_mnist(flatten=False):
"""taken from https://github.com/Lasagne/Lasagne/blob/master/examples/mnist.py"""
# We first define a download function, supporting both Python 2 and 3.
if sys.version_info[0] == 2:
from urllib import urlretrieve
else:
from urllib.request import urlretrieve

def download(filename, source='http://yann.lecun.com/exdb/mnist/'):
print("Downloading %s" % filename)
urlretrieve(source + filename, filename)

# We then define functions for loading MNIST images and labels.
# For convenience, they also download the requested files if needed.
import gzip

def load_mnist_images(filename):
if not os.path.exists(filename):
download(filename)
# Read the inputs in Yann LeCun's binary format.
with gzip.open(filename, 'rb') as f:
data = np.frombuffer(f.read(), np.uint8, offset=16)
# The inputs are vectors now, we reshape them to monochrome 2D images,
# following the shape convention: (examples, channels, rows, columns)
data = data.reshape(-1, 1, 28, 28)
# The inputs come as bytes, we convert them to float32 in range [0,1].
# (Actually to range [0, 255/256], for compatibility to the version
# provided at http://deeplearning.net/data/mnist/mnist.pkl.gz.)
return data / np.float32(256)

def load_mnist_labels(filename):
if not os.path.exists(filename):
download(filename)
# Read the labels in Yann LeCun's binary format.
with gzip.open(filename, 'rb') as f:
data = np.frombuffer(f.read(), np.uint8, offset=8)
# The labels are vectors of integers now, that's exactly what we want.
return data

# We can now download and read the training and test set images and labels.
X_train = load_mnist_images('train-images-idx3-ubyte.gz')
y_train = load_mnist_labels('train-labels-idx1-ubyte.gz')
X_test = load_mnist_images('t10k-images-idx3-ubyte.gz')
y_test = load_mnist_labels('t10k-labels-idx1-ubyte.gz')

# We reserve the last 10000 training examples for validation.
X_train, X_val = X_train[:-10000], X_train[-10000:]
y_train, y_val = y_train[:-10000], y_train[-10000:]

if flatten:
X_train = X_train.reshape([X_train.shape[0], -1])
X_val = X_val.reshape([X_val.shape[0], -1])
X_test = X_test.reshape([X_test.shape[0], -1])

# We just return all the arrays in order, as expected in main().
# (It doesn't matter how we do this as long as we can read them again.)
return X_train, y_train, X_val, y_val, X_test, y_test


def iterate_minibatches(inputs, targets, batchsize, shuffle=False):
assert len(inputs) == len(targets)
if shuffle:
indices = np.random.permutation(len(inputs))
for start_idx in tqdm(range(0, len(inputs) - batchsize + 1, batchsize)):
if shuffle:
excerpt = indices[start_idx:start_idx + batchsize]
else:
excerpt = slice(start_idx, start_idx + batchsize)
yield inputs[excerpt], targets[excerpt]


def test_model(X_val, y_val, model):
loss_log, acc_log = [], []
model.eval()
for x_batch, y_batch in iterate_minibatches(X_val, y_val, batchsize=32, shuffle=True):
# data preparation
data = torch.from_numpy(x_batch.astype(np.float32))
target = torch.from_numpy(y_batch.astype(np.int64))

output = model(data)
loss = F.nll_loss(output, target)

pred = torch.max(output, 1)[1].numpy()
acc = np.mean(pred == y_batch)
acc_log.append(acc)

# compute gradients
loss = loss.item()
loss_log.append(loss)
return loss_log, acc_log

682 changes: 682 additions & 0 deletions labs_dl/lab04/lab04_16/lab04_16.ipynb

Large diffs are not rendered by default.