{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport tensorflow as tf\nimport requests\nimport sys\nsys.path.append(\"../input/ai-training-challenge-1/cifar10_input.py\")\nimport cifar10_input\nsys.path.append(\"../input/ai-training-challenge-1/model.py\")\nfrom model_challenge import Model_Plain_Net\nimport pandas as pd\nimport os","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-04-02T17:01:40.181232Z","iopub.execute_input":"2022-04-02T17:01:40.181602Z","iopub.status.idle":"2022-04-02T17:01:44.70666Z","shell.execute_reply.started":"2022-04-02T17:01:40.181516Z","shell.execute_reply":"2022-04-02T17:01:44.705935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Get Model","metadata":{}},{"cell_type":"markdown","source":"Define Model Parameters","metadata":{}},{"cell_type":"code","source":"input_shape = (32,32,3)\nclass_num = 10\n# Construct model\nmodel = Model_Plain_Net(tf.keras.layers.Input(shape = input_shape),class_num)\n# The path for CIFAR-10 dataset\ndata_path = \"../input/ai-training-challenge-1/Dataset/\"","metadata":{"execution":{"iopub.status.busy":"2022-04-02T17:01:44.708322Z","iopub.execute_input":"2022-04-02T17:01:44.708568Z","iopub.status.idle":"2022-04-02T17:01:48.152252Z","shell.execute_reply.started":"2022-04-02T17:01:44.708533Z","shell.execute_reply":"2022-04-02T17:01:48.151543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Read dataset for training and testing","metadata":{}},{"cell_type":"code","source":"# Get object for reading CIFAR-10 test datasset\nraw_cifar = cifar10_input.CIFAR10Data(data_path)\n# Get training dataset\nx_train = raw_cifar.train_data.xs / 255.0\ny_train = raw_cifar.train_data.ys\n# Get test dataset\nx_test = raw_cifar.eval_data.xs\ny_test = raw_cifar.eval_data.ys","metadata":{"execution":{"iopub.status.busy":"2022-04-02T17:01:48.153456Z","iopub.execute_input":"2022-04-02T17:01:48.153721Z","iopub.status.idle":"2022-04-02T17:01:52.094931Z","shell.execute_reply.started":"2022-04-02T17:01:48.153687Z","shell.execute_reply":"2022-04-02T17:01:52.094146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training\n**You should modify this section to define your optimizer method and train the given model with best accuracy.**\n\n**Good luck and have fun**","metadata":{}},{"cell_type":"markdown","source":"Specify the training configuration (optimizer, loss, metrics)","metadata":{}},{"cell_type":"code","source":"model.net.compile(\n    optimizer=tf.keras.optimizers.RMSprop(),  # Optimizer\n    # Loss function to minimize\n    loss=tf.keras.losses.SparseCategoricalCrossentropy(),\n    # List of metrics to monitor\n    metrics=[tf.keras.metrics.SparseCategoricalAccuracy()],\n)","metadata":{"execution":{"iopub.status.busy":"2022-04-02T17:01:52.096873Z","iopub.execute_input":"2022-04-02T17:01:52.097132Z","iopub.status.idle":"2022-04-02T17:01:52.117396Z","shell.execute_reply.started":"2022-04-02T17:01:52.097098Z","shell.execute_reply":"2022-04-02T17:01:52.116788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Set callback for model saving","metadata":{}},{"cell_type":"code","source":"callbacks = [\n    tf.keras.callbacks.ModelCheckpoint(\n        # Path where to save the model\n        # The two parameters below mean that we will overwrite\n        # the current checkpoint if and only if\n        # the `val_loss` score has improved.\n        filepath=\"./my_trained_model.hdf5\",\n        save_best_only=True,  # Only save a model if `val_loss` has improved.\n        monitor=\"val_loss\",\n        verbose=1,\n    )\n]","metadata":{"execution":{"iopub.status.busy":"2022-04-02T17:01:52.11871Z","iopub.execute_input":"2022-04-02T17:01:52.119003Z","iopub.status.idle":"2022-04-02T17:01:52.125086Z","shell.execute_reply.started":"2022-04-02T17:01:52.118951Z","shell.execute_reply":"2022-04-02T17:01:52.124305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Fit model on training data","metadata":{}},{"cell_type":"code","source":"history = model.net.fit(\n    x_train,\n    y_train,\n    batch_size=128,\n    epochs=5,\n    callbacks=callbacks,\n    # We pass some validation for\n    # monitoring validation loss and metrics\n    # at the end of each epoch\n    validation_data=(x_test, y_test),\n)","metadata":{"execution":{"iopub.status.busy":"2022-04-02T17:01:52.126115Z","iopub.execute_input":"2022-04-02T17:01:52.126327Z","iopub.status.idle":"2022-04-02T17:04:24.554932Z","shell.execute_reply.started":"2022-04-02T17:01:52.126304Z","shell.execute_reply":"2022-04-02T17:04:24.554158Z"},"trusted":true},"execution_count":null,"outputs":[]}]}