{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"#importing the tensorflow\n!pip install tensorflow==1.15.3 --user","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#importing the numpy and tensorflow\nimport numpy as np\nimport tensorflow as tf","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_ds='../input/109-1-ntut-dl-app-hw1/emnist-byclass-train.npz'\ntest_ds='../input/109-1-ntut-dl-app-hw1/emnist-byclass-test.npz'\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df=np.load(train_ds) #loading to the train set\n\nti=df['training_images'] #ti->train images, calling the label column","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df=np.load(train_ds) #loading to the train set\ntl=df['training_labels'] #tl->train labels, calling the  label column\n# detect and init the TPU\ntpu = tf.distribute.cluster_resolver.TPUClusterResolver()\ntf.config.experimental_connect_to_cluster(tpu)\ntf.tpu.experimental.initialize_tpu_system(tpu)\n\n# instantiate a distribution strategy\ntpu_strategy = tf.distribute.experimental.TPUStrategy(tpu)\n\n# instantiating the model in the strategy scope creates the model on the TPU\nwith tpu_strategy.scope():\n    # define your model normally\n    model= tf.keras.models.Sequential()\n    model.add(tf.keras.layers.Conv2D(32, kernel_size=(5, 5),activation='relu'))\n    model.add(tf.keras.layers.Dropout(0.1))\n    model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2)))\n\n    model.add(tf.keras.layers.Conv2D(64, (3, 3), activation='relu'))\n    model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2)))\n    model.add(tf.keras.layers.Dropout(0.1))\n    model.add(tf.keras.layers.Flatten())\n    \n    model.add(tf.keras.layers.Dense(128,activation=tf.nn.relu))\n    model.add(tf.keras.layers.Dropout(0.15))\n    model.add(tf.keras.layers.Dense(62, activation=tf.nn.softmax))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(optimizer='adam',loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.fit(ti, tl, epochs=9,batch_size=128)    \n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ti = np.load(test_ds)['testing_images']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"r=model.predict_classes(ti)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"        \n# Print results in CSV format and upload to Kaggle\nwith open('Emnist_pred_results.csv', 'w') as f:\n    f.write('Id,Category\\n')\n    for i in range(len(r)):\n        f.write(str(i) + ',' + str(r[i]) + '\\n')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Download your results!\nfrom IPython.display import FileLink\nFileLink('Emnist_pred_results.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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":4}