{"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":"# 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\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 read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-06-05T09:59:17.805347Z","iopub.execute_input":"2022-06-05T09:59:17.805925Z","iopub.status.idle":"2022-06-05T09:59:17.838641Z","shell.execute_reply.started":"2022-06-05T09:59:17.805813Z","shell.execute_reply":"2022-06-05T09:59:17.837808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom tensorflow.keras import callbacks, datasets, layers","metadata":{"execution":{"iopub.status.busy":"2022-06-05T10:06:02.951838Z","iopub.execute_input":"2022-06-05T10:06:02.952213Z","iopub.status.idle":"2022-06-05T10:06:02.956692Z","shell.execute_reply.started":"2022-06-05T10:06:02.952180Z","shell.execute_reply":"2022-06-05T10:06:02.955714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(train_images, train_labels), (test_images, test_labels) = datasets.mnist.load_data() ","metadata":{"execution":{"iopub.status.busy":"2022-06-05T09:59:23.313446Z","iopub.execute_input":"2022-06-05T09:59:23.314027Z","iopub.status.idle":"2022-06-05T09:59:24.829403Z","shell.execute_reply.started":"2022-06-05T09:59:23.313996Z","shell.execute_reply":"2022-06-05T09:59:24.828586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images = train_images.astype(np.float32).reshape(-1, 28, 28, 1)\ntest_images = test_images.astype(np.float32).reshape(-1, 28, 28, 1)\ntrain_images /= 255\ntest_images /= 255\n\nX_val = train_images[:10_000]\ny_val = train_labels[:10_000]\npartial_X_train = train_images[10_000:]\npartial_y_train = train_labels[10_000:]","metadata":{"execution":{"iopub.status.busy":"2022-06-05T09:59:24.830642Z","iopub.execute_input":"2022-06-05T09:59:24.831073Z","iopub.status.idle":"2022-06-05T09:59:24.913095Z","shell.execute_reply.started":"2022-06-05T09:59:24.831036Z","shell.execute_reply":"2022-06-05T09:59:24.912277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(3, 3)\nfig.set_size_inches(8, 8)\n\nfor i, ax in enumerate(axes.flatten()):\n    ax.imshow(train_images[i])\n    ax.axis(\"off\")\n    ax.set_title(f\"Label: {train_labels[i]}\")","metadata":{"execution":{"iopub.status.busy":"2022-06-05T09:59:24.914910Z","iopub.execute_input":"2022-06-05T09:59:24.915380Z","iopub.status.idle":"2022-06-05T09:59:25.453770Z","shell.execute_reply.started":"2022-06-05T09:59:24.915340Z","shell.execute_reply":"2022-06-05T09:59:25.453037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.Sequential([\n    layers.Conv2D(filters=6, kernel_size=5, activation=\"tanh\", input_shape=(28, 28, 1), name=\"C1\"),\n    layers.AveragePooling2D(name=\"S2\"),\n    layers.Conv2D(filters=16, kernel_size=5, activation=\"tanh\", name=\"C3\"),\n    layers.AveragePooling2D(name=\"S4\"),\n    layers.Flatten(name=\"FLATTEN\"),\n    layers.Dense(120, activation=\"tanh\", name=\"F5\"),\n    layers.Dense(84, activation=\"tanh\", name=\"F6\"),\n    layers.Dense(10, activation=\"softmax\", name=\"OUTPUT\")\n], name=\"LeNet-5\")\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-06-05T10:33:56.055282Z","iopub.execute_input":"2022-06-05T10:33:56.055829Z","iopub.status.idle":"2022-06-05T10:33:56.108514Z","shell.execute_reply.started":"2022-06-05T10:33:56.055792Z","shell.execute_reply":"2022-06-05T10:33:56.107723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n    optimizer=\"adam\",\n    loss=\"sparse_categorical_crossentropy\",\n    metrics=[\"accuracy\"]\n)\n\nearly_stopping = callbacks.EarlyStopping(\n    min_delta=0.001,\n    patience=10,\n    restore_best_weights=True\n)\n\nhistory = model.fit(\n    partial_X_train,\n    partial_y_train,\n    epochs=100,\n    batch_size=512,\n    validation_data=(X_val, y_val),\n    verbose=2,\n    callbacks=[early_stopping]\n)","metadata":{"execution":{"iopub.status.busy":"2022-06-05T10:28:53.894882Z","iopub.execute_input":"2022-06-05T10:28:53.895607Z","iopub.status.idle":"2022-06-05T10:29:08.437273Z","shell.execute_reply.started":"2022-06-05T10:28:53.895569Z","shell.execute_reply":"2022-06-05T10:29:08.436524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_history = pd.DataFrame(history.history)\ndf_history.loc[:, [\"loss\", \"val_loss\"]].plot(xlabel=\"Epochs\", ylabel=\"Loss\", figsize=(7, 4))\ndf_history.loc[:, [\"accuracy\", \"val_accuracy\"]].plot(xlabel=\"Epochs\", ylabel=\"Accuracy\", figsize=(7, 4))","metadata":{"execution":{"iopub.status.busy":"2022-06-05T10:29:17.341379Z","iopub.execute_input":"2022-06-05T10:29:17.341984Z","iopub.status.idle":"2022-06-05T10:29:17.703960Z","shell.execute_reply.started":"2022-06-05T10:29:17.341946Z","shell.execute_reply":"2022-06-05T10:29:17.703203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.Sequential([\n    layers.Conv2D(filters=6, kernel_size=5, activation=\"tanh\", input_shape=(28, 28, 1), name=\"C1\"),\n    layers.AveragePooling2D(name=\"S2\"),\n    layers.Conv2D(filters=16, kernel_size=5, activation=\"tanh\", name=\"C3\"),\n    layers.AveragePooling2D(name=\"S4\"),\n    layers.Flatten(name=\"FLATTEN\"),\n    layers.Dense(120, activation=\"tanh\", name=\"F5\"),\n    layers.Dense(84, activation=\"tanh\", name=\"F6\"),\n    layers.Dense(10, activation=\"softmax\", name=\"OUTPUT\")\n], name=\"LeNet-5\")\n\nmodel.compile(\n    optimizer=\"adam\",\n    loss=\"sparse_categorical_crossentropy\",\n    metrics=[\"accuracy\"]\n)\n\nnum_epochs = np.argmin(history.history[\"val_loss\"]) + 1\n\nmodel.fit(\n    train_images,\n    train_labels,\n    epochs=num_epochs,\n    batch_size=512,\n    verbose=2,\n)","metadata":{"execution":{"iopub.status.busy":"2022-06-05T10:32:49.685871Z","iopub.execute_input":"2022-06-05T10:32:49.686284Z","iopub.status.idle":"2022-06-05T10:33:00.398893Z","shell.execute_reply.started":"2022-06-05T10:32:49.686244Z","shell.execute_reply":"2022-06-05T10:33:00.398035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(test_images, test_labels)","metadata":{"execution":{"iopub.status.busy":"2022-06-05T10:33:03.674781Z","iopub.execute_input":"2022-06-05T10:33:03.675428Z","iopub.status.idle":"2022-06-05T10:33:05.106137Z","shell.execute_reply.started":"2022-06-05T10:33:03.675391Z","shell.execute_reply":"2022-06-05T10:33:05.105281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}