{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nfrom keras import models, layers\nfrom keras.utils import to_categorical","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Input data files are available in the \"/kaggle/input/108-1-dl-app-hw1/\" directory.\nDATA_DIR = '/kaggle/input/108-1-ntut-ml-hw1/'\nTRAIN_DATA_FILE = DATA_DIR + 'emnist-byclass-train.npz'\nTEST_DATA_FILE = DATA_DIR + 'emnist-byclass-test.npz'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Load training data\ndata = np.load(TRAIN_DATA_FILE)\ntrain_labels = data['training_labels']\ntrain_images = data['training_images']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"trn_images = train_images.reshape((train_images.shape[0], 28 * 28))\ntrn_images = trn_images.astype('float32') / 255\ntrn_labels = to_categorical(train_labels)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Define Your Own Network\nnetwork = models.Sequential()\nnetwork.add(layers.Dense(512, activation='relu', input_shape=(28 * 28,)))\nnetwork.add(layers.Dense(256, activation='relu', input_shape=(512,)))\nnetwork.add(layers.Dense(128, activation='relu', input_shape=(256,)))\nnetwork.add(layers.Dense(62, activation='softmax'))\n\nnetwork.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"network.fit(trn_images, trn_labels, epochs=5, batch_size=128)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Preparing the test data\ntest_images = np.load(TEST_DATA_FILE)['testing_images']\ntst_images = test_images.reshape((test_images.shape[0], 28 * 28))\ntst_images = tst_images.astype('float32') / 255","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Evalute our model on test data\nresults = network.predict_classes(tst_images)\nresults","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Print results in CSV format and upload to Kaggle\nwith open('pred_results.csv', 'w') as f:\n    f.write('Id,Category\\n')\n    for i in range(len(results)):\n        f.write(str(i) + ',' + str(results[i]) + '\\n')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Download your results!\nfrom IPython.display import FileLink\nFileLink('pred_results.csv')","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":1}