{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "id": "r997fIhZa7-A"
      },
      "outputs": [],
      "source": [
        "import os"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {
        "id": "U8PYD8odcxpc"
      },
      "outputs": [],
      "source": [
        "os.chdir('/content/drive/MyDrive/Colab Notebooks/lab2')\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "g5cp1saPc6Xa",
        "outputId": "3089c8c8-d518-48f7-ac90-262398be06cf"
      },
      "outputs": [],
      "source": [
        "# скачивание библиотеки\n",
        "!wget -N http://uit.mpei.ru/git/main/is_dnn/raw/branch/main/labworks/LW2/lab02_lib.py"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "pHg8jhkZc90x",
        "outputId": "0b42b9c8-f640-4a2a-e90b-3954ade65113"
      },
      "outputs": [],
      "source": [
        "# скачивание выборок\n",
        "!wget -N http://uit.mpei.ru/git/main/is_dnn/raw/branch/main/labworks/LW2/data/letter_train.txt"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "tQhqaB8Pc7Yi",
        "outputId": "74c6fac7-59f8-4ea9-e6f9-f6aad4196326"
      },
      "outputs": [],
      "source": [
        "!wget -N http://uit.mpei.ru/git/main/is_dnn/raw/branch/main/labworks/LW2/data/letter_test.txt"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "metadata": {
        "id": "4DfLVzP-dv_M"
      },
      "outputs": [],
      "source": [
        "#импортмодулей\n",
        "import numpy as np\n",
        "import lab02_lib as lib"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "9_66PjVYd5ch",
        "outputId": "e6ab839f-f62d-4fd8-cecb-cd02ccecc1cd"
      },
      "outputs": [],
      "source": [
        "os.getcwd()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 718
        },
        "id": "HFbXybBWeDN0",
        "outputId": "57c234a4-55c0-488d-bf1f-971fae63cbcc"
      },
      "outputs": [],
      "source": [
        "#генерациядатасета\n",
        "data=lib.datagen(5,5,1000,2)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "dZzF5IKBeKh-",
        "outputId": "6cf53296-1ed0-4a4c-d05c-ac659fea7184"
      },
      "outputs": [],
      "source": [
        "#вывод данных и размерности\n",
        "print('Исходные данные:')\n",
        "print(data)\n",
        "print('Размерность данных:')\n",
        "print(data.shape)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "9l-lC2gueRbY",
        "outputId": "43eaaa96-90ff-457b-f7ad-4cdadb6e2b73"
      },
      "outputs": [],
      "source": [
        "# обучение AE1\n",
        "patience= 300\n",
        "ae1_trained, IRE1, IREth1= lib.create_fit_save_ae(data,'out/AE1.h5','out/AE1_ire_th.txt', 1000, False, patience)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 744
        },
        "id": "e1-t84RdeyU0",
        "outputId": "f14d5576-8d67-4b9e-b736-edee255d1996"
      },
      "outputs": [],
      "source": [
        "#Построение графика ошибки реконструкции\n",
        "lib.ire_plot('training', IRE1, IREth1, 'AE1')\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 955
        },
        "id": "b94IU-MMe50Z",
        "outputId": "f03f9534-2cad-4216-e157-18c7f9368671"
      },
      "outputs": [],
      "source": [
        "# обучение AE2\n",
        "ae2_trained, IRE2, IREth2= lib.create_fit_save_ae(data,'out/AE2.h5','out/AE2_ire_th.txt', 3000, False, patience)\n",
        "lib.ire_plot('training', IRE2, IREth2, 'AE2')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 1000
        },
        "id": "6PE0WU11f0r7",
        "outputId": "bb22942d-629d-4d44-c742-98f9e372ab3e"
      },
      "outputs": [],
      "source": [
        "#построение областей покрытия и границ классов\n",
        "#расчет характеристик качестваобучения\n",
        "numb_square= 20\n",
        "xx,yy,Z1=lib.square_calc(numb_square,data,ae1_trained,IREth1,'1',True)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 1000
        },
        "id": "yfb90GShgYfv",
        "outputId": "99ac3a35-ef93-48e2-9880-100b72bfb156"
      },
      "outputs": [],
      "source": [
        "xx,yy,Z2=lib.square_calc(numb_square,data,ae2_trained,IREth2,'2',True)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 472
        },
        "id": "wYAaU-Rbgjj7",
        "outputId": "1c8fedd9-85df-4f49-805c-baa3e7b0b74d"
      },
      "outputs": [],
      "source": [
        "#сравнение характеристик качества обучения и областей аппроксимации\n",
        "lib.plot2in1(data,xx,yy,Z1,Z2)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 20,
      "metadata": {
        "id": "0oebq1QhgrZC"
      },
      "outputs": [],
      "source": [
        "#загрузка тестового набора\n",
        "data_test= np.loadtxt('data_test.txt', dtype=float)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "K_PYA6l6jEJf",
        "outputId": "17e032e7-c2df-4523-861e-93c29ebfd0c1"
      },
      "outputs": [],
      "source": [
        "#тестирование АE1\n",
        "predicted_labels1, ire1 = lib.predict_ae(ae1_trained, data_test, IREth1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 1000
        },
        "id": "aWXqWrJQjObc",
        "outputId": "2cc638cd-d355-4c61-ab6d-0dc19de94af5"
      },
      "outputs": [],
      "source": [
        "#тестирование АE1\n",
        "lib.anomaly_detection_ae(predicted_labels1, ire1, IREth1)\n",
        "lib.ire_plot('test', ire1, IREth1, 'AE1')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Of4-wpFDj8kn",
        "outputId": "eb148cc7-50a9-4efd-be97-16753abddb90"
      },
      "outputs": [],
      "source": [
        "#тестирование АE2\n",
        "predicted_labels2, ire2 = lib.predict_ae(ae2_trained, data_test, IREth2)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 1000
        },
        "id": "2E9N8TSKkDUZ",
        "outputId": "1a5d6cd2-d237-47ec-ab17-bb0454c4d6ff"
      },
      "outputs": [],
      "source": [
        "#тестирование АE2\n",
        "lib.anomaly_detection_ae(predicted_labels2, ire2, IREth2)\n",
        "lib.ire_plot('test', ire2, IREth2, 'AE2')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 472
        },
        "id": "bdxVRsSMkQet",
        "outputId": "14f3ccbb-e1b0-4911-c0ad-ad5f41c118a1"
      },
      "outputs": [],
      "source": [
        "#построение областей аппроксимации и точек тестового набора\n",
        "lib.plot2in1_anomaly(data, xx, yy, Z1, Z2, data_test)\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 27,
      "metadata": {
        "id": "m9x03V--uEl6"
      },
      "outputs": [],
      "source": [
        "#загрузка выборок\n",
        "train= np.loadtxt('letter_train.txt', dtype=float)\n",
        "test = np.loadtxt('letter_test.txt', dtype=float)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 1000
        },
        "id": "vYlzC3RKucGi",
        "outputId": "95cf1135-6d97-4fc7-9e45-99d43ba82044"
      },
      "outputs": [],
      "source": [
        "# обучение AE3\n",
        "patience=5000\n",
        "ae3_trained, IRE3, IREth3= lib.create_fit_save_ae(train,'out/AE3.h5','out/AE3_ire_th.txt', 100000, False, patience)\n",
        "lib.ire_plot('training', IRE3, IREth3, 'AE3')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "pfcKYERV3c5-",
        "outputId": "e486caf8-67ab-4255-85b4-91c852bb4eef"
      },
      "outputs": [],
      "source": [
        "#тестирование АE3\n",
        "predicted_labels3, ire3 = lib.predict_ae(ae3_trained, test, IREth3)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 1000
        },
        "id": "QCwzSFvU3net",
        "outputId": "161a8c60-c332-48d2-a082-dfce1332c011"
      },
      "outputs": [],
      "source": [
        "#тестированиеАE3\n",
        "lib.anomaly_detection_ae(predicted_labels3, ire3, IREth3)\n",
        "lib.ire_plot('test', ire3, IREth3, 'AE3')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 1000
        },
        "id": "m_ZsgfDo41Wj",
        "outputId": "d0309f17-5878-4fc7-ce54-d9db48292d3b"
      },
      "outputs": [],
      "source": [
        "# обучение AE3.2\n",
        "patience=5000\n",
        "ae32_trained, IRE32, IREth32= lib.create_fit_save_ae(train,'out/AE32.h5','out/AE32_ire_th.txt', 100000, False, patience)\n",
        "lib.ire_plot('training', IRE32, IREth32, 'AE32')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Jp7OVlnqBDtI",
        "outputId": "c89e7c86-271a-4c95-ae2c-10c7e8d44f62"
      },
      "outputs": [],
      "source": [
        "#тестирование АE32\n",
        "predicted_labels32, ire32 = lib.predict_ae(ae32_trained, test, IREth32)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 1000
        },
        "id": "QGhswhfsBI49",
        "outputId": "ce8d2bf4-e500-42ef-f25b-cdde7d8c9cfe"
      },
      "outputs": [],
      "source": [
        "#тестированиеАE32\n",
        "lib.anomaly_detection_ae(predicted_labels32, ire32, IREth32)\n",
        "lib.ire_plot('test', ire32, IREth32, 'AE32')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 1000
        },
        "id": "xcBsBGB_BqSs",
        "outputId": "cfa85dff-a0a5-4b30-ec94-adf4e4fc9ae7"
      },
      "outputs": [],
      "source": [
        "# обучение AE3.3\n",
        "patience=5000\n",
        "ae33_trained, IRE33, IREth33= lib.create_fit_save_ae(train,'out/AE33.h5','out/AE33_ire_th.txt', 100000, False, patience)\n",
        "lib.ire_plot('training', IRE33, IREth33, 'AE33')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "vie1zEOTQgKO",
        "outputId": "3176a42d-2edc-4866-c6ed-88fcc29a8b4e"
      },
      "outputs": [],
      "source": [
        "#тестирование АE33\n",
        "predicted_labels33, ire33 = lib.predict_ae(ae33_trained, test, IREth33)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 761
        },
        "id": "5LA0JPraQkIp",
        "outputId": "85f0c50b-8223-4c95-84e4-675733fe61e2"
      },
      "outputs": [],
      "source": [
        "#тестированиеАE33\n",
        "lib.anomaly_detection_ae(predicted_labels33, ire33, IREth33)\n",
        "lib.ire_plot('test', ire33, IREth33, 'AE33')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 1000
        },
        "id": "Xk9QG5I9Qrc-",
        "outputId": "c978348f-cb4e-49f0-d863-8d03c19947fc"
      },
      "outputs": [],
      "source": [
        "# обучение AE3.4\n",
        "patience=5000\n",
        "ae34_trained, IRE34, IREth34=lib.create_fit_save_ae(train,'out/AE34.h5','out/AE34_ire_th.txt', 100000, False, patience)\n",
        "lib.ire_plot('training', IRE34, IREth34, 'AE34')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 1000
        },
        "id": "EWRa-rSVbGmx",
        "outputId": "0241dc58-7e85-4a7b-fb45-37c85f5e4e34"
      },
      "outputs": [],
      "source": [
        "#тестированиеАE34\n",
        "lib.anomaly_detection_ae(predicted_labels34, ire34, IREth34)\n",
        "lib.ire_plot('test', ire34, IREth34, 'AE34')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "GVODHJ6XbBLU",
        "outputId": "e12cc3e2-e6d6-4384-c72a-6e48f2f1111d"
      },
      "outputs": [],
      "source": [
        "#тестирование АE34\n",
        "predicted_labels34, ire34 = lib.predict_ae(ae34_trained, test, IREth34)"
      ]
    }
  ],
  "metadata": {
    "accelerator": "GPU",
    "colab": {
      "gpuType": "T4",
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
