{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","collapsed":true,"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":false},"cell_type":"markdown","source":"**Import các thư viện cần thiết**"},{"metadata":{"trusted":true,"_uuid":"a285ef49c1f968613e97d706a671029591ab7602"},"cell_type":"code","source":"import numpy as np \nimport keras \nimport pandas as pd\nfrom keras.datasets import fashion_mnist\nfrom keras.layers import Dense, Flatten\nfrom keras.models import Sequential\nfrom keras import metrics \nfrom keras import optimizers","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0a32ef0793ba84f85fcf2e2e3c29c22245d0598c"},"cell_type":"markdown","source":"**Đọc dữ liệu**"},{"metadata":{"trusted":true,"_uuid":"f67efb07f4f8344452e0900b795d537e49960ba4"},"cell_type":"code","source":"df = pd.read_csv('../input/train.csv')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"79ed8af9c3d67d758ba67244aa51b71f91f65b51"},"cell_type":"markdown","source":"**Tách feature và target**"},{"metadata":{"trusted":true,"_uuid":"be04fb652fad958bd55d373a6911dffe5f4fce0f"},"cell_type":"code","source":"y_train = df['label'].values.reshape(df.shape[0], 1)\nX_train = df.drop('label', axis = 1).values\nprint(X_train.shape)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"61f78b1d6481397355b4f3a6dee5a6f1f0b6ba49"},"cell_type":"markdown","source":"**Chuẩn hóa dữ liệu**"},{"metadata":{"trusted":true,"_uuid":"306cc47d1256b3997078f61514e438ea6a2ef6f3"},"cell_type":"code","source":"X_train = X_train / 255.\nnum_classes = 10 ","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"bd5afcbd1e3251c29ad11b83a2ea2755d989dc5d"},"cell_type":"markdown","source":"**Chuyển y_train về dạng one_hot coding**"},{"metadata":{"trusted":true,"_uuid":"e4301f9563cbc5e4ecd1a83b8a34006d1c65aaee"},"cell_type":"code","source":"y_train = keras.utils.to_categorical(y_train, num_classes)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7380f8f3dda66e51e8599f9de7ce37f43a4f7d07"},"cell_type":"markdown","source":"**Xây dựng mô hình sequential**"},{"metadata":{"trusted":true,"_uuid":"b530c9730af57831ca96fde89d8dd41da20fe168"},"cell_type":"code","source":"model = Sequential()\nmodel.add(Dense(128, input_shape = (784,), activation = 'linear'))\nmodel.add(Dense(256, activation = 'relu'))\nmodel.add(Dense(512, activation = 'relu'))\nmodel.add(Dense(1024,activation = 'relu'))\nmodel.add(Dense(num_classes, activation = 'softmax'))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"778cadbe5bf6732b276506255398b54826db0b8a"},"cell_type":"markdown","source":"**Hàm mất mát, metrics**"},{"metadata":{"trusted":true,"_uuid":"ba8108bb69cbd4aa8adb07e07179c95fff24daa6"},"cell_type":"code","source":"sgd = optimizers.SGD(lr = 0.12)\nmodel.compile(loss = keras.losses.categorical_crossentropy,\n              optimizer = 'adam', metrics = ['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"44bac8954381a5f05403a4a23ed438147388aca1"},"cell_type":"markdown","source":"**Huấn luyện mô hình**"},{"metadata":{"trusted":true,"_uuid":"f4c4eabea953e08c3c3a713640b600e79ff3507f","scrolled":true},"cell_type":"code","source":"model.fit(X_train, y_train, batch_size = 256, epochs = 50)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"bf65cd920d531521b45e35086b4264894c29eafa"},"cell_type":"markdown","source":"**Predict kết quả**"},{"metadata":{"trusted":true,"_uuid":"b6c54da4255ca5cf269d139c875d3f2bcd68f211"},"cell_type":"code","source":"df2 = pd.read_csv('../input/test.csv')\nX_test = df2.values\npredictions = model.predict_classes(X_test)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b1fa4c14f3c1674887f4c1c9d14d7cd0ee7c7a7d"},"cell_type":"markdown","source":"**Submit kết quả**"},{"metadata":{"trusted":true,"_uuid":"7a9862c47afa41f7dff93ce23b8841c97c566026"},"cell_type":"code","source":"submission = pd.DataFrame({'ImageId':range(1,28001), 'Label':predictions})\nsubmission.to_csv('submission.csv',index=False)\nprint('Save to file submission.csv')","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}