├── .gitignore ├── assignment1 ├── berend │ ├── .gitignore │ ├── README.md │ ├── collectSubmission.sh │ ├── cs231n │ │ ├── __init__.py │ │ ├── classifiers │ │ │ ├── __init__.py │ │ │ ├── k_nearest_neighbor.py │ │ │ ├── linear_classifier.py │ │ │ ├── linear_svm.py │ │ │ ├── neural_net.py │ │ │ └── softmax.py │ │ ├── data_utils.py │ │ ├── datasets │ │ │ ├── .gitignore │ │ │ └── get_datasets.sh │ │ ├── features.py │ │ ├── gradient_check.py │ │ └── vis_utils.py │ ├── features.ipynb │ ├── knn.ipynb │ ├── neuralnet.py │ ├── requirements.txt │ ├── softmax.ipynb │ ├── svm.ipynb │ ├── svm.py │ └── two_layer_net.ipynb ├── carlos │ ├── .gitignore │ ├── README.md │ ├── collectSubmission.sh │ ├── cs231n │ │ ├── __init__.py │ │ ├── classifiers │ │ │ ├── __init__.py │ │ │ ├── k_nearest_neighbor.py │ │ │ ├── linear_classifier.py │ │ │ ├── linear_svm.py │ │ │ ├── neural_net.py │ │ │ └── softmax.py │ │ ├── data_utils.py │ │ ├── datasets │ │ │ ├── .gitignore │ │ │ └── get_datasets.sh │ │ ├── features.py │ │ ├── gradient_check.py │ │ └── vis_utils.py │ ├── features.ipynb │ ├── frameworkpython │ ├── knn.ipynb │ ├── requirements.txt │ ├── softmax.ipynb │ ├── start_ipython_osx.sh │ ├── svm.ipynb │ └── two_layer_net.ipynb ├── christiaan │ ├── README.md │ ├── collectSubmission.sh │ ├── cs231n │ │ ├── __init__.py │ │ ├── classifiers │ │ │ ├── __init__.py │ │ │ ├── k_nearest_neighbor.py │ │ │ ├── linear_classifier.py │ │ │ ├── linear_svm.py │ │ │ ├── neural_net.py │ │ │ └── softmax.py │ │ ├── data_utils.py │ │ ├── datasets │ │ │ ├── .gitignore │ │ │ └── get_datasets.sh │ │ ├── features.py │ │ ├── gradient_check.py │ │ └── vis_utils.py │ ├── features.ipynb │ ├── frameworkpython │ ├── knn.ipynb │ ├── requirements.txt │ ├── softmax.ipynb │ ├── softmax_gradient.jpg │ ├── start_ipython_osx.sh │ ├── svm.ipynb │ └── two_layer_net.ipynb ├── dafne │ ├── .ipynb_checkpoints │ │ └── two_layer_net-checkpoint.ipynb │ ├── README.md │ ├── Tests.ipynb │ ├── collectSubmission.sh │ ├── cs231n │ │ ├── __init__.py │ │ ├── classifiers │ │ │ ├── __init__.py │ │ │ ├── k_nearest_neighbor.py │ │ │ ├── linear_classifier.py │ │ │ ├── linear_svm.py │ │ │ ├── neural_net.py │ │ │ └── softmax.py │ │ ├── data_utils.py │ │ ├── datasets │ │ │ ├── .gitignore │ │ │ └── get_datasets.sh │ │ ├── features.py │ │ ├── gradient_check.py │ │ └── vis_utils.py │ ├── features.ipynb │ ├── frameworkpython │ ├── knn.ipynb │ ├── requirements.txt │ ├── softmax.ipynb │ ├── start_ipython_osx.sh │ ├── svm.ipynb │ └── two_layer_net.ipynb ├── elena │ ├── .gitignore │ ├── README.md │ ├── collectSubmission.sh │ ├── cs231n │ │ ├── __init__.py │ │ ├── classifiers │ │ │ ├── __init__.py │ │ │ ├── k_nearest_neighbor.py │ │ │ ├── linear_classifier.py │ │ │ ├── linear_svm.py │ │ │ ├── neural_net.py │ │ │ └── softmax.py │ │ ├── data_utils.py │ │ ├── datasets │ │ │ ├── .gitignore │ │ │ └── get_datasets.sh │ │ ├── features.py │ │ ├── gradient_check.py │ │ └── vis_utils.py │ ├── features.ipynb │ ├── frameworkpython │ ├── knn.ipynb │ ├── requirements.txt │ ├── softmax.ipynb │ ├── start_ipython_osx.sh │ ├── svm.ipynb │ └── two_layer_net.ipynb └── vincent │ ├── README.md │ ├── collectSubmission.sh │ ├── cs231n │ ├── __init__.py │ ├── __init__.pyc │ ├── classifiers │ │ ├── __init__.py │ │ ├── __init__.pyc │ │ ├── backup │ │ │ ├── k_nearest_neighbor.py │ │ │ ├── k_nearest_neighbor.pyc │ │ │ ├── linear_classifier.py │ │ │ ├── linear_classifier.pyc │ │ │ ├── linear_classifier_berend.py │ │ │ ├── linear_svm.py │ │ │ ├── linear_svm.pyc │ │ │ ├── neural_net.py │ │ │ ├── softmax.py │ │ │ └── softmax.pyc │ │ ├── k_nearest_neighbor.py │ │ ├── k_nearest_neighbor.pyc │ │ ├── k_nearest_neighbor.py~ │ │ ├── linear_classifier.py │ │ ├── linear_classifier.pyc │ │ ├── linear_classifier_berend.py │ │ ├── linear_svm.py │ │ ├── linear_svm.pyc │ │ ├── neural_net.py │ │ ├── neural_net.pyc │ │ ├── softmax.py │ │ └── softmax.pyc │ ├── data_utils.py │ ├── data_utils.pyc │ ├── datasets │ │ ├── .gitignore │ │ └── get_datasets.sh │ ├── features.py │ ├── gradient_check.py │ └── vis_utils.py │ ├── features.ipynb │ ├── frameworkpython │ ├── knn.ipynb │ ├── requirements.txt │ ├── softmax.ipynb │ ├── start_ipython_osx.sh │ ├── svm.ipynb │ ├── svm.v3.ipynb │ └── two_layer_net.ipynb ├── assignment2 ├── berend │ ├── .gitignore │ ├── .idea │ │ ├── berend.iml │ │ ├── encodings.xml │ │ ├── inspectionProfiles │ │ │ ├── Project_Default.xml │ │ │ └── profiles_settings.xml │ │ ├── misc.xml │ │ ├── modules.xml │ │ ├── vcs.xml │ │ └── workspace.xml │ ├── .ipynb_checkpoints │ │ ├── BatchNormalization-checkpoint.ipynb │ │ └── FullyConnectedNets-checkpoint.ipynb │ ├── BatchNormalization.ipynb │ ├── ConvolutionalNetworks.ipynb │ ├── Dropout.ipynb │ ├── FullyConnectedNets.ipynb │ ├── README.md │ ├── collectSubmission.sh │ ├── cs231n │ │ ├── .gitignore │ │ ├── __init__.py │ │ ├── classifiers │ │ │ ├── __init__.py │ │ │ ├── cnn.py │ │ │ └── fc_net.py │ │ ├── data_utils.py │ │ ├── datasets │ │ │ ├── .gitignore │ │ │ └── get_datasets.sh │ │ ├── fast_layers.py │ │ ├── gradient_check.py │ │ ├── im2col.py │ │ ├── im2col_cython.pyx │ │ ├── layer_utils.py │ │ ├── layers.py │ │ ├── optim.py │ │ ├── setup.py │ │ ├── solver.py │ │ └── vis_utils.py │ ├── frameworkpython │ ├── kitten.jpg │ ├── puppy.jpg │ ├── requirements.txt │ └── start_ipython_osx.sh ├── carlos │ ├── .gitignore │ ├── BatchNormalization.ipynb │ ├── ConvolutionalNetworks.ipynb │ ├── Dropout.ipynb │ ├── FullyConnectedNets.ipynb │ └── cs231n │ │ ├── .gitignore │ │ ├── __init__.py │ │ ├── classifiers │ │ ├── __init__.py │ │ ├── cnn.py │ │ └── fc_net.py │ │ ├── data_utils.py │ │ ├── datasets │ │ ├── .gitignore │ │ └── get_datasets.sh │ │ ├── fast_layers.py │ │ ├── gradient_check.py │ │ ├── im2col.py │ │ ├── im2col_cython.pyx │ │ ├── layer_utils.py │ │ ├── layers.py │ │ ├── optim.py │ │ ├── setup.py │ │ ├── solver.py │ │ └── vis_utils.py ├── christiaan │ ├── .idea │ │ ├── .name │ │ ├── christiaan.iml │ │ ├── encodings.xml │ │ ├── misc.xml │ │ ├── modules.xml │ │ └── workspace.xml │ ├── .ipynb_checkpoints │ │ └── BatchNormalization-checkpoint.ipynb │ ├── BatchNormalization.ipynb │ ├── ConvolutionalNetworks.ipynb │ ├── Dropout.ipynb │ ├── FullyConnectedNets.ipynb │ ├── README.md │ ├── collectSubmission.sh │ ├── cs231n │ │ ├── .gitignore │ │ ├── __init__.py │ │ ├── classifiers │ │ │ ├── __init__.py │ │ │ ├── cnn.py │ │ │ └── fc_net.py │ │ ├── data_utils.py │ │ ├── datasets │ │ │ ├── .gitignore │ │ │ └── get_datasets.sh │ │ ├── fast_layers.py │ │ ├── gradient_check.py │ │ ├── im2col.py │ │ ├── im2col_cython.pyx │ │ ├── layer_utils.py │ │ ├── layers.py │ │ ├── optim.py │ │ ├── setup.py │ │ ├── solver.py │ │ └── vis_utils.py │ ├── frameworkpython │ ├── kitten.jpg │ ├── puppy.jpg │ ├── requirements.txt │ └── start_ipython_osx.sh ├── dafne │ ├── .ipynb_checkpoints │ │ ├── BatchNormalization-checkpoint.ipynb │ │ ├── ConvolutionalNetworks-checkpoint.ipynb │ │ ├── Dropout-checkpoint.ipynb │ │ ├── FullyConnectedNets-checkpoint.ipynb │ │ └── Tests-checkpoint.ipynb │ ├── BatchNormalization.ipynb │ ├── ConvolutionalNetworks.ipynb │ ├── Dropout.ipynb │ ├── FullyConnectedNets.ipynb │ ├── README.md │ ├── Tests.ipynb │ ├── collectSubmission.sh │ ├── cs231n │ │ ├── .gitignore │ │ ├── __init__.py │ │ ├── classifiers │ │ │ ├── __init__.py │ │ │ ├── cnn.py │ │ │ └── fc_net.py │ │ ├── data_utils.py │ │ ├── datasets │ │ │ ├── .gitignore │ │ │ └── get_datasets.sh │ │ ├── fast_layers.py │ │ ├── gradient_check.py │ │ ├── im2col.py │ │ ├── im2col_cython.pyx │ │ ├── layer_utils.py │ │ ├── layers.py │ │ ├── optim.py │ │ ├── setup.py │ │ ├── solver.py │ │ └── vis_utils.py │ ├── frameworkpython │ ├── kitten.jpg │ ├── puppy.jpg │ ├── requirements.txt │ └── start_ipython_osx.sh ├── elena │ ├── .gitignore │ ├── .ipynb_checkpoints │ │ ├── BatchNormalization-checkpoint.ipynb │ │ ├── ConvolutionalNetworks-checkpoint.ipynb │ │ └── FullyConnectedNets-checkpoint.ipynb │ ├── BatchNormalization.ipynb │ ├── ConvolutionalNetworks.ipynb │ ├── Dropout.ipynb │ ├── FullyConnectedNets.ipynb │ ├── README.md │ ├── collectSubmission.sh │ ├── cs231n │ │ ├── .gitignore │ │ ├── __init__.py │ │ ├── classifiers │ │ │ ├── __init__.py │ │ │ ├── cnn.py │ │ │ └── fc_net.py │ │ ├── data_utils.py │ │ ├── datasets │ │ │ ├── .gitignore │ │ │ └── get_datasets.sh │ │ ├── fast_layers.py │ │ ├── gradient_check.py │ │ ├── im2col.py │ │ ├── im2col_cython.pyx │ │ ├── layer_utils.py │ │ ├── layers.py │ │ ├── optim.py │ │ ├── setup.py │ │ ├── solver.py │ │ └── vis_utils.py │ ├── frameworkpython │ ├── kitten.jpg │ ├── puppy.jpg │ ├── requirements.txt │ └── start_ipython_osx.sh └── vincent │ ├── BatchNormalization.ipynb │ ├── ConvolutionalNetworks.ipynb │ ├── Dropout.ipynb │ ├── FullyConnectedNets.ipynb │ ├── README.md │ ├── collectSubmission.sh │ ├── cs231n │ ├── .gitignore │ ├── __init__.py │ ├── classifiers │ │ ├── __init__.py │ │ ├── cnn.py │ │ └── fc_net.py │ ├── data_utils.py │ ├── fast_layers.py │ ├── gradient_check.py │ ├── im2col.py │ ├── im2col_cython.pyx │ ├── layer_utils.py │ ├── layers.py │ ├── optim.py │ ├── setup.py │ ├── solver.py │ └── vis_utils.py │ ├── frameworkpython │ ├── kitten.jpg │ ├── puppy.jpg │ ├── requirements.txt │ └── start_ipython_osx.sh ├── assignment3 ├── berend │ ├── .gitignore │ ├── ImageGeneration.ipynb │ ├── ImageGradients.ipynb │ ├── LSTM_Captioning.ipynb │ ├── RNN_Captioning.ipynb │ ├── collectSubmission.sh │ ├── cs231n │ │ ├── .gitignore │ │ ├── __init__.py │ │ ├── bin │ │ │ ├── activate │ │ │ ├── conda │ │ │ └── deactivate │ │ ├── captioning_solver.py │ │ ├── classifiers │ │ │ ├── __init__.py │ │ │ ├── pretrained_cnn.py │ │ │ └── rnn.py │ │ ├── coco_utils.py │ │ ├── conda-meta │ │ │ ├── history │ │ │ ├── libgfortran-3.0.0-1.json │ │ │ └── openblas-0.2.14-4.json │ │ ├── data_utils.py │ │ ├── fast_layers.py │ │ ├── gradient_check.py │ │ ├── im2col.py │ │ ├── im2col_cython.pyx │ │ ├── image_utils.py │ │ ├── include │ │ │ ├── cblas.h │ │ │ ├── f77blas.h │ │ │ ├── lapacke.h │ │ │ ├── lapacke_config.h │ │ │ ├── lapacke_mangling.h │ │ │ ├── lapacke_utils.h │ │ │ └── openblas_config.h │ │ ├── layer_utils.py │ │ ├── layers.py │ │ ├── lib │ │ │ ├── cmake │ │ │ │ └── openblas │ │ │ │ │ └── OpenBLASConfig.cmake │ │ │ ├── libgfortran.so.3 │ │ │ ├── libgfortran.so.3.0.0 │ │ │ ├── libopenblas.so │ │ │ ├── libopenblas.so.0 │ │ │ └── libopenblas_nehalemp-r0.2.14.so │ │ ├── optim.py │ │ ├── rnn_layers.py │ │ └── setup.py │ ├── frameworkpython │ ├── kitten.jpg │ ├── requirements.txt │ ├── sky.jpg │ └── start_ipython_osx.sh ├── carlos │ ├── .gitignore │ ├── ImageGeneration.ipynb │ ├── ImageGradients.ipynb │ ├── LSTM_Captioning.ipynb │ ├── RNN_Captioning.ipynb │ ├── collectSubmission.sh │ ├── cs231n │ │ ├── .gitignore │ │ ├── __init__.py │ │ ├── captioning_solver.py │ │ ├── classifiers │ │ │ ├── __init__.py │ │ │ ├── pretrained_cnn.py │ │ │ └── rnn.py │ │ ├── coco_utils.py │ │ ├── data_utils.py │ │ ├── fast_layers.py │ │ ├── gradient_check.py │ │ ├── im2col.py │ │ ├── im2col_cython.pyx │ │ ├── image_utils.py │ │ ├── layer_utils.py │ │ ├── layers.py │ │ ├── optim.py │ │ ├── rnn_layers.py │ │ └── setup.py │ ├── frameworkpython │ ├── kitten.jpg │ ├── requirements.txt │ ├── sky.jpg │ └── start_ipython_osx.sh ├── chris │ ├── ImageGeneration.ipynb │ ├── ImageGradients.ipynb │ ├── LSTM_Captioning.ipynb │ ├── RNN_Captioning.ipynb │ ├── collectSubmission.sh │ ├── cs231n │ │ ├── __init__.py │ │ ├── captioning_solver.py │ │ ├── classifiers │ │ │ ├── __init__.py │ │ │ ├── pretrained_cnn.py │ │ │ └── rnn.py │ │ ├── coco_utils.py │ │ ├── data_utils.py │ │ ├── datasets │ │ │ ├── get_coco_captioning.sh │ │ │ ├── get_pretrained_model.sh │ │ │ └── get_tiny_imagenet_a.sh │ │ ├── fast_layers.py │ │ ├── gradient_check.py │ │ ├── im2col.py │ │ ├── im2col_cython.pyx │ │ ├── image_utils.py │ │ ├── layer_utils.py │ │ ├── layers.py │ │ ├── optim.py │ │ ├── rnn_layers.py │ │ └── setup.py │ ├── frameworkpython │ ├── kitten.jpg │ ├── requirements.txt │ ├── sky.jpg │ └── start_ipython_osx.sh └── dafne │ └── assignment3 │ ├── .gitignore │ ├── ImageGeneration.ipynb │ ├── ImageGradients.ipynb │ ├── LSTM_Captioning.ipynb │ ├── RNN_Captioning.ipynb │ ├── collectSubmission.sh │ ├── cs231n │ ├── .gitignore │ ├── __init__.py │ ├── captioning_solver.py │ ├── classifiers │ │ ├── __init__.py │ │ ├── pretrained_cnn.py │ │ └── rnn.py │ ├── coco_utils.py │ ├── data_utils.py │ ├── fast_layers.py │ ├── gradient_check.py │ ├── im2col.py │ ├── im2col_cython.pyx │ ├── image_utils.py │ ├── layer_utils.py │ ├── layers.py │ ├── optim.py │ ├── rnn_layers.py │ └── setup.py │ ├── frameworkpython │ ├── kitten.jpg │ ├── requirements.txt │ ├── sky.jpg │ └── start_ipython_osx.sh └── eeggroup ├── deeplearning_eeg_vincent.ipynb ├── deeplearning_guinneabissau-80_percent_validation_accuracy_CNN.ipynb └── files.zip /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | -------------------------------------------------------------------------------- /assignment1/berend/.gitignore: -------------------------------------------------------------------------------- 1 | .idea/* 2 | .ipynb_checkpoints/* 3 | *.swp 4 | *.pyc 5 | .env/* 6 | -------------------------------------------------------------------------------- /assignment1/berend/README.md: -------------------------------------------------------------------------------- 1 | Details about this assignment can be found [on the course webpage](https://cs231n.github.io/assignment1/). 2 | -------------------------------------------------------------------------------- /assignment1/berend/collectSubmission.sh: -------------------------------------------------------------------------------- 1 | rm -f assignment1.zip 2 | zip -r assignment1.zip . -x "*.git*" "*cs231n/datasets*" "*.ipynb_checkpoints*" "*README.md" "*collectSubmission.sh" "*requirements.txt" 3 | -------------------------------------------------------------------------------- /assignment1/berend/cs231n/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment1/berend/cs231n/__init__.py -------------------------------------------------------------------------------- /assignment1/berend/cs231n/classifiers/__init__.py: -------------------------------------------------------------------------------- 1 | from cs231n.classifiers.k_nearest_neighbor import * 2 | from cs231n.classifiers.linear_classifier import * 3 | -------------------------------------------------------------------------------- /assignment1/berend/cs231n/classifiers/softmax.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from random import shuffle 3 | 4 | 5 | def softmax_loss_naive(W, X, y, reg): 6 | """ 7 | Softmax loss function, naive implementation (with loops) 8 | Inputs: 9 | - W: C x D array of weights 10 | - X: D x N array of data. Data are D-dimensional columns 11 | - y: 1-dimensional array of length N with labels 0...K-1, for K classes 12 | - reg: (float) regularization strength 13 | Returns: 14 | a tuple of: 15 | - loss as single float 16 | - gradient with respect to weights W, an array of same size as W 17 | """ 18 | # Initialize the loss and gradient to zero. 19 | loss = 0.0 20 | dW = np.zeros_like(W) 21 | 22 | num_classes = W.shape[0] 23 | num_train = X.shape[1] 24 | 25 | ############################################################################# 26 | # TODO: Compute the softmax loss and its gradient using explicit loops. # 27 | # Store the loss in loss and the gradient in dW. If you are not careful # 28 | # here, it is easy to run into numeric instability. Don't forget the # 29 | # regularization! # 30 | ############################################################################# 31 | for i in xrange(num_train): 32 | scores = W.dot(X[:, i]) 33 | scores -= np.max(scores) 34 | 35 | score_correct = scores[y[i]] 36 | sum_incorrect = 0.0 37 | for j in xrange(num_classes): 38 | if j == y[i]: 39 | continue 40 | sum_incorrect += np.exp(scores[j]) 41 | 42 | loss -= np.log(np.exp(score_correct) / sum_incorrect) 43 | 44 | 45 | loss /= num_train 46 | loss += 0.5 * reg * np.sum(W * W) 47 | ############################################################################# 48 | # END OF YOUR CODE # 49 | ############################################################################# 50 | 51 | 52 | return loss, dW 53 | 54 | 55 | def softmax_loss_vectorized(W, X, y, reg): 56 | """ 57 | Softmax loss function, vectorized version. 58 | 59 | Inputs and outputs are the same as softmax_loss_naive. 60 | """ 61 | # Initialize the loss and gradient to zero. 62 | loss = 0.0 63 | dW = np.zeros_like(W) 64 | 65 | ############################################################################# 66 | # TODO: Compute the softmax loss and its gradient using no explicit loops. # 67 | # Store the loss in loss and the gradient in dW. If you are not careful # 68 | # here, it is easy to run into numeric instability. Don't forget the # 69 | # regularization! # 70 | ############################################################################# 71 | pass 72 | ############################################################################# 73 | # END OF YOUR CODE # 74 | ############################################################################# 75 | 76 | return loss, dW 77 | -------------------------------------------------------------------------------- /assignment1/berend/cs231n/datasets/.gitignore: -------------------------------------------------------------------------------- 1 | cifar-10-batches-py/* 2 | -------------------------------------------------------------------------------- /assignment1/berend/cs231n/datasets/get_datasets.sh: -------------------------------------------------------------------------------- 1 | # Get CIFAR10 2 | wget http://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz 3 | tar -xzvf cifar-10-python.tar.gz 4 | rm cifar-10-python.tar.gz 5 | -------------------------------------------------------------------------------- /assignment1/berend/cs231n/gradient_check.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from random import randrange 3 | 4 | def eval_numerical_gradient(f, x, verbose=True, h=0.00001): 5 | """ 6 | a naive implementation of numerical gradient of f at x 7 | - f should be a function that takes a single argument 8 | - x is the point (numpy array) to evaluate the gradient at 9 | """ 10 | 11 | fx = f(x) # evaluate function value at original point 12 | grad = np.zeros_like(x) 13 | # iterate over all indexes in x 14 | it = np.nditer(x, flags=['multi_index'], op_flags=['readwrite']) 15 | while not it.finished: 16 | 17 | # evaluate function at x+h 18 | ix = it.multi_index 19 | oldval = x[ix] 20 | x[ix] = oldval + h # increment by h 21 | fxph = f(x) # evalute f(x + h) 22 | x[ix] = oldval - h 23 | fxmh = f(x) # evaluate f(x - h) 24 | x[ix] = oldval # restore 25 | 26 | # compute the partial derivative with centered formula 27 | grad[ix] = (fxph - fxmh) / (2 * h) # the slope 28 | if verbose: 29 | print ix, grad[ix] 30 | it.iternext() # step to next dimension 31 | 32 | return grad 33 | 34 | 35 | def eval_numerical_gradient_array(f, x, df, h=1e-5): 36 | """ 37 | Evaluate a numeric gradient for a function that accepts a numpy 38 | array and returns a numpy array. 39 | """ 40 | grad = np.zeros_like(x) 41 | it = np.nditer(x, flags=['multi_index'], op_flags=['readwrite']) 42 | while not it.finished: 43 | ix = it.multi_index 44 | 45 | oldval = x[ix] 46 | x[ix] = oldval + h 47 | pos = f(x).copy() 48 | x[ix] = oldval - h 49 | neg = f(x).copy() 50 | x[ix] = oldval 51 | 52 | grad[ix] = np.sum((pos - neg) * df) / (2 * h) 53 | it.iternext() 54 | return grad 55 | 56 | 57 | def eval_numerical_gradient_blobs(f, inputs, output, h=1e-5): 58 | """ 59 | Compute numeric gradients for a function that operates on input 60 | and output blobs. 61 | 62 | We assume that f accepts several input blobs as arguments, followed by a blob 63 | into which outputs will be written. For example, f might be called like this: 64 | 65 | f(x, w, out) 66 | 67 | where x and w are input Blobs, and the result of f will be written to out. 68 | 69 | Inputs: 70 | - f: function 71 | - inputs: tuple of input blobs 72 | - output: output blob 73 | - h: step size 74 | """ 75 | numeric_diffs = [] 76 | for input_blob in inputs: 77 | diff = np.zeros_like(input_blob.diffs) 78 | it = np.nditer(input_blob.vals, flags=['multi_index'], 79 | op_flags=['readwrite']) 80 | while not it.finished: 81 | idx = it.multi_index 82 | orig = input_blob.vals[idx] 83 | 84 | input_blob.vals[idx] = orig + h 85 | f(*(inputs + (output,))) 86 | pos = np.copy(output.vals) 87 | input_blob.vals[idx] = orig - h 88 | f(*(inputs + (output,))) 89 | neg = np.copy(output.vals) 90 | input_blob.vals[idx] = orig 91 | 92 | diff[idx] = np.sum((pos - neg) * output.diffs) / (2.0 * h) 93 | 94 | it.iternext() 95 | numeric_diffs.append(diff) 96 | return numeric_diffs 97 | 98 | 99 | def eval_numerical_gradient_net(net, inputs, output, h=1e-5): 100 | return eval_numerical_gradient_blobs(lambda *args: net.forward(), 101 | inputs, output, h=h) 102 | 103 | 104 | def grad_check_sparse(f, x, analytic_grad, num_checks=10, h=1e-5): 105 | """ 106 | sample a few random elements and only return numerical 107 | in this dimensions. 108 | """ 109 | 110 | for i in xrange(num_checks): 111 | ix = tuple([randrange(m) for m in x.shape]) 112 | 113 | oldval = x[ix] 114 | x[ix] = oldval + h # increment by h 115 | fxph = f(x) # evaluate f(x + h) 116 | x[ix] = oldval - h # increment by h 117 | fxmh = f(x) # evaluate f(x - h) 118 | x[ix] = oldval # reset 119 | 120 | grad_numerical = (fxph - fxmh) / (2 * h) 121 | grad_analytic = analytic_grad[ix] 122 | rel_error = abs(grad_numerical - grad_analytic) / (abs(grad_numerical) + abs(grad_analytic)) 123 | print 'numerical: %f analytic: %f, relative error: %e' % (grad_numerical, grad_analytic, rel_error) 124 | 125 | -------------------------------------------------------------------------------- /assignment1/berend/cs231n/vis_utils.py: -------------------------------------------------------------------------------- 1 | from math import sqrt, ceil 2 | import numpy as np 3 | 4 | def visualize_grid(Xs, ubound=255.0, padding=1): 5 | """ 6 | Reshape a 4D tensor of image data to a grid for easy visualization. 7 | 8 | Inputs: 9 | - Xs: Data of shape (N, H, W, C) 10 | - ubound: Output grid will have values scaled to the range [0, ubound] 11 | - padding: The number of blank pixels between elements of the grid 12 | """ 13 | (N, H, W, C) = Xs.shape 14 | grid_size = int(ceil(sqrt(N))) 15 | grid_height = H * grid_size + padding * (grid_size - 1) 16 | grid_width = W * grid_size + padding * (grid_size - 1) 17 | grid = np.zeros((grid_height, grid_width, C)) 18 | next_idx = 0 19 | y0, y1 = 0, H 20 | for y in xrange(grid_size): 21 | x0, x1 = 0, W 22 | for x in xrange(grid_size): 23 | if next_idx < N: 24 | img = Xs[next_idx] 25 | low, high = np.min(img), np.max(img) 26 | grid[y0:y1, x0:x1] = ubound * (img - low) / (high - low) 27 | # grid[y0:y1, x0:x1] = Xs[next_idx] 28 | next_idx += 1 29 | x0 += W + padding 30 | x1 += W + padding 31 | y0 += H + padding 32 | y1 += H + padding 33 | # grid_max = np.max(grid) 34 | # grid_min = np.min(grid) 35 | # grid = ubound * (grid - grid_min) / (grid_max - grid_min) 36 | return grid 37 | 38 | def vis_grid(Xs): 39 | """ visualize a grid of images """ 40 | (N, H, W, C) = Xs.shape 41 | A = int(ceil(sqrt(N))) 42 | G = np.ones((A*H+A, A*W+A, C), Xs.dtype) 43 | G *= np.min(Xs) 44 | n = 0 45 | for y in range(A): 46 | for x in range(A): 47 | if n < N: 48 | G[y*H+y:(y+1)*H+y, x*W+x:(x+1)*W+x, :] = Xs[n,:,:,:] 49 | n += 1 50 | # normalize to [0,1] 51 | maxg = G.max() 52 | ming = G.min() 53 | G = (G - ming)/(maxg-ming) 54 | return G 55 | 56 | def vis_nn(rows): 57 | """ visualize array of arrays of images """ 58 | N = len(rows) 59 | D = len(rows[0]) 60 | H,W,C = rows[0][0].shape 61 | Xs = rows[0][0] 62 | G = np.ones((N*H+N, D*W+D, C), Xs.dtype) 63 | for y in range(N): 64 | for x in range(D): 65 | G[y*H+y:(y+1)*H+y, x*W+x:(x+1)*W+x, :] = rows[y][x] 66 | # normalize to [0,1] 67 | maxg = G.max() 68 | ming = G.min() 69 | G = (G - ming)/(maxg-ming) 70 | return G 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /assignment1/berend/requirements.txt: -------------------------------------------------------------------------------- 1 | Jinja2 2 | MarkupSafe 3 | backports.ssl-match-hostname 4 | certifi 5 | gnureadline 6 | ipython 7 | matplotlib 8 | mock 9 | nose 10 | numpy 11 | pyparsing 12 | python-dateutil 13 | pytz 14 | pyzmq 15 | scipy 16 | six 17 | tornado 18 | wsgiref 19 | -------------------------------------------------------------------------------- /assignment1/carlos/.gitignore: -------------------------------------------------------------------------------- 1 | venv/ 2 | *.pyc 3 | .ipynb_checkpoints/ 4 | -------------------------------------------------------------------------------- /assignment1/carlos/README.md: -------------------------------------------------------------------------------- 1 | Details about this assignment can be found [on the course webpage](http://cs231n.github.io/), under Assignment #1 of Winter 2016. 2 | -------------------------------------------------------------------------------- /assignment1/carlos/collectSubmission.sh: -------------------------------------------------------------------------------- 1 | rm -f assignment1.zip 2 | zip -r assignment1.zip . -x "*.git*" "*cs231n/datasets*" "*.ipynb_checkpoints*" "*README.md" "*collectSubmission.sh" "*requirements.txt" 3 | -------------------------------------------------------------------------------- /assignment1/carlos/cs231n/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment1/carlos/cs231n/__init__.py -------------------------------------------------------------------------------- /assignment1/carlos/cs231n/classifiers/__init__.py: -------------------------------------------------------------------------------- 1 | from cs231n.classifiers.k_nearest_neighbor import * 2 | from cs231n.classifiers.linear_classifier import * 3 | -------------------------------------------------------------------------------- /assignment1/carlos/cs231n/classifiers/softmax.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from random import shuffle 3 | 4 | def softmax_loss_naive(W, X, y, reg): 5 | """ 6 | Softmax loss function, naive implementation (with loops) 7 | 8 | Inputs have dimension D, there are C classes, and we operate on minibatches 9 | of N examples. 10 | 11 | Inputs: 12 | - W: A numpy array of shape (D, C) containing weights. 13 | - X: A numpy array of shape (N, D) containing a minibatch of data. 14 | - y: A numpy array of shape (N,) containing training labels; y[i] = c means 15 | that X[i] has label c, where 0 <= c < C. 16 | - reg: (float) regularization strength 17 | 18 | Returns a tuple of: 19 | - loss as single float 20 | - gradient with respect to weights W; an array of same shape as W 21 | """ 22 | # Initialize the loss and gradient to zero. 23 | loss = 0.0 24 | dW = np.zeros_like(W) 25 | 26 | ############################################################################# 27 | # TODO: Compute the softmax loss and its gradient using explicit loops. # 28 | # Store the loss in loss and the gradient in dW. If you are not careful # 29 | # here, it is easy to run into numeric instability. Don't forget the # 30 | # regularization! # 31 | ############################################################################# 32 | pass 33 | ############################################################################# 34 | # END OF YOUR CODE # 35 | ############################################################################# 36 | 37 | return loss, dW 38 | 39 | 40 | def softmax_loss_vectorized(W, X, y, reg): 41 | """ 42 | Softmax loss function, vectorized version. 43 | 44 | Inputs and outputs are the same as softmax_loss_naive. 45 | """ 46 | # Initialize the loss and gradient to zero. 47 | loss = 0.0 48 | dW = np.zeros_like(W) 49 | 50 | ############################################################################# 51 | # TODO: Compute the softmax loss and its gradient using no explicit loops. # 52 | # Store the loss in loss and the gradient in dW. If you are not careful # 53 | # here, it is easy to run into numeric instability. Don't forget the # 54 | # regularization! # 55 | ############################################################################# 56 | pass 57 | ############################################################################# 58 | # END OF YOUR CODE # 59 | ############################################################################# 60 | 61 | return loss, dW 62 | 63 | -------------------------------------------------------------------------------- /assignment1/carlos/cs231n/datasets/.gitignore: -------------------------------------------------------------------------------- 1 | cifar-10-batches-py/* 2 | tiny-imagenet-100-A* 3 | tiny-imagenet-100-B* 4 | tiny-100-A-pretrained/* 5 | -------------------------------------------------------------------------------- /assignment1/carlos/cs231n/datasets/get_datasets.sh: -------------------------------------------------------------------------------- 1 | # Get CIFAR10 2 | wget http://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz 3 | tar -xzvf cifar-10-python.tar.gz 4 | rm cifar-10-python.tar.gz 5 | -------------------------------------------------------------------------------- /assignment1/carlos/cs231n/gradient_check.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from random import randrange 3 | 4 | def eval_numerical_gradient(f, x, verbose=True, h=0.00001): 5 | """ 6 | a naive implementation of numerical gradient of f at x 7 | - f should be a function that takes a single argument 8 | - x is the point (numpy array) to evaluate the gradient at 9 | """ 10 | 11 | fx = f(x) # evaluate function value at original point 12 | grad = np.zeros_like(x) 13 | # iterate over all indexes in x 14 | it = np.nditer(x, flags=['multi_index'], op_flags=['readwrite']) 15 | while not it.finished: 16 | 17 | # evaluate function at x+h 18 | ix = it.multi_index 19 | oldval = x[ix] 20 | x[ix] = oldval + h # increment by h 21 | fxph = f(x) # evalute f(x + h) 22 | x[ix] = oldval - h 23 | fxmh = f(x) # evaluate f(x - h) 24 | x[ix] = oldval # restore 25 | 26 | # compute the partial derivative with centered formula 27 | grad[ix] = (fxph - fxmh) / (2 * h) # the slope 28 | if verbose: 29 | print ix, grad[ix] 30 | it.iternext() # step to next dimension 31 | 32 | return grad 33 | 34 | 35 | def eval_numerical_gradient_array(f, x, df, h=1e-5): 36 | """ 37 | Evaluate a numeric gradient for a function that accepts a numpy 38 | array and returns a numpy array. 39 | """ 40 | grad = np.zeros_like(x) 41 | it = np.nditer(x, flags=['multi_index'], op_flags=['readwrite']) 42 | while not it.finished: 43 | ix = it.multi_index 44 | 45 | oldval = x[ix] 46 | x[ix] = oldval + h 47 | pos = f(x).copy() 48 | x[ix] = oldval - h 49 | neg = f(x).copy() 50 | x[ix] = oldval 51 | 52 | grad[ix] = np.sum((pos - neg) * df) / (2 * h) 53 | it.iternext() 54 | return grad 55 | 56 | 57 | def eval_numerical_gradient_blobs(f, inputs, output, h=1e-5): 58 | """ 59 | Compute numeric gradients for a function that operates on input 60 | and output blobs. 61 | 62 | We assume that f accepts several input blobs as arguments, followed by a blob 63 | into which outputs will be written. For example, f might be called like this: 64 | 65 | f(x, w, out) 66 | 67 | where x and w are input Blobs, and the result of f will be written to out. 68 | 69 | Inputs: 70 | - f: function 71 | - inputs: tuple of input blobs 72 | - output: output blob 73 | - h: step size 74 | """ 75 | numeric_diffs = [] 76 | for input_blob in inputs: 77 | diff = np.zeros_like(input_blob.diffs) 78 | it = np.nditer(input_blob.vals, flags=['multi_index'], 79 | op_flags=['readwrite']) 80 | while not it.finished: 81 | idx = it.multi_index 82 | orig = input_blob.vals[idx] 83 | 84 | input_blob.vals[idx] = orig + h 85 | f(*(inputs + (output,))) 86 | pos = np.copy(output.vals) 87 | input_blob.vals[idx] = orig - h 88 | f(*(inputs + (output,))) 89 | neg = np.copy(output.vals) 90 | input_blob.vals[idx] = orig 91 | 92 | diff[idx] = np.sum((pos - neg) * output.diffs) / (2.0 * h) 93 | 94 | it.iternext() 95 | numeric_diffs.append(diff) 96 | return numeric_diffs 97 | 98 | 99 | def eval_numerical_gradient_net(net, inputs, output, h=1e-5): 100 | return eval_numerical_gradient_blobs(lambda *args: net.forward(), 101 | inputs, output, h=h) 102 | 103 | 104 | def grad_check_sparse(f, x, analytic_grad, num_checks=10, h=1e-5): 105 | """ 106 | sample a few random elements and only return numerical 107 | in this dimensions. 108 | """ 109 | 110 | for i in xrange(num_checks): 111 | ix = tuple([randrange(m) for m in x.shape]) 112 | 113 | oldval = x[ix] 114 | x[ix] = oldval + h # increment by h 115 | fxph = f(x) # evaluate f(x + h) 116 | x[ix] = oldval - h # increment by h 117 | fxmh = f(x) # evaluate f(x - h) 118 | x[ix] = oldval # reset 119 | 120 | grad_numerical = (fxph - fxmh) / (2 * h) 121 | grad_analytic = analytic_grad[ix] 122 | rel_error = abs(grad_numerical - grad_analytic) / (abs(grad_numerical) + abs(grad_analytic)) 123 | print 'numerical: %f analytic: %f, relative error: %e' % (grad_numerical, grad_analytic, rel_error) 124 | 125 | -------------------------------------------------------------------------------- /assignment1/carlos/cs231n/vis_utils.py: -------------------------------------------------------------------------------- 1 | from math import sqrt, ceil 2 | import numpy as np 3 | 4 | def visualize_grid(Xs, ubound=255.0, padding=1): 5 | """ 6 | Reshape a 4D tensor of image data to a grid for easy visualization. 7 | 8 | Inputs: 9 | - Xs: Data of shape (N, H, W, C) 10 | - ubound: Output grid will have values scaled to the range [0, ubound] 11 | - padding: The number of blank pixels between elements of the grid 12 | """ 13 | (N, H, W, C) = Xs.shape 14 | grid_size = int(ceil(sqrt(N))) 15 | grid_height = H * grid_size + padding * (grid_size - 1) 16 | grid_width = W * grid_size + padding * (grid_size - 1) 17 | grid = np.zeros((grid_height, grid_width, C)) 18 | next_idx = 0 19 | y0, y1 = 0, H 20 | for y in xrange(grid_size): 21 | x0, x1 = 0, W 22 | for x in xrange(grid_size): 23 | if next_idx < N: 24 | img = Xs[next_idx] 25 | low, high = np.min(img), np.max(img) 26 | grid[y0:y1, x0:x1] = ubound * (img - low) / (high - low) 27 | # grid[y0:y1, x0:x1] = Xs[next_idx] 28 | next_idx += 1 29 | x0 += W + padding 30 | x1 += W + padding 31 | y0 += H + padding 32 | y1 += H + padding 33 | # grid_max = np.max(grid) 34 | # grid_min = np.min(grid) 35 | # grid = ubound * (grid - grid_min) / (grid_max - grid_min) 36 | return grid 37 | 38 | def vis_grid(Xs): 39 | """ visualize a grid of images """ 40 | (N, H, W, C) = Xs.shape 41 | A = int(ceil(sqrt(N))) 42 | G = np.ones((A*H+A, A*W+A, C), Xs.dtype) 43 | G *= np.min(Xs) 44 | n = 0 45 | for y in range(A): 46 | for x in range(A): 47 | if n < N: 48 | G[y*H+y:(y+1)*H+y, x*W+x:(x+1)*W+x, :] = Xs[n,:,:,:] 49 | n += 1 50 | # normalize to [0,1] 51 | maxg = G.max() 52 | ming = G.min() 53 | G = (G - ming)/(maxg-ming) 54 | return G 55 | 56 | def vis_nn(rows): 57 | """ visualize array of arrays of images """ 58 | N = len(rows) 59 | D = len(rows[0]) 60 | H,W,C = rows[0][0].shape 61 | Xs = rows[0][0] 62 | G = np.ones((N*H+N, D*W+D, C), Xs.dtype) 63 | for y in range(N): 64 | for x in range(D): 65 | G[y*H+y:(y+1)*H+y, x*W+x:(x+1)*W+x, :] = rows[y][x] 66 | # normalize to [0,1] 67 | maxg = G.max() 68 | ming = G.min() 69 | G = (G - ming)/(maxg-ming) 70 | return G 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /assignment1/carlos/frameworkpython: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # what real Python executable to use 4 | PYVER=2.7 5 | PATHTOPYTHON=/usr/local/bin/ 6 | PYTHON=${PATHTOPYTHON}python${PYVER} 7 | 8 | # find the root of the virtualenv, it should be the parent of the dir this script is in 9 | ENV=`$PYTHON -c "import os; print os.path.abspath(os.path.join(os.path.dirname(\"$0\"), '..'))"` 10 | 11 | # now run Python with the virtualenv set as Python's HOME 12 | export PYTHONHOME=$ENV 13 | exec $PYTHON "$@" 14 | -------------------------------------------------------------------------------- /assignment1/carlos/requirements.txt: -------------------------------------------------------------------------------- 1 | Jinja2==2.8 2 | MarkupSafe==0.23 3 | Pillow==3.0.0 4 | Pygments==2.0.2 5 | appnope==0.1.0 6 | backports-abc==0.4 7 | backports.ssl-match-hostname==3.5.0.1 8 | certifi==2015.11.20.1 9 | cycler==0.9.0 10 | decorator==4.0.6 11 | functools32==3.2.3-2 12 | gnureadline==6.3.3 13 | ipykernel==4.2.2 14 | ipython==4.0.1 15 | ipython-genutils==0.1.0 16 | ipywidgets==4.1.1 17 | jsonschema==2.5.1 18 | jupyter==1.0.0 19 | jupyter-client==4.1.1 20 | jupyter-console==4.0.3 21 | jupyter-core==4.0.6 22 | matplotlib==1.5.0 23 | mistune==0.7.1 24 | nbconvert==4.1.0 25 | nbformat==4.0.1 26 | notebook==4.0.6 27 | numpy==1.10.4 28 | path.py==8.1.2 29 | pexpect==4.0.1 30 | pickleshare==0.5 31 | ptyprocess==0.5 32 | pyparsing==2.0.7 33 | python-dateutil==2.4.2 34 | pytz==2015.7 35 | pyzmq==15.1.0 36 | qtconsole==4.1.1 37 | scipy==0.16.1 38 | simplegeneric==0.8.1 39 | singledispatch==3.4.0.3 40 | six==1.10.0 41 | terminado==0.5 42 | tornado==4.3 43 | traitlets==4.0.0 44 | wsgiref==0.1.2 45 | jupyter==1.0.0 46 | pillow==3.1.0 47 | -------------------------------------------------------------------------------- /assignment1/carlos/start_ipython_osx.sh: -------------------------------------------------------------------------------- 1 | # Assume the virtualenv is called .env 2 | 3 | cp frameworkpython .env/bin 4 | .env/bin/frameworkpython -m IPython notebook 5 | -------------------------------------------------------------------------------- /assignment1/christiaan/README.md: -------------------------------------------------------------------------------- 1 | Details about this assignment can be found [on the course webpage](http://cs231n.github.io/), under Assignment #1 of Winter 2016. 2 | -------------------------------------------------------------------------------- /assignment1/christiaan/collectSubmission.sh: -------------------------------------------------------------------------------- 1 | rm -f assignment1.zip 2 | zip -r assignment1.zip . -x "*.git*" "*cs231n/datasets*" "*.ipynb_checkpoints*" "*README.md" "*collectSubmission.sh" "*requirements.txt" 3 | -------------------------------------------------------------------------------- /assignment1/christiaan/cs231n/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment1/christiaan/cs231n/__init__.py -------------------------------------------------------------------------------- /assignment1/christiaan/cs231n/classifiers/__init__.py: -------------------------------------------------------------------------------- 1 | from cs231n.classifiers.k_nearest_neighbor import * 2 | from cs231n.classifiers.linear_classifier import * 3 | -------------------------------------------------------------------------------- /assignment1/christiaan/cs231n/datasets/.gitignore: -------------------------------------------------------------------------------- 1 | cifar-10-batches-py/* 2 | tiny-imagenet-100-A* 3 | tiny-imagenet-100-B* 4 | tiny-100-A-pretrained/* 5 | -------------------------------------------------------------------------------- /assignment1/christiaan/cs231n/datasets/get_datasets.sh: -------------------------------------------------------------------------------- 1 | # Get CIFAR10 2 | wget http://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz 3 | tar -xzvf cifar-10-python.tar.gz 4 | rm cifar-10-python.tar.gz 5 | -------------------------------------------------------------------------------- /assignment1/christiaan/cs231n/vis_utils.py: -------------------------------------------------------------------------------- 1 | from math import sqrt, ceil 2 | import numpy as np 3 | 4 | def visualize_grid(Xs, ubound=255.0, padding=1): 5 | """ 6 | Reshape a 4D tensor of image data to a grid for easy visualization. 7 | 8 | Inputs: 9 | - Xs: Data of shape (N, H, W, C) 10 | - ubound: Output grid will have values scaled to the range [0, ubound] 11 | - padding: The number of blank pixels between elements of the grid 12 | """ 13 | (N, H, W, C) = Xs.shape 14 | grid_size = int(ceil(sqrt(N))) 15 | grid_height = H * grid_size + padding * (grid_size - 1) 16 | grid_width = W * grid_size + padding * (grid_size - 1) 17 | grid = np.zeros((grid_height, grid_width, C)) 18 | next_idx = 0 19 | y0, y1 = 0, H 20 | for y in xrange(grid_size): 21 | x0, x1 = 0, W 22 | for x in xrange(grid_size): 23 | if next_idx < N: 24 | img = Xs[next_idx] 25 | low, high = np.min(img), np.max(img) 26 | grid[y0:y1, x0:x1] = ubound * (img - low) / (high - low) 27 | # grid[y0:y1, x0:x1] = Xs[next_idx] 28 | next_idx += 1 29 | x0 += W + padding 30 | x1 += W + padding 31 | y0 += H + padding 32 | y1 += H + padding 33 | # grid_max = np.max(grid) 34 | # grid_min = np.min(grid) 35 | # grid = ubound * (grid - grid_min) / (grid_max - grid_min) 36 | return grid 37 | 38 | def vis_grid(Xs): 39 | """ visualize a grid of images """ 40 | (N, H, W, C) = Xs.shape 41 | A = int(ceil(sqrt(N))) 42 | G = np.ones((A*H+A, A*W+A, C), Xs.dtype) 43 | G *= np.min(Xs) 44 | n = 0 45 | for y in range(A): 46 | for x in range(A): 47 | if n < N: 48 | G[y*H+y:(y+1)*H+y, x*W+x:(x+1)*W+x, :] = Xs[n,:,:,:] 49 | n += 1 50 | # normalize to [0,1] 51 | maxg = G.max() 52 | ming = G.min() 53 | G = (G - ming)/(maxg-ming) 54 | return G 55 | 56 | def vis_nn(rows): 57 | """ visualize array of arrays of images """ 58 | N = len(rows) 59 | D = len(rows[0]) 60 | H,W,C = rows[0][0].shape 61 | Xs = rows[0][0] 62 | G = np.ones((N*H+N, D*W+D, C), Xs.dtype) 63 | for y in range(N): 64 | for x in range(D): 65 | G[y*H+y:(y+1)*H+y, x*W+x:(x+1)*W+x, :] = rows[y][x] 66 | # normalize to [0,1] 67 | maxg = G.max() 68 | ming = G.min() 69 | G = (G - ming)/(maxg-ming) 70 | return G 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /assignment1/christiaan/frameworkpython: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # what real Python executable to use 4 | PYVER=2.7 5 | PATHTOPYTHON=/usr/local/bin/ 6 | PYTHON=${PATHTOPYTHON}python${PYVER} 7 | 8 | # find the root of the virtualenv, it should be the parent of the dir this script is in 9 | ENV=`$PYTHON -c "import os; print os.path.abspath(os.path.join(os.path.dirname(\"$0\"), '..'))"` 10 | 11 | # now run Python with the virtualenv set as Python's HOME 12 | export PYTHONHOME=$ENV 13 | exec $PYTHON "$@" 14 | -------------------------------------------------------------------------------- /assignment1/christiaan/requirements.txt: -------------------------------------------------------------------------------- 1 | Jinja2==2.8 2 | MarkupSafe==0.23 3 | Pillow==3.0.0 4 | Pygments==2.0.2 5 | appnope==0.1.0 6 | backports-abc==0.4 7 | backports.ssl-match-hostname==3.5.0.1 8 | certifi==2015.11.20.1 9 | cycler==0.9.0 10 | decorator==4.0.6 11 | functools32==3.2.3-2 12 | gnureadline==6.3.3 13 | ipykernel==4.2.2 14 | ipython==4.0.1 15 | ipython-genutils==0.1.0 16 | ipywidgets==4.1.1 17 | jsonschema==2.5.1 18 | jupyter==1.0.0 19 | jupyter-client==4.1.1 20 | jupyter-console==4.0.3 21 | jupyter-core==4.0.6 22 | matplotlib==1.5.0 23 | mistune==0.7.1 24 | nbconvert==4.1.0 25 | nbformat==4.0.1 26 | notebook==4.0.6 27 | numpy==1.10.4 28 | path.py==8.1.2 29 | pexpect==4.0.1 30 | pickleshare==0.5 31 | ptyprocess==0.5 32 | pyparsing==2.0.7 33 | python-dateutil==2.4.2 34 | pytz==2015.7 35 | pyzmq==15.1.0 36 | qtconsole==4.1.1 37 | scipy==0.16.1 38 | simplegeneric==0.8.1 39 | singledispatch==3.4.0.3 40 | six==1.10.0 41 | terminado==0.5 42 | tornado==4.3 43 | traitlets==4.0.0 44 | wsgiref==0.1.2 45 | #jupyter==1.0.0 46 | #pillow==3.1.0 47 | -------------------------------------------------------------------------------- /assignment1/christiaan/softmax_gradient.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment1/christiaan/softmax_gradient.jpg -------------------------------------------------------------------------------- /assignment1/christiaan/start_ipython_osx.sh: -------------------------------------------------------------------------------- 1 | # Assume the virtualenv is called .env 2 | 3 | cp frameworkpython .env/bin 4 | .env/bin/frameworkpython -m IPython notebook 5 | -------------------------------------------------------------------------------- /assignment1/dafne/README.md: -------------------------------------------------------------------------------- 1 | Details about this assignment can be found [on the course webpage](http://cs231n.github.io/), under Assignment #1 of Winter 2016. 2 | -------------------------------------------------------------------------------- /assignment1/dafne/collectSubmission.sh: -------------------------------------------------------------------------------- 1 | rm -f assignment1.zip 2 | zip -r assignment1.zip . -x "*.git*" "*cs231n/datasets*" "*.ipynb_checkpoints*" "*README.md" "*collectSubmission.sh" "*requirements.txt" 3 | -------------------------------------------------------------------------------- /assignment1/dafne/cs231n/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment1/dafne/cs231n/__init__.py -------------------------------------------------------------------------------- /assignment1/dafne/cs231n/classifiers/__init__.py: -------------------------------------------------------------------------------- 1 | from cs231n.classifiers.k_nearest_neighbor import * 2 | from cs231n.classifiers.linear_classifier import * 3 | -------------------------------------------------------------------------------- /assignment1/dafne/cs231n/classifiers/linear_svm.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from random import shuffle 3 | 4 | def svm_loss_naive(W, X, y, reg): 5 | """ 6 | Structured SVM loss function, naive implementation (with loops). 7 | 8 | Inputs have dimension D, there are C classes, and we operate on minibatches 9 | of N examples. 10 | 11 | Inputs: 12 | - W: A numpy array of shape (D, C) containing weights. 13 | - X: A numpy array of shape (N, D) containing a minibatch of data. 14 | - y: A numpy array of shape (N,) containing training labels; y[i] = c means 15 | that X[i] has label c, where 0 <= c < C. 16 | - reg: (float) regularization strength 17 | 18 | Returns a tuple of: 19 | - loss as single float 20 | - gradient with respect to weights W; an array of same shape as W 21 | """ 22 | dW = np.zeros(W.shape) # initialize the gradient as zero 23 | 24 | # compute the loss and the gradient 25 | num_classes = W.shape[1] 26 | num_train = X.shape[0] 27 | loss = 0.0 28 | for i in xrange(num_train): 29 | scores = X[i].dot(W) 30 | correct_class_score = scores[y[i]] 31 | subgrad = np.zeros(W.shape) 32 | nrMarginOverflows = 0 33 | for j in xrange(num_classes): 34 | if j == y[i]: 35 | continue 36 | margin = scores[j] - correct_class_score + 1 # note delta = 1 37 | if margin > 0: 38 | loss += margin 39 | subgrad[:,j] = X[i] 40 | nrMarginOverflows += 1 41 | subgrad[:,y[i]] = -1*nrMarginOverflows*X[i] 42 | dW += subgrad 43 | 44 | # Right now the loss is a sum over all training examples, but we want it 45 | # to be an average instead so we divide by num_train. 46 | loss /= num_train 47 | 48 | # Add regularization to the loss. 49 | loss += 0.5 * reg * np.sum(W * W) 50 | 51 | #Scale the gradient 52 | dW /= num_train 53 | 54 | 55 | return loss, dW 56 | 57 | 58 | def svm_loss_vectorized(W, X, y, reg): 59 | """ 60 | Structured SVM loss function, vectorized implementation. 61 | 62 | Inputs and outputs are the same as svm_loss_naive. 63 | """ 64 | loss = 0.0 65 | dW = np.zeros(W.shape) # initialize the gradient as zero 66 | 67 | num_classes = W.shape[1] 68 | num_train = X.shape[0] 69 | 70 | scores = X.dot(W) 71 | correct_class_score = scores[xrange(num_train), y] 72 | margins = np.maximum(scores - np.transpose([correct_class_score]*num_classes) + 1, 0) 73 | margins[xrange(num_train), y] = 0 74 | loss = np.sum(margins)/num_train 75 | loss += 0.5 * reg * np.sum(W * W) 76 | 77 | #Compute the gradient 78 | margindices_incorrect = 1*(margins > 0) 79 | nOverflow = margindices_incorrect.sum(1) 80 | true_indices = np.zeros(margindices_incorrect.shape) 81 | true_indices[xrange(num_train),y] = 1 82 | margindices_correct = true_indices * np.transpose([nOverflow]*num_classes) 83 | 84 | dW = X.transpose().dot(margindices_incorrect) - X.transpose().dot(margindices_correct) 85 | dW /= num_train 86 | 87 | return loss, dW 88 | -------------------------------------------------------------------------------- /assignment1/dafne/cs231n/classifiers/softmax.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from random import shuffle 3 | 4 | def softmax_loss_naive(W, X, y, reg): 5 | """ 6 | Softmax loss function, naive implementation (with loops) 7 | 8 | Inputs have dimension D, there are C classes, and we operate on minibatches 9 | of N examples. 10 | 11 | Inputs: 12 | - W: A numpy array of shape (D, C) containing weights. 13 | - X: A numpy array of shape (N, D) containing a minibatch of data. 14 | - y: A numpy array of shape (N,) containing training labels; y[i] = c means 15 | that X[i] has label c, where 0 <= c < C. 16 | - reg: (float) regularization strength 17 | 18 | Returns a tuple of: 19 | - loss as single float 20 | - gradient with respect to weights W; an array of same shape as W 21 | """ 22 | # Initialize the loss and gradient to zero. 23 | loss = 0.0 24 | dW = np.zeros_like(W) 25 | 26 | ############################################################################# 27 | # TODO: Compute the softmax loss and its gradient using explicit loops. # 28 | # Store the loss in loss and the gradient in dW. If you are not careful # 29 | # here, it is easy to run into numeric instability. Don't forget the # 30 | # regularization! # 31 | ############################################################################# 32 | num_classes = W.shape[1] 33 | num_train = X.shape[0] 34 | 35 | loss = 0.0 36 | for i in xrange(num_train): 37 | scores = X[i].dot(W) 38 | maxscore = np.max(scores) 39 | shifted_scores = scores - maxscore 40 | probs = np.exp(shifted_scores) / np.sum(np.exp(shifted_scores)) 41 | loss += -np.log( probs[y[i]] ) 42 | for j in xrange(num_classes): 43 | if y[i] == j: 44 | dW[:,j] += -X[i] * (1-probs[j]) 45 | else: 46 | dW[:,j] += -X[i] * (-probs[j]) 47 | # Right now the loss is a sum over all training examples, but we want it 48 | # to be an average instead so we divide by num_train. 49 | loss /= num_train 50 | loss += 0.5 * reg * np.sum(W * W) 51 | 52 | dW /= num_train 53 | ############################################################################# 54 | # END OF YOUR CODE # 55 | ############################################################################# 56 | 57 | return loss, dW 58 | 59 | 60 | def softmax_loss_vectorized(W, X, y, reg): 61 | """ 62 | Softmax loss function, vectorized version. 63 | 64 | Inputs and outputs are the same as softmax_loss_naive. 65 | """ 66 | # Initialize the loss and gradient to zero. 67 | loss = 0.0 68 | dW = np.zeros_like(W) 69 | 70 | ############################################################################# 71 | # TODO: Compute the softmax loss and its gradient using no explicit loops. # 72 | # Store the loss in loss and the gradient in dW. If you are not careful # 73 | # here, it is easy to run into numeric instability. Don't forget the # 74 | # regularization! # 75 | ############################################################################# 76 | num_classes = W.shape[1] 77 | num_train = X.shape[0] 78 | 79 | scores = X.dot(W) 80 | maxscore = np.max(scores, 1) 81 | shifted_scores = scores - maxscore[:,np.newaxis] 82 | probs = np.exp(shifted_scores) / np.sum(np.exp(shifted_scores), 1)[:,np.newaxis] 83 | 84 | losses = -np.log( probs[xrange(num_train), y] ) 85 | loss = losses.sum() / num_train 86 | loss += 0.5 * reg * np.sum(W * W) 87 | 88 | true_indices = np.zeros(probs.shape) 89 | true_indices[xrange(num_train),y] = 1 90 | dW = - X.transpose().dot(true_indices - probs) 91 | dW /= num_train 92 | ############################################################################# 93 | # END OF YOUR CODE # 94 | ############################################################################# 95 | 96 | return loss, dW 97 | 98 | -------------------------------------------------------------------------------- /assignment1/dafne/cs231n/datasets/.gitignore: -------------------------------------------------------------------------------- 1 | cifar-10-batches-py/* 2 | tiny-imagenet-100-A* 3 | tiny-imagenet-100-B* 4 | tiny-100-A-pretrained/* 5 | -------------------------------------------------------------------------------- /assignment1/dafne/cs231n/datasets/get_datasets.sh: -------------------------------------------------------------------------------- 1 | # Get CIFAR10 2 | wget http://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz 3 | tar -xzvf cifar-10-python.tar.gz 4 | rm cifar-10-python.tar.gz 5 | -------------------------------------------------------------------------------- /assignment1/dafne/cs231n/gradient_check.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from random import randrange 3 | 4 | def eval_numerical_gradient(f, x, verbose=True, h=0.00001): 5 | """ 6 | a naive implementation of numerical gradient of f at x 7 | - f should be a function that takes a single argument 8 | - x is the point (numpy array) to evaluate the gradient at 9 | """ 10 | 11 | fx = f(x) # evaluate function value at original point 12 | grad = np.zeros_like(x) 13 | # iterate over all indexes in x 14 | it = np.nditer(x, flags=['multi_index'], op_flags=['readwrite']) 15 | while not it.finished: 16 | 17 | # evaluate function at x+h 18 | ix = it.multi_index 19 | oldval = x[ix] 20 | x[ix] = oldval + h # increment by h 21 | fxph = f(x) # evalute f(x + h) 22 | x[ix] = oldval - h 23 | fxmh = f(x) # evaluate f(x - h) 24 | x[ix] = oldval # restore 25 | 26 | # compute the partial derivative with centered formula 27 | grad[ix] = (fxph - fxmh) / (2 * h) # the slope 28 | if verbose: 29 | print ix, grad[ix] 30 | it.iternext() # step to next dimension 31 | 32 | return grad 33 | 34 | 35 | def eval_numerical_gradient_array(f, x, df, h=1e-5): 36 | """ 37 | Evaluate a numeric gradient for a function that accepts a numpy 38 | array and returns a numpy array. 39 | """ 40 | grad = np.zeros_like(x) 41 | it = np.nditer(x, flags=['multi_index'], op_flags=['readwrite']) 42 | while not it.finished: 43 | ix = it.multi_index 44 | 45 | oldval = x[ix] 46 | x[ix] = oldval + h 47 | pos = f(x).copy() 48 | x[ix] = oldval - h 49 | neg = f(x).copy() 50 | x[ix] = oldval 51 | 52 | grad[ix] = np.sum((pos - neg) * df) / (2 * h) 53 | it.iternext() 54 | return grad 55 | 56 | 57 | def eval_numerical_gradient_blobs(f, inputs, output, h=1e-5): 58 | """ 59 | Compute numeric gradients for a function that operates on input 60 | and output blobs. 61 | 62 | We assume that f accepts several input blobs as arguments, followed by a blob 63 | into which outputs will be written. For example, f might be called like this: 64 | 65 | f(x, w, out) 66 | 67 | where x and w are input Blobs, and the result of f will be written to out. 68 | 69 | Inputs: 70 | - f: function 71 | - inputs: tuple of input blobs 72 | - output: output blob 73 | - h: step size 74 | """ 75 | numeric_diffs = [] 76 | for input_blob in inputs: 77 | diff = np.zeros_like(input_blob.diffs) 78 | it = np.nditer(input_blob.vals, flags=['multi_index'], 79 | op_flags=['readwrite']) 80 | while not it.finished: 81 | idx = it.multi_index 82 | orig = input_blob.vals[idx] 83 | 84 | input_blob.vals[idx] = orig + h 85 | f(*(inputs + (output,))) 86 | pos = np.copy(output.vals) 87 | input_blob.vals[idx] = orig - h 88 | f(*(inputs + (output,))) 89 | neg = np.copy(output.vals) 90 | input_blob.vals[idx] = orig 91 | 92 | diff[idx] = np.sum((pos - neg) * output.diffs) / (2.0 * h) 93 | 94 | it.iternext() 95 | numeric_diffs.append(diff) 96 | return numeric_diffs 97 | 98 | 99 | def eval_numerical_gradient_net(net, inputs, output, h=1e-5): 100 | return eval_numerical_gradient_blobs(lambda *args: net.forward(), 101 | inputs, output, h=h) 102 | 103 | 104 | def grad_check_sparse(f, x, analytic_grad, num_checks=10, h=1e-5): 105 | """ 106 | sample a few random elements and only return numerical 107 | in this dimensions. 108 | """ 109 | 110 | for i in xrange(num_checks): 111 | ix = tuple([randrange(m) for m in x.shape]) 112 | 113 | oldval = x[ix] 114 | x[ix] = oldval + h # increment by h 115 | fxph = f(x) # evaluate f(x + h) 116 | x[ix] = oldval - h # increment by h 117 | fxmh = f(x) # evaluate f(x - h) 118 | x[ix] = oldval # reset 119 | 120 | grad_numerical = (fxph - fxmh) / (2 * h) 121 | grad_analytic = analytic_grad[ix] 122 | rel_error = abs(grad_numerical - grad_analytic) / (abs(grad_numerical) + abs(grad_analytic)) 123 | print 'numerical: %f analytic: %f, relative error: %e' % (grad_numerical, grad_analytic, rel_error) 124 | 125 | -------------------------------------------------------------------------------- /assignment1/dafne/cs231n/vis_utils.py: -------------------------------------------------------------------------------- 1 | from math import sqrt, ceil 2 | import numpy as np 3 | 4 | def visualize_grid(Xs, ubound=255.0, padding=1): 5 | """ 6 | Reshape a 4D tensor of image data to a grid for easy visualization. 7 | 8 | Inputs: 9 | - Xs: Data of shape (N, H, W, C) 10 | - ubound: Output grid will have values scaled to the range [0, ubound] 11 | - padding: The number of blank pixels between elements of the grid 12 | """ 13 | (N, H, W, C) = Xs.shape 14 | grid_size = int(ceil(sqrt(N))) 15 | grid_height = H * grid_size + padding * (grid_size - 1) 16 | grid_width = W * grid_size + padding * (grid_size - 1) 17 | grid = np.zeros((grid_height, grid_width, C)) 18 | next_idx = 0 19 | y0, y1 = 0, H 20 | for y in xrange(grid_size): 21 | x0, x1 = 0, W 22 | for x in xrange(grid_size): 23 | if next_idx < N: 24 | img = Xs[next_idx] 25 | low, high = np.min(img), np.max(img) 26 | grid[y0:y1, x0:x1] = ubound * (img - low) / (high - low) 27 | # grid[y0:y1, x0:x1] = Xs[next_idx] 28 | next_idx += 1 29 | x0 += W + padding 30 | x1 += W + padding 31 | y0 += H + padding 32 | y1 += H + padding 33 | # grid_max = np.max(grid) 34 | # grid_min = np.min(grid) 35 | # grid = ubound * (grid - grid_min) / (grid_max - grid_min) 36 | return grid 37 | 38 | def vis_grid(Xs): 39 | """ visualize a grid of images """ 40 | (N, H, W, C) = Xs.shape 41 | A = int(ceil(sqrt(N))) 42 | G = np.ones((A*H+A, A*W+A, C), Xs.dtype) 43 | G *= np.min(Xs) 44 | n = 0 45 | for y in range(A): 46 | for x in range(A): 47 | if n < N: 48 | G[y*H+y:(y+1)*H+y, x*W+x:(x+1)*W+x, :] = Xs[n,:,:,:] 49 | n += 1 50 | # normalize to [0,1] 51 | maxg = G.max() 52 | ming = G.min() 53 | G = (G - ming)/(maxg-ming) 54 | return G 55 | 56 | def vis_nn(rows): 57 | """ visualize array of arrays of images """ 58 | N = len(rows) 59 | D = len(rows[0]) 60 | H,W,C = rows[0][0].shape 61 | Xs = rows[0][0] 62 | G = np.ones((N*H+N, D*W+D, C), Xs.dtype) 63 | for y in range(N): 64 | for x in range(D): 65 | G[y*H+y:(y+1)*H+y, x*W+x:(x+1)*W+x, :] = rows[y][x] 66 | # normalize to [0,1] 67 | maxg = G.max() 68 | ming = G.min() 69 | G = (G - ming)/(maxg-ming) 70 | return G 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /assignment1/dafne/frameworkpython: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # what real Python executable to use 4 | PYVER=2.7 5 | PATHTOPYTHON=/usr/local/bin/ 6 | PYTHON=${PATHTOPYTHON}python${PYVER} 7 | 8 | # find the root of the virtualenv, it should be the parent of the dir this script is in 9 | ENV=`$PYTHON -c "import os; print os.path.abspath(os.path.join(os.path.dirname(\"$0\"), '..'))"` 10 | 11 | # now run Python with the virtualenv set as Python's HOME 12 | export PYTHONHOME=$ENV 13 | exec $PYTHON "$@" 14 | -------------------------------------------------------------------------------- /assignment1/dafne/requirements.txt: -------------------------------------------------------------------------------- 1 | Jinja2==2.8 2 | MarkupSafe==0.23 3 | Pillow==3.0.0 4 | Pygments==2.0.2 5 | appnope==0.1.0 6 | backports-abc==0.4 7 | backports.ssl-match-hostname==3.5.0.1 8 | certifi==2015.11.20.1 9 | cycler==0.9.0 10 | decorator==4.0.6 11 | functools32==3.2.3-2 12 | gnureadline==6.3.3 13 | ipykernel==4.2.2 14 | ipython==4.0.1 15 | ipython-genutils==0.1.0 16 | ipywidgets==4.1.1 17 | jsonschema==2.5.1 18 | jupyter==1.0.0 19 | jupyter-client==4.1.1 20 | jupyter-console==4.0.3 21 | jupyter-core==4.0.6 22 | matplotlib==1.5.0 23 | mistune==0.7.1 24 | nbconvert==4.1.0 25 | nbformat==4.0.1 26 | notebook==4.0.6 27 | numpy==1.10.4 28 | path.py==8.1.2 29 | pexpect==4.0.1 30 | pickleshare==0.5 31 | ptyprocess==0.5 32 | pyparsing==2.0.7 33 | python-dateutil==2.4.2 34 | pytz==2015.7 35 | pyzmq==15.1.0 36 | qtconsole==4.1.1 37 | scipy==0.16.1 38 | simplegeneric==0.8.1 39 | singledispatch==3.4.0.3 40 | six==1.10.0 41 | terminado==0.5 42 | tornado==4.3 43 | traitlets==4.0.0 44 | wsgiref==0.1.2 45 | jupyter==1.0.0 46 | pillow==3.1.0 47 | -------------------------------------------------------------------------------- /assignment1/dafne/start_ipython_osx.sh: -------------------------------------------------------------------------------- 1 | # Assume the virtualenv is called .env 2 | 3 | cp frameworkpython .env/bin 4 | .env/bin/frameworkpython -m IPython notebook 5 | -------------------------------------------------------------------------------- /assignment1/elena/.gitignore: -------------------------------------------------------------------------------- 1 | venv/ 2 | *.pyc 3 | .ipynb_checkpoints/ 4 | -------------------------------------------------------------------------------- /assignment1/elena/README.md: -------------------------------------------------------------------------------- 1 | Details about this assignment can be found [on the course webpage](http://cs231n.github.io/), under Assignment #1 of Winter 2016. 2 | -------------------------------------------------------------------------------- /assignment1/elena/collectSubmission.sh: -------------------------------------------------------------------------------- 1 | rm -f assignment1.zip 2 | zip -r assignment1.zip . -x "*.git*" "*cs231n/datasets*" "*.ipynb_checkpoints*" "*README.md" "*collectSubmission.sh" "*requirements.txt" 3 | -------------------------------------------------------------------------------- /assignment1/elena/cs231n/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment1/elena/cs231n/__init__.py -------------------------------------------------------------------------------- /assignment1/elena/cs231n/classifiers/__init__.py: -------------------------------------------------------------------------------- 1 | from cs231n.classifiers.k_nearest_neighbor import * 2 | from cs231n.classifiers.linear_classifier import * 3 | -------------------------------------------------------------------------------- /assignment1/elena/cs231n/classifiers/softmax.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from random import shuffle 3 | 4 | def softmax_loss_naive(W, X, y, reg): 5 | """ 6 | Softmax loss function, naive implementation (with loops) 7 | 8 | Inputs have dimension D, there are C classes, and we operate on minibatches 9 | of N examples. 10 | 11 | Inputs: 12 | - W: A numpy array of shape (D, C) containing weights. 13 | - X: A numpy array of shape (N, D) containing a minibatch of data. 14 | - y: A numpy array of shape (N,) containing training labels; y[i] = c means 15 | that X[i] has label c, where 0 <= c < C. 16 | - reg: (float) regularization strength 17 | 18 | Returns a tuple of: 19 | - loss as single float 20 | - gradient with respect to weights W; an array of same shape as W 21 | """ 22 | # Initialize the loss and gradient to zero. 23 | loss = 0.0 24 | dW = np.zeros_like(W) 25 | 26 | ############################################################################# 27 | # TODO: Compute the softmax loss and its gradient using explicit loops. # 28 | # Store the loss in loss and the gradient in dW. If you are not careful # 29 | # here, it is easy to run into numeric instability. Don't forget the # 30 | # regularization! # 31 | ############################################################################# 32 | pass 33 | ############################################################################# 34 | # END OF YOUR CODE # 35 | ############################################################################# 36 | 37 | return loss, dW 38 | 39 | 40 | def softmax_loss_vectorized(W, X, y, reg): 41 | """ 42 | Softmax loss function, vectorized version. 43 | 44 | Inputs and outputs are the same as softmax_loss_naive. 45 | """ 46 | # Initialize the loss and gradient to zero. 47 | loss = 0.0 48 | dW = np.zeros_like(W) 49 | 50 | ############################################################################# 51 | # TODO: Compute the softmax loss and its gradient using no explicit loops. # 52 | # Store the loss in loss and the gradient in dW. If you are not careful # 53 | # here, it is easy to run into numeric instability. Don't forget the # 54 | # regularization! # 55 | ############################################################################# 56 | pass 57 | ############################################################################# 58 | # END OF YOUR CODE # 59 | ############################################################################# 60 | 61 | return loss, dW 62 | 63 | -------------------------------------------------------------------------------- /assignment1/elena/cs231n/datasets/.gitignore: -------------------------------------------------------------------------------- 1 | cifar-10-batches-py/* 2 | tiny-imagenet-100-A* 3 | tiny-imagenet-100-B* 4 | tiny-100-A-pretrained/* 5 | -------------------------------------------------------------------------------- /assignment1/elena/cs231n/datasets/get_datasets.sh: -------------------------------------------------------------------------------- 1 | # Get CIFAR10 2 | wget http://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz 3 | tar -xzvf cifar-10-python.tar.gz 4 | rm cifar-10-python.tar.gz 5 | -------------------------------------------------------------------------------- /assignment1/elena/cs231n/vis_utils.py: -------------------------------------------------------------------------------- 1 | from math import sqrt, ceil 2 | import numpy as np 3 | 4 | def visualize_grid(Xs, ubound=255.0, padding=1): 5 | """ 6 | Reshape a 4D tensor of image data to a grid for easy visualization. 7 | 8 | Inputs: 9 | - Xs: Data of shape (N, H, W, C) 10 | - ubound: Output grid will have values scaled to the range [0, ubound] 11 | - padding: The number of blank pixels between elements of the grid 12 | """ 13 | (N, H, W, C) = Xs.shape 14 | grid_size = int(ceil(sqrt(N))) 15 | grid_height = H * grid_size + padding * (grid_size - 1) 16 | grid_width = W * grid_size + padding * (grid_size - 1) 17 | grid = np.zeros((grid_height, grid_width, C)) 18 | next_idx = 0 19 | y0, y1 = 0, H 20 | for y in xrange(grid_size): 21 | x0, x1 = 0, W 22 | for x in xrange(grid_size): 23 | if next_idx < N: 24 | img = Xs[next_idx] 25 | low, high = np.min(img), np.max(img) 26 | grid[y0:y1, x0:x1] = ubound * (img - low) / (high - low) 27 | # grid[y0:y1, x0:x1] = Xs[next_idx] 28 | next_idx += 1 29 | x0 += W + padding 30 | x1 += W + padding 31 | y0 += H + padding 32 | y1 += H + padding 33 | # grid_max = np.max(grid) 34 | # grid_min = np.min(grid) 35 | # grid = ubound * (grid - grid_min) / (grid_max - grid_min) 36 | return grid 37 | 38 | def vis_grid(Xs): 39 | """ visualize a grid of images """ 40 | (N, H, W, C) = Xs.shape 41 | A = int(ceil(sqrt(N))) 42 | G = np.ones((A*H+A, A*W+A, C), Xs.dtype) 43 | G *= np.min(Xs) 44 | n = 0 45 | for y in range(A): 46 | for x in range(A): 47 | if n < N: 48 | G[y*H+y:(y+1)*H+y, x*W+x:(x+1)*W+x, :] = Xs[n,:,:,:] 49 | n += 1 50 | # normalize to [0,1] 51 | maxg = G.max() 52 | ming = G.min() 53 | G = (G - ming)/(maxg-ming) 54 | return G 55 | 56 | def vis_nn(rows): 57 | """ visualize array of arrays of images """ 58 | N = len(rows) 59 | D = len(rows[0]) 60 | H,W,C = rows[0][0].shape 61 | Xs = rows[0][0] 62 | G = np.ones((N*H+N, D*W+D, C), Xs.dtype) 63 | for y in range(N): 64 | for x in range(D): 65 | G[y*H+y:(y+1)*H+y, x*W+x:(x+1)*W+x, :] = rows[y][x] 66 | # normalize to [0,1] 67 | maxg = G.max() 68 | ming = G.min() 69 | G = (G - ming)/(maxg-ming) 70 | return G 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /assignment1/elena/frameworkpython: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # what real Python executable to use 4 | PYVER=2.7 5 | PATHTOPYTHON=/usr/local/bin/ 6 | PYTHON=${PATHTOPYTHON}python${PYVER} 7 | 8 | # find the root of the virtualenv, it should be the parent of the dir this script is in 9 | ENV=`$PYTHON -c "import os; print os.path.abspath(os.path.join(os.path.dirname(\"$0\"), '..'))"` 10 | 11 | # now run Python with the virtualenv set as Python's HOME 12 | export PYTHONHOME=$ENV 13 | exec $PYTHON "$@" 14 | -------------------------------------------------------------------------------- /assignment1/elena/requirements.txt: -------------------------------------------------------------------------------- 1 | Jinja2==2.8 2 | MarkupSafe==0.23 3 | Pillow==3.0.0 4 | Pygments==2.0.2 5 | appnope==0.1.0 6 | backports-abc==0.4 7 | backports.ssl-match-hostname==3.5.0.1 8 | certifi==2015.11.20.1 9 | cycler==0.9.0 10 | decorator==4.0.6 11 | functools32==3.2.3-2 12 | gnureadline==6.3.3 13 | ipykernel==4.2.2 14 | ipython==4.0.1 15 | ipython-genutils==0.1.0 16 | ipywidgets==4.1.1 17 | jsonschema==2.5.1 18 | jupyter==1.0.0 19 | jupyter-client==4.1.1 20 | jupyter-console==4.0.3 21 | jupyter-core==4.0.6 22 | matplotlib==1.5.0 23 | mistune==0.7.1 24 | nbconvert==4.1.0 25 | nbformat==4.0.1 26 | notebook==4.0.6 27 | numpy==1.10.4 28 | path.py==8.1.2 29 | pexpect==4.0.1 30 | pickleshare==0.5 31 | ptyprocess==0.5 32 | pyparsing==2.0.7 33 | python-dateutil==2.4.2 34 | pytz==2015.7 35 | pyzmq==15.1.0 36 | qtconsole==4.1.1 37 | scipy==0.16.1 38 | simplegeneric==0.8.1 39 | singledispatch==3.4.0.3 40 | six==1.10.0 41 | terminado==0.5 42 | tornado==4.3 43 | traitlets==4.0.0 44 | wsgiref==0.1.2 45 | jupyter==1.0.0 46 | pillow==3.1.0 47 | -------------------------------------------------------------------------------- /assignment1/elena/start_ipython_osx.sh: -------------------------------------------------------------------------------- 1 | # Assume the virtualenv is called .env 2 | 3 | cp frameworkpython .env/bin 4 | .env/bin/frameworkpython -m IPython notebook 5 | -------------------------------------------------------------------------------- /assignment1/vincent/README.md: -------------------------------------------------------------------------------- 1 | Details about this assignment can be found [on the course webpage](http://cs231n.github.io/), under Assignment #1 of Winter 2016. 2 | -------------------------------------------------------------------------------- /assignment1/vincent/collectSubmission.sh: -------------------------------------------------------------------------------- 1 | rm -f assignment1.zip 2 | zip -r assignment1.zip . -x "*.git*" "*cs231n/datasets*" "*.ipynb_checkpoints*" "*README.md" "*collectSubmission.sh" "*requirements.txt" 3 | -------------------------------------------------------------------------------- /assignment1/vincent/cs231n/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment1/vincent/cs231n/__init__.py -------------------------------------------------------------------------------- /assignment1/vincent/cs231n/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment1/vincent/cs231n/__init__.pyc -------------------------------------------------------------------------------- /assignment1/vincent/cs231n/classifiers/__init__.py: -------------------------------------------------------------------------------- 1 | from cs231n.classifiers.k_nearest_neighbor import * 2 | from cs231n.classifiers.linear_classifier import * 3 | -------------------------------------------------------------------------------- /assignment1/vincent/cs231n/classifiers/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment1/vincent/cs231n/classifiers/__init__.pyc -------------------------------------------------------------------------------- /assignment1/vincent/cs231n/classifiers/backup/k_nearest_neighbor.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment1/vincent/cs231n/classifiers/backup/k_nearest_neighbor.pyc -------------------------------------------------------------------------------- /assignment1/vincent/cs231n/classifiers/backup/linear_classifier.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment1/vincent/cs231n/classifiers/backup/linear_classifier.pyc -------------------------------------------------------------------------------- /assignment1/vincent/cs231n/classifiers/backup/linear_svm.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment1/vincent/cs231n/classifiers/backup/linear_svm.pyc -------------------------------------------------------------------------------- /assignment1/vincent/cs231n/classifiers/backup/softmax.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from random import shuffle 3 | 4 | def softmax_loss_naive(W, X, y, reg): 5 | """ 6 | Softmax loss function, naive implementation (with loops) 7 | 8 | Inputs have dimension D, there are C classes, and we operate on minibatches 9 | of N examples. 10 | 11 | Inputs: 12 | - W: A numpy array of shape (D, C) containing weights. 13 | - X: A numpy array of shape (N, D) containing a minibatch of data. 14 | - y: A numpy array of shape (N,) containing training labels; y[i] = c means 15 | that X[i] has label c, where 0 <= c < C. 16 | - reg: (float) regularization strength 17 | 18 | Returns a tuple of: 19 | - loss as single float 20 | - gradient with respect to weights W; an array of same shape as W 21 | """ 22 | # Initialize the loss and gradient to zero. 23 | loss = 0.0 24 | dW = np.zeros_like(W) 25 | 26 | ############################################################################# 27 | # TODO: Compute the softmax loss and its gradient using explicit loops. # 28 | # Store the loss in loss and the gradient in dW. If you are not careful # 29 | # here, it is easy to run into numeric instability. Don't forget the # 30 | # regularization! # 31 | ############################################################################# 32 | pass 33 | ############################################################################# 34 | # END OF YOUR CODE # 35 | ############################################################################# 36 | 37 | return loss, dW 38 | 39 | 40 | def softmax_loss_vectorized(W, X, y, reg): 41 | """ 42 | Softmax loss function, vectorized version. 43 | 44 | Inputs and outputs are the same as softmax_loss_naive. 45 | """ 46 | # Initialize the loss and gradient to zero. 47 | loss = 0.0 48 | dW = np.zeros_like(W) 49 | 50 | ############################################################################# 51 | # TODO: Compute the softmax loss and its gradient using no explicit loops. # 52 | # Store the loss in loss and the gradient in dW. If you are not careful # 53 | # here, it is easy to run into numeric instability. Don't forget the # 54 | # regularization! # 55 | ############################################################################# 56 | pass 57 | ############################################################################# 58 | # END OF YOUR CODE # 59 | ############################################################################# 60 | 61 | return loss, dW 62 | 63 | -------------------------------------------------------------------------------- /assignment1/vincent/cs231n/classifiers/backup/softmax.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment1/vincent/cs231n/classifiers/backup/softmax.pyc -------------------------------------------------------------------------------- /assignment1/vincent/cs231n/classifiers/k_nearest_neighbor.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment1/vincent/cs231n/classifiers/k_nearest_neighbor.pyc -------------------------------------------------------------------------------- /assignment1/vincent/cs231n/classifiers/linear_classifier.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment1/vincent/cs231n/classifiers/linear_classifier.pyc -------------------------------------------------------------------------------- /assignment1/vincent/cs231n/classifiers/linear_svm.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment1/vincent/cs231n/classifiers/linear_svm.pyc -------------------------------------------------------------------------------- /assignment1/vincent/cs231n/classifiers/neural_net.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment1/vincent/cs231n/classifiers/neural_net.pyc -------------------------------------------------------------------------------- /assignment1/vincent/cs231n/classifiers/softmax.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment1/vincent/cs231n/classifiers/softmax.pyc -------------------------------------------------------------------------------- /assignment1/vincent/cs231n/data_utils.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment1/vincent/cs231n/data_utils.pyc -------------------------------------------------------------------------------- /assignment1/vincent/cs231n/datasets/.gitignore: -------------------------------------------------------------------------------- 1 | cifar-10-batches-py/* 2 | tiny-imagenet-100-A* 3 | tiny-imagenet-100-B* 4 | tiny-100-A-pretrained/* 5 | -------------------------------------------------------------------------------- /assignment1/vincent/cs231n/datasets/get_datasets.sh: -------------------------------------------------------------------------------- 1 | # Get CIFAR10 2 | wget http://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz 3 | tar -xzvf cifar-10-python.tar.gz 4 | rm cifar-10-python.tar.gz 5 | -------------------------------------------------------------------------------- /assignment1/vincent/cs231n/vis_utils.py: -------------------------------------------------------------------------------- 1 | from math import sqrt, ceil 2 | import numpy as np 3 | 4 | def visualize_grid(Xs, ubound=255.0, padding=1): 5 | """ 6 | Reshape a 4D tensor of image data to a grid for easy visualization. 7 | 8 | Inputs: 9 | - Xs: Data of shape (N, H, W, C) 10 | - ubound: Output grid will have values scaled to the range [0, ubound] 11 | - padding: The number of blank pixels between elements of the grid 12 | """ 13 | (N, H, W, C) = Xs.shape 14 | grid_size = int(ceil(sqrt(N))) 15 | grid_height = H * grid_size + padding * (grid_size - 1) 16 | grid_width = W * grid_size + padding * (grid_size - 1) 17 | grid = np.zeros((grid_height, grid_width, C)) 18 | next_idx = 0 19 | y0, y1 = 0, H 20 | for y in xrange(grid_size): 21 | x0, x1 = 0, W 22 | for x in xrange(grid_size): 23 | if next_idx < N: 24 | img = Xs[next_idx] 25 | low, high = np.min(img), np.max(img) 26 | grid[y0:y1, x0:x1] = ubound * (img - low) / (high - low) 27 | # grid[y0:y1, x0:x1] = Xs[next_idx] 28 | next_idx += 1 29 | x0 += W + padding 30 | x1 += W + padding 31 | y0 += H + padding 32 | y1 += H + padding 33 | # grid_max = np.max(grid) 34 | # grid_min = np.min(grid) 35 | # grid = ubound * (grid - grid_min) / (grid_max - grid_min) 36 | return grid 37 | 38 | def vis_grid(Xs): 39 | """ visualize a grid of images """ 40 | (N, H, W, C) = Xs.shape 41 | A = int(ceil(sqrt(N))) 42 | G = np.ones((A*H+A, A*W+A, C), Xs.dtype) 43 | G *= np.min(Xs) 44 | n = 0 45 | for y in range(A): 46 | for x in range(A): 47 | if n < N: 48 | G[y*H+y:(y+1)*H+y, x*W+x:(x+1)*W+x, :] = Xs[n,:,:,:] 49 | n += 1 50 | # normalize to [0,1] 51 | maxg = G.max() 52 | ming = G.min() 53 | G = (G - ming)/(maxg-ming) 54 | return G 55 | 56 | def vis_nn(rows): 57 | """ visualize array of arrays of images """ 58 | N = len(rows) 59 | D = len(rows[0]) 60 | H,W,C = rows[0][0].shape 61 | Xs = rows[0][0] 62 | G = np.ones((N*H+N, D*W+D, C), Xs.dtype) 63 | for y in range(N): 64 | for x in range(D): 65 | G[y*H+y:(y+1)*H+y, x*W+x:(x+1)*W+x, :] = rows[y][x] 66 | # normalize to [0,1] 67 | maxg = G.max() 68 | ming = G.min() 69 | G = (G - ming)/(maxg-ming) 70 | return G 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /assignment1/vincent/frameworkpython: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # what real Python executable to use 4 | PYVER=2.7 5 | PATHTOPYTHON=/usr/local/bin/ 6 | PYTHON=${PATHTOPYTHON}python${PYVER} 7 | 8 | # find the root of the virtualenv, it should be the parent of the dir this script is in 9 | ENV=`$PYTHON -c "import os; print os.path.abspath(os.path.join(os.path.dirname(\"$0\"), '..'))"` 10 | 11 | # now run Python with the virtualenv set as Python's HOME 12 | export PYTHONHOME=$ENV 13 | exec $PYTHON "$@" 14 | -------------------------------------------------------------------------------- /assignment1/vincent/requirements.txt: -------------------------------------------------------------------------------- 1 | Jinja2==2.8 2 | MarkupSafe==0.23 3 | Pillow==3.0.0 4 | Pygments==2.0.2 5 | appnope==0.1.0 6 | backports-abc==0.4 7 | backports.ssl-match-hostname==3.5.0.1 8 | certifi==2015.11.20.1 9 | cycler==0.9.0 10 | decorator==4.0.6 11 | functools32==3.2.3-2 12 | gnureadline==6.3.3 13 | ipykernel==4.2.2 14 | ipython==4.0.1 15 | ipython-genutils==0.1.0 16 | ipywidgets==4.1.1 17 | jsonschema==2.5.1 18 | jupyter==1.0.0 19 | jupyter-client==4.1.1 20 | jupyter-console==4.0.3 21 | jupyter-core==4.0.6 22 | matplotlib==1.5.0 23 | mistune==0.7.1 24 | nbconvert==4.1.0 25 | nbformat==4.0.1 26 | notebook==4.0.6 27 | numpy==1.10.4 28 | path.py==8.1.2 29 | pexpect==4.0.1 30 | pickleshare==0.5 31 | ptyprocess==0.5 32 | pyparsing==2.0.7 33 | python-dateutil==2.4.2 34 | pytz==2015.7 35 | pyzmq==15.1.0 36 | qtconsole==4.1.1 37 | scipy==0.16.1 38 | simplegeneric==0.8.1 39 | singledispatch==3.4.0.3 40 | six==1.10.0 41 | terminado==0.5 42 | tornado==4.3 43 | traitlets==4.0.0 44 | wsgiref==0.1.2 45 | jupyter==1.0.0 46 | pillow==3.1.0 47 | -------------------------------------------------------------------------------- /assignment1/vincent/start_ipython_osx.sh: -------------------------------------------------------------------------------- 1 | # Assume the virtualenv is called .env 2 | 3 | cp frameworkpython .env/bin 4 | .env/bin/frameworkpython -m IPython notebook 5 | -------------------------------------------------------------------------------- /assignment2/berend/.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | *.pyc 3 | .env/* 4 | -------------------------------------------------------------------------------- /assignment2/berend/.idea/berend.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 11 | 12 | 15 | -------------------------------------------------------------------------------- /assignment2/berend/.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /assignment2/berend/.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 31 | -------------------------------------------------------------------------------- /assignment2/berend/.idea/inspectionProfiles/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | -------------------------------------------------------------------------------- /assignment2/berend/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /assignment2/berend/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /assignment2/berend/.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /assignment2/berend/collectSubmission.sh: -------------------------------------------------------------------------------- 1 | rm -f assignment2.zip 2 | zip -r assignment2.zip . -x "*.git*" "*cs231n/datasets*" "*.ipynb_checkpoints*" "*README.md" "*collectSubmission.sh" "*requirements.txt" ".env/*" "*.pyc" "*cs231n/build/*" 3 | -------------------------------------------------------------------------------- /assignment2/berend/cs231n/.gitignore: -------------------------------------------------------------------------------- 1 | build/* 2 | im2col_cython.c 3 | im2col_cython.so 4 | -------------------------------------------------------------------------------- /assignment2/berend/cs231n/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment2/berend/cs231n/__init__.py -------------------------------------------------------------------------------- /assignment2/berend/cs231n/classifiers/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment2/berend/cs231n/classifiers/__init__.py -------------------------------------------------------------------------------- /assignment2/berend/cs231n/datasets/.gitignore: -------------------------------------------------------------------------------- 1 | cifar-10-batches-py/* 2 | tiny-imagenet-100-A* 3 | tiny-imagenet-100-B* 4 | tiny-100-A-pretrained/* 5 | -------------------------------------------------------------------------------- /assignment2/berend/cs231n/datasets/get_datasets.sh: -------------------------------------------------------------------------------- 1 | # Get CIFAR10 2 | wget http://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz 3 | tar -xzvf cifar-10-python.tar.gz 4 | rm cifar-10-python.tar.gz 5 | -------------------------------------------------------------------------------- /assignment2/berend/cs231n/im2col.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def get_im2col_indices(x_shape, field_height, field_width, padding=1, stride=1): 5 | # First figure out what the size of the output should be 6 | N, C, H, W = x_shape 7 | assert (H + 2 * padding - field_height) % stride == 0 8 | assert (W + 2 * padding - field_height) % stride == 0 9 | out_height = (H + 2 * padding - field_height) / stride + 1 10 | out_width = (W + 2 * padding - field_width) / stride + 1 11 | 12 | i0 = np.repeat(np.arange(field_height), field_width) 13 | i0 = np.tile(i0, C) 14 | i1 = stride * np.repeat(np.arange(out_height), out_width) 15 | j0 = np.tile(np.arange(field_width), field_height * C) 16 | j1 = stride * np.tile(np.arange(out_width), out_height) 17 | i = i0.reshape(-1, 1) + i1.reshape(1, -1) 18 | j = j0.reshape(-1, 1) + j1.reshape(1, -1) 19 | 20 | k = np.repeat(np.arange(C), field_height * field_width).reshape(-1, 1) 21 | 22 | return (k, i, j) 23 | 24 | 25 | def im2col_indices(x, field_height, field_width, padding=1, stride=1): 26 | """ An implementation of im2col based on some fancy indexing """ 27 | # Zero-pad the input 28 | p = padding 29 | x_padded = np.pad(x, ((0, 0), (0, 0), (p, p), (p, p)), mode='constant') 30 | 31 | k, i, j = get_im2col_indices(x.shape, field_height, field_width, padding, 32 | stride) 33 | 34 | cols = x_padded[:, k, i, j] 35 | C = x.shape[1] 36 | cols = cols.transpose(1, 2, 0).reshape(field_height * field_width * C, -1) 37 | return cols 38 | 39 | 40 | def col2im_indices(cols, x_shape, field_height=3, field_width=3, padding=1, 41 | stride=1): 42 | """ An implementation of col2im based on fancy indexing and np.add.at """ 43 | N, C, H, W = x_shape 44 | H_padded, W_padded = H + 2 * padding, W + 2 * padding 45 | x_padded = np.zeros((N, C, H_padded, W_padded), dtype=cols.dtype) 46 | k, i, j = get_im2col_indices(x_shape, field_height, field_width, padding, 47 | stride) 48 | cols_reshaped = cols.reshape(C * field_height * field_width, -1, N) 49 | cols_reshaped = cols_reshaped.transpose(2, 0, 1) 50 | np.add.at(x_padded, (slice(None), k, i, j), cols_reshaped) 51 | if padding == 0: 52 | return x_padded 53 | return x_padded[:, :, padding:-padding, padding:-padding] 54 | 55 | pass 56 | -------------------------------------------------------------------------------- /assignment2/berend/cs231n/layer_utils.py: -------------------------------------------------------------------------------- 1 | from cs231n.layers import * 2 | from cs231n.fast_layers import * 3 | 4 | 5 | def affine_relu_forward(x, w, b): 6 | """ 7 | Convenience layer that perorms an affine transform followed by a ReLU 8 | 9 | Inputs: 10 | - x: Input to the affine layer 11 | - w, b: Weights for the affine layer 12 | 13 | Returns a tuple of: 14 | - out: Output from the ReLU 15 | - cache: Object to give to the backward pass 16 | """ 17 | a, fc_cache = affine_forward(x, w, b) 18 | out, relu_cache = relu_forward(a) 19 | cache = (fc_cache, relu_cache) 20 | return out, cache 21 | 22 | 23 | def affine_relu_backward(dout, cache): 24 | """ 25 | Backward pass for the affine-relu convenience layer 26 | """ 27 | fc_cache, relu_cache = cache 28 | da = relu_backward(dout, relu_cache) 29 | dx, dw, db = affine_backward(da, fc_cache) 30 | return dx, dw, db 31 | 32 | 33 | def conv_relu_forward(x, w, b, conv_param): 34 | """ 35 | A convenience layer that performs a convolution followed by a ReLU. 36 | 37 | Inputs: 38 | - x: Input to the convolutional layer 39 | - w, b, conv_param: Weights and parameters for the convolutional layer 40 | 41 | Returns a tuple of: 42 | - out: Output from the ReLU 43 | - cache: Object to give to the backward pass 44 | """ 45 | a, conv_cache = conv_forward_fast(x, w, b, conv_param) 46 | out, relu_cache = relu_forward(a) 47 | cache = (conv_cache, relu_cache) 48 | return out, cache 49 | 50 | 51 | def conv_relu_backward(dout, cache): 52 | """ 53 | Backward pass for the conv-relu convenience layer. 54 | """ 55 | conv_cache, relu_cache = cache 56 | da = relu_backward(dout, relu_cache) 57 | dx, dw, db = conv_backward_fast(da, conv_cache) 58 | return dx, dw, db 59 | 60 | 61 | def conv_relu_pool_forward(x, w, b, conv_param, pool_param): 62 | """ 63 | Convenience layer that performs a convolution, a ReLU, and a pool. 64 | 65 | Inputs: 66 | - x: Input to the convolutional layer 67 | - w, b, conv_param: Weights and parameters for the convolutional layer 68 | - pool_param: Parameters for the pooling layer 69 | 70 | Returns a tuple of: 71 | - out: Output from the pooling layer 72 | - cache: Object to give to the backward pass 73 | """ 74 | a, conv_cache = conv_forward_fast(x, w, b, conv_param) 75 | s, relu_cache = relu_forward(a) 76 | out, pool_cache = max_pool_forward_fast(s, pool_param) 77 | cache = (conv_cache, relu_cache, pool_cache) 78 | return out, cache 79 | 80 | 81 | def conv_relu_pool_backward(dout, cache): 82 | """ 83 | Backward pass for the conv-relu-pool convenience layer 84 | """ 85 | conv_cache, relu_cache, pool_cache = cache 86 | ds = max_pool_backward_fast(dout, pool_cache) 87 | da = relu_backward(ds, relu_cache) 88 | dx, dw, db = conv_backward_fast(da, conv_cache) 89 | return dx, dw, db 90 | 91 | -------------------------------------------------------------------------------- /assignment2/berend/cs231n/setup.py: -------------------------------------------------------------------------------- 1 | from distutils.core import setup 2 | from distutils.extension import Extension 3 | from Cython.Build import cythonize 4 | import numpy 5 | 6 | extensions = [ 7 | Extension('im2col_cython', ['im2col_cython.pyx'], 8 | include_dirs = [numpy.get_include()] 9 | ), 10 | ] 11 | 12 | setup( 13 | ext_modules = cythonize(extensions), 14 | ) 15 | -------------------------------------------------------------------------------- /assignment2/berend/cs231n/vis_utils.py: -------------------------------------------------------------------------------- 1 | from math import sqrt, ceil 2 | import numpy as np 3 | 4 | def visualize_grid(Xs, ubound=255.0, padding=1): 5 | """ 6 | Reshape a 4D tensor of image data to a grid for easy visualization. 7 | 8 | Inputs: 9 | - Xs: Data of shape (N, H, W, C) 10 | - ubound: Output grid will have values scaled to the range [0, ubound] 11 | - padding: The number of blank pixels between elements of the grid 12 | """ 13 | (N, H, W, C) = Xs.shape 14 | grid_size = int(ceil(sqrt(N))) 15 | grid_height = H * grid_size + padding * (grid_size - 1) 16 | grid_width = W * grid_size + padding * (grid_size - 1) 17 | grid = np.zeros((grid_height, grid_width, C)) 18 | next_idx = 0 19 | y0, y1 = 0, H 20 | for y in xrange(grid_size): 21 | x0, x1 = 0, W 22 | for x in xrange(grid_size): 23 | if next_idx < N: 24 | img = Xs[next_idx] 25 | low, high = np.min(img), np.max(img) 26 | grid[y0:y1, x0:x1] = ubound * (img - low) / (high - low) 27 | # grid[y0:y1, x0:x1] = Xs[next_idx] 28 | next_idx += 1 29 | x0 += W + padding 30 | x1 += W + padding 31 | y0 += H + padding 32 | y1 += H + padding 33 | # grid_max = np.max(grid) 34 | # grid_min = np.min(grid) 35 | # grid = ubound * (grid - grid_min) / (grid_max - grid_min) 36 | return grid 37 | 38 | def vis_grid(Xs): 39 | """ visualize a grid of images """ 40 | (N, H, W, C) = Xs.shape 41 | A = int(ceil(sqrt(N))) 42 | G = np.ones((A*H+A, A*W+A, C), Xs.dtype) 43 | G *= np.min(Xs) 44 | n = 0 45 | for y in range(A): 46 | for x in range(A): 47 | if n < N: 48 | G[y*H+y:(y+1)*H+y, x*W+x:(x+1)*W+x, :] = Xs[n,:,:,:] 49 | n += 1 50 | # normalize to [0,1] 51 | maxg = G.max() 52 | ming = G.min() 53 | G = (G - ming)/(maxg-ming) 54 | return G 55 | 56 | def vis_nn(rows): 57 | """ visualize array of arrays of images """ 58 | N = len(rows) 59 | D = len(rows[0]) 60 | H,W,C = rows[0][0].shape 61 | Xs = rows[0][0] 62 | G = np.ones((N*H+N, D*W+D, C), Xs.dtype) 63 | for y in range(N): 64 | for x in range(D): 65 | G[y*H+y:(y+1)*H+y, x*W+x:(x+1)*W+x, :] = rows[y][x] 66 | # normalize to [0,1] 67 | maxg = G.max() 68 | ming = G.min() 69 | G = (G - ming)/(maxg-ming) 70 | return G 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /assignment2/berend/frameworkpython: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # what real Python executable to use 4 | PYVER=2.7 5 | PATHTOPYTHON=/usr/local/bin/ 6 | PYTHON=${PATHTOPYTHON}python${PYVER} 7 | 8 | # find the root of the virtualenv, it should be the parent of the dir this script is in 9 | ENV=`$PYTHON -c "import os; print os.path.abspath(os.path.join(os.path.dirname(\"$0\"), '..'))"` 10 | 11 | # now run Python with the virtualenv set as Python's HOME 12 | export PYTHONHOME=$ENV 13 | exec $PYTHON "$@" 14 | -------------------------------------------------------------------------------- /assignment2/berend/kitten.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment2/berend/kitten.jpg -------------------------------------------------------------------------------- /assignment2/berend/puppy.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment2/berend/puppy.jpg -------------------------------------------------------------------------------- /assignment2/berend/requirements.txt: -------------------------------------------------------------------------------- 1 | Cython==0.23.4 2 | Jinja2==2.8 3 | MarkupSafe==0.23 4 | Pillow==3.0.0 5 | Pygments==2.0.2 6 | appnope==0.1.0 7 | argparse==1.2.1 8 | backports-abc==0.4 9 | backports.ssl-match-hostname==3.5.0.1 10 | certifi==2015.11.20.1 11 | cycler==0.9.0 12 | decorator==4.0.6 13 | functools32==3.2.3-2 14 | gnureadline==6.3.3 15 | ipykernel==4.2.2 16 | ipython==4.0.1 17 | ipython-genutils==0.1.0 18 | ipywidgets==4.1.1 19 | jsonschema==2.5.1 20 | jupyter==1.0.0 21 | jupyter-client==4.1.1 22 | jupyter-console==4.0.3 23 | jupyter-core==4.0.6 24 | matplotlib==1.5.0 25 | mistune==0.7.1 26 | nbconvert==4.1.0 27 | nbformat==4.0.1 28 | notebook==4.0.6 29 | numpy>=1.10.4 30 | path.py==8.1.2 31 | pexpect==4.0.1 32 | pickleshare==0.5 33 | ptyprocess==0.5 34 | pyparsing==2.0.7 35 | python-dateutil>=2.4.2 36 | pytz>=2015.7 37 | pyzmq==15.1.0 38 | qtconsole==4.1.1 39 | scipy==0.16.1 40 | simplegeneric==0.8.1 41 | singledispatch==3.4.0.3 42 | six==1.10.0 43 | terminado==0.5 44 | tornado==4.3 45 | traitlets==4.0.0 46 | wsgiref==0.1.2 47 | -------------------------------------------------------------------------------- /assignment2/berend/start_ipython_osx.sh: -------------------------------------------------------------------------------- 1 | # Assume the virtualenv is called .env 2 | 3 | cp frameworkpython .env/bin 4 | .env/bin/frameworkpython -m IPython notebook 5 | -------------------------------------------------------------------------------- /assignment2/carlos/.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | *.pyc 3 | .env/* 4 | -------------------------------------------------------------------------------- /assignment2/carlos/cs231n/.gitignore: -------------------------------------------------------------------------------- 1 | build/* 2 | im2col_cython.c 3 | im2col_cython.so 4 | -------------------------------------------------------------------------------- /assignment2/carlos/cs231n/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment2/carlos/cs231n/__init__.py -------------------------------------------------------------------------------- /assignment2/carlos/cs231n/classifiers/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment2/carlos/cs231n/classifiers/__init__.py -------------------------------------------------------------------------------- /assignment2/carlos/cs231n/datasets/.gitignore: -------------------------------------------------------------------------------- 1 | cifar-10-batches-py/* 2 | tiny-imagenet-100-A* 3 | tiny-imagenet-100-B* 4 | tiny-100-A-pretrained/* 5 | -------------------------------------------------------------------------------- /assignment2/carlos/cs231n/datasets/get_datasets.sh: -------------------------------------------------------------------------------- 1 | # Get CIFAR10 2 | wget http://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz 3 | tar -xzvf cifar-10-python.tar.gz 4 | rm cifar-10-python.tar.gz 5 | -------------------------------------------------------------------------------- /assignment2/carlos/cs231n/im2col.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def get_im2col_indices(x_shape, field_height, field_width, padding=1, stride=1): 5 | # First figure out what the size of the output should be 6 | N, C, H, W = x_shape 7 | assert (H + 2 * padding - field_height) % stride == 0 8 | assert (W + 2 * padding - field_height) % stride == 0 9 | out_height = (H + 2 * padding - field_height) / stride + 1 10 | out_width = (W + 2 * padding - field_width) / stride + 1 11 | 12 | i0 = np.repeat(np.arange(field_height), field_width) 13 | i0 = np.tile(i0, C) 14 | i1 = stride * np.repeat(np.arange(out_height), out_width) 15 | j0 = np.tile(np.arange(field_width), field_height * C) 16 | j1 = stride * np.tile(np.arange(out_width), out_height) 17 | i = i0.reshape(-1, 1) + i1.reshape(1, -1) 18 | j = j0.reshape(-1, 1) + j1.reshape(1, -1) 19 | 20 | k = np.repeat(np.arange(C), field_height * field_width).reshape(-1, 1) 21 | 22 | return (k, i, j) 23 | 24 | 25 | def im2col_indices(x, field_height, field_width, padding=1, stride=1): 26 | """ An implementation of im2col based on some fancy indexing """ 27 | # Zero-pad the input 28 | p = padding 29 | x_padded = np.pad(x, ((0, 0), (0, 0), (p, p), (p, p)), mode='constant') 30 | 31 | k, i, j = get_im2col_indices(x.shape, field_height, field_width, padding, 32 | stride) 33 | 34 | cols = x_padded[:, k, i, j] 35 | C = x.shape[1] 36 | cols = cols.transpose(1, 2, 0).reshape(field_height * field_width * C, -1) 37 | return cols 38 | 39 | 40 | def col2im_indices(cols, x_shape, field_height=3, field_width=3, padding=1, 41 | stride=1): 42 | """ An implementation of col2im based on fancy indexing and np.add.at """ 43 | N, C, H, W = x_shape 44 | H_padded, W_padded = H + 2 * padding, W + 2 * padding 45 | x_padded = np.zeros((N, C, H_padded, W_padded), dtype=cols.dtype) 46 | k, i, j = get_im2col_indices(x_shape, field_height, field_width, padding, 47 | stride) 48 | cols_reshaped = cols.reshape(C * field_height * field_width, -1, N) 49 | cols_reshaped = cols_reshaped.transpose(2, 0, 1) 50 | np.add.at(x_padded, (slice(None), k, i, j), cols_reshaped) 51 | if padding == 0: 52 | return x_padded 53 | return x_padded[:, :, padding:-padding, padding:-padding] 54 | 55 | pass 56 | -------------------------------------------------------------------------------- /assignment2/carlos/cs231n/layer_utils.py: -------------------------------------------------------------------------------- 1 | from cs231n.layers import * 2 | from cs231n.fast_layers import * 3 | 4 | 5 | def affine_relu_forward(x, w, b): 6 | """ 7 | Convenience layer that perorms an affine transform followed by a ReLU 8 | 9 | Inputs: 10 | - x: Input to the affine layer 11 | - w, b: Weights for the affine layer 12 | 13 | Returns a tuple of: 14 | - out: Output from the ReLU 15 | - cache: Object to give to the backward pass 16 | """ 17 | a, fc_cache = affine_forward(x, w, b) 18 | out, relu_cache = relu_forward(a) 19 | cache = (fc_cache, relu_cache) 20 | return out, cache 21 | 22 | 23 | def affine_relu_backward(dout, cache): 24 | """ 25 | Backward pass for the affine-relu convenience layer 26 | """ 27 | fc_cache, relu_cache = cache 28 | da = relu_backward(dout, relu_cache) 29 | dx, dw, db = affine_backward(da, fc_cache) 30 | return dx, dw, db 31 | 32 | 33 | pass 34 | 35 | 36 | def conv_relu_forward(x, w, b, conv_param): 37 | """ 38 | A convenience layer that performs a convolution followed by a ReLU. 39 | 40 | Inputs: 41 | - x: Input to the convolutional layer 42 | - w, b, conv_param: Weights and parameters for the convolutional layer 43 | 44 | Returns a tuple of: 45 | - out: Output from the ReLU 46 | - cache: Object to give to the backward pass 47 | """ 48 | a, conv_cache = conv_forward_fast(x, w, b, conv_param) 49 | out, relu_cache = relu_forward(a) 50 | cache = (conv_cache, relu_cache) 51 | return out, cache 52 | 53 | 54 | def conv_relu_backward(dout, cache): 55 | """ 56 | Backward pass for the conv-relu convenience layer. 57 | """ 58 | conv_cache, relu_cache = cache 59 | da = relu_backward(dout, relu_cache) 60 | dx, dw, db = conv_backward_fast(da, conv_cache) 61 | return dx, dw, db 62 | 63 | 64 | def conv_relu_pool_forward(x, w, b, conv_param, pool_param): 65 | """ 66 | Convenience layer that performs a convolution, a ReLU, and a pool. 67 | 68 | Inputs: 69 | - x: Input to the convolutional layer 70 | - w, b, conv_param: Weights and parameters for the convolutional layer 71 | - pool_param: Parameters for the pooling layer 72 | 73 | Returns a tuple of: 74 | - out: Output from the pooling layer 75 | - cache: Object to give to the backward pass 76 | """ 77 | a, conv_cache = conv_forward_fast(x, w, b, conv_param) 78 | s, relu_cache = relu_forward(a) 79 | out, pool_cache = max_pool_forward_fast(s, pool_param) 80 | cache = (conv_cache, relu_cache, pool_cache) 81 | return out, cache 82 | 83 | 84 | def conv_relu_pool_backward(dout, cache): 85 | """ 86 | Backward pass for the conv-relu-pool convenience layer 87 | """ 88 | conv_cache, relu_cache, pool_cache = cache 89 | ds = max_pool_backward_fast(dout, pool_cache) 90 | da = relu_backward(ds, relu_cache) 91 | dx, dw, db = conv_backward_fast(da, conv_cache) 92 | return dx, dw, db 93 | 94 | -------------------------------------------------------------------------------- /assignment2/carlos/cs231n/setup.py: -------------------------------------------------------------------------------- 1 | from distutils.core import setup 2 | from distutils.extension import Extension 3 | from Cython.Build import cythonize 4 | import numpy 5 | 6 | extensions = [ 7 | Extension('im2col_cython', ['im2col_cython.pyx'], 8 | include_dirs = [numpy.get_include()] 9 | ), 10 | ] 11 | 12 | setup( 13 | ext_modules = cythonize(extensions), 14 | ) 15 | -------------------------------------------------------------------------------- /assignment2/carlos/cs231n/vis_utils.py: -------------------------------------------------------------------------------- 1 | from math import sqrt, ceil 2 | import numpy as np 3 | 4 | def visualize_grid(Xs, ubound=255.0, padding=1): 5 | """ 6 | Reshape a 4D tensor of image data to a grid for easy visualization. 7 | 8 | Inputs: 9 | - Xs: Data of shape (N, H, W, C) 10 | - ubound: Output grid will have values scaled to the range [0, ubound] 11 | - padding: The number of blank pixels between elements of the grid 12 | """ 13 | (N, H, W, C) = Xs.shape 14 | grid_size = int(ceil(sqrt(N))) 15 | grid_height = H * grid_size + padding * (grid_size - 1) 16 | grid_width = W * grid_size + padding * (grid_size - 1) 17 | grid = np.zeros((grid_height, grid_width, C)) 18 | next_idx = 0 19 | y0, y1 = 0, H 20 | for y in xrange(grid_size): 21 | x0, x1 = 0, W 22 | for x in xrange(grid_size): 23 | if next_idx < N: 24 | img = Xs[next_idx] 25 | low, high = np.min(img), np.max(img) 26 | grid[y0:y1, x0:x1] = ubound * (img - low) / (high - low) 27 | # grid[y0:y1, x0:x1] = Xs[next_idx] 28 | next_idx += 1 29 | x0 += W + padding 30 | x1 += W + padding 31 | y0 += H + padding 32 | y1 += H + padding 33 | # grid_max = np.max(grid) 34 | # grid_min = np.min(grid) 35 | # grid = ubound * (grid - grid_min) / (grid_max - grid_min) 36 | return grid 37 | 38 | def vis_grid(Xs): 39 | """ visualize a grid of images """ 40 | (N, H, W, C) = Xs.shape 41 | A = int(ceil(sqrt(N))) 42 | G = np.ones((A*H+A, A*W+A, C), Xs.dtype) 43 | G *= np.min(Xs) 44 | n = 0 45 | for y in range(A): 46 | for x in range(A): 47 | if n < N: 48 | G[y*H+y:(y+1)*H+y, x*W+x:(x+1)*W+x, :] = Xs[n,:,:,:] 49 | n += 1 50 | # normalize to [0,1] 51 | maxg = G.max() 52 | ming = G.min() 53 | G = (G - ming)/(maxg-ming) 54 | return G 55 | 56 | def vis_nn(rows): 57 | """ visualize array of arrays of images """ 58 | N = len(rows) 59 | D = len(rows[0]) 60 | H,W,C = rows[0][0].shape 61 | Xs = rows[0][0] 62 | G = np.ones((N*H+N, D*W+D, C), Xs.dtype) 63 | for y in range(N): 64 | for x in range(D): 65 | G[y*H+y:(y+1)*H+y, x*W+x:(x+1)*W+x, :] = rows[y][x] 66 | # normalize to [0,1] 67 | maxg = G.max() 68 | ming = G.min() 69 | G = (G - ming)/(maxg-ming) 70 | return G 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /assignment2/christiaan/.idea/.name: -------------------------------------------------------------------------------- 1 | christiaan -------------------------------------------------------------------------------- /assignment2/christiaan/.idea/christiaan.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 12 | 13 | 15 | -------------------------------------------------------------------------------- /assignment2/christiaan/.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /assignment2/christiaan/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /assignment2/christiaan/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /assignment2/christiaan/collectSubmission.sh: -------------------------------------------------------------------------------- 1 | rm -f assignment2.zip 2 | zip -r assignment2.zip . -x "*.git*" "*cs231n/datasets*" "*.ipynb_checkpoints*" "*README.md" "*collectSubmission.sh" "*requirements.txt" ".env/*" "*.pyc" "*cs231n/build/*" 3 | -------------------------------------------------------------------------------- /assignment2/christiaan/cs231n/.gitignore: -------------------------------------------------------------------------------- 1 | build/* 2 | im2col_cython.c 3 | im2col_cython.so 4 | -------------------------------------------------------------------------------- /assignment2/christiaan/cs231n/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment2/christiaan/cs231n/__init__.py -------------------------------------------------------------------------------- /assignment2/christiaan/cs231n/classifiers/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment2/christiaan/cs231n/classifiers/__init__.py -------------------------------------------------------------------------------- /assignment2/christiaan/cs231n/datasets/.gitignore: -------------------------------------------------------------------------------- 1 | cifar-10-batches-py/* 2 | tiny-imagenet-100-A* 3 | tiny-imagenet-100-B* 4 | tiny-100-A-pretrained/* 5 | -------------------------------------------------------------------------------- /assignment2/christiaan/cs231n/datasets/get_datasets.sh: -------------------------------------------------------------------------------- 1 | # Get CIFAR10 2 | wget http://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz 3 | tar -xzvf cifar-10-python.tar.gz 4 | rm cifar-10-python.tar.gz 5 | -------------------------------------------------------------------------------- /assignment2/christiaan/cs231n/im2col.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def get_im2col_indices(x_shape, field_height, field_width, padding=1, stride=1): 5 | # First figure out what the size of the output should be 6 | N, C, H, W = x_shape 7 | assert (H + 2 * padding - field_height) % stride == 0 8 | assert (W + 2 * padding - field_height) % stride == 0 9 | out_height = (H + 2 * padding - field_height) / stride + 1 10 | out_width = (W + 2 * padding - field_width) / stride + 1 11 | 12 | i0 = np.repeat(np.arange(field_height), field_width) 13 | i0 = np.tile(i0, C) 14 | i1 = stride * np.repeat(np.arange(out_height), out_width) 15 | j0 = np.tile(np.arange(field_width), field_height * C) 16 | j1 = stride * np.tile(np.arange(out_width), out_height) 17 | i = i0.reshape(-1, 1) + i1.reshape(1, -1) 18 | j = j0.reshape(-1, 1) + j1.reshape(1, -1) 19 | 20 | k = np.repeat(np.arange(C), field_height * field_width).reshape(-1, 1) 21 | 22 | return (k, i, j) 23 | 24 | 25 | def im2col_indices(x, field_height, field_width, padding=1, stride=1): 26 | """ An implementation of im2col based on some fancy indexing """ 27 | # Zero-pad the input 28 | p = padding 29 | x_padded = np.pad(x, ((0, 0), (0, 0), (p, p), (p, p)), mode='constant') 30 | 31 | k, i, j = get_im2col_indices(x.shape, field_height, field_width, padding, 32 | stride) 33 | 34 | cols = x_padded[:, k, i, j] 35 | C = x.shape[1] 36 | cols = cols.transpose(1, 2, 0).reshape(field_height * field_width * C, -1) 37 | return cols 38 | 39 | 40 | def col2im_indices(cols, x_shape, field_height=3, field_width=3, padding=1, 41 | stride=1): 42 | """ An implementation of col2im based on fancy indexing and np.add.at """ 43 | N, C, H, W = x_shape 44 | H_padded, W_padded = H + 2 * padding, W + 2 * padding 45 | x_padded = np.zeros((N, C, H_padded, W_padded), dtype=cols.dtype) 46 | k, i, j = get_im2col_indices(x_shape, field_height, field_width, padding, 47 | stride) 48 | cols_reshaped = cols.reshape(C * field_height * field_width, -1, N) 49 | cols_reshaped = cols_reshaped.transpose(2, 0, 1) 50 | np.add.at(x_padded, (slice(None), k, i, j), cols_reshaped) 51 | if padding == 0: 52 | return x_padded 53 | return x_padded[:, :, padding:-padding, padding:-padding] 54 | 55 | pass 56 | -------------------------------------------------------------------------------- /assignment2/christiaan/cs231n/layer_utils.py: -------------------------------------------------------------------------------- 1 | from cs231n.layers import * 2 | from cs231n.fast_layers import * 3 | 4 | 5 | def affine_relu_forward(x, w, b): 6 | """ 7 | Convenience layer that perorms an affine transform followed by a ReLU 8 | 9 | Inputs: 10 | - x: Input to the affine layer 11 | - w, b: Weights for the affine layer 12 | 13 | Returns a tuple of: 14 | - out: Output from the ReLU 15 | - cache: Object to give to the backward pass 16 | """ 17 | a, fc_cache = affine_forward(x, w, b) 18 | out, relu_cache = relu_forward(a) 19 | cache = (fc_cache, relu_cache) 20 | return out, cache 21 | 22 | 23 | def affine_relu_backward(dout, cache): 24 | """ 25 | Backward pass for the affine-relu convenience layer 26 | """ 27 | fc_cache, relu_cache = cache 28 | da = relu_backward(dout, relu_cache) 29 | dx, dw, db = affine_backward(da, fc_cache) 30 | return dx, dw, db 31 | 32 | 33 | pass 34 | 35 | 36 | def conv_relu_forward(x, w, b, conv_param): 37 | """ 38 | A convenience layer that performs a convolution followed by a ReLU. 39 | 40 | Inputs: 41 | - x: Input to the convolutional layer 42 | - w, b, conv_param: Weights and parameters for the convolutional layer 43 | 44 | Returns a tuple of: 45 | - out: Output from the ReLU 46 | - cache: Object to give to the backward pass 47 | """ 48 | a, conv_cache = conv_forward_fast(x, w, b, conv_param) 49 | out, relu_cache = relu_forward(a) 50 | cache = (conv_cache, relu_cache) 51 | return out, cache 52 | 53 | 54 | def conv_relu_backward(dout, cache): 55 | """ 56 | Backward pass for the conv-relu convenience layer. 57 | """ 58 | conv_cache, relu_cache = cache 59 | da = relu_backward(dout, relu_cache) 60 | dx, dw, db = conv_backward_fast(da, conv_cache) 61 | return dx, dw, db 62 | 63 | 64 | def conv_relu_pool_forward(x, w, b, conv_param, pool_param): 65 | """ 66 | Convenience layer that performs a convolution, a ReLU, and a pool. 67 | 68 | Inputs: 69 | - x: Input to the convolutional layer 70 | - w, b, conv_param: Weights and parameters for the convolutional layer 71 | - pool_param: Parameters for the pooling layer 72 | 73 | Returns a tuple of: 74 | - out: Output from the pooling layer 75 | - cache: Object to give to the backward pass 76 | """ 77 | a, conv_cache = conv_forward_fast(x, w, b, conv_param) 78 | s, relu_cache = relu_forward(a) 79 | out, pool_cache = max_pool_forward_fast(s, pool_param) 80 | cache = (conv_cache, relu_cache, pool_cache) 81 | return out, cache 82 | 83 | 84 | def conv_relu_pool_backward(dout, cache): 85 | """ 86 | Backward pass for the conv-relu-pool convenience layer 87 | """ 88 | conv_cache, relu_cache, pool_cache = cache 89 | ds = max_pool_backward_fast(dout, pool_cache) 90 | da = relu_backward(ds, relu_cache) 91 | dx, dw, db = conv_backward_fast(da, conv_cache) 92 | return dx, dw, db 93 | 94 | -------------------------------------------------------------------------------- /assignment2/christiaan/cs231n/setup.py: -------------------------------------------------------------------------------- 1 | from distutils.core import setup 2 | from distutils.extension import Extension 3 | from Cython.Build import cythonize 4 | import numpy 5 | 6 | extensions = [ 7 | Extension('im2col_cython', ['im2col_cython.pyx'], 8 | include_dirs = [numpy.get_include()] 9 | ), 10 | ] 11 | 12 | setup( 13 | ext_modules = cythonize(extensions), 14 | ) 15 | -------------------------------------------------------------------------------- /assignment2/christiaan/cs231n/vis_utils.py: -------------------------------------------------------------------------------- 1 | from math import sqrt, ceil 2 | import numpy as np 3 | 4 | def visualize_grid(Xs, ubound=255.0, padding=1): 5 | """ 6 | Reshape a 4D tensor of image data to a grid for easy visualization. 7 | 8 | Inputs: 9 | - Xs: Data of shape (N, H, W, C) 10 | - ubound: Output grid will have values scaled to the range [0, ubound] 11 | - padding: The number of blank pixels between elements of the grid 12 | """ 13 | (N, H, W, C) = Xs.shape 14 | grid_size = int(ceil(sqrt(N))) 15 | grid_height = H * grid_size + padding * (grid_size - 1) 16 | grid_width = W * grid_size + padding * (grid_size - 1) 17 | grid = np.zeros((grid_height, grid_width, C)) 18 | next_idx = 0 19 | y0, y1 = 0, H 20 | for y in xrange(grid_size): 21 | x0, x1 = 0, W 22 | for x in xrange(grid_size): 23 | if next_idx < N: 24 | img = Xs[next_idx] 25 | low, high = np.min(img), np.max(img) 26 | grid[y0:y1, x0:x1] = ubound * (img - low) / (high - low) 27 | # grid[y0:y1, x0:x1] = Xs[next_idx] 28 | next_idx += 1 29 | x0 += W + padding 30 | x1 += W + padding 31 | y0 += H + padding 32 | y1 += H + padding 33 | # grid_max = np.max(grid) 34 | # grid_min = np.min(grid) 35 | # grid = ubound * (grid - grid_min) / (grid_max - grid_min) 36 | return grid 37 | 38 | def vis_grid(Xs): 39 | """ visualize a grid of images """ 40 | (N, H, W, C) = Xs.shape 41 | A = int(ceil(sqrt(N))) 42 | G = np.ones((A*H+A, A*W+A, C), Xs.dtype) 43 | G *= np.min(Xs) 44 | n = 0 45 | for y in range(A): 46 | for x in range(A): 47 | if n < N: 48 | G[y*H+y:(y+1)*H+y, x*W+x:(x+1)*W+x, :] = Xs[n,:,:,:] 49 | n += 1 50 | # normalize to [0,1] 51 | maxg = G.max() 52 | ming = G.min() 53 | G = (G - ming)/(maxg-ming) 54 | return G 55 | 56 | def vis_nn(rows): 57 | """ visualize array of arrays of images """ 58 | N = len(rows) 59 | D = len(rows[0]) 60 | H,W,C = rows[0][0].shape 61 | Xs = rows[0][0] 62 | G = np.ones((N*H+N, D*W+D, C), Xs.dtype) 63 | for y in range(N): 64 | for x in range(D): 65 | G[y*H+y:(y+1)*H+y, x*W+x:(x+1)*W+x, :] = rows[y][x] 66 | # normalize to [0,1] 67 | maxg = G.max() 68 | ming = G.min() 69 | G = (G - ming)/(maxg-ming) 70 | return G 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /assignment2/christiaan/frameworkpython: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # what real Python executable to use 4 | PYVER=2.7 5 | PATHTOPYTHON=/usr/local/bin/ 6 | PYTHON=${PATHTOPYTHON}python${PYVER} 7 | 8 | # find the root of the virtualenv, it should be the parent of the dir this script is in 9 | ENV=`$PYTHON -c "import os; print os.path.abspath(os.path.join(os.path.dirname(\"$0\"), '..'))"` 10 | 11 | # now run Python with the virtualenv set as Python's HOME 12 | export PYTHONHOME=$ENV 13 | exec $PYTHON "$@" 14 | -------------------------------------------------------------------------------- /assignment2/christiaan/kitten.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment2/christiaan/kitten.jpg -------------------------------------------------------------------------------- /assignment2/christiaan/puppy.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment2/christiaan/puppy.jpg -------------------------------------------------------------------------------- /assignment2/christiaan/requirements.txt: -------------------------------------------------------------------------------- 1 | Cython==0.23.4 2 | Jinja2==2.8 3 | MarkupSafe==0.23 4 | Pillow==3.0.0 5 | Pygments==2.0.2 6 | appnope==0.1.0 7 | argparse==1.2.1 8 | backports-abc==0.4 9 | backports.ssl-match-hostname==3.5.0.1 10 | certifi==2015.11.20.1 11 | cycler==0.9.0 12 | decorator==4.0.6 13 | functools32==3.2.3-2 14 | gnureadline==6.3.3 15 | ipykernel==4.2.2 16 | ipython==4.0.1 17 | ipython-genutils==0.1.0 18 | ipywidgets==4.1.1 19 | jsonschema==2.5.1 20 | jupyter==1.0.0 21 | jupyter-client==4.1.1 22 | jupyter-console==4.0.3 23 | jupyter-core==4.0.6 24 | matplotlib==1.5.0 25 | mistune==0.7.1 26 | nbconvert==4.1.0 27 | nbformat==4.0.1 28 | notebook==4.0.6 29 | numpy==1.10.4 30 | path.py==8.1.2 31 | pexpect==4.0.1 32 | pickleshare==0.5 33 | ptyprocess==0.5 34 | pyparsing==2.0.7 35 | python-dateutil==2.4.2 36 | pytz==2015.7 37 | pyzmq==15.1.0 38 | qtconsole==4.1.1 39 | scipy==0.16.1 40 | simplegeneric==0.8.1 41 | singledispatch==3.4.0.3 42 | six==1.10.0 43 | terminado==0.5 44 | tornado==4.3 45 | traitlets==4.0.0 46 | wsgiref==0.1.2 47 | -------------------------------------------------------------------------------- /assignment2/christiaan/start_ipython_osx.sh: -------------------------------------------------------------------------------- 1 | # Assume the virtualenv is called .env 2 | 3 | cp frameworkpython .env/bin 4 | .env/bin/frameworkpython -m IPython notebook 5 | -------------------------------------------------------------------------------- /assignment2/dafne/collectSubmission.sh: -------------------------------------------------------------------------------- 1 | rm -f assignment2.zip 2 | zip -r assignment2.zip . -x "*.git*" "*cs231n/datasets*" "*.ipynb_checkpoints*" "*README.md" "*collectSubmission.sh" "*requirements.txt" ".env/*" "*.pyc" "*cs231n/build/*" 3 | -------------------------------------------------------------------------------- /assignment2/dafne/cs231n/.gitignore: -------------------------------------------------------------------------------- 1 | build/* 2 | im2col_cython.c 3 | im2col_cython.so 4 | -------------------------------------------------------------------------------- /assignment2/dafne/cs231n/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment2/dafne/cs231n/__init__.py -------------------------------------------------------------------------------- /assignment2/dafne/cs231n/classifiers/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment2/dafne/cs231n/classifiers/__init__.py -------------------------------------------------------------------------------- /assignment2/dafne/cs231n/datasets/.gitignore: -------------------------------------------------------------------------------- 1 | cifar-10-batches-py/* 2 | tiny-imagenet-100-A* 3 | tiny-imagenet-100-B* 4 | tiny-100-A-pretrained/* 5 | -------------------------------------------------------------------------------- /assignment2/dafne/cs231n/datasets/get_datasets.sh: -------------------------------------------------------------------------------- 1 | # Get CIFAR10 2 | wget http://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz 3 | tar -xzvf cifar-10-python.tar.gz 4 | rm cifar-10-python.tar.gz 5 | -------------------------------------------------------------------------------- /assignment2/dafne/cs231n/im2col.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def get_im2col_indices(x_shape, field_height, field_width, padding=1, stride=1): 5 | # First figure out what the size of the output should be 6 | N, C, H, W = x_shape 7 | assert (H + 2 * padding - field_height) % stride == 0 8 | assert (W + 2 * padding - field_height) % stride == 0 9 | out_height = (H + 2 * padding - field_height) / stride + 1 10 | out_width = (W + 2 * padding - field_width) / stride + 1 11 | 12 | i0 = np.repeat(np.arange(field_height), field_width) 13 | i0 = np.tile(i0, C) 14 | i1 = stride * np.repeat(np.arange(out_height), out_width) 15 | j0 = np.tile(np.arange(field_width), field_height * C) 16 | j1 = stride * np.tile(np.arange(out_width), out_height) 17 | i = i0.reshape(-1, 1) + i1.reshape(1, -1) 18 | j = j0.reshape(-1, 1) + j1.reshape(1, -1) 19 | 20 | k = np.repeat(np.arange(C), field_height * field_width).reshape(-1, 1) 21 | 22 | return (k, i, j) 23 | 24 | 25 | def im2col_indices(x, field_height, field_width, padding=1, stride=1): 26 | """ An implementation of im2col based on some fancy indexing """ 27 | # Zero-pad the input 28 | p = padding 29 | x_padded = np.pad(x, ((0, 0), (0, 0), (p, p), (p, p)), mode='constant') 30 | 31 | k, i, j = get_im2col_indices(x.shape, field_height, field_width, padding, 32 | stride) 33 | 34 | cols = x_padded[:, k, i, j] 35 | C = x.shape[1] 36 | cols = cols.transpose(1, 2, 0).reshape(field_height * field_width * C, -1) 37 | return cols 38 | 39 | 40 | def col2im_indices(cols, x_shape, field_height=3, field_width=3, padding=1, 41 | stride=1): 42 | """ An implementation of col2im based on fancy indexing and np.add.at """ 43 | N, C, H, W = x_shape 44 | H_padded, W_padded = H + 2 * padding, W + 2 * padding 45 | x_padded = np.zeros((N, C, H_padded, W_padded), dtype=cols.dtype) 46 | k, i, j = get_im2col_indices(x_shape, field_height, field_width, padding, 47 | stride) 48 | cols_reshaped = cols.reshape(C * field_height * field_width, -1, N) 49 | cols_reshaped = cols_reshaped.transpose(2, 0, 1) 50 | np.add.at(x_padded, (slice(None), k, i, j), cols_reshaped) 51 | if padding == 0: 52 | return x_padded 53 | return x_padded[:, :, padding:-padding, padding:-padding] 54 | 55 | pass 56 | -------------------------------------------------------------------------------- /assignment2/dafne/cs231n/layer_utils.py: -------------------------------------------------------------------------------- 1 | from cs231n.layers import * 2 | from cs231n.fast_layers import * 3 | 4 | 5 | def affine_relu_forward(x, w, b): 6 | """ 7 | Convenience layer that perorms an affine transform followed by a ReLU 8 | 9 | Inputs: 10 | - x: Input to the affine layer 11 | - w, b: Weights for the affine layer 12 | 13 | Returns a tuple of: 14 | - out: Output from the ReLU 15 | - cache: Object to give to the backward pass 16 | """ 17 | a, fc_cache = affine_forward(x, w, b) 18 | out, relu_cache = relu_forward(a) 19 | cache = (fc_cache, relu_cache) 20 | return out, cache 21 | 22 | 23 | def affine_relu_backward(dout, cache): 24 | """ 25 | Backward pass for the affine-relu convenience layer 26 | """ 27 | fc_cache, relu_cache = cache 28 | da = relu_backward(dout, relu_cache) 29 | dx, dw, db = affine_backward(da, fc_cache) 30 | return dx, dw, db 31 | 32 | 33 | pass 34 | 35 | 36 | def conv_relu_forward(x, w, b, conv_param): 37 | """ 38 | A convenience layer that performs a convolution followed by a ReLU. 39 | 40 | Inputs: 41 | - x: Input to the convolutional layer 42 | - w, b, conv_param: Weights and parameters for the convolutional layer 43 | 44 | Returns a tuple of: 45 | - out: Output from the ReLU 46 | - cache: Object to give to the backward pass 47 | """ 48 | a, conv_cache = conv_forward_fast(x, w, b, conv_param) 49 | out, relu_cache = relu_forward(a) 50 | cache = (conv_cache, relu_cache) 51 | return out, cache 52 | 53 | 54 | def conv_relu_backward(dout, cache): 55 | """ 56 | Backward pass for the conv-relu convenience layer. 57 | """ 58 | conv_cache, relu_cache = cache 59 | da = relu_backward(dout, relu_cache) 60 | dx, dw, db = conv_backward_fast(da, conv_cache) 61 | return dx, dw, db 62 | 63 | 64 | def conv_relu_pool_forward(x, w, b, conv_param, pool_param): 65 | """ 66 | Convenience layer that performs a convolution, a ReLU, and a pool. 67 | 68 | Inputs: 69 | - x: Input to the convolutional layer 70 | - w, b, conv_param: Weights and parameters for the convolutional layer 71 | - pool_param: Parameters for the pooling layer 72 | 73 | Returns a tuple of: 74 | - out: Output from the pooling layer 75 | - cache: Object to give to the backward pass 76 | """ 77 | a, conv_cache = conv_forward_fast(x, w, b, conv_param) 78 | s, relu_cache = relu_forward(a) 79 | out, pool_cache = max_pool_forward_fast(s, pool_param) 80 | cache = (conv_cache, relu_cache, pool_cache) 81 | return out, cache 82 | 83 | 84 | def conv_relu_pool_backward(dout, cache): 85 | """ 86 | Backward pass for the conv-relu-pool convenience layer 87 | """ 88 | conv_cache, relu_cache, pool_cache = cache 89 | ds = max_pool_backward_fast(dout, pool_cache) 90 | da = relu_backward(ds, relu_cache) 91 | dx, dw, db = conv_backward_fast(da, conv_cache) 92 | return dx, dw, db 93 | 94 | -------------------------------------------------------------------------------- /assignment2/dafne/cs231n/setup.py: -------------------------------------------------------------------------------- 1 | from distutils.core import setup 2 | from distutils.extension import Extension 3 | from Cython.Build import cythonize 4 | import numpy 5 | 6 | extensions = [ 7 | Extension('im2col_cython', ['im2col_cython.pyx'], 8 | include_dirs = [numpy.get_include()] 9 | ), 10 | ] 11 | 12 | setup( 13 | ext_modules = cythonize(extensions), 14 | ) 15 | -------------------------------------------------------------------------------- /assignment2/dafne/cs231n/vis_utils.py: -------------------------------------------------------------------------------- 1 | from math import sqrt, ceil 2 | import numpy as np 3 | 4 | def visualize_grid(Xs, ubound=255.0, padding=1): 5 | """ 6 | Reshape a 4D tensor of image data to a grid for easy visualization. 7 | 8 | Inputs: 9 | - Xs: Data of shape (N, H, W, C) 10 | - ubound: Output grid will have values scaled to the range [0, ubound] 11 | - padding: The number of blank pixels between elements of the grid 12 | """ 13 | (N, H, W, C) = Xs.shape 14 | grid_size = int(ceil(sqrt(N))) 15 | grid_height = H * grid_size + padding * (grid_size - 1) 16 | grid_width = W * grid_size + padding * (grid_size - 1) 17 | grid = np.zeros((grid_height, grid_width, C)) 18 | next_idx = 0 19 | y0, y1 = 0, H 20 | for y in xrange(grid_size): 21 | x0, x1 = 0, W 22 | for x in xrange(grid_size): 23 | if next_idx < N: 24 | img = Xs[next_idx] 25 | low, high = np.min(img), np.max(img) 26 | grid[y0:y1, x0:x1] = ubound * (img - low) / (high - low) 27 | # grid[y0:y1, x0:x1] = Xs[next_idx] 28 | next_idx += 1 29 | x0 += W + padding 30 | x1 += W + padding 31 | y0 += H + padding 32 | y1 += H + padding 33 | # grid_max = np.max(grid) 34 | # grid_min = np.min(grid) 35 | # grid = ubound * (grid - grid_min) / (grid_max - grid_min) 36 | return grid 37 | 38 | def vis_grid(Xs): 39 | """ visualize a grid of images """ 40 | (N, H, W, C) = Xs.shape 41 | A = int(ceil(sqrt(N))) 42 | G = np.ones((A*H+A, A*W+A, C), Xs.dtype) 43 | G *= np.min(Xs) 44 | n = 0 45 | for y in range(A): 46 | for x in range(A): 47 | if n < N: 48 | G[y*H+y:(y+1)*H+y, x*W+x:(x+1)*W+x, :] = Xs[n,:,:,:] 49 | n += 1 50 | # normalize to [0,1] 51 | maxg = G.max() 52 | ming = G.min() 53 | G = (G - ming)/(maxg-ming) 54 | return G 55 | 56 | def vis_nn(rows): 57 | """ visualize array of arrays of images """ 58 | N = len(rows) 59 | D = len(rows[0]) 60 | H,W,C = rows[0][0].shape 61 | Xs = rows[0][0] 62 | G = np.ones((N*H+N, D*W+D, C), Xs.dtype) 63 | for y in range(N): 64 | for x in range(D): 65 | G[y*H+y:(y+1)*H+y, x*W+x:(x+1)*W+x, :] = rows[y][x] 66 | # normalize to [0,1] 67 | maxg = G.max() 68 | ming = G.min() 69 | G = (G - ming)/(maxg-ming) 70 | return G 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /assignment2/dafne/frameworkpython: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # what real Python executable to use 4 | PYVER=2.7 5 | PATHTOPYTHON=/usr/local/bin/ 6 | PYTHON=${PATHTOPYTHON}python${PYVER} 7 | 8 | # find the root of the virtualenv, it should be the parent of the dir this script is in 9 | ENV=`$PYTHON -c "import os; print os.path.abspath(os.path.join(os.path.dirname(\"$0\"), '..'))"` 10 | 11 | # now run Python with the virtualenv set as Python's HOME 12 | export PYTHONHOME=$ENV 13 | exec $PYTHON "$@" 14 | -------------------------------------------------------------------------------- /assignment2/dafne/kitten.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment2/dafne/kitten.jpg -------------------------------------------------------------------------------- /assignment2/dafne/puppy.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment2/dafne/puppy.jpg -------------------------------------------------------------------------------- /assignment2/dafne/requirements.txt: -------------------------------------------------------------------------------- 1 | Cython==0.23.4 2 | Jinja2==2.8 3 | MarkupSafe==0.23 4 | Pillow==3.0.0 5 | Pygments==2.0.2 6 | appnope==0.1.0 7 | argparse==1.2.1 8 | backports-abc==0.4 9 | backports.ssl-match-hostname==3.5.0.1 10 | certifi==2015.11.20.1 11 | cycler==0.9.0 12 | decorator==4.0.6 13 | functools32==3.2.3-2 14 | gnureadline==6.3.3 15 | ipykernel==4.2.2 16 | ipython==4.0.1 17 | ipython-genutils==0.1.0 18 | ipywidgets==4.1.1 19 | jsonschema==2.5.1 20 | jupyter==1.0.0 21 | jupyter-client==4.1.1 22 | jupyter-console==4.0.3 23 | jupyter-core==4.0.6 24 | matplotlib==1.5.0 25 | mistune==0.7.1 26 | nbconvert==4.1.0 27 | nbformat==4.0.1 28 | notebook==4.0.6 29 | numpy==1.10.4 30 | path.py==8.1.2 31 | pexpect==4.0.1 32 | pickleshare==0.5 33 | ptyprocess==0.5 34 | pyparsing==2.0.7 35 | python-dateutil==2.4.2 36 | pytz==2015.7 37 | pyzmq==15.1.0 38 | qtconsole==4.1.1 39 | scipy==0.16.1 40 | simplegeneric==0.8.1 41 | singledispatch==3.4.0.3 42 | six==1.10.0 43 | terminado==0.5 44 | tornado==4.3 45 | traitlets==4.0.0 46 | wsgiref==0.1.2 47 | -------------------------------------------------------------------------------- /assignment2/dafne/start_ipython_osx.sh: -------------------------------------------------------------------------------- 1 | # Assume the virtualenv is called .env 2 | 3 | cp frameworkpython .env/bin 4 | .env/bin/frameworkpython -m IPython notebook 5 | -------------------------------------------------------------------------------- /assignment2/elena/.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | *.pyc 3 | .env/* 4 | -------------------------------------------------------------------------------- /assignment2/elena/collectSubmission.sh: -------------------------------------------------------------------------------- 1 | rm -f assignment2.zip 2 | zip -r assignment2.zip . -x "*.git*" "*cs231n/datasets*" "*.ipynb_checkpoints*" "*README.md" "*collectSubmission.sh" "*requirements.txt" ".env/*" "*.pyc" "*cs231n/build/*" 3 | -------------------------------------------------------------------------------- /assignment2/elena/cs231n/.gitignore: -------------------------------------------------------------------------------- 1 | build/* 2 | im2col_cython.c 3 | im2col_cython.so 4 | -------------------------------------------------------------------------------- /assignment2/elena/cs231n/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment2/elena/cs231n/__init__.py -------------------------------------------------------------------------------- /assignment2/elena/cs231n/classifiers/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment2/elena/cs231n/classifiers/__init__.py -------------------------------------------------------------------------------- /assignment2/elena/cs231n/datasets/.gitignore: -------------------------------------------------------------------------------- 1 | cifar-10-batches-py/* 2 | tiny-imagenet-100-A* 3 | tiny-imagenet-100-B* 4 | tiny-100-A-pretrained/* 5 | -------------------------------------------------------------------------------- /assignment2/elena/cs231n/datasets/get_datasets.sh: -------------------------------------------------------------------------------- 1 | # Get CIFAR10 2 | wget http://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz 3 | tar -xzvf cifar-10-python.tar.gz 4 | rm cifar-10-python.tar.gz 5 | -------------------------------------------------------------------------------- /assignment2/elena/cs231n/im2col.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def get_im2col_indices(x_shape, field_height, field_width, padding=1, stride=1): 5 | # First figure out what the size of the output should be 6 | N, C, H, W = x_shape 7 | assert (H + 2 * padding - field_height) % stride == 0 8 | assert (W + 2 * padding - field_height) % stride == 0 9 | out_height = (H + 2 * padding - field_height) / stride + 1 10 | out_width = (W + 2 * padding - field_width) / stride + 1 11 | 12 | i0 = np.repeat(np.arange(field_height), field_width) 13 | i0 = np.tile(i0, C) 14 | i1 = stride * np.repeat(np.arange(out_height), out_width) 15 | j0 = np.tile(np.arange(field_width), field_height * C) 16 | j1 = stride * np.tile(np.arange(out_width), out_height) 17 | i = i0.reshape(-1, 1) + i1.reshape(1, -1) 18 | j = j0.reshape(-1, 1) + j1.reshape(1, -1) 19 | 20 | k = np.repeat(np.arange(C), field_height * field_width).reshape(-1, 1) 21 | 22 | return (k, i, j) 23 | 24 | 25 | def im2col_indices(x, field_height, field_width, padding=1, stride=1): 26 | """ An implementation of im2col based on some fancy indexing """ 27 | # Zero-pad the input 28 | p = padding 29 | x_padded = np.pad(x, ((0, 0), (0, 0), (p, p), (p, p)), mode='constant') 30 | 31 | k, i, j = get_im2col_indices(x.shape, field_height, field_width, padding, 32 | stride) 33 | 34 | cols = x_padded[:, k, i, j] 35 | C = x.shape[1] 36 | cols = cols.transpose(1, 2, 0).reshape(field_height * field_width * C, -1) 37 | return cols 38 | 39 | 40 | def col2im_indices(cols, x_shape, field_height=3, field_width=3, padding=1, 41 | stride=1): 42 | """ An implementation of col2im based on fancy indexing and np.add.at """ 43 | N, C, H, W = x_shape 44 | H_padded, W_padded = H + 2 * padding, W + 2 * padding 45 | x_padded = np.zeros((N, C, H_padded, W_padded), dtype=cols.dtype) 46 | k, i, j = get_im2col_indices(x_shape, field_height, field_width, padding, 47 | stride) 48 | cols_reshaped = cols.reshape(C * field_height * field_width, -1, N) 49 | cols_reshaped = cols_reshaped.transpose(2, 0, 1) 50 | np.add.at(x_padded, (slice(None), k, i, j), cols_reshaped) 51 | if padding == 0: 52 | return x_padded 53 | return x_padded[:, :, padding:-padding, padding:-padding] 54 | 55 | pass 56 | -------------------------------------------------------------------------------- /assignment2/elena/cs231n/layer_utils.py: -------------------------------------------------------------------------------- 1 | from cs231n.layers import * 2 | from cs231n.fast_layers import * 3 | 4 | 5 | def affine_relu_forward(x, w, b): 6 | """ 7 | Convenience layer that perorms an affine transform followed by a ReLU 8 | 9 | Inputs: 10 | - x: Input to the affine layer 11 | - w, b: Weights for the affine layer 12 | 13 | Returns a tuple of: 14 | - out: Output from the ReLU 15 | - cache: Object to give to the backward pass 16 | """ 17 | a, fc_cache = affine_forward(x, w, b) 18 | out, relu_cache = relu_forward(a) 19 | cache = (fc_cache, relu_cache) 20 | return out, cache 21 | 22 | 23 | def affine_relu_backward(dout, cache): 24 | """ 25 | Backward pass for the affine-relu convenience layer 26 | """ 27 | fc_cache, relu_cache = cache 28 | da = relu_backward(dout, relu_cache) 29 | dx, dw, db = affine_backward(da, fc_cache) 30 | return dx, dw, db 31 | 32 | 33 | pass 34 | 35 | 36 | def conv_relu_forward(x, w, b, conv_param): 37 | """ 38 | A convenience layer that performs a convolution followed by a ReLU. 39 | 40 | Inputs: 41 | - x: Input to the convolutional layer 42 | - w, b, conv_param: Weights and parameters for the convolutional layer 43 | 44 | Returns a tuple of: 45 | - out: Output from the ReLU 46 | - cache: Object to give to the backward pass 47 | """ 48 | a, conv_cache = conv_forward_fast(x, w, b, conv_param) 49 | out, relu_cache = relu_forward(a) 50 | cache = (conv_cache, relu_cache) 51 | return out, cache 52 | 53 | 54 | def conv_relu_backward(dout, cache): 55 | """ 56 | Backward pass for the conv-relu convenience layer. 57 | """ 58 | conv_cache, relu_cache = cache 59 | da = relu_backward(dout, relu_cache) 60 | dx, dw, db = conv_backward_fast(da, conv_cache) 61 | return dx, dw, db 62 | 63 | 64 | def conv_relu_pool_forward(x, w, b, conv_param, pool_param): 65 | """ 66 | Convenience layer that performs a convolution, a ReLU, and a pool. 67 | 68 | Inputs: 69 | - x: Input to the convolutional layer 70 | - w, b, conv_param: Weights and parameters for the convolutional layer 71 | - pool_param: Parameters for the pooling layer 72 | 73 | Returns a tuple of: 74 | - out: Output from the pooling layer 75 | - cache: Object to give to the backward pass 76 | """ 77 | a, conv_cache = conv_forward_fast(x, w, b, conv_param) 78 | s, relu_cache = relu_forward(a) 79 | out, pool_cache = max_pool_forward_fast(s, pool_param) 80 | cache = (conv_cache, relu_cache, pool_cache) 81 | return out, cache 82 | 83 | 84 | def conv_relu_pool_backward(dout, cache): 85 | """ 86 | Backward pass for the conv-relu-pool convenience layer 87 | """ 88 | conv_cache, relu_cache, pool_cache = cache 89 | ds = max_pool_backward_fast(dout, pool_cache) 90 | da = relu_backward(ds, relu_cache) 91 | dx, dw, db = conv_backward_fast(da, conv_cache) 92 | return dx, dw, db 93 | 94 | -------------------------------------------------------------------------------- /assignment2/elena/cs231n/setup.py: -------------------------------------------------------------------------------- 1 | from distutils.core import setup 2 | from distutils.extension import Extension 3 | from Cython.Build import cythonize 4 | import numpy 5 | 6 | extensions = [ 7 | Extension('im2col_cython', ['im2col_cython.pyx'], 8 | include_dirs = [numpy.get_include()] 9 | ), 10 | ] 11 | 12 | setup( 13 | ext_modules = cythonize(extensions), 14 | ) 15 | -------------------------------------------------------------------------------- /assignment2/elena/cs231n/vis_utils.py: -------------------------------------------------------------------------------- 1 | from math import sqrt, ceil 2 | import numpy as np 3 | 4 | def visualize_grid(Xs, ubound=255.0, padding=1): 5 | """ 6 | Reshape a 4D tensor of image data to a grid for easy visualization. 7 | 8 | Inputs: 9 | - Xs: Data of shape (N, H, W, C) 10 | - ubound: Output grid will have values scaled to the range [0, ubound] 11 | - padding: The number of blank pixels between elements of the grid 12 | """ 13 | (N, H, W, C) = Xs.shape 14 | grid_size = int(ceil(sqrt(N))) 15 | grid_height = H * grid_size + padding * (grid_size - 1) 16 | grid_width = W * grid_size + padding * (grid_size - 1) 17 | grid = np.zeros((grid_height, grid_width, C)) 18 | next_idx = 0 19 | y0, y1 = 0, H 20 | for y in xrange(grid_size): 21 | x0, x1 = 0, W 22 | for x in xrange(grid_size): 23 | if next_idx < N: 24 | img = Xs[next_idx] 25 | low, high = np.min(img), np.max(img) 26 | grid[y0:y1, x0:x1] = ubound * (img - low) / (high - low) 27 | # grid[y0:y1, x0:x1] = Xs[next_idx] 28 | next_idx += 1 29 | x0 += W + padding 30 | x1 += W + padding 31 | y0 += H + padding 32 | y1 += H + padding 33 | # grid_max = np.max(grid) 34 | # grid_min = np.min(grid) 35 | # grid = ubound * (grid - grid_min) / (grid_max - grid_min) 36 | return grid 37 | 38 | def vis_grid(Xs): 39 | """ visualize a grid of images """ 40 | (N, H, W, C) = Xs.shape 41 | A = int(ceil(sqrt(N))) 42 | G = np.ones((A*H+A, A*W+A, C), Xs.dtype) 43 | G *= np.min(Xs) 44 | n = 0 45 | for y in range(A): 46 | for x in range(A): 47 | if n < N: 48 | G[y*H+y:(y+1)*H+y, x*W+x:(x+1)*W+x, :] = Xs[n,:,:,:] 49 | n += 1 50 | # normalize to [0,1] 51 | maxg = G.max() 52 | ming = G.min() 53 | G = (G - ming)/(maxg-ming) 54 | return G 55 | 56 | def vis_nn(rows): 57 | """ visualize array of arrays of images """ 58 | N = len(rows) 59 | D = len(rows[0]) 60 | H,W,C = rows[0][0].shape 61 | Xs = rows[0][0] 62 | G = np.ones((N*H+N, D*W+D, C), Xs.dtype) 63 | for y in range(N): 64 | for x in range(D): 65 | G[y*H+y:(y+1)*H+y, x*W+x:(x+1)*W+x, :] = rows[y][x] 66 | # normalize to [0,1] 67 | maxg = G.max() 68 | ming = G.min() 69 | G = (G - ming)/(maxg-ming) 70 | return G 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /assignment2/elena/frameworkpython: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # what real Python executable to use 4 | PYVER=2.7 5 | PATHTOPYTHON=/usr/local/bin/ 6 | PYTHON=${PATHTOPYTHON}python${PYVER} 7 | 8 | # find the root of the virtualenv, it should be the parent of the dir this script is in 9 | ENV=`$PYTHON -c "import os; print os.path.abspath(os.path.join(os.path.dirname(\"$0\"), '..'))"` 10 | 11 | # now run Python with the virtualenv set as Python's HOME 12 | export PYTHONHOME=$ENV 13 | exec $PYTHON "$@" 14 | -------------------------------------------------------------------------------- /assignment2/elena/kitten.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment2/elena/kitten.jpg -------------------------------------------------------------------------------- /assignment2/elena/puppy.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment2/elena/puppy.jpg -------------------------------------------------------------------------------- /assignment2/elena/requirements.txt: -------------------------------------------------------------------------------- 1 | Cython==0.23.4 2 | Jinja2==2.8 3 | MarkupSafe==0.23 4 | Pillow==3.0.0 5 | Pygments==2.0.2 6 | appnope==0.1.0 7 | argparse==1.2.1 8 | backports-abc==0.4 9 | backports.ssl-match-hostname==3.5.0.1 10 | certifi==2015.11.20.1 11 | cycler==0.9.0 12 | decorator==4.0.6 13 | functools32==3.2.3-2 14 | gnureadline==6.3.3 15 | ipykernel==4.2.2 16 | ipython==4.0.1 17 | ipython-genutils==0.1.0 18 | ipywidgets==4.1.1 19 | jsonschema==2.5.1 20 | jupyter==1.0.0 21 | jupyter-client==4.1.1 22 | jupyter-console==4.0.3 23 | jupyter-core==4.0.6 24 | matplotlib==1.5.0 25 | mistune==0.7.1 26 | nbconvert==4.1.0 27 | nbformat==4.0.1 28 | notebook==4.0.6 29 | numpy==1.10.4 30 | path.py==8.1.2 31 | pexpect==4.0.1 32 | pickleshare==0.5 33 | ptyprocess==0.5 34 | pyparsing==2.0.7 35 | python-dateutil==2.4.2 36 | pytz==2015.7 37 | pyzmq==15.1.0 38 | qtconsole==4.1.1 39 | scipy==0.16.1 40 | simplegeneric==0.8.1 41 | singledispatch==3.4.0.3 42 | six==1.10.0 43 | terminado==0.5 44 | tornado==4.3 45 | traitlets==4.0.0 46 | wsgiref==0.1.2 47 | -------------------------------------------------------------------------------- /assignment2/elena/start_ipython_osx.sh: -------------------------------------------------------------------------------- 1 | # Assume the virtualenv is called .env 2 | 3 | cp frameworkpython .env/bin 4 | .env/bin/frameworkpython -m IPython notebook 5 | -------------------------------------------------------------------------------- /assignment2/vincent/collectSubmission.sh: -------------------------------------------------------------------------------- 1 | rm -f assignment2.zip 2 | zip -r assignment2.zip . -x "*.git*" "*cs231n/datasets*" "*.ipynb_checkpoints*" "*README.md" "*collectSubmission.sh" "*requirements.txt" ".env/*" "*.pyc" "*cs231n/build/*" 3 | -------------------------------------------------------------------------------- /assignment2/vincent/cs231n/.gitignore: -------------------------------------------------------------------------------- 1 | build/* 2 | im2col_cython.c 3 | im2col_cython.so 4 | -------------------------------------------------------------------------------- /assignment2/vincent/cs231n/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment2/vincent/cs231n/__init__.py -------------------------------------------------------------------------------- /assignment2/vincent/cs231n/classifiers/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment2/vincent/cs231n/classifiers/__init__.py -------------------------------------------------------------------------------- /assignment2/vincent/cs231n/im2col.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def get_im2col_indices(x_shape, field_height, field_width, padding=1, stride=1): 5 | # First figure out what the size of the output should be 6 | N, C, H, W = x_shape 7 | assert (H + 2 * padding - field_height) % stride == 0 8 | assert (W + 2 * padding - field_height) % stride == 0 9 | out_height = (H + 2 * padding - field_height) / stride + 1 10 | out_width = (W + 2 * padding - field_width) / stride + 1 11 | 12 | i0 = np.repeat(np.arange(field_height), field_width) 13 | i0 = np.tile(i0, C) 14 | i1 = stride * np.repeat(np.arange(out_height), out_width) 15 | j0 = np.tile(np.arange(field_width), field_height * C) 16 | j1 = stride * np.tile(np.arange(out_width), out_height) 17 | i = i0.reshape(-1, 1) + i1.reshape(1, -1) 18 | j = j0.reshape(-1, 1) + j1.reshape(1, -1) 19 | 20 | k = np.repeat(np.arange(C), field_height * field_width).reshape(-1, 1) 21 | 22 | return (k, i, j) 23 | 24 | 25 | def im2col_indices(x, field_height, field_width, padding=1, stride=1): 26 | """ An implementation of im2col based on some fancy indexing """ 27 | # Zero-pad the input 28 | p = padding 29 | x_padded = np.pad(x, ((0, 0), (0, 0), (p, p), (p, p)), mode='constant') 30 | 31 | k, i, j = get_im2col_indices(x.shape, field_height, field_width, padding, 32 | stride) 33 | 34 | cols = x_padded[:, k, i, j] 35 | C = x.shape[1] 36 | cols = cols.transpose(1, 2, 0).reshape(field_height * field_width * C, -1) 37 | return cols 38 | 39 | 40 | def col2im_indices(cols, x_shape, field_height=3, field_width=3, padding=1, 41 | stride=1): 42 | """ An implementation of col2im based on fancy indexing and np.add.at """ 43 | N, C, H, W = x_shape 44 | H_padded, W_padded = H + 2 * padding, W + 2 * padding 45 | x_padded = np.zeros((N, C, H_padded, W_padded), dtype=cols.dtype) 46 | k, i, j = get_im2col_indices(x_shape, field_height, field_width, padding, 47 | stride) 48 | cols_reshaped = cols.reshape(C * field_height * field_width, -1, N) 49 | cols_reshaped = cols_reshaped.transpose(2, 0, 1) 50 | np.add.at(x_padded, (slice(None), k, i, j), cols_reshaped) 51 | if padding == 0: 52 | return x_padded 53 | return x_padded[:, :, padding:-padding, padding:-padding] 54 | 55 | pass 56 | -------------------------------------------------------------------------------- /assignment2/vincent/cs231n/layer_utils.py: -------------------------------------------------------------------------------- 1 | from cs231n.layers import * 2 | from cs231n.fast_layers import * 3 | 4 | 5 | def affine_relu_forward(x, w, b): 6 | """ 7 | Convenience layer that perorms an affine transform followed by a ReLU 8 | 9 | Inputs: 10 | - x: Input to the affine layer 11 | - w, b: Weights for the affine layer 12 | 13 | Returns a tuple of: 14 | - out: Output from the ReLU 15 | - cache: Object to give to the backward pass 16 | """ 17 | a, fc_cache = affine_forward(x, w, b) 18 | out, relu_cache = relu_forward(a) 19 | cache = (fc_cache, relu_cache) 20 | return out, cache 21 | 22 | 23 | def affine_relu_backward(dout, cache): 24 | """ 25 | Backward pass for the affine-relu convenience layer 26 | """ 27 | fc_cache, relu_cache = cache 28 | da = relu_backward(dout, relu_cache) 29 | dx, dw, db = affine_backward(da, fc_cache) 30 | return dx, dw, db 31 | 32 | 33 | pass 34 | 35 | 36 | def conv_relu_forward(x, w, b, conv_param): 37 | """ 38 | A convenience layer that performs a convolution followed by a ReLU. 39 | 40 | Inputs: 41 | - x: Input to the convolutional layer 42 | - w, b, conv_param: Weights and parameters for the convolutional layer 43 | 44 | Returns a tuple of: 45 | - out: Output from the ReLU 46 | - cache: Object to give to the backward pass 47 | """ 48 | a, conv_cache = conv_forward_fast(x, w, b, conv_param) 49 | out, relu_cache = relu_forward(a) 50 | cache = (conv_cache, relu_cache) 51 | return out, cache 52 | 53 | 54 | def conv_relu_backward(dout, cache): 55 | """ 56 | Backward pass for the conv-relu convenience layer. 57 | """ 58 | conv_cache, relu_cache = cache 59 | da = relu_backward(dout, relu_cache) 60 | dx, dw, db = conv_backward_fast(da, conv_cache) 61 | return dx, dw, db 62 | 63 | 64 | def conv_relu_pool_forward(x, w, b, conv_param, pool_param): 65 | """ 66 | Convenience layer that performs a convolution, a ReLU, and a pool. 67 | 68 | Inputs: 69 | - x: Input to the convolutional layer 70 | - w, b, conv_param: Weights and parameters for the convolutional layer 71 | - pool_param: Parameters for the pooling layer 72 | 73 | Returns a tuple of: 74 | - out: Output from the pooling layer 75 | - cache: Object to give to the backward pass 76 | """ 77 | a, conv_cache = conv_forward_fast(x, w, b, conv_param) 78 | s, relu_cache = relu_forward(a) 79 | out, pool_cache = max_pool_forward_fast(s, pool_param) 80 | cache = (conv_cache, relu_cache, pool_cache) 81 | return out, cache 82 | 83 | 84 | def conv_relu_pool_backward(dout, cache): 85 | """ 86 | Backward pass for the conv-relu-pool convenience layer 87 | """ 88 | conv_cache, relu_cache, pool_cache = cache 89 | ds = max_pool_backward_fast(dout, pool_cache) 90 | da = relu_backward(ds, relu_cache) 91 | dx, dw, db = conv_backward_fast(da, conv_cache) 92 | return dx, dw, db 93 | 94 | -------------------------------------------------------------------------------- /assignment2/vincent/cs231n/setup.py: -------------------------------------------------------------------------------- 1 | from distutils.core import setup 2 | from distutils.extension import Extension 3 | from Cython.Build import cythonize 4 | import numpy 5 | 6 | extensions = [ 7 | Extension('im2col_cython', ['im2col_cython.pyx'], 8 | include_dirs = [numpy.get_include()] 9 | ), 10 | ] 11 | 12 | setup( 13 | ext_modules = cythonize(extensions), 14 | ) 15 | -------------------------------------------------------------------------------- /assignment2/vincent/cs231n/vis_utils.py: -------------------------------------------------------------------------------- 1 | from math import sqrt, ceil 2 | import numpy as np 3 | 4 | def visualize_grid(Xs, ubound=255.0, padding=1): 5 | """ 6 | Reshape a 4D tensor of image data to a grid for easy visualization. 7 | 8 | Inputs: 9 | - Xs: Data of shape (N, H, W, C) 10 | - ubound: Output grid will have values scaled to the range [0, ubound] 11 | - padding: The number of blank pixels between elements of the grid 12 | """ 13 | (N, H, W, C) = Xs.shape 14 | grid_size = int(ceil(sqrt(N))) 15 | grid_height = H * grid_size + padding * (grid_size - 1) 16 | grid_width = W * grid_size + padding * (grid_size - 1) 17 | grid = np.zeros((grid_height, grid_width, C)) 18 | next_idx = 0 19 | y0, y1 = 0, H 20 | for y in xrange(grid_size): 21 | x0, x1 = 0, W 22 | for x in xrange(grid_size): 23 | if next_idx < N: 24 | img = Xs[next_idx] 25 | low, high = np.min(img), np.max(img) 26 | grid[y0:y1, x0:x1] = ubound * (img - low) / (high - low) 27 | # grid[y0:y1, x0:x1] = Xs[next_idx] 28 | next_idx += 1 29 | x0 += W + padding 30 | x1 += W + padding 31 | y0 += H + padding 32 | y1 += H + padding 33 | # grid_max = np.max(grid) 34 | # grid_min = np.min(grid) 35 | # grid = ubound * (grid - grid_min) / (grid_max - grid_min) 36 | return grid 37 | 38 | def vis_grid(Xs): 39 | """ visualize a grid of images """ 40 | (N, H, W, C) = Xs.shape 41 | A = int(ceil(sqrt(N))) 42 | G = np.ones((A*H+A, A*W+A, C), Xs.dtype) 43 | G *= np.min(Xs) 44 | n = 0 45 | for y in range(A): 46 | for x in range(A): 47 | if n < N: 48 | G[y*H+y:(y+1)*H+y, x*W+x:(x+1)*W+x, :] = Xs[n,:,:,:] 49 | n += 1 50 | # normalize to [0,1] 51 | maxg = G.max() 52 | ming = G.min() 53 | G = (G - ming)/(maxg-ming) 54 | return G 55 | 56 | def vis_nn(rows): 57 | """ visualize array of arrays of images """ 58 | N = len(rows) 59 | D = len(rows[0]) 60 | H,W,C = rows[0][0].shape 61 | Xs = rows[0][0] 62 | G = np.ones((N*H+N, D*W+D, C), Xs.dtype) 63 | for y in range(N): 64 | for x in range(D): 65 | G[y*H+y:(y+1)*H+y, x*W+x:(x+1)*W+x, :] = rows[y][x] 66 | # normalize to [0,1] 67 | maxg = G.max() 68 | ming = G.min() 69 | G = (G - ming)/(maxg-ming) 70 | return G 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /assignment2/vincent/frameworkpython: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # what real Python executable to use 4 | PYVER=2.7 5 | PATHTOPYTHON=/usr/local/bin/ 6 | PYTHON=${PATHTOPYTHON}python${PYVER} 7 | 8 | # find the root of the virtualenv, it should be the parent of the dir this script is in 9 | ENV=`$PYTHON -c "import os; print os.path.abspath(os.path.join(os.path.dirname(\"$0\"), '..'))"` 10 | 11 | # now run Python with the virtualenv set as Python's HOME 12 | export PYTHONHOME=$ENV 13 | exec $PYTHON "$@" 14 | -------------------------------------------------------------------------------- /assignment2/vincent/kitten.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment2/vincent/kitten.jpg -------------------------------------------------------------------------------- /assignment2/vincent/puppy.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment2/vincent/puppy.jpg -------------------------------------------------------------------------------- /assignment2/vincent/requirements.txt: -------------------------------------------------------------------------------- 1 | Cython==0.23.4 2 | Jinja2==2.8 3 | MarkupSafe==0.23 4 | Pillow==3.0.0 5 | Pygments==2.0.2 6 | appnope==0.1.0 7 | argparse==1.2.1 8 | backports-abc==0.4 9 | backports.ssl-match-hostname==3.5.0.1 10 | certifi==2015.11.20.1 11 | cycler==0.9.0 12 | decorator==4.0.6 13 | functools32==3.2.3-2 14 | gnureadline==6.3.3 15 | ipykernel==4.2.2 16 | ipython==4.0.1 17 | ipython-genutils==0.1.0 18 | ipywidgets==4.1.1 19 | jsonschema==2.5.1 20 | jupyter==1.0.0 21 | jupyter-client==4.1.1 22 | jupyter-console==4.0.3 23 | jupyter-core==4.0.6 24 | matplotlib==1.5.0 25 | mistune==0.7.1 26 | nbconvert==4.1.0 27 | nbformat==4.0.1 28 | notebook==4.0.6 29 | numpy==1.10.4 30 | path.py==8.1.2 31 | pexpect==4.0.1 32 | pickleshare==0.5 33 | ptyprocess==0.5 34 | pyparsing==2.0.7 35 | python-dateutil==2.4.2 36 | pytz==2015.7 37 | pyzmq==15.1.0 38 | qtconsole==4.1.1 39 | scipy==0.16.1 40 | simplegeneric==0.8.1 41 | singledispatch==3.4.0.3 42 | six==1.10.0 43 | terminado==0.5 44 | tornado==4.3 45 | traitlets==4.0.0 46 | wsgiref==0.1.2 47 | -------------------------------------------------------------------------------- /assignment2/vincent/start_ipython_osx.sh: -------------------------------------------------------------------------------- 1 | # Assume the virtualenv is called .env 2 | 3 | cp frameworkpython .env/bin 4 | .env/bin/frameworkpython -m IPython notebook 5 | -------------------------------------------------------------------------------- /assignment3/berend/.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | *.pyc 3 | .env/* 4 | -------------------------------------------------------------------------------- /assignment3/berend/collectSubmission.sh: -------------------------------------------------------------------------------- 1 | rm -f assignment3.zip 2 | zip -r assignment3.zip . -x "*.git" "*cs231n/datasets*" "*.ipynb_checkpoints*" "*README.md" "*collectSubmission.sh" "*requirements.txt" ".env/*" "*.pyc" "*cs231n/build/*" 3 | -------------------------------------------------------------------------------- /assignment3/berend/cs231n/.gitignore: -------------------------------------------------------------------------------- 1 | build/* 2 | im2col_cython.c 3 | im2col_cython.so 4 | -------------------------------------------------------------------------------- /assignment3/berend/cs231n/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment3/berend/cs231n/__init__.py -------------------------------------------------------------------------------- /assignment3/berend/cs231n/bin/activate: -------------------------------------------------------------------------------- 1 | /home/bweel/anaconda2/bin/activate -------------------------------------------------------------------------------- /assignment3/berend/cs231n/bin/conda: -------------------------------------------------------------------------------- 1 | /home/bweel/anaconda2/bin/conda -------------------------------------------------------------------------------- /assignment3/berend/cs231n/bin/deactivate: -------------------------------------------------------------------------------- 1 | /home/bweel/anaconda2/bin/deactivate -------------------------------------------------------------------------------- /assignment3/berend/cs231n/classifiers/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment3/berend/cs231n/classifiers/__init__.py -------------------------------------------------------------------------------- /assignment3/berend/cs231n/coco_utils.py: -------------------------------------------------------------------------------- 1 | import os, json 2 | import numpy as np 3 | import h5py 4 | 5 | 6 | def load_coco_data(base_dir='cs231n/datasets/coco_captioning', 7 | max_train=None, 8 | pca_features=True): 9 | data = {} 10 | caption_file = os.path.join(base_dir, 'coco2014_captions.h5') 11 | with h5py.File(caption_file, 'r') as f: 12 | for k, v in f.iteritems(): 13 | data[k] = np.asarray(v) 14 | 15 | if pca_features: 16 | train_feat_file = os.path.join(base_dir, 'train2014_vgg16_fc7_pca.h5') 17 | else: 18 | train_feat_file = os.path.join(base_dir, 'train2014_vgg16_fc7.h5') 19 | with h5py.File(train_feat_file, 'r') as f: 20 | data['train_features'] = np.asarray(f['features']) 21 | 22 | if pca_features: 23 | val_feat_file = os.path.join(base_dir, 'val2014_vgg16_fc7_pca.h5') 24 | else: 25 | val_feat_file = os.path.join(base_dir, 'val2014_vgg16_fc7.h5') 26 | with h5py.File(val_feat_file, 'r') as f: 27 | data['val_features'] = np.asarray(f['features']) 28 | 29 | dict_file = os.path.join(base_dir, 'coco2014_vocab.json') 30 | with open(dict_file, 'r') as f: 31 | dict_data = json.load(f) 32 | for k, v in dict_data.iteritems(): 33 | data[k] = v 34 | 35 | train_url_file = os.path.join(base_dir, 'train2014_urls.txt') 36 | with open(train_url_file, 'r') as f: 37 | train_urls = np.asarray([line.strip() for line in f]) 38 | data['train_urls'] = train_urls 39 | 40 | val_url_file = os.path.join(base_dir, 'val2014_urls.txt') 41 | with open(val_url_file, 'r') as f: 42 | val_urls = np.asarray([line.strip() for line in f]) 43 | data['val_urls'] = val_urls 44 | 45 | # Maybe subsample the training data 46 | if max_train is not None: 47 | num_train = data['train_captions'].shape[0] 48 | mask = np.random.randint(num_train, size=max_train) 49 | data['train_captions'] = data['train_captions'][mask] 50 | data['train_image_idxs'] = data['train_image_idxs'][mask] 51 | 52 | return data 53 | 54 | 55 | def decode_captions(captions, idx_to_word): 56 | singleton = False 57 | if captions.ndim == 1: 58 | singleton = True 59 | captions = captions[None] 60 | decoded = [] 61 | N, T = captions.shape 62 | for i in xrange(N): 63 | words = [] 64 | for t in xrange(T): 65 | word = idx_to_word[captions[i, t]] 66 | if word != '': 67 | words.append(word) 68 | if word == '': 69 | break 70 | decoded.append(' '.join(words)) 71 | if singleton: 72 | decoded = decoded[0] 73 | return decoded 74 | 75 | 76 | def sample_coco_minibatch(data, batch_size=100, split='train'): 77 | split_size = data['%s_captions' % split].shape[0] 78 | mask = np.random.choice(split_size, batch_size) 79 | captions = data['%s_captions' % split][mask] 80 | image_idxs = data['%s_image_idxs' % split][mask] 81 | image_features = data['%s_features' % split][image_idxs] 82 | urls = data['%s_urls' % split][image_idxs] 83 | return captions, image_features, urls 84 | 85 | -------------------------------------------------------------------------------- /assignment3/berend/cs231n/conda-meta/history: -------------------------------------------------------------------------------- 1 | ==> 2016-09-02 11:00:59 <== 2 | # cmd: /home/bweel/Documents/projects/cs231n/deep-learning-assignments-solutions/assignment3/berend/cs231n/bin/conda install openblas 3 | libgfortran-3.0.0-1 4 | openblas-0.2.14-4 5 | # install specs: ['openblas'] 6 | -------------------------------------------------------------------------------- /assignment3/berend/cs231n/conda-meta/libgfortran-3.0.0-1.json: -------------------------------------------------------------------------------- 1 | { 2 | "arch": "x86_64", 3 | "build": "1", 4 | "build_number": 1, 5 | "channel": "https://repo.continuum.io/pkgs/free/linux-64", 6 | "date": "2016-04-30", 7 | "depends": [], 8 | "files": [ 9 | "lib/libgfortran.so.3", 10 | "lib/libgfortran.so.3.0.0" 11 | ], 12 | "fn": "libgfortran-3.0.0-1.tar.bz2", 13 | "license": "GPL3", 14 | "link": { 15 | "source": "/home/bweel/anaconda2/pkgs/libgfortran-3.0.0-1", 16 | "type": "hard-link" 17 | }, 18 | "md5": "d7c7e92a8ccc518709474dd3eda896b9", 19 | "name": "libgfortran", 20 | "platform": "linux", 21 | "priority": 1, 22 | "requires": [], 23 | "schannel": "defaults", 24 | "size": 287481, 25 | "subdir": "linux-64", 26 | "url": "https://repo.continuum.io/pkgs/free/linux-64/libgfortran-3.0.0-1.tar.bz2", 27 | "version": "3.0.0" 28 | } -------------------------------------------------------------------------------- /assignment3/berend/cs231n/conda-meta/openblas-0.2.14-4.json: -------------------------------------------------------------------------------- 1 | { 2 | "arch": "x86_64", 3 | "build": "4", 4 | "build_number": 4, 5 | "channel": "https://repo.continuum.io/pkgs/free/linux-64", 6 | "date": "2016-03-03", 7 | "depends": [ 8 | "libgfortran 3.0*" 9 | ], 10 | "files": [ 11 | "include/cblas.h", 12 | "include/f77blas.h", 13 | "include/lapacke.h", 14 | "include/lapacke_config.h", 15 | "include/lapacke_mangling.h", 16 | "include/lapacke_utils.h", 17 | "include/openblas_config.h", 18 | "lib/cmake/openblas/OpenBLASConfig.cmake", 19 | "lib/libopenblas.so", 20 | "lib/libopenblas.so.0", 21 | "lib/libopenblas_nehalemp-r0.2.14.so" 22 | ], 23 | "fn": "openblas-0.2.14-4.tar.bz2", 24 | "license": "BSD", 25 | "link": { 26 | "source": "/home/bweel/anaconda2/pkgs/openblas-0.2.14-4", 27 | "type": "hard-link" 28 | }, 29 | "md5": "e14c4d2a6f882d7bdae2fcb5e24f96b1", 30 | "name": "openblas", 31 | "platform": "linux", 32 | "priority": 1, 33 | "requires": [], 34 | "schannel": "defaults", 35 | "size": 3764588, 36 | "subdir": "linux-64", 37 | "url": "https://repo.continuum.io/pkgs/free/linux-64/openblas-0.2.14-4.tar.bz2", 38 | "version": "0.2.14" 39 | } -------------------------------------------------------------------------------- /assignment3/berend/cs231n/im2col.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def get_im2col_indices(x_shape, field_height, field_width, padding=1, stride=1): 5 | # First figure out what the size of the output should be 6 | N, C, H, W = x_shape 7 | assert (H + 2 * padding - field_height) % stride == 0 8 | assert (W + 2 * padding - field_height) % stride == 0 9 | out_height = (H + 2 * padding - field_height) / stride + 1 10 | out_width = (W + 2 * padding - field_width) / stride + 1 11 | 12 | i0 = np.repeat(np.arange(field_height), field_width) 13 | i0 = np.tile(i0, C) 14 | i1 = stride * np.repeat(np.arange(out_height), out_width) 15 | j0 = np.tile(np.arange(field_width), field_height * C) 16 | j1 = stride * np.tile(np.arange(out_width), out_height) 17 | i = i0.reshape(-1, 1) + i1.reshape(1, -1) 18 | j = j0.reshape(-1, 1) + j1.reshape(1, -1) 19 | 20 | k = np.repeat(np.arange(C), field_height * field_width).reshape(-1, 1) 21 | 22 | return (k, i, j) 23 | 24 | 25 | def im2col_indices(x, field_height, field_width, padding=1, stride=1): 26 | """ An implementation of im2col based on some fancy indexing """ 27 | # Zero-pad the input 28 | p = padding 29 | x_padded = np.pad(x, ((0, 0), (0, 0), (p, p), (p, p)), mode='constant') 30 | 31 | k, i, j = get_im2col_indices(x.shape, field_height, field_width, padding, 32 | stride) 33 | 34 | cols = x_padded[:, k, i, j] 35 | C = x.shape[1] 36 | cols = cols.transpose(1, 2, 0).reshape(field_height * field_width * C, -1) 37 | return cols 38 | 39 | 40 | def col2im_indices(cols, x_shape, field_height=3, field_width=3, padding=1, 41 | stride=1): 42 | """ An implementation of col2im based on fancy indexing and np.add.at """ 43 | N, C, H, W = x_shape 44 | H_padded, W_padded = H + 2 * padding, W + 2 * padding 45 | x_padded = np.zeros((N, C, H_padded, W_padded), dtype=cols.dtype) 46 | k, i, j = get_im2col_indices(x_shape, field_height, field_width, padding, 47 | stride) 48 | cols_reshaped = cols.reshape(C * field_height * field_width, -1, N) 49 | cols_reshaped = cols_reshaped.transpose(2, 0, 1) 50 | np.add.at(x_padded, (slice(None), k, i, j), cols_reshaped) 51 | if padding == 0: 52 | return x_padded 53 | return x_padded[:, :, padding:-padding, padding:-padding] 54 | 55 | pass 56 | -------------------------------------------------------------------------------- /assignment3/berend/cs231n/image_utils.py: -------------------------------------------------------------------------------- 1 | import urllib2, os, tempfile 2 | 3 | import numpy as np 4 | from scipy.misc import imread 5 | 6 | from cs231n.fast_layers import conv_forward_fast 7 | 8 | 9 | """ 10 | Utility functions used for viewing and processing images. 11 | """ 12 | 13 | 14 | def blur_image(X): 15 | """ 16 | A very gentle image blurring operation, to be used as a regularizer for image 17 | generation. 18 | 19 | Inputs: 20 | - X: Image data of shape (N, 3, H, W) 21 | 22 | Returns: 23 | - X_blur: Blurred version of X, of shape (N, 3, H, W) 24 | """ 25 | w_blur = np.zeros((3, 3, 3, 3)) 26 | b_blur = np.zeros(3) 27 | blur_param = {'stride': 1, 'pad': 1} 28 | for i in xrange(3): 29 | w_blur[i, i] = np.asarray([[1, 2, 1], [2, 188, 2], [1, 2, 1]], dtype=np.float32) 30 | w_blur /= 200.0 31 | return conv_forward_fast(X, w_blur, b_blur, blur_param)[0] 32 | 33 | 34 | def preprocess_image(img, mean_img, mean='image'): 35 | """ 36 | Convert to float, transepose, and subtract mean pixel 37 | 38 | Input: 39 | - img: (H, W, 3) 40 | 41 | Returns: 42 | - (1, 3, H, 3) 43 | """ 44 | if mean == 'image': 45 | mean = mean_img 46 | elif mean == 'pixel': 47 | mean = mean_img.mean(axis=(1, 2), keepdims=True) 48 | elif mean == 'none': 49 | mean = 0 50 | else: 51 | raise ValueError('mean must be image or pixel or none') 52 | return img.astype(np.float32).transpose(2, 0, 1)[None] - mean 53 | 54 | 55 | def deprocess_image(img, mean_img, mean='image', renorm=False): 56 | """ 57 | Add mean pixel, transpose, and convert to uint8 58 | 59 | Input: 60 | - (1, 3, H, W) or (3, H, W) 61 | 62 | Returns: 63 | - (H, W, 3) 64 | """ 65 | if mean == 'image': 66 | mean = mean_img 67 | elif mean == 'pixel': 68 | mean = mean_img.mean(axis=(1, 2), keepdims=True) 69 | elif mean == 'none': 70 | mean = 0 71 | else: 72 | raise ValueError('mean must be image or pixel or none') 73 | if img.ndim == 3: 74 | img = img[None] 75 | img = (img + mean)[0].transpose(1, 2, 0) 76 | if renorm: 77 | low, high = img.min(), img.max() 78 | img = 255.0 * (img - low) / (high - low) 79 | return img.astype(np.uint8) 80 | 81 | 82 | def image_from_url(url): 83 | """ 84 | Read an image from a URL. Returns a numpy array with the pixel data. 85 | We write the image to a temporary file then read it back. Kinda gross. 86 | """ 87 | try: 88 | f = urllib2.urlopen(url) 89 | _, fname = tempfile.mkstemp() 90 | with open(fname, 'wb') as ff: 91 | ff.write(f.read()) 92 | img = imread(fname) 93 | os.remove(fname) 94 | return img 95 | except urllib2.URLError as e: 96 | print 'URL Error: ', e.reason, url 97 | except urllib2.HTTPError as e: 98 | print 'HTTP Error: ', e.code, url 99 | -------------------------------------------------------------------------------- /assignment3/berend/cs231n/include/lapacke_mangling.h: -------------------------------------------------------------------------------- 1 | #ifndef LAPACK_HEADER_INCLUDED 2 | #define LAPACK_HEADER_INCLUDED 3 | 4 | #ifndef LAPACK_GLOBAL 5 | #if defined(LAPACK_GLOBAL_PATTERN_LC) || defined(ADD_) 6 | #define LAPACK_GLOBAL(lcname,UCNAME) lcname##_ 7 | #elif defined(LAPACK_GLOBAL_PATTERN_UC) || defined(UPPER) 8 | #define LAPACK_GLOBAL(lcname,UCNAME) UCNAME 9 | #elif defined(LAPACK_GLOBAL_PATTERN_MC) || defined(NOCHANGE) 10 | #define LAPACK_GLOBAL(lcname,UCNAME) lcname 11 | #else 12 | #define LAPACK_GLOBAL(lcname,UCNAME) lcname##_ 13 | #endif 14 | #endif 15 | 16 | #endif 17 | 18 | -------------------------------------------------------------------------------- /assignment3/berend/cs231n/lib/cmake/openblas/OpenBLASConfig.cmake: -------------------------------------------------------------------------------- 1 | SET(OpenBLAS_VERSION "0.2.14") 2 | SET(OpenBLAS_INCLUDE_DIRS /home/bweel/Documents/projects/cs231n/deep-learning-assignments-solutions/assignment3/berend/cs231n/include) 3 | SET(OpenBLAS_LIBRARIES /home/bweel/Documents/projects/cs231n/deep-learning-assignments-solutions/assignment3/berend/cs231n/lib/libopenblas.so) 4 | -------------------------------------------------------------------------------- /assignment3/berend/cs231n/lib/libgfortran.so.3: -------------------------------------------------------------------------------- 1 | libgfortran.so.3.0.0 -------------------------------------------------------------------------------- /assignment3/berend/cs231n/lib/libgfortran.so.3.0.0: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment3/berend/cs231n/lib/libgfortran.so.3.0.0 -------------------------------------------------------------------------------- /assignment3/berend/cs231n/lib/libopenblas.so: -------------------------------------------------------------------------------- 1 | libopenblas_nehalemp-r0.2.14.so -------------------------------------------------------------------------------- /assignment3/berend/cs231n/lib/libopenblas.so.0: -------------------------------------------------------------------------------- 1 | libopenblas_nehalemp-r0.2.14.so -------------------------------------------------------------------------------- /assignment3/berend/cs231n/lib/libopenblas_nehalemp-r0.2.14.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment3/berend/cs231n/lib/libopenblas_nehalemp-r0.2.14.so -------------------------------------------------------------------------------- /assignment3/berend/cs231n/optim.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | """ 4 | This file implements various first-order update rules that are commonly used for 5 | training neural networks. Each update rule accepts current weights and the 6 | gradient of the loss with respect to those weights and produces the next set of 7 | weights. Each update rule has the same interface: 8 | 9 | def update(w, dw, config=None): 10 | 11 | Inputs: 12 | - w: A numpy array giving the current weights. 13 | - dw: A numpy array of the same shape as w giving the gradient of the 14 | loss with respect to w. 15 | - config: A dictionary containing hyperparameter values such as learning rate, 16 | momentum, etc. If the update rule requires caching values over many 17 | iterations, then config will also hold these cached values. 18 | 19 | Returns: 20 | - next_w: The next point after the update. 21 | - config: The config dictionary to be passed to the next iteration of the 22 | update rule. 23 | 24 | NOTE: For most update rules, the default learning rate will probably not perform 25 | well; however the default values of the other hyperparameters should work well 26 | for a variety of different problems. 27 | 28 | For efficiency, update rules may perform in-place updates, mutating w and 29 | setting next_w equal to w. 30 | """ 31 | 32 | 33 | def sgd(w, dw, config=None): 34 | """ 35 | Performs vanilla stochastic gradient descent. 36 | 37 | config format: 38 | - learning_rate: Scalar learning rate. 39 | """ 40 | if config is None: config = {} 41 | config.setdefault('learning_rate', 1e-2) 42 | 43 | w -= config['learning_rate'] * dw 44 | return w, config 45 | 46 | 47 | def adam(x, dx, config=None): 48 | """ 49 | Uses the Adam update rule, which incorporates moving averages of both the 50 | gradient and its square and a bias correction term. 51 | 52 | config format: 53 | - learning_rate: Scalar learning rate. 54 | - beta1: Decay rate for moving average of first moment of gradient. 55 | - beta2: Decay rate for moving average of second moment of gradient. 56 | - epsilon: Small scalar used for smoothing to avoid dividing by zero. 57 | - m: Moving average of gradient. 58 | - v: Moving average of squared gradient. 59 | - t: Iteration number. 60 | """ 61 | if config is None: config = {} 62 | config.setdefault('learning_rate', 1e-3) 63 | config.setdefault('beta1', 0.9) 64 | config.setdefault('beta2', 0.999) 65 | config.setdefault('epsilon', 1e-8) 66 | config.setdefault('m', np.zeros_like(x)) 67 | config.setdefault('v', np.zeros_like(x)) 68 | config.setdefault('t', 0) 69 | 70 | next_x = None 71 | beta1, beta2, eps = config['beta1'], config['beta2'], config['epsilon'] 72 | t, m, v = config['t'], config['m'], config['v'] 73 | m = beta1 * m + (1 - beta1) * dx 74 | v = beta2 * v + (1 - beta2) * (dx * dx) 75 | t += 1 76 | alpha = config['learning_rate'] * np.sqrt(1 - beta2 ** t) / (1 - beta1 ** t) 77 | x -= alpha * (m / (np.sqrt(v) + eps)) 78 | config['t'] = t 79 | config['m'] = m 80 | config['v'] = v 81 | next_x = x 82 | 83 | return next_x, config 84 | 85 | 86 | -------------------------------------------------------------------------------- /assignment3/berend/cs231n/setup.py: -------------------------------------------------------------------------------- 1 | from distutils.core import setup 2 | from distutils.extension import Extension 3 | from Cython.Build import cythonize 4 | import numpy 5 | 6 | extensions = [ 7 | Extension('im2col_cython', ['im2col_cython.pyx'], 8 | include_dirs = [numpy.get_include()] 9 | ), 10 | ] 11 | 12 | setup( 13 | ext_modules = cythonize(extensions), 14 | ) 15 | -------------------------------------------------------------------------------- /assignment3/berend/frameworkpython: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # what real Python executable to use 4 | PYVER=2.7 5 | PATHTOPYTHON=/usr/local/bin/ 6 | PYTHON=${PATHTOPYTHON}python${PYVER} 7 | 8 | # find the root of the virtualenv, it should be the parent of the dir this script is in 9 | ENV=`$PYTHON -c "import os; print os.path.abspath(os.path.join(os.path.dirname(\"$0\"), '..'))"` 10 | 11 | # now run Python with the virtualenv set as Python's HOME 12 | export PYTHONHOME=$ENV 13 | exec $PYTHON "$@" 14 | -------------------------------------------------------------------------------- /assignment3/berend/kitten.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment3/berend/kitten.jpg -------------------------------------------------------------------------------- /assignment3/berend/requirements.txt: -------------------------------------------------------------------------------- 1 | Cython==0.23.4 2 | Jinja2==2.8 3 | MarkupSafe==0.23 4 | Pillow==3.0.0 5 | Pygments==2.0.2 6 | appnope==0.1.0 7 | argparse==1.2.1 8 | backports-abc==0.4 9 | backports.ssl-match-hostname==3.5.0.1 10 | certifi==2015.11.20.1 11 | cycler==0.9.0 12 | decorator==4.0.6 13 | functools32==3.2.3-2 14 | gnureadline==6.3.3 15 | ipykernel==4.2.2 16 | ipython==4.0.1 17 | ipython-genutils==0.1.0 18 | ipywidgets==4.1.1 19 | jsonschema==2.5.1 20 | jupyter==1.0.0 21 | jupyter-client==4.1.1 22 | jupyter-console==4.0.3 23 | jupyter-core==4.0.6 24 | matplotlib==1.5.0 25 | mistune==0.7.1 26 | nbconvert==4.1.0 27 | nbformat==4.0.1 28 | notebook==4.0.6 29 | numpy==1.10.4 30 | path.py==8.1.2 31 | pexpect==4.0.1 32 | pickleshare==0.5 33 | ptyprocess==0.5 34 | pyparsing==2.0.7 35 | python-dateutil==2.4.2 36 | pytz==2015.7 37 | pyzmq==15.1.0 38 | qtconsole==4.1.1 39 | scipy==0.16.1 40 | simplegeneric==0.8.1 41 | singledispatch==3.4.0.3 42 | six==1.10.0 43 | terminado==0.5 44 | tornado==4.3 45 | traitlets==4.0.0 46 | wsgiref==0.1.2 47 | -------------------------------------------------------------------------------- /assignment3/berend/sky.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment3/berend/sky.jpg -------------------------------------------------------------------------------- /assignment3/berend/start_ipython_osx.sh: -------------------------------------------------------------------------------- 1 | # Assume the virtualenv is called .env 2 | 3 | cp frameworkpython .env/bin 4 | .env/bin/frameworkpython -m IPython notebook 5 | -------------------------------------------------------------------------------- /assignment3/carlos/.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | *.pyc 3 | .env/* 4 | venv/* 5 | cs231n/datasets/ 6 | -------------------------------------------------------------------------------- /assignment3/carlos/collectSubmission.sh: -------------------------------------------------------------------------------- 1 | rm -f assignment3.zip 2 | zip -r assignment3.zip . -x "*.git" "*cs231n/datasets*" "*.ipynb_checkpoints*" "*README.md" "*collectSubmission.sh" "*requirements.txt" ".env/*" "*.pyc" "*cs231n/build/*" 3 | -------------------------------------------------------------------------------- /assignment3/carlos/cs231n/.gitignore: -------------------------------------------------------------------------------- 1 | build/* 2 | im2col_cython.c 3 | im2col_cython.so 4 | -------------------------------------------------------------------------------- /assignment3/carlos/cs231n/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment3/carlos/cs231n/__init__.py -------------------------------------------------------------------------------- /assignment3/carlos/cs231n/classifiers/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment3/carlos/cs231n/classifiers/__init__.py -------------------------------------------------------------------------------- /assignment3/carlos/cs231n/coco_utils.py: -------------------------------------------------------------------------------- 1 | import os, json 2 | import numpy as np 3 | import h5py 4 | 5 | 6 | def load_coco_data(base_dir='cs231n/datasets/coco_captioning', 7 | max_train=None, 8 | pca_features=True): 9 | data = {} 10 | caption_file = os.path.join(base_dir, 'coco2014_captions.h5') 11 | with h5py.File(caption_file, 'r') as f: 12 | for k, v in f.iteritems(): 13 | data[k] = np.asarray(v) 14 | 15 | if pca_features: 16 | train_feat_file = os.path.join(base_dir, 'train2014_vgg16_fc7_pca.h5') 17 | else: 18 | train_feat_file = os.path.join(base_dir, 'train2014_vgg16_fc7.h5') 19 | with h5py.File(train_feat_file, 'r') as f: 20 | data['train_features'] = np.asarray(f['features']) 21 | 22 | if pca_features: 23 | val_feat_file = os.path.join(base_dir, 'val2014_vgg16_fc7_pca.h5') 24 | else: 25 | val_feat_file = os.path.join(base_dir, 'val2014_vgg16_fc7.h5') 26 | with h5py.File(val_feat_file, 'r') as f: 27 | data['val_features'] = np.asarray(f['features']) 28 | 29 | dict_file = os.path.join(base_dir, 'coco2014_vocab.json') 30 | with open(dict_file, 'r') as f: 31 | dict_data = json.load(f) 32 | for k, v in dict_data.iteritems(): 33 | data[k] = v 34 | 35 | train_url_file = os.path.join(base_dir, 'train2014_urls.txt') 36 | with open(train_url_file, 'r') as f: 37 | train_urls = np.asarray([line.strip() for line in f]) 38 | data['train_urls'] = train_urls 39 | 40 | val_url_file = os.path.join(base_dir, 'val2014_urls.txt') 41 | with open(val_url_file, 'r') as f: 42 | val_urls = np.asarray([line.strip() for line in f]) 43 | data['val_urls'] = val_urls 44 | 45 | # Maybe subsample the training data 46 | if max_train is not None: 47 | num_train = data['train_captions'].shape[0] 48 | mask = np.random.randint(num_train, size=max_train) 49 | data['train_captions'] = data['train_captions'][mask] 50 | data['train_image_idxs'] = data['train_image_idxs'][mask] 51 | 52 | return data 53 | 54 | 55 | def decode_captions(captions, idx_to_word): 56 | singleton = False 57 | if captions.ndim == 1: 58 | singleton = True 59 | captions = captions[None] 60 | decoded = [] 61 | N, T = captions.shape 62 | for i in xrange(N): 63 | words = [] 64 | for t in xrange(T): 65 | word = idx_to_word[captions[i, t]] 66 | if word != '': 67 | words.append(word) 68 | if word == '': 69 | break 70 | decoded.append(' '.join(words)) 71 | if singleton: 72 | decoded = decoded[0] 73 | return decoded 74 | 75 | 76 | def sample_coco_minibatch(data, batch_size=100, split='train'): 77 | split_size = data['%s_captions' % split].shape[0] 78 | mask = np.random.choice(split_size, batch_size) 79 | captions = data['%s_captions' % split][mask] 80 | image_idxs = data['%s_image_idxs' % split][mask] 81 | image_features = data['%s_features' % split][image_idxs] 82 | urls = data['%s_urls' % split][image_idxs] 83 | return captions, image_features, urls 84 | 85 | -------------------------------------------------------------------------------- /assignment3/carlos/cs231n/im2col.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def get_im2col_indices(x_shape, field_height, field_width, padding=1, stride=1): 5 | # First figure out what the size of the output should be 6 | N, C, H, W = x_shape 7 | assert (H + 2 * padding - field_height) % stride == 0 8 | assert (W + 2 * padding - field_height) % stride == 0 9 | out_height = (H + 2 * padding - field_height) / stride + 1 10 | out_width = (W + 2 * padding - field_width) / stride + 1 11 | 12 | i0 = np.repeat(np.arange(field_height), field_width) 13 | i0 = np.tile(i0, C) 14 | i1 = stride * np.repeat(np.arange(out_height), out_width) 15 | j0 = np.tile(np.arange(field_width), field_height * C) 16 | j1 = stride * np.tile(np.arange(out_width), out_height) 17 | i = i0.reshape(-1, 1) + i1.reshape(1, -1) 18 | j = j0.reshape(-1, 1) + j1.reshape(1, -1) 19 | 20 | k = np.repeat(np.arange(C), field_height * field_width).reshape(-1, 1) 21 | 22 | return (k, i, j) 23 | 24 | 25 | def im2col_indices(x, field_height, field_width, padding=1, stride=1): 26 | """ An implementation of im2col based on some fancy indexing """ 27 | # Zero-pad the input 28 | p = padding 29 | x_padded = np.pad(x, ((0, 0), (0, 0), (p, p), (p, p)), mode='constant') 30 | 31 | k, i, j = get_im2col_indices(x.shape, field_height, field_width, padding, 32 | stride) 33 | 34 | cols = x_padded[:, k, i, j] 35 | C = x.shape[1] 36 | cols = cols.transpose(1, 2, 0).reshape(field_height * field_width * C, -1) 37 | return cols 38 | 39 | 40 | def col2im_indices(cols, x_shape, field_height=3, field_width=3, padding=1, 41 | stride=1): 42 | """ An implementation of col2im based on fancy indexing and np.add.at """ 43 | N, C, H, W = x_shape 44 | H_padded, W_padded = H + 2 * padding, W + 2 * padding 45 | x_padded = np.zeros((N, C, H_padded, W_padded), dtype=cols.dtype) 46 | k, i, j = get_im2col_indices(x_shape, field_height, field_width, padding, 47 | stride) 48 | cols_reshaped = cols.reshape(C * field_height * field_width, -1, N) 49 | cols_reshaped = cols_reshaped.transpose(2, 0, 1) 50 | np.add.at(x_padded, (slice(None), k, i, j), cols_reshaped) 51 | if padding == 0: 52 | return x_padded 53 | return x_padded[:, :, padding:-padding, padding:-padding] 54 | 55 | pass 56 | -------------------------------------------------------------------------------- /assignment3/carlos/cs231n/image_utils.py: -------------------------------------------------------------------------------- 1 | import urllib2, os, tempfile 2 | 3 | import numpy as np 4 | from scipy.misc import imread 5 | 6 | from cs231n.fast_layers import conv_forward_fast 7 | 8 | 9 | """ 10 | Utility functions used for viewing and processing images. 11 | """ 12 | 13 | 14 | def blur_image(X): 15 | """ 16 | A very gentle image blurring operation, to be used as a regularizer for image 17 | generation. 18 | 19 | Inputs: 20 | - X: Image data of shape (N, 3, H, W) 21 | 22 | Returns: 23 | - X_blur: Blurred version of X, of shape (N, 3, H, W) 24 | """ 25 | w_blur = np.zeros((3, 3, 3, 3)) 26 | b_blur = np.zeros(3) 27 | blur_param = {'stride': 1, 'pad': 1} 28 | for i in xrange(3): 29 | w_blur[i, i] = np.asarray([[1, 2, 1], [2, 188, 2], [1, 2, 1]], dtype=np.float32) 30 | w_blur /= 200.0 31 | return conv_forward_fast(X, w_blur, b_blur, blur_param)[0] 32 | 33 | 34 | def preprocess_image(img, mean_img, mean='image'): 35 | """ 36 | Convert to float, transepose, and subtract mean pixel 37 | 38 | Input: 39 | - img: (H, W, 3) 40 | 41 | Returns: 42 | - (1, 3, H, 3) 43 | """ 44 | if mean == 'image': 45 | mean = mean_img 46 | elif mean == 'pixel': 47 | mean = mean_img.mean(axis=(1, 2), keepdims=True) 48 | elif mean == 'none': 49 | mean = 0 50 | else: 51 | raise ValueError('mean must be image or pixel or none') 52 | return img.astype(np.float32).transpose(2, 0, 1)[None] - mean 53 | 54 | 55 | def deprocess_image(img, mean_img, mean='image', renorm=False): 56 | """ 57 | Add mean pixel, transpose, and convert to uint8 58 | 59 | Input: 60 | - (1, 3, H, W) or (3, H, W) 61 | 62 | Returns: 63 | - (H, W, 3) 64 | """ 65 | if mean == 'image': 66 | mean = mean_img 67 | elif mean == 'pixel': 68 | mean = mean_img.mean(axis=(1, 2), keepdims=True) 69 | elif mean == 'none': 70 | mean = 0 71 | else: 72 | raise ValueError('mean must be image or pixel or none') 73 | if img.ndim == 3: 74 | img = img[None] 75 | img = (img + mean)[0].transpose(1, 2, 0) 76 | if renorm: 77 | low, high = img.min(), img.max() 78 | img = 255.0 * (img - low) / (high - low) 79 | return img.astype(np.uint8) 80 | 81 | 82 | def image_from_url(url): 83 | """ 84 | Read an image from a URL. Returns a numpy array with the pixel data. 85 | We write the image to a temporary file then read it back. Kinda gross. 86 | """ 87 | try: 88 | f = urllib2.urlopen(url) 89 | _, fname = tempfile.mkstemp() 90 | with open(fname, 'wb') as ff: 91 | ff.write(f.read()) 92 | img = imread(fname) 93 | os.remove(fname) 94 | return img 95 | except urllib2.URLError as e: 96 | print 'URL Error: ', e.reason, url 97 | except urllib2.HTTPError as e: 98 | print 'HTTP Error: ', e.code, url 99 | -------------------------------------------------------------------------------- /assignment3/carlos/cs231n/optim.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | """ 4 | This file implements various first-order update rules that are commonly used for 5 | training neural networks. Each update rule accepts current weights and the 6 | gradient of the loss with respect to those weights and produces the next set of 7 | weights. Each update rule has the same interface: 8 | 9 | def update(w, dw, config=None): 10 | 11 | Inputs: 12 | - w: A numpy array giving the current weights. 13 | - dw: A numpy array of the same shape as w giving the gradient of the 14 | loss with respect to w. 15 | - config: A dictionary containing hyperparameter values such as learning rate, 16 | momentum, etc. If the update rule requires caching values over many 17 | iterations, then config will also hold these cached values. 18 | 19 | Returns: 20 | - next_w: The next point after the update. 21 | - config: The config dictionary to be passed to the next iteration of the 22 | update rule. 23 | 24 | NOTE: For most update rules, the default learning rate will probably not perform 25 | well; however the default values of the other hyperparameters should work well 26 | for a variety of different problems. 27 | 28 | For efficiency, update rules may perform in-place updates, mutating w and 29 | setting next_w equal to w. 30 | """ 31 | 32 | 33 | def sgd(w, dw, config=None): 34 | """ 35 | Performs vanilla stochastic gradient descent. 36 | 37 | config format: 38 | - learning_rate: Scalar learning rate. 39 | """ 40 | if config is None: config = {} 41 | config.setdefault('learning_rate', 1e-2) 42 | 43 | w -= config['learning_rate'] * dw 44 | return w, config 45 | 46 | 47 | def adam(x, dx, config=None): 48 | """ 49 | Uses the Adam update rule, which incorporates moving averages of both the 50 | gradient and its square and a bias correction term. 51 | 52 | config format: 53 | - learning_rate: Scalar learning rate. 54 | - beta1: Decay rate for moving average of first moment of gradient. 55 | - beta2: Decay rate for moving average of second moment of gradient. 56 | - epsilon: Small scalar used for smoothing to avoid dividing by zero. 57 | - m: Moving average of gradient. 58 | - v: Moving average of squared gradient. 59 | - t: Iteration number. 60 | """ 61 | if config is None: config = {} 62 | config.setdefault('learning_rate', 1e-3) 63 | config.setdefault('beta1', 0.9) 64 | config.setdefault('beta2', 0.999) 65 | config.setdefault('epsilon', 1e-8) 66 | config.setdefault('m', np.zeros_like(x)) 67 | config.setdefault('v', np.zeros_like(x)) 68 | config.setdefault('t', 0) 69 | 70 | next_x = None 71 | beta1, beta2, eps = config['beta1'], config['beta2'], config['epsilon'] 72 | t, m, v = config['t'], config['m'], config['v'] 73 | m = beta1 * m + (1 - beta1) * dx 74 | v = beta2 * v + (1 - beta2) * (dx * dx) 75 | t += 1 76 | alpha = config['learning_rate'] * np.sqrt(1 - beta2 ** t) / (1 - beta1 ** t) 77 | x -= alpha * (m / (np.sqrt(v) + eps)) 78 | config['t'] = t 79 | config['m'] = m 80 | config['v'] = v 81 | next_x = x 82 | 83 | return next_x, config 84 | 85 | 86 | -------------------------------------------------------------------------------- /assignment3/carlos/cs231n/setup.py: -------------------------------------------------------------------------------- 1 | from distutils.core import setup 2 | from distutils.extension import Extension 3 | from Cython.Build import cythonize 4 | import numpy 5 | 6 | extensions = [ 7 | Extension('im2col_cython', ['im2col_cython.pyx'], 8 | include_dirs = [numpy.get_include()] 9 | ), 10 | ] 11 | 12 | setup( 13 | ext_modules = cythonize(extensions), 14 | ) 15 | -------------------------------------------------------------------------------- /assignment3/carlos/frameworkpython: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # what real Python executable to use 4 | PYVER=2.7 5 | PATHTOPYTHON=/usr/local/bin/ 6 | PYTHON=${PATHTOPYTHON}python${PYVER} 7 | 8 | # find the root of the virtualenv, it should be the parent of the dir this script is in 9 | ENV=`$PYTHON -c "import os; print os.path.abspath(os.path.join(os.path.dirname(\"$0\"), '..'))"` 10 | 11 | # now run Python with the virtualenv set as Python's HOME 12 | export PYTHONHOME=$ENV 13 | exec $PYTHON "$@" 14 | -------------------------------------------------------------------------------- /assignment3/carlos/kitten.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment3/carlos/kitten.jpg -------------------------------------------------------------------------------- /assignment3/carlos/requirements.txt: -------------------------------------------------------------------------------- 1 | Cython==0.23.4 2 | Jinja2==2.8 3 | MarkupSafe==0.23 4 | Pillow==3.0.0 5 | Pygments==2.0.2 6 | appnope==0.1.0 7 | argparse==1.2.1 8 | backports-abc==0.4 9 | backports.ssl-match-hostname==3.5.0.1 10 | certifi==2015.11.20.1 11 | cycler==0.9.0 12 | decorator==4.0.6 13 | functools32==3.2.3-2 14 | gnureadline==6.3.3 15 | ipykernel==4.2.2 16 | ipython==4.0.1 17 | ipython-genutils==0.1.0 18 | ipywidgets==4.1.1 19 | jsonschema==2.5.1 20 | jupyter==1.0.0 21 | jupyter-client==4.1.1 22 | jupyter-console==4.0.3 23 | jupyter-core==4.0.6 24 | matplotlib==1.5.0 25 | mistune==0.7.1 26 | nbconvert==4.1.0 27 | nbformat==4.0.1 28 | notebook==4.0.6 29 | numpy==1.10.4 30 | path.py==8.1.2 31 | pexpect==4.0.1 32 | pickleshare==0.5 33 | ptyprocess==0.5 34 | pyparsing==2.0.7 35 | python-dateutil==2.4.2 36 | pytz==2015.7 37 | pyzmq==15.1.0 38 | qtconsole==4.1.1 39 | scipy==0.16.1 40 | simplegeneric==0.8.1 41 | singledispatch==3.4.0.3 42 | six==1.10.0 43 | terminado==0.5 44 | tornado==4.3 45 | traitlets==4.0.0 46 | wsgiref==0.1.2 47 | -------------------------------------------------------------------------------- /assignment3/carlos/sky.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment3/carlos/sky.jpg -------------------------------------------------------------------------------- /assignment3/carlos/start_ipython_osx.sh: -------------------------------------------------------------------------------- 1 | # Assume the virtualenv is called .env 2 | 3 | cp frameworkpython .env/bin 4 | .env/bin/frameworkpython -m IPython notebook 5 | -------------------------------------------------------------------------------- /assignment3/chris/collectSubmission.sh: -------------------------------------------------------------------------------- 1 | rm -f assignment3.zip 2 | zip -r assignment3.zip . -x "*.git" "*cs231n/datasets*" "*.ipynb_checkpoints*" "*README.md" "*collectSubmission.sh" "*requirements.txt" ".env/*" "*.pyc" "*cs231n/build/*" 3 | -------------------------------------------------------------------------------- /assignment3/chris/cs231n/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment3/chris/cs231n/__init__.py -------------------------------------------------------------------------------- /assignment3/chris/cs231n/classifiers/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment3/chris/cs231n/classifiers/__init__.py -------------------------------------------------------------------------------- /assignment3/chris/cs231n/coco_utils.py: -------------------------------------------------------------------------------- 1 | import os, json 2 | import numpy as np 3 | import h5py 4 | 5 | 6 | def load_coco_data(base_dir='cs231n/datasets/coco_captioning', 7 | max_train=None, 8 | pca_features=True): 9 | data = {} 10 | caption_file = os.path.join(base_dir, 'coco2014_captions.h5') 11 | with h5py.File(caption_file, 'r') as f: 12 | for k, v in f.iteritems(): 13 | data[k] = np.asarray(v) 14 | 15 | if pca_features: 16 | train_feat_file = os.path.join(base_dir, 'train2014_vgg16_fc7_pca.h5') 17 | else: 18 | train_feat_file = os.path.join(base_dir, 'train2014_vgg16_fc7.h5') 19 | with h5py.File(train_feat_file, 'r') as f: 20 | data['train_features'] = np.asarray(f['features']) 21 | 22 | if pca_features: 23 | val_feat_file = os.path.join(base_dir, 'val2014_vgg16_fc7_pca.h5') 24 | else: 25 | val_feat_file = os.path.join(base_dir, 'val2014_vgg16_fc7.h5') 26 | with h5py.File(val_feat_file, 'r') as f: 27 | data['val_features'] = np.asarray(f['features']) 28 | 29 | dict_file = os.path.join(base_dir, 'coco2014_vocab.json') 30 | with open(dict_file, 'r') as f: 31 | dict_data = json.load(f) 32 | for k, v in dict_data.iteritems(): 33 | data[k] = v 34 | 35 | train_url_file = os.path.join(base_dir, 'train2014_urls.txt') 36 | with open(train_url_file, 'r') as f: 37 | train_urls = np.asarray([line.strip() for line in f]) 38 | data['train_urls'] = train_urls 39 | 40 | val_url_file = os.path.join(base_dir, 'val2014_urls.txt') 41 | with open(val_url_file, 'r') as f: 42 | val_urls = np.asarray([line.strip() for line in f]) 43 | data['val_urls'] = val_urls 44 | 45 | # Maybe subsample the training data 46 | if max_train is not None: 47 | num_train = data['train_captions'].shape[0] 48 | mask = np.random.randint(num_train, size=max_train) 49 | data['train_captions'] = data['train_captions'][mask] 50 | data['train_image_idxs'] = data['train_image_idxs'][mask] 51 | 52 | return data 53 | 54 | 55 | def decode_captions(captions, idx_to_word): 56 | singleton = False 57 | if captions.ndim == 1: 58 | singleton = True 59 | captions = captions[None] 60 | decoded = [] 61 | N, T = captions.shape 62 | for i in xrange(N): 63 | words = [] 64 | for t in xrange(T): 65 | word = idx_to_word[captions[i, t]] 66 | if word != '': 67 | words.append(word) 68 | if word == '': 69 | break 70 | decoded.append(' '.join(words)) 71 | if singleton: 72 | decoded = decoded[0] 73 | return decoded 74 | 75 | 76 | def sample_coco_minibatch(data, batch_size=100, split='train'): 77 | split_size = data['%s_captions' % split].shape[0] 78 | mask = np.random.choice(split_size, batch_size) 79 | captions = data['%s_captions' % split][mask] 80 | image_idxs = data['%s_image_idxs' % split][mask] 81 | image_features = data['%s_features' % split][image_idxs] 82 | urls = data['%s_urls' % split][image_idxs] 83 | return captions, image_features, urls 84 | 85 | -------------------------------------------------------------------------------- /assignment3/chris/cs231n/datasets/get_coco_captioning.sh: -------------------------------------------------------------------------------- 1 | wget "http://cs231n.stanford.edu/coco_captioning.zip" 2 | unzip coco_captioning.zip 3 | rm coco_captioning.zip 4 | -------------------------------------------------------------------------------- /assignment3/chris/cs231n/datasets/get_pretrained_model.sh: -------------------------------------------------------------------------------- 1 | wget http://cs231n.stanford.edu/pretrained_model.h5 2 | -------------------------------------------------------------------------------- /assignment3/chris/cs231n/datasets/get_tiny_imagenet_a.sh: -------------------------------------------------------------------------------- 1 | wget http://cs231n.stanford.edu/tiny-imagenet-100-A.zip 2 | unzip tiny-imagenet-100-A.zip 3 | rm tiny-imagenet-100-A.zip 4 | -------------------------------------------------------------------------------- /assignment3/chris/cs231n/im2col.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def get_im2col_indices(x_shape, field_height, field_width, padding=1, stride=1): 5 | # First figure out what the size of the output should be 6 | N, C, H, W = x_shape 7 | assert (H + 2 * padding - field_height) % stride == 0 8 | assert (W + 2 * padding - field_height) % stride == 0 9 | out_height = (H + 2 * padding - field_height) / stride + 1 10 | out_width = (W + 2 * padding - field_width) / stride + 1 11 | 12 | i0 = np.repeat(np.arange(field_height), field_width) 13 | i0 = np.tile(i0, C) 14 | i1 = stride * np.repeat(np.arange(out_height), out_width) 15 | j0 = np.tile(np.arange(field_width), field_height * C) 16 | j1 = stride * np.tile(np.arange(out_width), out_height) 17 | i = i0.reshape(-1, 1) + i1.reshape(1, -1) 18 | j = j0.reshape(-1, 1) + j1.reshape(1, -1) 19 | 20 | k = np.repeat(np.arange(C), field_height * field_width).reshape(-1, 1) 21 | 22 | return (k, i, j) 23 | 24 | 25 | def im2col_indices(x, field_height, field_width, padding=1, stride=1): 26 | """ An implementation of im2col based on some fancy indexing """ 27 | # Zero-pad the input 28 | p = padding 29 | x_padded = np.pad(x, ((0, 0), (0, 0), (p, p), (p, p)), mode='constant') 30 | 31 | k, i, j = get_im2col_indices(x.shape, field_height, field_width, padding, 32 | stride) 33 | 34 | cols = x_padded[:, k, i, j] 35 | C = x.shape[1] 36 | cols = cols.transpose(1, 2, 0).reshape(field_height * field_width * C, -1) 37 | return cols 38 | 39 | 40 | def col2im_indices(cols, x_shape, field_height=3, field_width=3, padding=1, 41 | stride=1): 42 | """ An implementation of col2im based on fancy indexing and np.add.at """ 43 | N, C, H, W = x_shape 44 | H_padded, W_padded = H + 2 * padding, W + 2 * padding 45 | x_padded = np.zeros((N, C, H_padded, W_padded), dtype=cols.dtype) 46 | k, i, j = get_im2col_indices(x_shape, field_height, field_width, padding, 47 | stride) 48 | cols_reshaped = cols.reshape(C * field_height * field_width, -1, N) 49 | cols_reshaped = cols_reshaped.transpose(2, 0, 1) 50 | np.add.at(x_padded, (slice(None), k, i, j), cols_reshaped) 51 | if padding == 0: 52 | return x_padded 53 | return x_padded[:, :, padding:-padding, padding:-padding] 54 | 55 | pass 56 | -------------------------------------------------------------------------------- /assignment3/chris/cs231n/image_utils.py: -------------------------------------------------------------------------------- 1 | import urllib2, os, tempfile 2 | 3 | import numpy as np 4 | from scipy.misc import imread 5 | 6 | from cs231n.fast_layers import conv_forward_fast 7 | 8 | 9 | """ 10 | Utility functions used for viewing and processing images. 11 | """ 12 | 13 | 14 | def blur_image(X): 15 | """ 16 | A very gentle image blurring operation, to be used as a regularizer for image 17 | generation. 18 | 19 | Inputs: 20 | - X: Image data of shape (N, 3, H, W) 21 | 22 | Returns: 23 | - X_blur: Blurred version of X, of shape (N, 3, H, W) 24 | """ 25 | w_blur = np.zeros((3, 3, 3, 3)) 26 | b_blur = np.zeros(3) 27 | blur_param = {'stride': 1, 'pad': 1} 28 | for i in xrange(3): 29 | w_blur[i, i] = np.asarray([[1, 2, 1], [2, 188, 2], [1, 2, 1]], dtype=np.float32) 30 | w_blur /= 200.0 31 | return conv_forward_fast(X, w_blur, b_blur, blur_param)[0] 32 | 33 | 34 | def preprocess_image(img, mean_img, mean='image'): 35 | """ 36 | Convert to float, transepose, and subtract mean pixel 37 | 38 | Input: 39 | - img: (H, W, 3) 40 | 41 | Returns: 42 | - (1, 3, H, 3) 43 | """ 44 | if mean == 'image': 45 | mean = mean_img 46 | elif mean == 'pixel': 47 | mean = mean_img.mean(axis=(1, 2), keepdims=True) 48 | elif mean == 'none': 49 | mean = 0 50 | else: 51 | raise ValueError('mean must be image or pixel or none') 52 | return img.astype(np.float32).transpose(2, 0, 1)[None] - mean 53 | 54 | 55 | def deprocess_image(img, mean_img, mean='image', renorm=False): 56 | """ 57 | Add mean pixel, transpose, and convert to uint8 58 | 59 | Input: 60 | - (1, 3, H, W) or (3, H, W) 61 | 62 | Returns: 63 | - (H, W, 3) 64 | """ 65 | if mean == 'image': 66 | mean = mean_img 67 | elif mean == 'pixel': 68 | mean = mean_img.mean(axis=(1, 2), keepdims=True) 69 | elif mean == 'none': 70 | mean = 0 71 | else: 72 | raise ValueError('mean must be image or pixel or none') 73 | if img.ndim == 3: 74 | img = img[None] 75 | img = (img + mean)[0].transpose(1, 2, 0) 76 | if renorm: 77 | low, high = img.min(), img.max() 78 | img = 255.0 * (img - low) / (high - low) 79 | return img.astype(np.uint8) 80 | 81 | 82 | def image_from_url(url): 83 | """ 84 | Read an image from a URL. Returns a numpy array with the pixel data. 85 | We write the image to a temporary file then read it back. Kinda gross. 86 | """ 87 | try: 88 | f = urllib2.urlopen(url) 89 | _, fname = tempfile.mkstemp() 90 | with open(fname, 'wb') as ff: 91 | ff.write(f.read()) 92 | img = imread(fname) 93 | os.remove(fname) 94 | return img 95 | except urllib2.URLError as e: 96 | print 'URL Error: ', e.reason, url 97 | except urllib2.HTTPError as e: 98 | print 'HTTP Error: ', e.code, url 99 | -------------------------------------------------------------------------------- /assignment3/chris/cs231n/optim.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | """ 4 | This file implements various first-order update rules that are commonly used for 5 | training neural networks. Each update rule accepts current weights and the 6 | gradient of the loss with respect to those weights and produces the next set of 7 | weights. Each update rule has the same interface: 8 | 9 | def update(w, dw, config=None): 10 | 11 | Inputs: 12 | - w: A numpy array giving the current weights. 13 | - dw: A numpy array of the same shape as w giving the gradient of the 14 | loss with respect to w. 15 | - config: A dictionary containing hyperparameter values such as learning rate, 16 | momentum, etc. If the update rule requires caching values over many 17 | iterations, then config will also hold these cached values. 18 | 19 | Returns: 20 | - next_w: The next point after the update. 21 | - config: The config dictionary to be passed to the next iteration of the 22 | update rule. 23 | 24 | NOTE: For most update rules, the default learning rate will probably not perform 25 | well; however the default values of the other hyperparameters should work well 26 | for a variety of different problems. 27 | 28 | For efficiency, update rules may perform in-place updates, mutating w and 29 | setting next_w equal to w. 30 | """ 31 | 32 | 33 | def sgd(w, dw, config=None): 34 | """ 35 | Performs vanilla stochastic gradient descent. 36 | 37 | config format: 38 | - learning_rate: Scalar learning rate. 39 | """ 40 | if config is None: config = {} 41 | config.setdefault('learning_rate', 1e-2) 42 | 43 | w -= config['learning_rate'] * dw 44 | return w, config 45 | 46 | 47 | def adam(x, dx, config=None): 48 | """ 49 | Uses the Adam update rule, which incorporates moving averages of both the 50 | gradient and its square and a bias correction term. 51 | 52 | config format: 53 | - learning_rate: Scalar learning rate. 54 | - beta1: Decay rate for moving average of first moment of gradient. 55 | - beta2: Decay rate for moving average of second moment of gradient. 56 | - epsilon: Small scalar used for smoothing to avoid dividing by zero. 57 | - m: Moving average of gradient. 58 | - v: Moving average of squared gradient. 59 | - t: Iteration number. 60 | """ 61 | if config is None: config = {} 62 | config.setdefault('learning_rate', 1e-3) 63 | config.setdefault('beta1', 0.9) 64 | config.setdefault('beta2', 0.999) 65 | config.setdefault('epsilon', 1e-8) 66 | config.setdefault('m', np.zeros_like(x)) 67 | config.setdefault('v', np.zeros_like(x)) 68 | config.setdefault('t', 0) 69 | 70 | next_x = None 71 | beta1, beta2, eps = config['beta1'], config['beta2'], config['epsilon'] 72 | t, m, v = config['t'], config['m'], config['v'] 73 | m = beta1 * m + (1 - beta1) * dx 74 | v = beta2 * v + (1 - beta2) * (dx * dx) 75 | t += 1 76 | alpha = config['learning_rate'] * np.sqrt(1 - beta2 ** t) / (1 - beta1 ** t) 77 | x -= alpha * (m / (np.sqrt(v) + eps)) 78 | config['t'] = t 79 | config['m'] = m 80 | config['v'] = v 81 | next_x = x 82 | 83 | return next_x, config 84 | 85 | 86 | -------------------------------------------------------------------------------- /assignment3/chris/cs231n/setup.py: -------------------------------------------------------------------------------- 1 | from distutils.core import setup 2 | from distutils.extension import Extension 3 | from Cython.Build import cythonize 4 | import numpy 5 | 6 | extensions = [ 7 | Extension('im2col_cython', ['im2col_cython.pyx'], 8 | include_dirs = [numpy.get_include()] 9 | ), 10 | ] 11 | 12 | setup( 13 | ext_modules = cythonize(extensions), 14 | ) 15 | -------------------------------------------------------------------------------- /assignment3/chris/frameworkpython: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # what real Python executable to use 4 | PYVER=2.7 5 | PATHTOPYTHON=/usr/local/bin/ 6 | PYTHON=${PATHTOPYTHON}python${PYVER} 7 | 8 | # find the root of the virtualenv, it should be the parent of the dir this script is in 9 | ENV=`$PYTHON -c "import os; print os.path.abspath(os.path.join(os.path.dirname(\"$0\"), '..'))"` 10 | 11 | # now run Python with the virtualenv set as Python's HOME 12 | export PYTHONHOME=$ENV 13 | exec $PYTHON "$@" 14 | -------------------------------------------------------------------------------- /assignment3/chris/kitten.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment3/chris/kitten.jpg -------------------------------------------------------------------------------- /assignment3/chris/requirements.txt: -------------------------------------------------------------------------------- 1 | Cython==0.23.4 2 | Jinja2==2.8 3 | MarkupSafe==0.23 4 | Pillow==3.0.0 5 | Pygments==2.0.2 6 | appnope==0.1.0 7 | argparse==1.2.1 8 | backports-abc==0.4 9 | backports.ssl-match-hostname==3.5.0.1 10 | certifi==2015.11.20.1 11 | cycler==0.9.0 12 | decorator==4.0.6 13 | functools32==3.2.3-2 14 | gnureadline==6.3.3 15 | ipykernel==4.2.2 16 | ipython==4.0.1 17 | ipython-genutils==0.1.0 18 | ipywidgets==4.1.1 19 | jsonschema==2.5.1 20 | jupyter==1.0.0 21 | jupyter-client==4.1.1 22 | jupyter-console==4.0.3 23 | jupyter-core==4.0.6 24 | matplotlib==1.5.0 25 | mistune==0.7.1 26 | nbconvert==4.1.0 27 | nbformat==4.0.1 28 | notebook==4.0.6 29 | numpy==1.10.4 30 | path.py==8.1.2 31 | pexpect==4.0.1 32 | pickleshare==0.5 33 | ptyprocess==0.5 34 | pyparsing==2.0.7 35 | python-dateutil==2.4.2 36 | pytz==2015.7 37 | pyzmq==15.1.0 38 | qtconsole==4.1.1 39 | scipy==0.16.1 40 | simplegeneric==0.8.1 41 | singledispatch==3.4.0.3 42 | six==1.10.0 43 | terminado==0.5 44 | tornado==4.3 45 | traitlets==4.0.0 46 | wsgiref==0.1.2 47 | -------------------------------------------------------------------------------- /assignment3/chris/sky.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment3/chris/sky.jpg -------------------------------------------------------------------------------- /assignment3/chris/start_ipython_osx.sh: -------------------------------------------------------------------------------- 1 | # Assume the virtualenv is called .env 2 | 3 | cp frameworkpython .env/bin 4 | .env/bin/frameworkpython -m IPython notebook 5 | -------------------------------------------------------------------------------- /assignment3/dafne/assignment3/.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | *.pyc 3 | .env/* 4 | -------------------------------------------------------------------------------- /assignment3/dafne/assignment3/collectSubmission.sh: -------------------------------------------------------------------------------- 1 | rm -f assignment3.zip 2 | zip -r assignment3.zip . -x "*.git" "*cs231n/datasets*" "*.ipynb_checkpoints*" "*README.md" "*collectSubmission.sh" "*requirements.txt" ".env/*" "*.pyc" "*cs231n/build/*" 3 | -------------------------------------------------------------------------------- /assignment3/dafne/assignment3/cs231n/.gitignore: -------------------------------------------------------------------------------- 1 | build/* 2 | im2col_cython.c 3 | im2col_cython.so 4 | -------------------------------------------------------------------------------- /assignment3/dafne/assignment3/cs231n/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment3/dafne/assignment3/cs231n/__init__.py -------------------------------------------------------------------------------- /assignment3/dafne/assignment3/cs231n/classifiers/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment3/dafne/assignment3/cs231n/classifiers/__init__.py -------------------------------------------------------------------------------- /assignment3/dafne/assignment3/cs231n/coco_utils.py: -------------------------------------------------------------------------------- 1 | import os, json 2 | import numpy as np 3 | import h5py 4 | 5 | 6 | def load_coco_data(base_dir='cs231n/datasets/coco_captioning', 7 | max_train=None, 8 | pca_features=True): 9 | data = {} 10 | caption_file = os.path.join(base_dir, 'coco2014_captions.h5') 11 | with h5py.File(caption_file, 'r') as f: 12 | for k, v in f.iteritems(): 13 | data[k] = np.asarray(v) 14 | 15 | if pca_features: 16 | train_feat_file = os.path.join(base_dir, 'train2014_vgg16_fc7_pca.h5') 17 | else: 18 | train_feat_file = os.path.join(base_dir, 'train2014_vgg16_fc7.h5') 19 | with h5py.File(train_feat_file, 'r') as f: 20 | data['train_features'] = np.asarray(f['features']) 21 | 22 | if pca_features: 23 | val_feat_file = os.path.join(base_dir, 'val2014_vgg16_fc7_pca.h5') 24 | else: 25 | val_feat_file = os.path.join(base_dir, 'val2014_vgg16_fc7.h5') 26 | with h5py.File(val_feat_file, 'r') as f: 27 | data['val_features'] = np.asarray(f['features']) 28 | 29 | dict_file = os.path.join(base_dir, 'coco2014_vocab.json') 30 | with open(dict_file, 'r') as f: 31 | dict_data = json.load(f) 32 | for k, v in dict_data.iteritems(): 33 | data[k] = v 34 | 35 | train_url_file = os.path.join(base_dir, 'train2014_urls.txt') 36 | with open(train_url_file, 'r') as f: 37 | train_urls = np.asarray([line.strip() for line in f]) 38 | data['train_urls'] = train_urls 39 | 40 | val_url_file = os.path.join(base_dir, 'val2014_urls.txt') 41 | with open(val_url_file, 'r') as f: 42 | val_urls = np.asarray([line.strip() for line in f]) 43 | data['val_urls'] = val_urls 44 | 45 | # Maybe subsample the training data 46 | if max_train is not None: 47 | num_train = data['train_captions'].shape[0] 48 | mask = np.random.randint(num_train, size=max_train) 49 | data['train_captions'] = data['train_captions'][mask] 50 | data['train_image_idxs'] = data['train_image_idxs'][mask] 51 | 52 | return data 53 | 54 | 55 | def decode_captions(captions, idx_to_word): 56 | singleton = False 57 | if captions.ndim == 1: 58 | singleton = True 59 | captions = captions[None] 60 | decoded = [] 61 | N, T = captions.shape 62 | for i in xrange(N): 63 | words = [] 64 | for t in xrange(T): 65 | word = idx_to_word[captions[i, t]] 66 | if word != '': 67 | words.append(word) 68 | if word == '': 69 | break 70 | decoded.append(' '.join(words)) 71 | if singleton: 72 | decoded = decoded[0] 73 | return decoded 74 | 75 | 76 | def sample_coco_minibatch(data, batch_size=100, split='train'): 77 | split_size = data['%s_captions' % split].shape[0] 78 | mask = np.random.choice(split_size, batch_size) 79 | captions = data['%s_captions' % split][mask] 80 | image_idxs = data['%s_image_idxs' % split][mask] 81 | image_features = data['%s_features' % split][image_idxs] 82 | urls = data['%s_urls' % split][image_idxs] 83 | return captions, image_features, urls 84 | 85 | -------------------------------------------------------------------------------- /assignment3/dafne/assignment3/cs231n/im2col.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def get_im2col_indices(x_shape, field_height, field_width, padding=1, stride=1): 5 | # First figure out what the size of the output should be 6 | N, C, H, W = x_shape 7 | assert (H + 2 * padding - field_height) % stride == 0 8 | assert (W + 2 * padding - field_height) % stride == 0 9 | out_height = (H + 2 * padding - field_height) / stride + 1 10 | out_width = (W + 2 * padding - field_width) / stride + 1 11 | 12 | i0 = np.repeat(np.arange(field_height), field_width) 13 | i0 = np.tile(i0, C) 14 | i1 = stride * np.repeat(np.arange(out_height), out_width) 15 | j0 = np.tile(np.arange(field_width), field_height * C) 16 | j1 = stride * np.tile(np.arange(out_width), out_height) 17 | i = i0.reshape(-1, 1) + i1.reshape(1, -1) 18 | j = j0.reshape(-1, 1) + j1.reshape(1, -1) 19 | 20 | k = np.repeat(np.arange(C), field_height * field_width).reshape(-1, 1) 21 | 22 | return (k, i, j) 23 | 24 | 25 | def im2col_indices(x, field_height, field_width, padding=1, stride=1): 26 | """ An implementation of im2col based on some fancy indexing """ 27 | # Zero-pad the input 28 | p = padding 29 | x_padded = np.pad(x, ((0, 0), (0, 0), (p, p), (p, p)), mode='constant') 30 | 31 | k, i, j = get_im2col_indices(x.shape, field_height, field_width, padding, 32 | stride) 33 | 34 | cols = x_padded[:, k, i, j] 35 | C = x.shape[1] 36 | cols = cols.transpose(1, 2, 0).reshape(field_height * field_width * C, -1) 37 | return cols 38 | 39 | 40 | def col2im_indices(cols, x_shape, field_height=3, field_width=3, padding=1, 41 | stride=1): 42 | """ An implementation of col2im based on fancy indexing and np.add.at """ 43 | N, C, H, W = x_shape 44 | H_padded, W_padded = H + 2 * padding, W + 2 * padding 45 | x_padded = np.zeros((N, C, H_padded, W_padded), dtype=cols.dtype) 46 | k, i, j = get_im2col_indices(x_shape, field_height, field_width, padding, 47 | stride) 48 | cols_reshaped = cols.reshape(C * field_height * field_width, -1, N) 49 | cols_reshaped = cols_reshaped.transpose(2, 0, 1) 50 | np.add.at(x_padded, (slice(None), k, i, j), cols_reshaped) 51 | if padding == 0: 52 | return x_padded 53 | return x_padded[:, :, padding:-padding, padding:-padding] 54 | 55 | pass 56 | -------------------------------------------------------------------------------- /assignment3/dafne/assignment3/cs231n/image_utils.py: -------------------------------------------------------------------------------- 1 | import urllib2, os, tempfile 2 | 3 | import numpy as np 4 | from scipy.misc import imread 5 | 6 | from cs231n.fast_layers import conv_forward_fast 7 | 8 | 9 | """ 10 | Utility functions used for viewing and processing images. 11 | """ 12 | 13 | 14 | def blur_image(X): 15 | """ 16 | A very gentle image blurring operation, to be used as a regularizer for image 17 | generation. 18 | 19 | Inputs: 20 | - X: Image data of shape (N, 3, H, W) 21 | 22 | Returns: 23 | - X_blur: Blurred version of X, of shape (N, 3, H, W) 24 | """ 25 | w_blur = np.zeros((3, 3, 3, 3)) 26 | b_blur = np.zeros(3) 27 | blur_param = {'stride': 1, 'pad': 1} 28 | for i in xrange(3): 29 | w_blur[i, i] = np.asarray([[1, 2, 1], [2, 188, 2], [1, 2, 1]], dtype=np.float32) 30 | w_blur /= 200.0 31 | return conv_forward_fast(X, w_blur, b_blur, blur_param)[0] 32 | 33 | 34 | def preprocess_image(img, mean_img, mean='image'): 35 | """ 36 | Convert to float, transepose, and subtract mean pixel 37 | 38 | Input: 39 | - img: (H, W, 3) 40 | 41 | Returns: 42 | - (1, 3, H, 3) 43 | """ 44 | if mean == 'image': 45 | mean = mean_img 46 | elif mean == 'pixel': 47 | mean = mean_img.mean(axis=(1, 2), keepdims=True) 48 | elif mean == 'none': 49 | mean = 0 50 | else: 51 | raise ValueError('mean must be image or pixel or none') 52 | return img.astype(np.float32).transpose(2, 0, 1)[None] - mean 53 | 54 | 55 | def deprocess_image(img, mean_img, mean='image', renorm=False): 56 | """ 57 | Add mean pixel, transpose, and convert to uint8 58 | 59 | Input: 60 | - (1, 3, H, W) or (3, H, W) 61 | 62 | Returns: 63 | - (H, W, 3) 64 | """ 65 | if mean == 'image': 66 | mean = mean_img 67 | elif mean == 'pixel': 68 | mean = mean_img.mean(axis=(1, 2), keepdims=True) 69 | elif mean == 'none': 70 | mean = 0 71 | else: 72 | raise ValueError('mean must be image or pixel or none') 73 | if img.ndim == 3: 74 | img = img[None] 75 | img = (img + mean)[0].transpose(1, 2, 0) 76 | if renorm: 77 | low, high = img.min(), img.max() 78 | img = 255.0 * (img - low) / (high - low) 79 | return img.astype(np.uint8) 80 | 81 | 82 | def image_from_url(url): 83 | """ 84 | Read an image from a URL. Returns a numpy array with the pixel data. 85 | We write the image to a temporary file then read it back. Kinda gross. 86 | """ 87 | try: 88 | f = urllib2.urlopen(url) 89 | _, fname = tempfile.mkstemp() 90 | with open(fname, 'wb') as ff: 91 | ff.write(f.read()) 92 | img = imread(fname) 93 | os.remove(fname) 94 | return img 95 | except urllib2.URLError as e: 96 | print 'URL Error: ', e.reason, url 97 | except urllib2.HTTPError as e: 98 | print 'HTTP Error: ', e.code, url 99 | -------------------------------------------------------------------------------- /assignment3/dafne/assignment3/cs231n/optim.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | """ 4 | This file implements various first-order update rules that are commonly used for 5 | training neural networks. Each update rule accepts current weights and the 6 | gradient of the loss with respect to those weights and produces the next set of 7 | weights. Each update rule has the same interface: 8 | 9 | def update(w, dw, config=None): 10 | 11 | Inputs: 12 | - w: A numpy array giving the current weights. 13 | - dw: A numpy array of the same shape as w giving the gradient of the 14 | loss with respect to w. 15 | - config: A dictionary containing hyperparameter values such as learning rate, 16 | momentum, etc. If the update rule requires caching values over many 17 | iterations, then config will also hold these cached values. 18 | 19 | Returns: 20 | - next_w: The next point after the update. 21 | - config: The config dictionary to be passed to the next iteration of the 22 | update rule. 23 | 24 | NOTE: For most update rules, the default learning rate will probably not perform 25 | well; however the default values of the other hyperparameters should work well 26 | for a variety of different problems. 27 | 28 | For efficiency, update rules may perform in-place updates, mutating w and 29 | setting next_w equal to w. 30 | """ 31 | 32 | 33 | def sgd(w, dw, config=None): 34 | """ 35 | Performs vanilla stochastic gradient descent. 36 | 37 | config format: 38 | - learning_rate: Scalar learning rate. 39 | """ 40 | if config is None: config = {} 41 | config.setdefault('learning_rate', 1e-2) 42 | 43 | w -= config['learning_rate'] * dw 44 | return w, config 45 | 46 | 47 | def adam(x, dx, config=None): 48 | """ 49 | Uses the Adam update rule, which incorporates moving averages of both the 50 | gradient and its square and a bias correction term. 51 | 52 | config format: 53 | - learning_rate: Scalar learning rate. 54 | - beta1: Decay rate for moving average of first moment of gradient. 55 | - beta2: Decay rate for moving average of second moment of gradient. 56 | - epsilon: Small scalar used for smoothing to avoid dividing by zero. 57 | - m: Moving average of gradient. 58 | - v: Moving average of squared gradient. 59 | - t: Iteration number. 60 | """ 61 | if config is None: config = {} 62 | config.setdefault('learning_rate', 1e-3) 63 | config.setdefault('beta1', 0.9) 64 | config.setdefault('beta2', 0.999) 65 | config.setdefault('epsilon', 1e-8) 66 | config.setdefault('m', np.zeros_like(x)) 67 | config.setdefault('v', np.zeros_like(x)) 68 | config.setdefault('t', 0) 69 | 70 | next_x = None 71 | beta1, beta2, eps = config['beta1'], config['beta2'], config['epsilon'] 72 | t, m, v = config['t'], config['m'], config['v'] 73 | m = beta1 * m + (1 - beta1) * dx 74 | v = beta2 * v + (1 - beta2) * (dx * dx) 75 | t += 1 76 | alpha = config['learning_rate'] * np.sqrt(1 - beta2 ** t) / (1 - beta1 ** t) 77 | x -= alpha * (m / (np.sqrt(v) + eps)) 78 | config['t'] = t 79 | config['m'] = m 80 | config['v'] = v 81 | next_x = x 82 | 83 | return next_x, config 84 | 85 | 86 | -------------------------------------------------------------------------------- /assignment3/dafne/assignment3/cs231n/setup.py: -------------------------------------------------------------------------------- 1 | from distutils.core import setup 2 | from distutils.extension import Extension 3 | from Cython.Build import cythonize 4 | import numpy 5 | 6 | extensions = [ 7 | Extension('im2col_cython', ['im2col_cython.pyx'], 8 | include_dirs = [numpy.get_include()] 9 | ), 10 | ] 11 | 12 | setup( 13 | ext_modules = cythonize(extensions), 14 | ) 15 | -------------------------------------------------------------------------------- /assignment3/dafne/assignment3/frameworkpython: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # what real Python executable to use 4 | PYVER=2.7 5 | PATHTOPYTHON=/usr/local/bin/ 6 | PYTHON=${PATHTOPYTHON}python${PYVER} 7 | 8 | # find the root of the virtualenv, it should be the parent of the dir this script is in 9 | ENV=`$PYTHON -c "import os; print os.path.abspath(os.path.join(os.path.dirname(\"$0\"), '..'))"` 10 | 11 | # now run Python with the virtualenv set as Python's HOME 12 | export PYTHONHOME=$ENV 13 | exec $PYTHON "$@" 14 | -------------------------------------------------------------------------------- /assignment3/dafne/assignment3/kitten.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment3/dafne/assignment3/kitten.jpg -------------------------------------------------------------------------------- /assignment3/dafne/assignment3/requirements.txt: -------------------------------------------------------------------------------- 1 | Cython==0.23.4 2 | Jinja2==2.8 3 | MarkupSafe==0.23 4 | Pillow==3.0.0 5 | Pygments==2.0.2 6 | appnope==0.1.0 7 | argparse==1.2.1 8 | backports-abc==0.4 9 | backports.ssl-match-hostname==3.5.0.1 10 | certifi==2015.11.20.1 11 | cycler==0.9.0 12 | decorator==4.0.6 13 | functools32==3.2.3-2 14 | gnureadline==6.3.3 15 | ipykernel==4.2.2 16 | ipython==4.0.1 17 | ipython-genutils==0.1.0 18 | ipywidgets==4.1.1 19 | jsonschema==2.5.1 20 | jupyter==1.0.0 21 | jupyter-client==4.1.1 22 | jupyter-console==4.0.3 23 | jupyter-core==4.0.6 24 | matplotlib==1.5.0 25 | mistune==0.7.1 26 | nbconvert==4.1.0 27 | nbformat==4.0.1 28 | notebook==4.0.6 29 | numpy==1.10.4 30 | path.py==8.1.2 31 | pexpect==4.0.1 32 | pickleshare==0.5 33 | ptyprocess==0.5 34 | pyparsing==2.0.7 35 | python-dateutil==2.4.2 36 | pytz==2015.7 37 | pyzmq==15.1.0 38 | qtconsole==4.1.1 39 | scipy==0.16.1 40 | simplegeneric==0.8.1 41 | singledispatch==3.4.0.3 42 | six==1.10.0 43 | terminado==0.5 44 | tornado==4.3 45 | traitlets==4.0.0 46 | wsgiref==0.1.2 47 | -------------------------------------------------------------------------------- /assignment3/dafne/assignment3/sky.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/assignment3/dafne/assignment3/sky.jpg -------------------------------------------------------------------------------- /assignment3/dafne/assignment3/start_ipython_osx.sh: -------------------------------------------------------------------------------- 1 | # Assume the virtualenv is called .env 2 | 3 | cp frameworkpython .env/bin 4 | .env/bin/frameworkpython -m IPython notebook 5 | -------------------------------------------------------------------------------- /eeggroup/files.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NLeSC/deep-learning-assignments-solutions/9a8f4612da0be6123cd1660254233f2cda6e1c6d/eeggroup/files.zip --------------------------------------------------------------------------------