In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from theano.printing import pydotprint
import pickle

Linear Regression - Housing Data Set


In [2]:
import mxnet as mx
import mxnet.symbol as S
import mxnet.ndarray as N


---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-2-d816eadb701d> in <module>()
      1 import mxnet as mx
----> 2 import mxnet.symbol as S
      3 import mxnet.ndarray as N

ImportError: No module named 'mxnet.symbol'

In [22]:
with open('data/housing.dframe', 'rb') as dframe:
    housing = pickle.load(dframe)
x_train = np.stack([np.ones((len(housing['TAX']))), housing['RM']],axis=1)
y_train = np.array(housing['MEDV'])

# plt.scatter(x_train[:,1], y_train ,marker='x')
# housing.head()

In [6]:
# model setup
x = S.Variable('x')
y = S.Variable('y')
m = S.Variable('m')
theta = S.Variable('theta')
h = S.dot(x, theta)
J = S.sum(S.pow(h - y, 2)) / (2 * m)
# gJ = T.grad(J, theta)

input_args = {}
input_args['x'] = mx.nd.array(x_train, ctx=mx.cpu())
input_args['y'] = mx.nd.array(np.transpose(np.array(y_train,ndmin=2)), ctx=mx.cpu()),
input_args['m'] = mx.nd.array([len(x_train)], ctx=mx.cpu()),
input_args['theta']=  mx.nd.array([[0], [0]], ctx=mx.cpu())

grad_arguments = { 'theta' : mx.nd.ones((2, ), ctx=mx.cpu())}
c_exec = J.bind(ctx=mx.cpu(), 
                args=input_args, 
                args_grad=grad_arguments)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-5d0b2030b409> in <module>()
     16 c_exec = J.bind(ctx=mx.cpu(), 
     17                 args=input_args,
---> 18                 args_grad=grad_arguments)

/home/jakob/miniconda2/envs/ml-software/lib/python3.5/site-packages/mxnet-0.7.0-py3.5.egg/mxnet/symbol.py in bind(self, ctx, args, args_grad, grad_req, aux_states, group2ctx, shared_exec)
    781 
    782         listed_arguments = self.list_arguments()
--> 783         args_handle, args = self._get_ndarray_inputs('args', args, listed_arguments, False)
    784         # setup args gradient
    785         if args_grad is None:

/home/jakob/miniconda2/envs/ml-software/lib/python3.5/site-packages/mxnet-0.7.0-py3.5.egg/mxnet/symbol.py in _get_ndarray_inputs(arg_key, args, arg_names, allow_missing)
    615                     narr = args[name]
    616                     if not isinstance(narr, NDArray):
--> 617                         raise TypeError('Only Accept list of NDArrays or dict of str to NDArray')
    618                     arg_handles.append(narr.handle)
    619                     arg_arrays.append(narr)

TypeError: Only Accept list of NDArrays or dict of str to NDArray

In [ ]:
out_grad = mx.nd.ones((2,), ctx=mx.cpu())

c_exec.forward()
c_exec.backward(out_grad)
c_exec.outputs[0].asnumpy()
c_exec.grad_arrays

In [ ]:
# TODO: Plotting

Binary Classification - Pima Indians Diabetes Data Set


In [7]:
with open('data/pima-indians-diabetes.tuple', 'rb') as xtuple:
    x_train, y_train, x_test, y_test = pickle.load(xtuple)
y_train = np.atleast_2d(y_train).transpose()

Image Classification - MNIST dataset


In [16]:
data_dir = 'data/'
data_shape = (784, )
batch_size = 128

In [11]:
train           = mx.io.MNISTIter(
    image       = data_dir + "train-images-idx3-ubyte",
    label       = data_dir + "train-labels-idx1-ubyte",
    input_shape = data_shape,
    batch_size  = batch_size,
    shuffle     = True,
    flat        = True,
#     num_parts   = kv.num_workers,
#     part_index  = kv.rank
)

val = mx.io.MNISTIter(
    image       = data_dir + "t10k-images-idx3-ubyte",
    label       = data_dir + "t10k-labels-idx1-ubyte",
    input_shape = data_shape,
    batch_size  = batch_size,
    flat        = True,
#     num_parts   = kv.num_workers,
#     part_index  = kv.rank
)

In [19]:
data = mx.symbol.Variable('data')
fc1 = mx.symbol.FullyConnected(data = data, name='fc1', num_hidden=128)
act1 = mx.symbol.Activation(data = fc1, name='relu1', act_type="relu")
fc2 = mx.symbol.FullyConnected(data = act1, name = 'fc2', num_hidden = 64)
act2 = mx.symbol.Activation(data = fc2, name='relu2', act_type="relu")
fc3 = mx.symbol.FullyConnected(data = act2, name='fc3', num_hidden=10)
mlp = mx.symbol.Softmax(data = fc3, name = 'mlp')

model = mx.model.FeedForward(
        ctx                = mx.cpu(),
        symbol             = mlp,
        num_epoch          = 10,
        learning_rate      = 0.1,
        momentum           = 0.9,
        wd                 = 0.00001,
        initializer        = mx.init.Xavier(factor_type="in", magnitude=2.34),
#         **model_args
)
batch_end_callback = [mx.callback.Speedometer(batch_size, 50)]
eval_metrics = ['accuracy']

In [21]:
model.fit(
    X                  = train,
    eval_data          = val,
    eval_metric        = eval_metrics,
    kvstore            = None,
    batch_end_callback = batch_end_callback,
    epoch_end_callback = None)


---------------------------------------------------------------------------
MXNetError                                Traceback (most recent call last)
<ipython-input-21-3fdb8d0501c5> in <module>()
      5     kvstore            = None,
      6     batch_end_callback = batch_end_callback,
----> 7     epoch_end_callback = None)

/home/jakob/miniconda2/envs/ml-software/lib/python3.5/site-packages/mxnet-0.7.0-py3.5.egg/mxnet/model.py in fit(self, X, y, eval_data, eval_metric, epoch_end_callback, batch_end_callback, kvstore, logger, work_load_list, monitor, eval_batch_end_callback)
    744 
    745         arg_names, param_names, aux_names = \
--> 746                 self._init_params(dict(data.provide_data+data.provide_label))
    747 
    748         # setup metric

/home/jakob/miniconda2/envs/ml-software/lib/python3.5/site-packages/mxnet-0.7.0-py3.5.egg/mxnet/model.py in _init_params(self, input_shapes, overwrite)
    484     def _init_params(self, input_shapes, overwrite=False):
    485         """Initialize weight parameters and auxiliary states"""
--> 486         arg_shapes, _, aux_shapes = self.symbol.infer_shape(**input_shapes)
    487         assert(arg_shapes is not None)
    488 

/home/jakob/miniconda2/envs/ml-software/lib/python3.5/site-packages/mxnet-0.7.0-py3.5.egg/mxnet/symbol.py in infer_shape(self, *args, **kwargs)
    451             The order is in the same order as list_auxiliary()
    452         """
--> 453         return self._infer_shape_impl(False, *args, **kwargs)
    454 
    455     def infer_shape_partial(self, *args, **kwargs):

/home/jakob/miniconda2/envs/ml-software/lib/python3.5/site-packages/mxnet-0.7.0-py3.5.egg/mxnet/symbol.py in _infer_shape_impl(self, partial, *args, **kwargs)
    511             ctypes.byref(aux_shape_ndim),
    512             ctypes.byref(aux_shape_data),
--> 513             ctypes.byref(complete)))
    514         if complete.value != 0:
    515             arg_shapes = [

/home/jakob/miniconda2/envs/ml-software/lib/python3.5/site-packages/mxnet-0.7.0-py3.5.egg/mxnet/base.py in check_call(ret)
     75     """
     76     if ret != 0:
---> 77         raise MXNetError(py_str(_LIB.MXGetLastError()))
     78 
     79 def c_str(string):

MXNetError: [02:14:07] src/symbol/symbol.cc:155: Symbol.InferShapeKeyword argument name softmax_label not found.
Candidate arguments:
	[0]data
	[1]fc1_weight
	[2]fc1_bias
	[3]fc2_weight
	[4]fc2_bias
	[5]fc3_weight
	[6]fc3_bias
	[7]mlp_label

Image Classification - CIFAR-10 dataset


In [ ]: