{"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":"2023-11-06T12:35:09.142229Z","iopub.execute_input":"2023-11-06T12:35:09.142610Z","iopub.status.idle":"2023-11-06T12:35:09.558422Z","shell.execute_reply.started":"2023-11-06T12:35:09.142579Z","shell.execute_reply":"2023-11-06T12:35:09.557274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Задача 2 (Циферки)  \nот 0.98 - 10 баллов   \nот 0.95 до 0.98 - 8 баллов  \nот 0.93 до 0.95 - 6 баллов ","metadata":{}},{"cell_type":"code","source":"train_dataset = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_dataset = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2023-11-06T12:37:35.540728Z","iopub.execute_input":"2023-11-06T12:37:35.541086Z","iopub.status.idle":"2023-11-06T12:37:41.421185Z","shell.execute_reply.started":"2023-11-06T12:37:35.541056Z","shell.execute_reply":"2023-11-06T12:37:41.419725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_numpy_data(dataset):\n    labels = dataset.get('label', pd.DataFrame()).to_numpy()\n    images = dataset.loc[:, dataset.columns != 'label'].to_numpy().astype(\"float32\") / 255\n    return images, labels","metadata":{"execution":{"iopub.status.busy":"2023-11-06T12:37:31.644512Z","iopub.execute_input":"2023-11-06T12:37:31.645012Z","iopub.status.idle":"2023-11-06T12:37:31.652347Z","shell.execute_reply.started":"2023-11-06T12:37:31.644962Z","shell.execute_reply":"2023-11-06T12:37:31.650457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images, labels = get_numpy_data(train_dataset) \ntest_images, _ = get_numpy_data(test_dataset)\n\nval_size = int(images.shape[0] * 0.1)\ntrain_images, val_images = images[val_size:], images[:val_size]\ntrain_labels, val_labels = labels[val_size:], labels[:val_size]","metadata":{"execution":{"iopub.status.busy":"2023-11-06T13:20:48.168940Z","iopub.execute_input":"2023-11-06T13:20:48.169377Z","iopub.status.idle":"2023-11-06T13:20:48.748232Z","shell.execute_reply.started":"2023-11-06T13:20:48.169346Z","shell.execute_reply":"2023-11-06T13:20:48.746895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import keras\nfrom tensorflow.keras import layers\n\ndef get_mnist_model():\n    inputs = keras.Input(shape=(28 * 28,))\n    features = layers.Dense(512, activation=\"relu\")(inputs)\n    features = layers.Dropout(0.3)(features)\n    features = layers.Dense(256, activation=\"relu\")(features)\n    features = layers.Dropout(0.2)(features)\n    features = layers.Dense(128, activation=\"relu\")(features)\n    outputs = layers.Dense(10, activation=\"softmax\")(features)\n    model = keras.Model(inputs, outputs)\n    return model","metadata":{"execution":{"iopub.status.busy":"2023-11-06T13:20:51.971360Z","iopub.execute_input":"2023-11-06T13:20:51.971892Z","iopub.status.idle":"2023-11-06T13:20:51.980597Z","shell.execute_reply.started":"2023-11-06T13:20:51.971853Z","shell.execute_reply":"2023-11-06T13:20:51.979245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau\ndef callbacks(name): \n    return [ \n        EarlyStopping(monitor = 'val_loss', patience = 6), \n        ReduceLROnPlateau(monitor = 'val_loss', patience = 3), \n    ]","metadata":{"execution":{"iopub.status.busy":"2023-11-06T14:02:32.560987Z","iopub.execute_input":"2023-11-06T14:02:32.561950Z","iopub.status.idle":"2023-11-06T14:02:32.576072Z","shell.execute_reply.started":"2023-11-06T14:02:32.561899Z","shell.execute_reply":"2023-11-06T14:02:32.574552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = get_mnist_model()\nmodel.compile(optimizer=\"rmsprop\",\n              loss='sparse_categorical_crossentropy',\n              metrics=[\"accuracy\"])\nhistory_model = model.fit(\n    train_images, train_labels,\n    epochs=40,\n    batch_size=1024,\n    callbacks = callbacks('mlp_reg'),\n    validation_data=(val_images, val_labels))","metadata":{"execution":{"iopub.status.busy":"2023-11-06T14:01:29.010713Z","iopub.execute_input":"2023-11-06T14:01:29.011130Z","iopub.status.idle":"2023-11-06T14:02:32.546606Z","shell.execute_reply.started":"2023-11-06T14:01:29.011099Z","shell.execute_reply":"2023-11-06T14:02:32.545608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(history_model.history[\"val_loss\"])","metadata":{"execution":{"iopub.status.busy":"2023-11-06T13:49:49.699261Z","iopub.execute_input":"2023-11-06T13:49:49.699685Z","iopub.status.idle":"2023-11-06T13:49:49.708370Z","shell.execute_reply.started":"2023-11-06T13:49:49.699652Z","shell.execute_reply":"2023-11-06T13:49:49.706580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport matplotlib.pyplot as plt\nval_loss = history_model.history[\"val_loss\"]\nepochs = range(1, len(val_loss) + 1)\nplt.plot(epochs, val_loss, \"b--\",\n         label=\"Validation loss\")\nplt.title(\"Effect of insufficient model capacity on validation loss\")\nplt.xlabel(\"Epochs\")\nplt.ylabel(\"Loss\")\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2023-11-06T14:02:43.030602Z","iopub.execute_input":"2023-11-06T14:02:43.031209Z","iopub.status.idle":"2023-11-06T14:02:43.411838Z","shell.execute_reply.started":"2023-11-06T14:02:43.031148Z","shell.execute_reply":"2023-11-06T14:02:43.410656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score = model.evaluate(val_images, val_labels, verbose = 0)\nprint('Test loss: {}%'.format(score[0] * 100))\nprint('Test score: {}%'.format(score[1] * 100))\n\nprint(\"MLP Error: %.2f%%\" % (100 - score[1] * 100))","metadata":{"execution":{"iopub.status.busy":"2023-11-06T14:02:53.751746Z","iopub.execute_input":"2023-11-06T14:02:53.752172Z","iopub.status.idle":"2023-11-06T14:02:54.472385Z","shell.execute_reply.started":"2023-11-06T14:02:53.752138Z","shell.execute_reply":"2023-11-06T14:02:54.470777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test_metrics = model.evaluate(test_images, test_labels)\npredictions = model.predict(test_images)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef prediction_to_dataframe(predictions):\n    result = pd.DataFrame({\n            \"Label\": predictions.argmax(axis=1)\n        })\n    result.index += 1\n    result.index.name = \"ImageId\"\n    return result\n\ndef make_submission(predictions_dataframe):\n    path=\"/kaggle/working/submission.csv\"\n    predictions_dataframe.to_csv(path)\n    print(f\"Submission exported to {path}\")\n    \npredictions_dataframe = prediction_to_dataframe(predictions)\nmake_submission(predictions_dataframe)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-11-05T20:06:52.017943Z","iopub.execute_input":"2023-11-05T20:06:52.018282Z","iopub.status.idle":"2023-11-05T20:06:52.055248Z","shell.execute_reply.started":"2023-11-05T20:06:52.018256Z","shell.execute_reply":"2023-11-05T20:06:52.053932Z"},"trusted":true},"execution_count":null,"outputs":[]}]}