{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30673,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# PyTorch NN\n## Распознавание рукописных символов","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nimport torch\n\n# модуль, где определены слои для нейронных сетей\nimport torch.nn as nn","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-03-31T17:07:06.020244Z","iopub.execute_input":"2024-03-31T17:07:06.021979Z","iopub.status.idle":"2024-03-31T17:07:06.028826Z","shell.execute_reply.started":"2024-03-31T17:07:06.021928Z","shell.execute_reply":"2024-03-31T17:07:06.027692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntrain","metadata":{"execution":{"iopub.status.busy":"2024-03-31T17:07:06.030947Z","iopub.execute_input":"2024-03-31T17:07:06.031868Z","iopub.status.idle":"2024-03-31T17:07:09.500569Z","shell.execute_reply.started":"2024-03-31T17:07:06.031833Z","shell.execute_reply":"2024-03-31T17:07:09.499221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pixels = np.array(train.iloc[123][1:])\npixels = pixels.reshape((28, 28))\nprint(pixels.shape)\nplt.imshow(pixels, cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2024-03-31T17:07:09.502217Z","iopub.execute_input":"2024-03-31T17:07:09.502690Z","iopub.status.idle":"2024-03-31T17:07:09.810554Z","shell.execute_reply.started":"2024-03-31T17:07:09.502651Z","shell.execute_reply":"2024-03-31T17:07:09.809411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train = np.array(train.drop(columns=['label'])) / 255\ny_train = np.array(train['label'])\n\nX_train, X_test, y_train, y_test = train_test_split(X_train, y_train, test_size=0.2)\n\nprint(f'X_train.shape: {X_train.shape}')\nprint(f'y_train.shape: {y_train.shape}')\nprint(f'X_test.shape: {X_test.shape}')","metadata":{"execution":{"iopub.status.busy":"2024-03-31T17:07:09.812346Z","iopub.execute_input":"2024-03-31T17:07:09.815628Z","iopub.status.idle":"2024-03-31T17:07:10.760981Z","shell.execute_reply.started":"2024-03-31T17:07:09.815588Z","shell.execute_reply":"2024-03-31T17:07:10.759927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\n# определяем веса\nx0 = np.random.normal(size=(784, 10)) / np.sqrt(784)\nneurons = torch.tensor(x0, requires_grad=True) # requires_grad говорит о том что для тензора необходимо вычислить градиент \n\n# конвертируем датасет в читаймый торчом формат\nX_tensor_train = torch.tensor(X_train)\ny_tensor_train = torch.tensor(y_train)\n\n# SGD\noptimizer = torch.optim.SGD(params=[neurons], lr=1)\nloss_history = []\n\niterCount = 100\n\nfor i in range(iterCount):\n    predictions = X_tensor_train @ neurons\n    \n    # вычисляем loss\n    loss = torch.nn.functional.cross_entropy(predictions, y_tensor_train) # функция активации уже включена\n    \n    # вычисляем градиент\n    optimizer.zero_grad() # зануляем предыдущий градиент\n    loss.backward() # вычисляем градиент\n    \n    # делаем шаг\n    optimizer.step() # корректируем веса в соотвествии с градиентом\n    \n    # логируем loss\n    loss_history.append(loss.data.numpy())","metadata":{"execution":{"iopub.status.busy":"2024-03-31T17:07:10.764330Z","iopub.execute_input":"2024-03-31T17:07:10.764859Z","iopub.status.idle":"2024-03-31T17:07:19.309330Z","shell.execute_reply.started":"2024-03-31T17:07:10.764827Z","shell.execute_reply":"2024-03-31T17:07:19.308365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.title('Bстория тренировки')\nplt.ylabel('loss')\nplt.xlabel('step')\nplt.plot(loss_history)","metadata":{"execution":{"iopub.status.busy":"2024-03-31T17:07:19.310535Z","iopub.execute_input":"2024-03-31T17:07:19.311277Z","iopub.status.idle":"2024-03-31T17:07:19.665785Z","shell.execute_reply.started":"2024-03-31T17:07:19.311242Z","shell.execute_reply":"2024-03-31T17:07:19.662779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(1, 10, figsize=(30, 30))\n\nfor i in range(10):\n    axes[i].set_title(i)\n    neuron_img = neurons[:, i].data.numpy().reshape(28, 28)\n    axes[i].imshow(neuron_img)","metadata":{"execution":{"iopub.status.busy":"2024-03-31T17:07:19.667417Z","iopub.execute_input":"2024-03-31T17:07:19.667871Z","iopub.status.idle":"2024-03-31T17:07:21.753736Z","shell.execute_reply.started":"2024-03-31T17:07:19.667830Z","shell.execute_reply":"2024-03-31T17:07:21.752437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import f1_score\n\ndef sigmoid(x):\n    ex = np.exp(x)\n    return ex / (1 + ex)\n\ndigit_probabilities_train = sigmoid((X_tensor_train @ neurons).data.numpy())\n\npredictions_train = np.argmax(digit_probabilities_train, axis=1)\nf1_train = f1_score(y_train, predictions_train, average='macro')\nprint(f'Train f1: {f1_train}')","metadata":{"execution":{"iopub.status.busy":"2024-03-31T17:07:21.755177Z","iopub.execute_input":"2024-03-31T17:07:21.755517Z","iopub.status.idle":"2024-03-31T17:07:21.839593Z","shell.execute_reply.started":"2024-03-31T17:07:21.755488Z","shell.execute_reply":"2024-03-31T17:07:21.838394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_tensor_test = torch.tensor(X_test)\ny_tensor_test = torch.tensor(y_test)\n\ndigit_probabilities_test = sigmoid((X_tensor_test @ neurons).data.numpy())\n\npredictions_test = np.argmax(digit_probabilities_test, axis=1)\nf1_test = f1_score(y_test, predictions_test, average='macro')\nprint(f'Test f1: {f1_test}')","metadata":{"execution":{"iopub.status.busy":"2024-03-31T17:07:21.840965Z","iopub.execute_input":"2024-03-31T17:07:21.841298Z","iopub.status.idle":"2024-03-31T17:07:21.872894Z","shell.execute_reply.started":"2024-03-31T17:07:21.841269Z","shell.execute_reply":"2024-03-31T17:07:21.871907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# модуль, где определены функции активации для слоев нейронных сетей\nimport torch.nn.functional as F\n\nclass FirstNN(nn.Module):\n    def __init__(self, n_hidden_neurons=200):\n        super().__init__()\n        \n        # Здесь мы определяем обучаемые параметры модели\n        # Первый слой весов\n        # init_1 = np.random.normal(size=(784, n_hidden_neurons)) / np.sqrt(784) # гуглите \"xavier initialization\"\n        # self.neurons_layer1 = torch.tensor(init_1, requires_grad=True) # веса для первого слоя нейронов\n        # self.neurons_layer1 = nn.Parameter(self.neurons_layer1)\n        self.neurons_layer1 = nn.Linear(784, n_hidden_neurons)\n        \n        # веса для второго слоя нейронов\n        # init_2 = np.random.normal(size=(n_hidden_neurons, 10)) / np.sqrt(n_hidden_neurons)\n        # self.neurons_layer2 = torch.tensor(init_2, requires_grad=True)\n        # self.neurons_layer2 = nn.Parameter(self.neurons_layer2)\n        self.neurons_layer2 = nn.Linear(n_hidden_neurons, 10)\n        \n    def forward(self, x):\n        # Здесь мы делаем все вычисления\n        # Первый слой\n        # h = x @ self.neurons_layer1\n        h = self.neurons_layer1(x)\n        \n        # Функция активации скрытого слоя\n        # h = torch.relu(h)\n        h = F.relu(h)\n        \n        # Выходной слой\n        # out = h @ self.neurons_layer2\n        out = self.neurons_layer2(h)\n        return out","metadata":{"execution":{"iopub.status.busy":"2024-03-31T17:07:21.874620Z","iopub.execute_input":"2024-03-31T17:07:21.874959Z","iopub.status.idle":"2024-03-31T17:07:21.884497Z","shell.execute_reply.started":"2024-03-31T17:07:21.874930Z","shell.execute_reply":"2024-03-31T17:07:21.883234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = FirstNN()\noptimizer = torch.optim.Adam(params=model.parameters(), lr=0.001)","metadata":{"execution":{"iopub.status.busy":"2024-03-31T17:07:21.886105Z","iopub.execute_input":"2024-03-31T17:07:21.886745Z","iopub.status.idle":"2024-03-31T17:07:21.906114Z","shell.execute_reply.started":"2024-03-31T17:07:21.886711Z","shell.execute_reply":"2024-03-31T17:07:21.904586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm\n\nN_EPOCHS = 50\n\nX_tensor_train = torch.tensor(X_train, dtype=torch.float32)\ny_tensor_train = torch.tensor(y_train, dtype=torch.long)\n\nX_tensor_test = torch.tensor(X_test, dtype=torch.float32)\ny_tensor_test = torch.tensor(y_test, dtype=torch.long)\n\ntrain_loss_history = []\ntest_loss_history = []\n\nfor i in tqdm(range(N_EPOCHS)):\n    # MyNN.forward()\n    predictions = model(X_tensor_train)\n    \n    loss = torch.nn.functional.cross_entropy(predictions, y_tensor_train) # активация уже включена\n    \n    # Градиенты\n    optimizer.zero_grad()\n    loss.backward()\n    \n    # Шаг спуска\n    optimizer.step()\n    train_loss_history.append(loss.item())\n    \n    # Валидируемся\n    predictions = model(X_tensor_test)\n    loss = torch.nn.functional.cross_entropy(predictions, y_tensor_test)\n    test_loss_history.append(loss.item())","metadata":{"execution":{"iopub.status.busy":"2024-03-31T17:07:21.908631Z","iopub.execute_input":"2024-03-31T17:07:21.909443Z","iopub.status.idle":"2024-03-31T17:07:35.474000Z","shell.execute_reply.started":"2024-03-31T17:07:21.909406Z","shell.execute_reply":"2024-03-31T17:07:35.472634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(16, 8))\nplt.title('Training history')\nplt.ylabel('loss')\nplt.xlabel('step')\nplt.plot(range(N_EPOCHS), train_loss_history, label='train_loss')\nplt.plot(range(N_EPOCHS), test_loss_history, label='test_loss')\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2024-03-31T17:07:35.475707Z","iopub.execute_input":"2024-03-31T17:07:35.476116Z","iopub.status.idle":"2024-03-31T17:07:35.932293Z","shell.execute_reply.started":"2024-03-31T17:07:35.476084Z","shell.execute_reply":"2024-03-31T17:07:35.930582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report\n\ndigit_probabilities = model(X_tensor_test).detach().numpy()\npredictions = np.argmax(digit_probabilities, axis=1)\nprint(classification_report(y_tensor_test.detach().cpu().numpy(), predictions))","metadata":{"execution":{"iopub.status.busy":"2024-03-31T17:07:35.937613Z","iopub.execute_input":"2024-03-31T17:07:35.937993Z","iopub.status.idle":"2024-03-31T17:07:36.003508Z","shell.execute_reply.started":"2024-03-31T17:07:35.937962Z","shell.execute_reply":"2024-03-31T17:07:36.001942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.nn import ReLU\n\nclass SecondNN(nn.Module):\n    def __init__(self, n_hidden_neurons=200):\n        super().__init__()\n        \n        self.linear1 = nn.Linear(784, 256)\n        self.linear2 = nn.Linear(256, n_hidden_neurons)\n        self.linear3 = nn.Linear(n_hidden_neurons, n_hidden_neurons)\n        self.linear4 = nn.Linear(n_hidden_neurons, 10)\n        \n    def forward(self, x):\n        x = F.relu(self.linear1(x))\n        x = F.relu(self.linear2(x))\n        x = F.relu(self.linear3(x))\n        x = self.linear4(x)\n        \n        return x","metadata":{"execution":{"iopub.status.busy":"2024-03-31T17:07:36.005064Z","iopub.execute_input":"2024-03-31T17:07:36.007899Z","iopub.status.idle":"2024-03-31T17:07:36.016991Z","shell.execute_reply.started":"2024-03-31T17:07:36.007853Z","shell.execute_reply":"2024-03-31T17:07:36.015507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = SecondNN()\n\n# инициализация Adam для накопления импульса во время спуска\noptimizer = torch.optim.Adam(params=model.parameters(), lr=0.001)","metadata":{"execution":{"iopub.status.busy":"2024-03-31T17:07:36.018802Z","iopub.execute_input":"2024-03-31T17:07:36.019309Z","iopub.status.idle":"2024-03-31T17:07:36.038294Z","shell.execute_reply.started":"2024-03-31T17:07:36.019265Z","shell.execute_reply":"2024-03-31T17:07:36.036677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"N_EPOCHS = 100\n\nX_tensor_train = torch.tensor(X_train, dtype=torch.float32)\ny_tensor_train = torch.tensor(y_train, dtype=torch.long)\nX_tensor_test = torch.tensor(X_test, dtype=torch.float32)\ny_tensor_test = torch.tensor(y_test, dtype=torch.long)\n\ntrain_loss_history = []\ntest_loss_history = []\n\nce = nn.CrossEntropyLoss()\n\nfor i in tqdm(range(N_EPOCHS)):\n    predictions = model(X_tensor_train)\n    loss = ce(predictions, y_tensor_train)\n    optimizer.zero_grad()\n    loss.backward()\n    \n    optimizer.step()\n    train_loss_history.append(loss.item())\n    \n    predictions = model(X_tensor_test)\n    loss = ce(predictions, y_tensor_test)\n    test_loss_history.append(loss.item())\n","metadata":{"execution":{"iopub.status.busy":"2024-03-31T17:07:36.040444Z","iopub.execute_input":"2024-03-31T17:07:36.040884Z","iopub.status.idle":"2024-03-31T17:08:30.965072Z","shell.execute_reply.started":"2024-03-31T17:07:36.040851Z","shell.execute_reply":"2024-03-31T17:08:30.963902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 8))\nplt.title(\"Training history\")\nplt.ylabel('loss')\nplt.xlabel('step')\nplt.plot(range(N_EPOCHS), train_loss_history, label=\"train loss\")\nplt.plot(range(N_EPOCHS), test_loss_history, label=\"test loss\")\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2024-03-31T17:08:30.966287Z","iopub.execute_input":"2024-03-31T17:08:30.967565Z","iopub.status.idle":"2024-03-31T17:08:31.301660Z","shell.execute_reply.started":"2024-03-31T17:08:30.967502Z","shell.execute_reply":"2024-03-31T17:08:31.300373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Классификация изображений","metadata":{}},{"cell_type":"code","source":"from torchvision import datasets, transforms\nfrom sklearn.metrics import accuracy_score\n\ntransform = transforms.Compose([\n    transforms.ToTensor()\n])","metadata":{"execution":{"iopub.status.busy":"2024-03-31T17:08:31.303565Z","iopub.execute_input":"2024-03-31T17:08:31.304332Z","iopub.status.idle":"2024-03-31T17:08:31.311472Z","shell.execute_reply.started":"2024-03-31T17:08:31.304288Z","shell.execute_reply":"2024-03-31T17:08:31.310337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = datasets.CIFAR10(root=\"/kaggle/working/data\", train=True, download=True, transform=transform)\ntest_data = datasets.CIFAR10(root=\"/kaggle/working/data\", train=False, download=True, transform=transform)","metadata":{"execution":{"iopub.status.busy":"2024-03-31T17:08:31.312972Z","iopub.execute_input":"2024-03-31T17:08:31.313300Z","iopub.status.idle":"2024-03-31T17:08:33.205185Z","shell.execute_reply.started":"2024-03-31T17:08:31.313270Z","shell.execute_reply":"2024-03-31T17:08:33.203914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loader = torch.utils.data.DataLoader(train_data, batch_size=16, shuffle=True, pin_memory=True, num_workers=2)\ntest_loader = torch.utils.data.DataLoader(test_data, batch_size=16, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2024-03-31T17:08:33.206690Z","iopub.execute_input":"2024-03-31T17:08:33.207028Z","iopub.status.idle":"2024-03-31T17:08:33.213894Z","shell.execute_reply.started":"2024-03-31T17:08:33.206999Z","shell.execute_reply":"2024-03-31T17:08:33.212490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataiter = iter(train_loader)\n\nimages, labels = next(dataiter)\nimages.shape, labels.shape","metadata":{"execution":{"iopub.status.busy":"2024-03-31T17:08:33.215404Z","iopub.execute_input":"2024-03-31T17:08:33.216496Z","iopub.status.idle":"2024-03-31T17:08:33.364094Z","shell.execute_reply.started":"2024-03-31T17:08:33.216450Z","shell.execute_reply":"2024-03-31T17:08:33.362669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_imgs(imgs, labels):\n    f, axes = plt.subplots(1, 10, figsize=(30, 5))\n    for i, axis in enumerate(axes):\n        axes[i].imshow(np.transpose(imgs[i].numpy(), (1, 2, 0)))\n        axes[i].set_title(labels[i].numpy())\n\nshow_imgs(images, labels)","metadata":{"execution":{"iopub.status.busy":"2024-03-31T17:08:33.367089Z","iopub.execute_input":"2024-03-31T17:08:33.367838Z","iopub.status.idle":"2024-03-31T17:08:36.055219Z","shell.execute_reply.started":"2024-03-31T17:08:33.367777Z","shell.execute_reply":"2024-03-31T17:08:36.053820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_to_name = {\n    0: \"Самолет\",\n    1: \"Автомобиль\",\n    2: \"Птица\",\n    3: \"Кошка\",\n    4: \"Олень\",\n    5: \"Собака\",\n    6: \"Лягушка\",\n    7: \"Лошадь\",\n    8: \"Корабль\",\n    9: \"Грузовик\"\n}","metadata":{"execution":{"iopub.status.busy":"2024-03-31T17:08:36.057036Z","iopub.execute_input":"2024-03-31T17:08:36.057433Z","iopub.status.idle":"2024-03-31T17:08:36.064531Z","shell.execute_reply.started":"2024-03-31T17:08:36.057401Z","shell.execute_reply":"2024-03-31T17:08:36.063210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# класс для удобного первода картинок из двумерного объекта в вектор\nclass Flatten(nn.Module):\n    def forward(self, input):\n        return input.view(input.size(0), -1)\n    \nclass SimpleNet(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.flatten = Flatten()\n        self.fc1 = nn.Linear(32*32*3, 256) # полносвязные слои - вход и выход\n        self.fc2 = nn.Linear(256, 10)\n        \n    def forward(self, x):\n        # переводим входной объект из картинки в вектор\n        x = self.flatten(x)\n        \n        # умножение на матрицу весов 1 слоя и применение функции активации\n        x = F.relu(self.fc1(x))\n        \n        # умножение на матрицу весов 2 слоя\n        x = self.fc2(x)\n        return x","metadata":{"execution":{"iopub.status.busy":"2024-03-31T17:08:36.066257Z","iopub.execute_input":"2024-03-31T17:08:36.066704Z","iopub.status.idle":"2024-03-31T17:08:36.079575Z","shell.execute_reply.started":"2024-03-31T17:08:36.066665Z","shell.execute_reply":"2024-03-31T17:08:36.078404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm.notebook import tqdm\n\ndef train(model, n_epoch=5, lr = 1e-3):\n    loss_fn = torch.nn.CrossEntropyLoss()\n    \n    # алгоритм оптимизации\n    optimizer = torch.optim.Adam(model.parameters(), lr=lr)\n    best_accuracy = 0\n    \n    for epoch in tqdm(range(n_epoch)):\n        running_loss = 0.0\n        train_dataiter = iter(train_loader)\n        \n        for i, batch in enumerate(tqdm(train_dataiter)):\n            X_batch, y_batch = batch # получаем текущий батч\n            optimizer.zero_grad() # обнуляем градиент\n            \n            # forward pass\n            y_pred = model(X_batch)\n            loss = loss_fn(y_pred, y_batch)\n            \n            loss.backward()\n            optimizer.step()\n            \n            running_loss += loss.item()\n            \n            if i % 500 == 499:\n                print('[%d, %5d] loss: %.3f' % (epoch + 1, i + 1, running_loss / 500))\n                running_loss = 0.0\n            \n        with torch.no_grad():\n            accuracy = []\n            for batch in test_loader:\n                x, y = batch\n                y_pred = model(x)\n\n                accuracy.append(accuracy_score(y.detach().numpy(), np.argmax(y_pred.detach().numpy(), axis=1)))\n            \n            accuracy = np.mean(np.array(accuracy))\n            if accuracy > best_accuracy:\n                print(f'New best model with test acc: {accuracy}')\n                torch.save(model.state_dict(), '/kaggle/working/best_model.pt')\n                best_accuracy = accuracy\n        \n    print('Обучение закончено')\n    return model","metadata":{"execution":{"iopub.status.busy":"2024-03-31T17:10:53.938590Z","iopub.execute_input":"2024-03-31T17:10:53.939243Z","iopub.status.idle":"2024-03-31T17:10:53.952059Z","shell.execute_reply.started":"2024-03-31T17:10:53.939209Z","shell.execute_reply":"2024-03-31T17:10:53.951006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = SimpleNet()\nmodel = train(model)","metadata":{"execution":{"iopub.status.busy":"2024-03-31T17:11:05.289117Z","iopub.execute_input":"2024-03-31T17:11:05.289536Z","iopub.status.idle":"2024-03-31T17:15:05.281762Z","shell.execute_reply.started":"2024-03-31T17:11:05.289505Z","shell.execute_reply":"2024-03-31T17:15:05.280302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = SimpleNet()\nmodel.load_state_dict(torch.load('/kaggle/working/best_model.pt'))\nmodel.eval()","metadata":{"execution":{"iopub.status.busy":"2024-03-31T17:15:09.888724Z","iopub.execute_input":"2024-03-31T17:15:09.889619Z","iopub.status.idle":"2024-03-31T17:15:09.910269Z","shell.execute_reply.started":"2024-03-31T17:15:09.889574Z","shell.execute_reply":"2024-03-31T17:15:09.909427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with torch.no_grad():\n    accuracy = []\n    for batch in test_loader:\n        x, y = batch\n        y_pred = model(x)\n        accuracy.append(accuracy_score(y.numpy(), np.argmax(y_pred.detach().numpy(), axis=1)))\n    accuracy = np.mean(np.array(accuracy))\nprint(f'accuracy: {accuracy}')","metadata":{"execution":{"iopub.status.busy":"2024-03-31T17:15:15.884839Z","iopub.execute_input":"2024-03-31T17:15:15.885538Z","iopub.status.idle":"2024-03-31T17:15:18.498200Z","shell.execute_reply.started":"2024-03-31T17:15:15.885503Z","shell.execute_reply":"2024-03-31T17:15:18.497010Z"},"trusted":true},"execution_count":null,"outputs":[]}]}