{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv\n\nimport os\nprint(os.listdir(\"../input\"))\n\n%load_ext autoreload\n%autoreload 2\n%matplotlib inline\n\nfrom fastai.imports import *\nfrom fastai.torch_imports import *\nfrom fastai.io import *\nimport torch.nn as nn\n","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"trn, test = pd.read_csv('../input/train.csv'), pd.read_csv('../input/test.csv')\ntrn.shape, test.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e9dc33ebd747f33a7b66ad96e41f28a1e682f0ba"},"cell_type":"code","source":"x0, y0 = trn.drop(['label'], axis =1).values, trn['label'].values\nx0.shape, y0.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b0e9c74bc9023607fa3628d35f22fda61d110aa2"},"cell_type":"code","source":"mean= x0.mean()\nstd = x0.std()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"51154c1de12240e250f3e9100d11b45437b920f9"},"cell_type":"code","source":"x0= (x0-mean)/std","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cace1b423722f36bad39957d4106ae707b056db9"},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx,x_valid, y, y_valid = train_test_split(x0, y0, test_size=0.02,random_state=42)\nx.shape, x_valid.shape, y.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"416ba8cdb334b693326cb53d6c8cc2d6cfb73a6f"},"cell_type":"code","source":"x_trn, y_trn = torch.from_numpy(x), torch.from_numpy(y)\ntype(x_trn)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ca523d53304333f7370c066f39c60ecaa9204d9d"},"cell_type":"code","source":"net = nn.Sequential(\n    nn.Linear(28*28, 100),\n    nn.ReLU(),\n    nn.Linear(100, 100),\n    nn.ReLU(),\n    nn.Linear(100, 10),\n    nn.LogSoftmax()\n).cuda()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c1b99dc3b9cb59e5dbde190100270e11eab73624"},"cell_type":"code","source":"from fastai.metrics import *\nfrom fastai.model import *\nfrom fastai.dataset import *\n\nmd = ImageClassifierData.from_arrays('tmp/tmp/', (x,y), (x_valid, y_valid))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"949e510f751c74109e29ca9d750c07f7ff2d79c0"},"cell_type":"code","source":"loss=nn.NLLLoss()\nmetrics=[accuracy]\n# opt=optim.SGD(net.parameters(), 1e-1, momentum=0.9)\nopt=optim.SGD(net.parameters(), 1e-1, momentum=0.9, weight_decay=1e-3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ecb5cc79c7dfee2b7b885157d684119dab3d43e1"},"cell_type":"code","source":"fit(net, md, n_epochs=5, crit=loss, opt=opt, metrics=metrics)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ce90d7edd98b17ab501df1f646ef51ae9cdd2f5d"},"cell_type":"code","source":"set_lrs(opt, 1e-2)\n\nfit(net, md, n_epochs=3, crit=loss, opt=opt, metrics=metrics)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"75789b9e8ae8c0752e75e640871c2cada2529a1e"},"cell_type":"code","source":"fit(net, md, n_epochs=6, crit=loss, opt=opt, metrics=metrics)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cf38cb762ea7ef5d636464a857d3e3b8fe7b1eb4"},"cell_type":"code","source":"test_data=(test-mean)/std","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}