Install the bleeding edge version from here: http://lasagne.readthedocs.org/en/latest/user/installation.html
In [ ]:
import numpy as np
from cifar import load_cifar10
X_train,y_train,X_val,y_val,X_test,y_test = load_cifar10("cifar_data")
class_names = np.array(['airplane','automobile ','bird ','cat ','deer ','dog ','frog ','horse ','ship ','truck'])
print X_train.shape,y_train.shape
In [ ]:
import matplotlib.pyplot as plt
%matplotlib inline
plt.figure(figsize=[12,10])
for i in range(12):
plt.subplot(3,4,i+1)
plt.xlabel(class_names[y_train[i]])
plt.imshow(np.transpose(X_train[i],[1,2,0]))
In [ ]:
import lasagne
import theano
import theano.tensor as T
input_X = T.tensor4("X")
#input dimention (None means "Arbitrary")
input_shape = [None,3,32,32]
target_y = T.vector("target Y integer",dtype='int32')
Defining network architecture
In [ ]:
#Input layer (auxilary)
input_layer = lasagne.layers.InputLayer(shape = input_shape,input_var=input_X)
#fully connected layer, that takes input layer and applies 50 neurons to it.
# nonlinearity here is sigmoid as in logistic regression
# you can give a name to each layer (optional)
dense_1 = lasagne.layers.DenseLayer(input_layer,num_units=100,
nonlinearity = lasagne.nonlinearities.sigmoid,
name = "hidden_dense_layer")
#fully connected output layer that takes dense_1 as input and has 10 neurons (1 for each digit)
#We use softmax nonlinearity to make probabilities add up to 1
dense_output = lasagne.layers.DenseLayer(dense_1,num_units = 10,
nonlinearity = lasagne.nonlinearities.softmax,
name='output')
In [ ]:
#network prediction (theano-transformation)
y_predicted = lasagne.layers.get_output(dense_output)
In [ ]:
#all network weights (shared variables)
all_weights = lasagne.layers.get_all_params(dense_output,trainable=True)
print all_weights
In [ ]:
#Mean categorical crossentropy as a loss function - similar to logistic loss but for multiclass targets
loss = lasagne.objectives.categorical_crossentropy(y_predicted,target_y).mean()
#prediction accuracy (WITH dropout)
accuracy = lasagne.objectives.categorical_accuracy(y_predicted,target_y).mean()
#This function computes gradient AND composes weight updates just like you did earlier
updates_sgd = lasagne.updates.sgd(loss, all_weights,learning_rate=0.01)
In [ ]:
#function that computes loss and updates weights
train_fun = theano.function([input_X,target_y],[loss,accuracy],updates= updates_sgd)
In [ ]:
#deterministic prediciton (without dropout)
y_predicted_det = lasagne.layers.get_output(dense_output,deterministic=True)
#prediction accuracy (without dropout)
accuracy_det = lasagne.objectives.categorical_accuracy(y_predicted_det,target_y).mean()
#function that just computes accuracy without dropout/noize -- for evaluation purposes
accuracy_fun = theano.function([input_X,target_y],accuracy_det)
In [ ]:
# An auxilary function that returns mini-batches for neural network training
#Parameters
# X - a tensor of images with shape (many, 3, 32, 32), e.g. X_train
# y - a vector of answers for corresponding images e.g. Y_train
#batch_size - a single number - the intended size of each batches
#What do need to implement
# 1) Shuffle data
# - Gotta shuffle X and y the same way not to break the correspondence between X_i and y_i
# 3) Split data into minibatches of batch_size
# - If data size is not a multiple of batch_size, make one last batch smaller.
# 4) return a list (or an iterator) of pairs
# - (подгруппа картинок, ответы из y на эту подгруппу)
def iterate_minibatches(X, y, batchsize):
<return an iterable of (X_batch, y_batch) batches of images and answers for them>
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
# You feel lost and wish you stayed home tonight?
# Go search for a similar function at
# https://github.com/Lasagne/Lasagne/blob/master/examples/mnist.py
In [ ]:
import time
num_epochs = 100 #amount of passes through the data
batch_size = 50 #number of samples processed at each function call
for epoch in range(num_epochs):
# In each epoch, we do a full pass over the training data:
train_err = 0
train_acc = 0
train_batches = 0
start_time = time.time()
for batch in iterate_minibatches(X_train, y_train,batch_size):
inputs, targets = batch
train_err_batch, train_acc_batch= train_fun(inputs, targets)
train_err += train_err_batch
train_acc += train_acc_batch
train_batches += 1
# And a full pass over the validation data:
val_acc = 0
val_batches = 0
for batch in iterate_minibatches(X_val, y_val, batch_size):
inputs, targets = batch
val_acc += accuracy_fun(inputs, targets)
val_batches += 1
# Then we print the results for this epoch:
print("Epoch {} of {} took {:.3f}s".format(
epoch + 1, num_epochs, time.time() - start_time))
print(" training loss (in-iteration):\t\t{:.6f}".format(train_err / train_batches))
print(" train accuracy:\t\t{:.2f} %".format(
train_acc / train_batches * 100))
print(" validation accuracy:\t\t{:.2f} %".format(
val_acc / val_batches * 100))
In [ ]:
test_acc = 0
test_batches = 0
for batch in iterate_minibatches(X_test, y_test, 500):
inputs, targets = batch
acc = accuracy_fun(inputs, targets)
test_acc += acc
test_batches += 1
print("Final results:")
print(" test accuracy:\t\t{:.2f} %".format(
test_acc / test_batches * 100))
if test_acc / test_batches * 100 > 95:
print "Double-check, than consider applying for NIPS'17. SRSly."
elif test_acc / test_batches * 100 > 90:
print "U'r freakin' amazin'!"
elif test_acc / test_batches * 100 > 80:
print "Achievement unlocked: 110lvl Warlock!"
elif test_acc / test_batches * 100 > 70:
print "Achievement unlocked: 80lvl Warlock!"
elif test_acc / test_batches * 100 > 50:
print "Achievement unlocked: 60lvl Warlock!"
else:
print "We need more magic!"
Let's create a mini-convolutional network with roughly such architecture:
Train it with Adam optimizer with default params.
Re-train the network with the same optimizer
(please read it at least diagonally)
Common ways to get bonus points are:
Network size
MOAR layers, (lasagne docs)
Nonlinearities in the hidden layers
Larger networks may take more epochs to train, so don't discard your net just because it could didn't beat the baseline in 5 epochs.
Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn!
Convolution layers
network = lasagne.layers.Conv2DLayer(prev_layer,
num_filters = n_neurons,
filter_size = (filter width, filter height),
nonlinearity = some_nonlinearity)
Warning! Training convolutional networks can take long without GPU. That's okay.
Plenty other layers and architectures
Early Stopping
lasagne.layers.DropoutLayer(prev_layer, p=probability_to_zero_out)
from scipy.misc import imrotate,imresize
There is a template for your solution below that you can opt to use or throw away and write it your way
In [ ]:
import numpy as np
from cifar import load_cifar10
X_train,y_train,X_val,y_val,X_test,y_test = load_cifar10("cifar_data")
class_names = np.array(['airplane','automobile ','bird ','cat ','deer ','dog ','frog ','horse ','ship ','truck'])
print X_train.shape,y_train.shape
In [ ]:
import lasagne
input_X = T.tensor4("X")
#input dimention (None means "Arbitrary" and only works at the first axes [samples])
input_shape = [None,3,32,32]
target_y = T.vector("target Y integer",dtype='int32')
In [ ]:
#Input layer (auxilary)
input_layer = lasagne.layers.InputLayer(shape = input_shape,input_var=input_X)
<student.code_neural_network_architecture()>
dense_output = <your network output>
In [ ]:
# Network predictions (theano-transformation)
y_predicted = lasagne.layers.get_output(dense_output)
In [ ]:
#All weights (shared-varaibles)
# "trainable" flag means not to return auxilary params like batch mean (for batch normalization)
all_weights = lasagne.layers.get_all_params(dense_output,trainable=True)
print all_weights
In [ ]:
#loss function
loss = <loss function>
#<optionally add regularization>
#accuracy with dropout/noize
accuracy = lasagne.objectives.categorical_accuracy(y_predicted,target_y).mean()
#weight updates
updates = <try different update methods>
In [ ]:
#A function that accepts X and y, returns loss functions and performs weight updates
train_fun = theano.function([input_X,target_y],[loss,accuracy],updates= updates_sgd)
In [ ]:
#deterministic prediciton (without dropout)
y_predicted_det = lasagne.layers.get_output(dense_output)
#prediction accuracy (without dropout)
accuracy_det = lasagne.objectives.categorical_accuracy(y_predicted_det,target_y).mean()
#function that just computes accuracy without dropout/noize -- for evaluation purposes
accuracy_fun = theano.function([input_X,target_y],accuracy_det)
In [ ]:
#итерации обучения
num_epochs = <how many times to iterate over the entire training set>
batch_size = <how many samples are processed at a single function call>
for epoch in range(num_epochs):
# In each epoch, we do a full pass over the training data:
train_err = 0
train_acc = 0
train_batches = 0
start_time = time.time()
for batch in iterate_minibatches(X_train, y_train,batch_size):
inputs, targets = batch
train_err_batch, train_acc_batch= train_fun(inputs, targets)
train_err += train_err_batch
train_acc += train_acc_batch
train_batches += 1
# And a full pass over the validation data:
val_acc = 0
val_batches = 0
for batch in iterate_minibatches(X_val, y_val, batch_size):
inputs, targets = batch
val_acc += accuracy_fun(inputs, targets)
val_batches += 1
# Then we print the results for this epoch:
print("Epoch {} of {} took {:.3f}s".format(
epoch + 1, num_epochs, time.time() - start_time))
print(" training loss (in-iteration):\t\t{:.6f}".format(train_err / train_batches))
print(" train accuracy:\t\t{:.2f} %".format(
train_acc / train_batches * 100))
print(" validation accuracy:\t\t{:.2f} %".format(
val_acc / val_batches * 100))
In [ ]:
test_acc = 0
test_batches = 0
for batch in iterate_minibatches(X_test, y_test, 500):
inputs, targets = batch
acc = accuracy_fun(inputs, targets)
test_acc += acc
test_batches += 1
print("Final results:")
print(" test accuracy:\t\t{:.2f} %".format(
test_acc / test_batches * 100))
if test_acc / test_batches * 100 > 80:
print "Achievement unlocked: 80lvl Warlock!"
else:
print "We need more magic!"
Report
All creative approaches are highly welcome, but at the very least it would be great to mention
There is no need to write strict mathematical proofs (unless you want to).
___ ___
, and here's my storyA long ago in a galaxy far far away, when it was still more than an hour before deadline, i got an idea:
How could i be so naive?!
This thing has finally converged and
That, having wasted __ [minutes, hours or days] of my life training, got
[an optional afterword and mortal curses on assignment authors]
In [ ]: