MNIST classification with Vowpal Wabbit

Neural Net


In [33]:
from __future__ import division
import re
import numpy as np
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt

%matplotlib inline

In [34]:
#%qtconsole

Train

I found some help with parameters here:

--cache_file train.cache
converts train_ALL.vw to a binary file for future faster processing. Next time we go through the model building, we will use the cache file and not the text file.

--passes
is the number of passes

--oaa 10
refers to oaa learning algorithm with 10 classes (1 to 10)

-q ii
creates interaction between variables in the two referred to namespaces which here are the same i.e. 'image' Namespace.
An interaction variable is created from two variables 'A' and 'B' by multiplying the values of 'A' and 'B'.

-f mnist_ALL.model
refers to file where model will be saved.

-b
refers to number of bits in the feature table.
Default number is 18 but as we have increased the number of features much more by introducing interaction features, value of '-b' has been increased to 22.

-l rate
Adjust the learning rate. Defaults to 0.5

--power_t p
This specifies the power on the learning rate decay. You can adjust this --power_t p where p is in the range [0,1]. 0 means the learning rate does not decay, which can be helpful when state tracking, while 1 is very aggressive. Defaults to 0.5


In [35]:
!rm train.vw.cache

In [36]:
!rm mnist_train_nn.model

In [37]:
!vw -d data/mnist_train.vw --cache_file train.vw.cache -f mnist_train_nn.model --nn 200 -b 19  --oaa 10  --passes 55 -l 0.4  --early_terminate 3 --power_t 0.6


final_regressor = mnist_train_nn.model
Num weight bits = 19
learning rate = 0.4
initial_t = 0
power_t = 0.6
decay_learning_rate = 1
can't open: train.vw.cache, error = No such file or directory
creating cache_file = train.vw.cache
Reading datafile = data/mnist_train.vw
num sources = 1
average  since         example        example  current  current  current
loss     last          counter         weight    label  predict features
1.000000 1.000000            1            1.0        6        1      167
1.000000 1.000000            2            2.0        1        6      177
1.000000 1.000000            4            4.0        2        5       97
1.000000 1.000000            8            8.0        4        2      201
1.000000 1.000000           16           16.0        3        4      151
0.906250 0.812500           32           32.0        1       10      184
0.859375 0.812500           64           64.0        2        1      123
0.656250 0.453125          128          128.0        8        8      131
0.554688 0.453125          256          256.0        1        1      235
0.453125 0.351562          512          512.0        8        8      116
0.347656 0.242188         1024         1024.0        7        6      172
0.260742 0.173828         2048         2048.0       10       10      136
0.199707 0.138672         4096         4096.0        3        3      136
0.168701 0.137695         8192         8192.0        1        1      169
0.143066 0.117432        16384        16384.0        5        5      145
0.116394 0.089722        32768        32768.0        6        6      150
0.091492 0.066589        65536        65536.0        6        6      177
0.049234 0.049234       131072       131072.0       10       10      153 h
0.038452 0.027671       262144       262144.0        5        5      112 h
0.029217 0.019981       524288       524288.0        7        7      225 h
0.023106 0.016995      1048576      1048576.0       10       10      150 h
0.018411 0.013716      2097152      2097152.0        3        3      146 h

finished run
number of examples per pass = 108000
passes used = 23
weighted example sum = 2484000.000000
weighted label sum = 0.000000
average loss = 0.012083 h
total feature number = 401616984

Predict

-t
is for test file

-i
specifies the model file created earlier

-p
where to store the class predictions [1,10]


In [38]:
!rm predict.txt

In [39]:
!vw -t data/mnist_test.vw -i mnist_train_nn.model  -p predict.txt


only testing
predictions = predict.txt
Num weight bits = 19
learning rate = 10
initial_t = 1
power_t = 0.5
using no cache
Reading datafile = data/mnist_test.vw
num sources = 1
average  since         example        example  current  current  current
loss     last          counter         weight    label  predict features
0.000000 0.000000            1            1.0        8        8      117
0.000000 0.000000            2            2.0        3        3      166
0.000000 0.000000            4            4.0        1        1      194
0.000000 0.000000            8            8.0       10       10      130
0.000000 0.000000           16           16.0        6        6      138
0.000000 0.000000           32           32.0        2        2       62
0.000000 0.000000           64           64.0        4        4      138
0.000000 0.000000          128          128.0        6        6      160
0.011719 0.023438          256          256.0        8        8      104
0.013672 0.015625          512          512.0        5        5      100
0.018555 0.023438         1024         1024.0        5        5      156
0.027832 0.037109         2048         2048.0        7        7      193
0.031738 0.035645         4096         4096.0       10       10      157
0.022461 0.013184         8192         8192.0        7        7      172
0.019897 0.017334        16384        16384.0        1        1      207

finished run
number of examples per pass = 20000
passes used = 1
weighted example sum = 20000.000000
weighted label sum = 0.000000
average loss = 0.017650
total feature number = 3273154

Analyze


In [40]:
y_true=[]
with open("data/mnist_test.vw", 'rb') as f:
    for line in f:
        m = re.search('^\d+', line)
        if m:
            found = m.group()
        y_true.append(int(found))


y_pred = []
with open("predict.txt", 'rb') as f:
    for line in f:
        m = re.search('^\d+', line)
        if m:
            found = m.group()
        y_pred.append(int(found))

target_names     = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"] # NOTE: plus one

In [41]:
def plot_confusion_matrix(cm, 
                          target_names,
                          title='Proportional Confusion matrix: VW on 784 pixels', 
                          cmap=plt.cm.Paired):  
    """
    given a confusion matrix (cm), make a nice plot
    see the skikit-learn documentation for the original done for the iris dataset
    """
    plt.figure(figsize=(8, 6))
    plt.imshow((cm/cm.sum(axis=1)), interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(target_names))
    plt.xticks(tick_marks, target_names, rotation=45)
    plt.yticks(tick_marks, target_names)
    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')
    
cm = confusion_matrix(y_true, y_pred)  

print(cm)
model_accuracy = sum(cm.diagonal())/len(y_pred)
model_misclass = 1 - model_accuracy
print("\nModel accuracy: {0}, model misclass rate: {1}".format(model_accuracy, model_misclass))

plot_confusion_matrix(cm, target_names)


[[1948    1    1    0    0    2    4    3    1    0]
 [   0 2253    4    2    0    3    3    2    3    0]
 [   8    2 2030    5    3    0    1    6    9    0]
 [   4    0    7 1983    1    9    0    7    3    6]
 [   1    0    4    4 1932    0    6    1    4   12]
 [   9    0    0    9    1 1759    5    0    1    0]
 [  11    5    0    1    6   20 1866    0    7    0]
 [   8    3   21    1    1    1    0 2009    2   10]
 [   9    1    7    7    6    5    1    4 1907    1]
 [   8    8    2    4   20    6    1    7    2 1960]]

Model accuracy: 0.98235, model misclass rate: 0.01765

In [ ]: