{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport mxnet as mx\nimport logging","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","collapsed":true,"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"# set to 0 to train on all available data\nVALIDATION_SIZE = 2000","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6f3bde0669b9ba5b1cfd42ed62f739eed27527aa"},"cell_type":"code","source":"# create the training \ntrain = pd.read_csv(\"../input/train.csv\")\ntarget = train['label'].values.ravel()\ntrain = train.iloc[:,1:].values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"203b4fa51a91353f66a9dbddde282feaddcfeccd"},"cell_type":"code","source":"def get_lenet():\n    data = mx.symbol.Variable('data')\n    # first conv\n    conv1 = mx.symbol.Convolution(data=data, kernel=(5,5), num_filter=20)\n    tanh1 = mx.symbol.Activation(data=conv1, act_type=\"tanh\")\n    pool1 = mx.symbol.Pooling(data=tanh1, pool_type=\"max\",\n                              kernel=(2,2), stride=(2,2))\n    # second conv\n    conv2 = mx.symbol.Convolution(data=pool1, kernel=(5,5), num_filter=50)\n    tanh2 = mx.symbol.Activation(data=conv2, act_type=\"tanh\")\n    pool2 = mx.symbol.Pooling(data=tanh2, pool_type=\"max\",\n                              kernel=(2,2), stride=(2,2))\n    # first fullc\n    flatten = mx.symbol.Flatten(data=pool2)\n    fc1 = mx.symbol.FullyConnected(data=flatten, num_hidden=500)\n    tanh3 = mx.symbol.Activation(data=fc1, act_type=\"tanh\")\n    # second fullc\n    fc2 = mx.symbol.FullyConnected(data=tanh3, num_hidden=10)\n    # loss\n    lenet = mx.symbol.SoftmaxOutput(data=fc2, name='softmax')\n    return lenet","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"f60f63c2ec1befeb9f2612534dc21cda95ef8852"},"cell_type":"code","source":"# split dataset\nval_data = train[:VALIDATION_SIZE].astype('float32')\nval_label = target[:VALIDATION_SIZE]\ntrain_data = train[VALIDATION_SIZE: , :].astype('float32')\ntrain_label = target[VALIDATION_SIZE:]\ntrain_data = np.array(train_data).reshape((-1, 1, 28, 28))\nval_data = np.array(val_data).reshape((-1, 1, 28, 28))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"10c5c2c4c7557874cef2a5c7dd8208a3843d6cd8"},"cell_type":"code","source":"# Normalize data\ntrain_data[:] /= 256.0\nval_data[:]/= 256.0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"2dc512ed25610e585bc15ac8fa97bb1f00dc8351"},"cell_type":"code","source":"batch_size = 500\ntrain_iter = mx.io.NDArrayIter(train_data, train_label, batch_size=batch_size, shuffle=True)\nval_iter = mx.io.NDArrayIter(val_data, val_label, batch_size=batch_size)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"ba482d4f4897ceb4b7ca141ddd3e03d02a4a2caa"},"cell_type":"code","source":"# logging\nhead = '%(asctime)-15s Node[0] %(message)s'\nlogging.basicConfig(level=logging.DEBUG, format=head)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"eae176e247609a8ab268e52604aafb3754e1abdf"},"cell_type":"code","source":"# create model \ndevs = mx.gpu(0)\nnetwork=get_lenet()\nmodel = mx.model.FeedForward(\n        #ctx                = devs,\n        symbol             = network,\n        num_epoch          = 4,\n        learning_rate      = 0.1,\n        momentum           = 0.9,\n        wd                 = 0.00001,\n        initializer        = mx.init.Xavier(factor_type=\"in\", magnitude=2.34)\n        )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"75c99844109eb77a42bb4870cfc9daff8debe5f3"},"cell_type":"code","source":"eval_metrics = ['accuracy']\nmodel.fit(\n    X=train_iter, \n    eval_metric = eval_metrics,\n    eval_data = val_iter\n    )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"6428362873442e078305b22c7d5bb01fe2619192"},"cell_type":"code","source":"#predict\ntest = pd.read_csv(\"../input/test.csv\").values\ntest_data = test.astype('float32')\ntest_data = np.array(test_data).reshape((-1, 1, 28, 28))\ntest_data[:]/= 256.0\ntest_iter = mx.io.NDArrayIter(test_data, batch_size=batch_size)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"4faadf22dbd48edc4d6381dab78f039281e4b11c"},"cell_type":"code","source":"pred = model.predict(X = test_iter)\npred = np.argsort(pred)\nnp.savetxt('mxnet_submission.csv', np.c_[range(1,len(test)+1),pred[:,9]], delimiter=',', header = 'ImageId,Label', comments = '', fmt='%d')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"1eeeceef345cdff057e62e225095c2189274b448"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}