In [1]:
 
import numpy as np
import math
import scipy.io as sio
import os
%matplotlib inline
import matplotlib.pylab as pylab
import matplotlib.pyplot as plt
import mpld3
mpld3.enable_notebook()
pylab.rcParams['figure.figsize'] = 12, 8
import sys
sys.path.append('/Users/pi/Downloads/HintonCoursera/Assignment Answers -- Sinclair (Python)')
#os.setcwd('/Users/pi/Downloads/HintonCoursera/Assignment Answers -- Sinclair (Python)')
#courseraneuralnet.bla
from utility.utils import loadmat, logistic
from assignment3 import A3Run
/Users/pi/anaconda/lib/python3.5/site-packages/scipy/_lib/decorator.py:205: DeprecationWarning: inspect.getargspec() is deprecated, use inspect.signature() instead
  first = inspect.getargspec(caller)[0][0]  # first arg
In [2]:
 
n_hid = 7
NUM_INPUT_UNITS = 256
NUM_CLASSES = 10
In [3]:
 
a3 = A3Run()
In [4]:
 
# lets look at our data
print( a3.data_sets.keys() )
v = a3.data_sets['validation']
print( v.keys() )
print( v['targets'].shape )
print( v['inputs'].shape )
dict_keys(['training', 'test', 'validation'])
dict_keys(['targets', 'inputs'])
(10, 1000)
(256, 1000)
In [5]:
 
w = v['inputs'][:, 33]
print( type(w) )
print( w.shape )
g = w.reshape(16,16)
from matplotlib import pyplot as plt
plt.imshow(g, interpolation='nearest', cmap='viridis')
plt.show()
print( v['targets'][:,33] )
<class 'numpy.ndarray'>
(256,)
0246810121402468101214
[ 0.  0.  0.  1.  0.  0.  0.  0.  0.  0.]
In [6]:
 
# http://stackoverflow.com/questions/3942549/display-numpy-array-as-continuously-updating-image-with-glumpy
# http://stackoverflow.com/questions/3886281/display-array-as-raster-image-in-python
import glumpy
window = glumpy.Window(512, 512)
im = glumpy.Image(z.astype(np.float32), cmap=glumpy.colormap.Grey)
@window.event
def on_draw():
    im.blit(0, 0, window.width, window.height)
window.mainloop()
usage: __main__.py [-h]
                   [--backend {glfw,sdl2,qt5,pyside,pyglet,sdl,freeglut,osxglut}]
                   [--record] [--interactive] [--framerate FRAMERATE]
                   [--display-fps] [--debug] [--size SIZE]
                   [--position POSITION] [--single-buffer] [--stereo]
                   [--vsync VSYNC] [--srgb] [--depth-size DEPTH_SIZE]
                   [--stencil-size STENCIL_SIZE] [--gl-api {GL,ES}]
                   [--gl-profile {none,core,compatibility}]
                   [--gl-version GL_VERSION]
__main__.py: error: argument --framerate/-f: invalid int value: '/Users/pi/Library/Jupyter/runtime/kernel-166ad1a3-d108-4ccc-97cc-56d7e264edbe.json'
An exception has occurred, use %tb to see the full traceback.

SystemExit: 2


In [7]:
 
%tb
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/Users/pi/anaconda/lib/python3.5/argparse.py in _get_value(self, action, arg_string)
   2285         try:
-> 2286             result = type_func(arg_string)
   2287 

ValueError: invalid literal for int() with base 10: '/Users/pi/Library/Jupyter/runtime/kernel-166ad1a3-d108-4ccc-97cc-56d7e264edbe.json'

During handling of the above exception, another exception occurred:

ArgumentError                             Traceback (most recent call last)
/Users/pi/anaconda/lib/python3.5/argparse.py in parse_known_args(self, args, namespace)
   1757         try:
-> 1758             namespace, args = self._parse_known_args(args, namespace)
   1759             if hasattr(namespace, _UNRECOGNIZED_ARGS_ATTR):

/Users/pi/anaconda/lib/python3.5/argparse.py in _parse_known_args(self, arg_strings, namespace)
   1963             # consume the next optional and any arguments for it
-> 1964             start_index = consume_optional(start_index)
   1965 

/Users/pi/anaconda/lib/python3.5/argparse.py in consume_optional(start_index)
   1903             for action, args, option_string in action_tuples:
-> 1904                 take_action(action, args, option_string)
   1905             return stop

/Users/pi/anaconda/lib/python3.5/argparse.py in take_action(action, argument_strings, option_string)
   1815             seen_actions.add(action)
-> 1816             argument_values = self._get_values(action, argument_strings)
   1817 

/Users/pi/anaconda/lib/python3.5/argparse.py in _get_values(self, action, arg_strings)
   2256             arg_string, = arg_strings
-> 2257             value = self._get_value(action, arg_string)
   2258             self._check_value(action, value)

/Users/pi/anaconda/lib/python3.5/argparse.py in _get_value(self, action, arg_string)
   2298             msg = _('invalid %(type)s value: %(value)r')
-> 2299             raise ArgumentError(action, msg % args)
   2300 

ArgumentError: argument --framerate/-f: invalid int value: '/Users/pi/Library/Jupyter/runtime/kernel-166ad1a3-d108-4ccc-97cc-56d7e264edbe.json'

During handling of the above exception, another exception occurred:

SystemExit                                Traceback (most recent call last)
<ipython-input-6-6000262847e6> in <module>()
      3 import glumpy
      4 
----> 5 window = glumpy.Window(512, 512)
      6 im = glumpy.Image(z.astype(np.float32), cmap=glumpy.colormap.Grey)
      7 

/Users/pi/anaconda/lib/python3.5/site-packages/glumpy/app/__init__.py in __new__(cls, *args, **kwargs)
    140 
    141         all = list(backends.__backends__)
--> 142         options = parser.get_options()
    143 
    144         # No backend was specified

/Users/pi/anaconda/lib/python3.5/site-packages/glumpy/app/parser.py in get_options()
     29     """ Parse and retrun command line options. """
     30 
---> 31     options, unknown = get_default().parse_known_args()
     32     return options
     33 

/Users/pi/anaconda/lib/python3.5/argparse.py in parse_known_args(self, args, namespace)
   1763         except ArgumentError:
   1764             err = _sys.exc_info()[1]
-> 1765             self.error(str(err))
   1766 
   1767     def _parse_known_args(self, arg_strings, namespace):

/Users/pi/anaconda/lib/python3.5/argparse.py in error(self, message)
   2383         self.print_usage(_sys.stderr)
   2384         args = {'prog': self.prog, 'message': message}
-> 2385         self.exit(2, _('%(prog)s: error: %(message)s\n') % args)

/Users/pi/anaconda/lib/python3.5/argparse.py in exit(self, status, message)
   2370         if message:
   2371             self._print_message(message, _sys.stderr)
-> 2372         _sys.exit(status)
   2373 
   2374     def error(self, message):

SystemExit: 2

In [ ]:
 
#Q1
a3.a3_main(0, n_hid=0, n_iterations=0, lr_net=0, train_momentum=0, early_stopping=False, mini_batch_size=0)
In [ ]:
 
#Q2
a3.a3_main(wd_coeff=0, n_hid=10, n_iterations=70, lr_net=0.005, train_momentum=0.0, early_stopping=False, 
           mini_batch_size=4)
In [ ]:
 
# Question 3-4
learning_rates = [0.002, 0.01, 0.05, 0.2, 1.0, 5.0, 20.0]
momentums = [0.0, 0.9]
for momentum in momentums:
    for learning_rate in learning_rates:
        print ("Momentum and learning rate are ({0}, {1})".format(momentum, learning_rate))
        a3.a3_main(0, n_hid=10, n_iterations=70, lr_net=learning_rate, train_momentum=momentum, 
                   early_stopping=False, mini_batch_size=4)
        print
In [ ]:
 
## Question 5
a3.a3_main(0, n_hid=200, n_iterations=1000, lr_net=0.35, train_momentum=0.9, 
           early_stopping=False, mini_batch_size=100)
In [ ]:
 
## Question 6
a3.a3_main(0, n_hid=200, n_iterations=1000, lr_net=0.35, train_momentum=0.9, 
           early_stopping=True, mini_batch_size=100)
In [ ]:
 
## Question 7
for decay in [0, 0.0001, 0.001, 0.01, 1., 5]:
    print (decay)
    a3.a3_main(decay, n_hid=200, n_iterations=1000, lr_net=0.35, train_momentum=0.9, 
               early_stopping=False, mini_batch_size=100)
    print ()
In [ ]:
 
## Question 8
for size in [10, 30, 100, 130, 170]:
    print (size)
    a3.a3_main(0, n_hid=size, n_iterations=1000, lr_net=0.35, train_momentum=0.9, 
               early_stopping=False, mini_batch_size=100)
    print
In [ ]:
 
## Question 9/10
for size in [18, 37, 83, 113, 189]:
    print (size)
    a3.a3_main(0, n_hid=size, n_iterations=1000, lr_net=0.35, train_momentum=0.9, 
               early_stopping=True, mini_batch_size=100)
    print ()
In [ ]: