#! /bin/zsh

test -e train-images-idx3-ubyte.gz || {
  echo "ERROR: you need to download train-image-idx3-ubyte.gz" 1>&2
  echo "ERROR: from http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz" 1>&2
  exit 1
}

test -e train-labels-idx1-ubyte.gz || {
  echo "ERROR: you need to download train-labels-idx1-ubyte.gz" 1>&2
  echo "ERROR: from http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz" 1>&2
  exit 1
}

SHUFFLE='BEGIN { srand 69; };
         $i = int rand 1000;
         print $b[$i] if $b[$i];
         $b[$i] = $_; } { print grep { defined $_ } @b;'

paste -d' '                                                             \
  <(gunzip -c train-labels-idx1-ubyte.gz | ./extract-labels)            \
  <(gunzip -c train-images-idx3-ubyte.gz | ./extractfeatures) |         \
perl -ne ${SHUFFLE} |                                                   \
time ../../vowpalwabbit/vw --oaa 10 -f mnist.model                      \
   -b 24 --adaptive --invariant --holdout_off                           \
   -l 0.1 --nn 40 --passes 24 -k --compressed --cache_file mnist.cache && \
rm -f mnist.cache
