{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport tensorflow as tf\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"data = tf.keras.datasets.cifar10","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"((train_images, train_labels), (test_images, test_labels)) = data.load_data()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('train_images :', train_images.shape)\nprint('train_labels :', train_labels.shape)\nprint('test_images :', test_images.shape)\nprint('test_images :', test_labels.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig = plt.figure(figsize=(8, 8))\nrows, cols = 5, 4\n\nfor i in range(1, rows*cols+1):\n    fig.add_subplot(rows, cols, i)\n    plt.imshow(train_images[i])\n    plt.axis('off')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_images_flat = train_images.reshape((-1, 32*32*3))\nprint(train_images_flat.shape)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Basic MLP","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = Sequential([\n    Dense(500, input_shape=(3072,), activation='relu'),\n    Dense(100, activation='relu'),\n    Dense(10, activation='softmax')\n])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history = model.fit(x=train_images_flat / 255,\n                    y=train_labels,\n                    validation_split=0.1, # 50000 -> 45000 / 5000\n                    epochs=30,\n                    batch_size=32,\n                    verbose=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def plot_history(history):\n    train_loss = history.history['loss']\n    valid_loss = history.history['val_loss']\n    \n    train_acc = history.history['accuracy']\n    valid_acc = history.history['val_accuracy']\n    \n    plt.figure(figsize=(8, 8))\n    \n    plt.subplot(2, 1, 1)\n    plt.plot(train_loss, label='Train Loss')\n    plt.plot(valid_loss, label='Validation Loss')\n    plt.legend()\n    plt.title('Train Loss vs Validation Loss')\n    \n    plt.subplot(2, 1, 2)\n    plt.plot(train_acc, label='Train Accuracy')\n    plt.plot(valid_acc, label='Validation Accuracy')\n    plt.legend()\n    plt.title('Train Accuracy vs Validation Accuracy')\n    \n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_history(history)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Regularization : 정규화/규제화\n파라미터 크기에 제한을 둔다\n\n데이터로부터 학습한 정보를 100% 믿지 않는다\n\n### BatchNormalization, Dropout","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow.keras.layers import BatchNormalization, Dropout, ReLU","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = Sequential([\n    Dense(500, input_shape=(3072,)),\n    BatchNormalization(),\n    Dropout(0.5),\n    ReLU(),\n    \n    Dense(100),\n    BatchNormalization(),\n    Dropout(0.5),\n    ReLU(),\n    \n    Dense(10, activation='softmax')\n])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history = model.fit(x=train_images_flat / 255,\n                    y=train_labels,\n                    validation_split=0.1, # 50000 -> 45000 / 5000\n                    epochs=30,\n                    batch_size=32,\n                    verbose=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_history(history)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Simple CNN","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = Sequential([\n    Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),\n    MaxPooling2D(2, 2),\n    \n    Conv2D(64, (3, 3), activation='relu'),\n    MaxPooling2D(2, 2),\n    \n    Conv2D(128, (3, 3), activation='relu'),\n    \n    Flatten(),\n    \n    Dense(500),\n    BatchNormalization(),\n    Dropout(0.5),\n    ReLU(),\n    \n    Dense(100),\n    BatchNormalization(),\n    Dropout(0.5),\n    ReLU(),\n    \n    Dense(10, activation='softmax')\n])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history = model.fit(x=train_images / 255,\n                    y=train_labels,\n                    validation_split=0.1, # 50000 -> 45000 / 5000\n                    epochs=30,\n                    batch_size=32,\n                    verbose=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_history(history)","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}