{"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-10-05T04:46:38.267684Z","iopub.execute_input":"2023-10-05T04:46:38.268024Z","iopub.status.idle":"2023-10-05T04:46:38.276508Z","shell.execute_reply.started":"2023-10-05T04:46:38.267991Z","shell.execute_reply":"2023-10-05T04:46:38.275432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Loaders","metadata":{}},{"cell_type":"code","source":"import torch\ndevice = (\n    \"cuda\"\n    if torch.cuda.is_available()\n    else \"mps\"\n    if torch.backends.mps.is_available()\n    else \"cpu\"\n)\nprint(f\"Using {device} device\")","metadata":{"execution":{"iopub.status.busy":"2023-10-05T04:46:38.278306Z","iopub.execute_input":"2023-10-05T04:46:38.281592Z","iopub.status.idle":"2023-10-05T04:46:38.288545Z","shell.execute_reply.started":"2023-10-05T04:46:38.281569Z","shell.execute_reply":"2023-10-05T04:46:38.287566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np","metadata":{"execution":{"iopub.status.busy":"2023-10-05T04:46:38.289786Z","iopub.execute_input":"2023-10-05T04:46:38.290617Z","iopub.status.idle":"2023-10-05T04:46:38.297535Z","shell.execute_reply.started":"2023-10-05T04:46:38.290588Z","shell.execute_reply":"2023-10-05T04:46:38.296556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport torch\nfrom torch import nn\nfrom torch.utils.data import DataLoader, Dataset\nfrom torchvision import datasets, transforms\nimport torchvision","metadata":{"execution":{"iopub.status.busy":"2023-10-05T04:46:38.299663Z","iopub.execute_input":"2023-10-05T04:46:38.300387Z","iopub.status.idle":"2023-10-05T04:46:38.308884Z","shell.execute_reply.started":"2023-10-05T04:46:38.300357Z","shell.execute_reply":"2023-10-05T04:46:38.308037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn","metadata":{"execution":{"iopub.status.busy":"2023-10-05T04:46:38.310350Z","iopub.execute_input":"2023-10-05T04:46:38.310701Z","iopub.status.idle":"2023-10-05T04:46:38.325478Z","shell.execute_reply.started":"2023-10-05T04:46:38.310672Z","shell.execute_reply":"2023-10-05T04:46:38.324616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd","metadata":{"execution":{"iopub.status.busy":"2023-10-05T04:46:38.326811Z","iopub.execute_input":"2023-10-05T04:46:38.327156Z","iopub.status.idle":"2023-10-05T04:46:38.338648Z","shell.execute_reply.started":"2023-10-05T04:46:38.327096Z","shell.execute_reply":"2023-10-05T04:46:38.337868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/Kannada-MNIST/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/Kannada-MNIST/test.csv\")\nval = pd.read_csv(\"/kaggle/input/Kannada-MNIST/Dig-MNIST.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-10-05T04:46:38.339589Z","iopub.execute_input":"2023-10-05T04:46:38.340715Z","iopub.status.idle":"2023-10-05T04:46:41.345352Z","shell.execute_reply.started":"2023-10-05T04:46:38.340685Z","shell.execute_reply":"2023-10-05T04:46:41.344312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tr_label = train[\"label\"]\ntr_pixel_vals = train.drop(\"label\",axis = 1)\n\ntst_label = test[\"id\"]\ntst_pixel_vals = test.drop(\"id\", axis = 1)\n\nv_label = val['label']\nv_pixel_vals = val.drop(\"label\",axis = 1)","metadata":{"execution":{"iopub.status.busy":"2023-10-05T04:46:41.346829Z","iopub.execute_input":"2023-10-05T04:46:41.347189Z","iopub.status.idle":"2023-10-05T04:46:41.496520Z","shell.execute_reply.started":"2023-10-05T04:46:41.347158Z","shell.execute_reply":"2023-10-05T04:46:41.495325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nfrom torch.utils.data import Dataset\nimport matplotlib.pyplot as plt\n\nclass CustomImageDataset(Dataset):\n    def __init__(self, labels, pixel_vals, transform=None, target_transform=None):\n        self.labels = labels\n        self.pixels = pixel_vals\n        self.transform = transform\n        self.target_transform = target_transform\n\n    def __len__(self):\n        return len(self.labels)\n\n    def __getitem__(self, idx):\n        pixel_values = self.pixels[idx].astype(float).reshape(28, 28).astype(np.float32)  # Cast to float32\n        label = self.labels[idx]  # Assuming the label is in the second column\n\n        if self.transform:\n            pixel_values = self.transform(pixel_values)\n\n        if self.target_transform:\n            label = self.target_transform(label)\n\n        return pixel_values[None, :, :], label  # Add an extra dimension for the channel (1)\n\n\nTrain = CustomImageDataset(tr_label, tr_pixel_vals.values)\nTest = CustomImageDataset(tst_label, tst_pixel_vals.values)\nVal = CustomImageDataset(v_label, v_pixel_vals.values)","metadata":{"execution":{"iopub.status.busy":"2023-10-05T04:46:41.500375Z","iopub.execute_input":"2023-10-05T04:46:41.500769Z","iopub.status.idle":"2023-10-05T04:46:41.510148Z","shell.execute_reply.started":"2023-10-05T04:46:41.500743Z","shell.execute_reply":"2023-10-05T04:46:41.509170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(3, 3, figsize=(8, 8))\n\nfor idx in range(9):\n    pixel_values, label = Train[idx]\n    image = pixel_values.squeeze()  \n\n    row_idx = idx // 3\n    col_idx = idx % 3\n    axes[row_idx, col_idx].imshow(image, cmap='gray')  # Specify the colormap\n    axes[row_idx, col_idx].set_title(f\"Label: {label}\")\n    axes[row_idx, col_idx].axis('off')\n\nplt.tight_layout()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-05T04:46:41.512584Z","iopub.execute_input":"2023-10-05T04:46:41.513467Z","iopub.status.idle":"2023-10-05T04:46:42.200131Z","shell.execute_reply.started":"2023-10-05T04:46:41.513434Z","shell.execute_reply":"2023-10-05T04:46:42.199252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import DataLoader\n\ntrain_dataloader = DataLoader(Train, batch_size=32, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2023-10-05T04:46:42.201489Z","iopub.execute_input":"2023-10-05T04:46:42.202039Z","iopub.status.idle":"2023-10-05T04:46:42.208951Z","shell.execute_reply.started":"2023-10-05T04:46:42.202004Z","shell.execute_reply":"2023-10-05T04:46:42.208162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_dataloader = DataLoader(Val, batch_size=32, shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2023-10-05T04:46:42.210249Z","iopub.execute_input":"2023-10-05T04:46:42.210838Z","iopub.status.idle":"2023-10-05T04:46:42.223182Z","shell.execute_reply.started":"2023-10-05T04:46:42.210803Z","shell.execute_reply":"2023-10-05T04:46:42.222237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataloader = DataLoader(Test, batch_size=32, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2023-10-05T04:46:42.224846Z","iopub.execute_input":"2023-10-05T04:46:42.225208Z","iopub.status.idle":"2023-10-05T04:46:42.235351Z","shell.execute_reply.started":"2023-10-05T04:46:42.225174Z","shell.execute_reply":"2023-10-05T04:46:42.234494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Display image and label.\ntrain_features, train_labels = next(iter(train_dataloader))\nprint(f\"Feature batch shape: {train_features.size()}\")\nprint(f\"Labels batch shape: {train_labels.size()}\")\nimg = train_features[0].squeeze()\nlabel = train_labels[0]\nplt.imshow(img, cmap=\"gray\")\nplt.show()\nprint(f\"Label: {label}\")","metadata":{"execution":{"iopub.status.busy":"2023-10-05T04:46:42.236575Z","iopub.execute_input":"2023-10-05T04:46:42.237272Z","iopub.status.idle":"2023-10-05T04:46:42.465140Z","shell.execute_reply.started":"2023-10-05T04:46:42.237242Z","shell.execute_reply":"2023-10-05T04:46:42.464052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# PCA","metadata":{}},{"cell_type":"code","source":"eig_val,eig_vec = np.linalg.eig(np.array((train.drop(train.columns[0],axis = 1)).cov()))","metadata":{"execution":{"iopub.status.busy":"2023-10-05T04:46:42.466404Z","iopub.execute_input":"2023-10-05T04:46:42.467143Z","iopub.status.idle":"2023-10-05T04:46:44.628778Z","shell.execute_reply.started":"2023-10-05T04:46:42.467096Z","shell.execute_reply":"2023-10-05T04:46:44.627495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eig_vec[5]","metadata":{"execution":{"iopub.status.busy":"2023-10-05T04:46:44.630681Z","iopub.execute_input":"2023-10-05T04:46:44.631335Z","iopub.status.idle":"2023-10-05T04:46:44.664007Z","shell.execute_reply.started":"2023-10-05T04:46:44.631289Z","shell.execute_reply":"2023-10-05T04:46:44.663116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eig_val","metadata":{"execution":{"iopub.status.busy":"2023-10-05T04:46:44.665806Z","iopub.execute_input":"2023-10-05T04:46:44.666486Z","iopub.status.idle":"2023-10-05T04:46:44.701535Z","shell.execute_reply.started":"2023-10-05T04:46:44.666452Z","shell.execute_reply":"2023-10-05T04:46:44.700620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(list(eig_val))","metadata":{"execution":{"iopub.status.busy":"2023-10-05T04:46:44.703018Z","iopub.execute_input":"2023-10-05T04:46:44.703764Z","iopub.status.idle":"2023-10-05T04:46:44.715276Z","shell.execute_reply.started":"2023-10-05T04:46:44.703729Z","shell.execute_reply":"2023-10-05T04:46:44.714135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eig_val_contribution = [i/sum(eig_val) for i in eig_val]","metadata":{"execution":{"iopub.status.busy":"2023-10-05T04:46:44.716976Z","iopub.execute_input":"2023-10-05T04:46:44.723277Z","iopub.status.idle":"2023-10-05T04:46:44.793264Z","shell.execute_reply.started":"2023-10-05T04:46:44.723229Z","shell.execute_reply":"2023-10-05T04:46:44.792505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = [i for i in range(len(eig_val))]","metadata":{"execution":{"iopub.status.busy":"2023-10-05T04:46:44.796998Z","iopub.execute_input":"2023-10-05T04:46:44.797479Z","iopub.status.idle":"2023-10-05T04:46:44.801990Z","shell.execute_reply.started":"2023-10-05T04:46:44.797457Z","shell.execute_reply":"2023-10-05T04:46:44.801123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-10-05T04:46:44.803070Z","iopub.execute_input":"2023-10-05T04:46:44.803870Z","iopub.status.idle":"2023-10-05T04:46:44.814054Z","shell.execute_reply.started":"2023-10-05T04:46:44.803841Z","shell.execute_reply":"2023-10-05T04:46:44.813141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(x, eig_val_contribution, '*-', linewidth=2, color='blue')\nplt.title('Scree Plot')\nplt.xlabel('Principal Component')\nplt.ylabel('Variance Explained')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-05T04:46:44.815860Z","iopub.execute_input":"2023-10-05T04:46:44.816256Z","iopub.status.idle":"2023-10-05T04:46:45.089050Z","shell.execute_reply.started":"2023-10-05T04:46:44.816204Z","shell.execute_reply":"2023-10-05T04:46:45.088166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sum = 0\ncount = 0\nfor i in eig_val_contribution:\n    if sum > 0.85:\n        break\n    sum = sum+i\n    count = count + 1\nprint(f\"eigenvalues that contribute {sum} sum is {count}\")","metadata":{"execution":{"iopub.status.busy":"2023-10-05T04:46:45.090201Z","iopub.execute_input":"2023-10-05T04:46:45.091009Z","iopub.status.idle":"2023-10-05T04:46:45.097315Z","shell.execute_reply.started":"2023-10-05T04:46:45.090975Z","shell.execute_reply":"2023-10-05T04:46:45.096374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler = StandardScaler()\nscaled_df=train.drop(train.columns[0],axis = 1).copy()\nscaled_df=pd.DataFrame(scaler.fit_transform(scaled_df), columns=scaled_df.columns)","metadata":{"execution":{"iopub.status.busy":"2023-10-05T04:46:45.098475Z","iopub.execute_input":"2023-10-05T04:46:45.099487Z","iopub.status.idle":"2023-10-05T04:46:46.589722Z","shell.execute_reply.started":"2023-10-05T04:46:45.099452Z","shell.execute_reply":"2023-10-05T04:46:46.588594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaled_df","metadata":{"execution":{"iopub.status.busy":"2023-10-05T04:46:46.591393Z","iopub.execute_input":"2023-10-05T04:46:46.592030Z","iopub.status.idle":"2023-10-05T04:46:46.646126Z","shell.execute_reply.started":"2023-10-05T04:46:46.591995Z","shell.execute_reply":"2023-10-05T04:46:46.645256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.decomposition import PCA\n\npca = PCA(n_components=784)\npca_fit = pca.fit(scaled_df)","metadata":{"execution":{"iopub.status.busy":"2023-10-05T04:46:46.650100Z","iopub.execute_input":"2023-10-05T04:46:46.652161Z","iopub.status.idle":"2023-10-05T04:46:57.150423Z","shell.execute_reply.started":"2023-10-05T04:46:46.652110Z","shell.execute_reply":"2023-10-05T04:46:57.149439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca_fit","metadata":{"execution":{"iopub.status.busy":"2023-10-05T04:46:57.151978Z","iopub.execute_input":"2023-10-05T04:46:57.152856Z","iopub.status.idle":"2023-10-05T04:46:57.165615Z","shell.execute_reply.started":"2023-10-05T04:46:57.152818Z","shell.execute_reply":"2023-10-05T04:46:57.164497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\n\nPCAvals = np.arange(pca.n_components_) + 1\nplt.plot(PCAvals, pca.explained_variance_ratio_, '*-', linewidth=2, color='red')\nplt.title('Scree Plot')\nplt.xlabel('Principal Component')\nplt.ylabel('Variance')\nplt.show()\n     ","metadata":{"execution":{"iopub.status.busy":"2023-10-05T04:46:57.167089Z","iopub.execute_input":"2023-10-05T04:46:57.167466Z","iopub.status.idle":"2023-10-05T04:46:57.422069Z","shell.execute_reply.started":"2023-10-05T04:46:57.167433Z","shell.execute_reply":"2023-10-05T04:46:57.421014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"desired_variance = 0.9 \ncumulative_variance = np.cumsum(pca.explained_variance_ratio_)\nbest_n_components = np.argmax(cumulative_variance >= desired_variance) + 1\n\nprint(f\"Number of components required to explain {desired_variance*100}% variance = {best_n_components}\")","metadata":{"execution":{"iopub.status.busy":"2023-10-05T04:46:57.423224Z","iopub.execute_input":"2023-10-05T04:46:57.423557Z","iopub.status.idle":"2023-10-05T04:46:57.431399Z","shell.execute_reply.started":"2023-10-05T04:46:57.423523Z","shell.execute_reply":"2023-10-05T04:46:57.428997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca = PCA(n_components=328)\npca_fit = pca.fit(scaled_df)","metadata":{"execution":{"iopub.status.busy":"2023-10-05T04:46:57.433092Z","iopub.execute_input":"2023-10-05T04:46:57.433915Z","iopub.status.idle":"2023-10-05T04:47:08.675805Z","shell.execute_reply.started":"2023-10-05T04:46:57.433850Z","shell.execute_reply":"2023-10-05T04:47:08.674754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\n\nPCAvals = np.arange(pca.n_components_) + 1\nplt.plot(PCAvals, pca.explained_variance_ratio_, '*-', linewidth=2, color='green')\nplt.title('Scree Plot')\nplt.xlabel('Principal Component')\nplt.ylabel('Variance')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-05T04:47:08.677174Z","iopub.execute_input":"2023-10-05T04:47:08.677542Z","iopub.status.idle":"2023-10-05T04:47:08.913930Z","shell.execute_reply.started":"2023-10-05T04:47:08.677509Z","shell.execute_reply":"2023-10-05T04:47:08.912958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Auto Encoders","metadata":{}},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\n\n# Define the Encoder\nclass Encoder(nn.Module):\n    def __init__(self):\n        super(Encoder, self).__init__()\n        self.encoder = nn.Sequential(\n            nn.Linear(28*28, 256),  # Increase the hidden layer size\n            nn.ReLU(True),\n            nn.Linear(256, 128),  # Add another hidden layer\n            nn.ReLU(True),\n            nn.Linear(128, 64),\n            nn.ReLU(True),\n            nn.Linear(64, 32)\n        )\n        \n    def forward(self, x):\n        x = x.view(x.size(0), -1)  # Flatten the input\n        x = self.encoder(x)\n        return x\n\n# Define the Decoder\nclass Decoder(nn.Module):\n    def __init__(self):\n        super(Decoder, self).__init__()\n        self.decoder = nn.Sequential(\n            nn.Linear(32, 64),\n            nn.ReLU(True),\n            nn.Linear(64, 128),\n            nn.ReLU(True),\n            nn.Linear(128, 256),  # Add another hidden layer\n            nn.ReLU(True),\n            nn.Linear(256, 28*28),  # Output should match the input size\n            nn.Sigmoid()\n        )\n        \n    def forward(self, x):\n        x = self.decoder(x)\n        x = x.view(x.size(0), 1, 28, 28)  # Reshape back to image dimensions\n        return x\n\n# Define the Autoencoder\nclass Autoencoder(nn.Module):\n    def __init__(self):\n        super(Autoencoder, self).__init__()\n        self.encoder = Encoder()\n        self.decoder = Decoder()\n        \n    def forward(self, x):\n        x = self.encoder(x)\n        x = self.decoder(x)\n        return x\n","metadata":{"execution":{"iopub.status.busy":"2023-10-05T04:47:08.915203Z","iopub.execute_input":"2023-10-05T04:47:08.916149Z","iopub.status.idle":"2023-10-05T04:47:08.925521Z","shell.execute_reply.started":"2023-10-05T04:47:08.916115Z","shell.execute_reply":"2023-10-05T04:47:08.924587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.utils.data import DataLoader\nimport matplotlib.pyplot as plt\nimport numpy as np\n\n# Check if CUDA is available\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\nautoencoder = Autoencoder().to(device)\ncriterion = nn.MSELoss()\noptimizer = optim.Adam(autoencoder.parameters(), lr=0.001)\n\nnum_epochs = 20\nfor epoch in range(num_epochs):\n    autoencoder.train()\n    total_loss = 0.0\n\n    # Training\n    for batch_idx, (inputs, _) in enumerate(train_dataloader):\n        # Move inputs to the GPU\n        inputs = inputs.to(device)\n\n        optimizer.zero_grad()\n        outputs = autoencoder(inputs)\n        loss = criterion(outputs, inputs)\n        loss.backward()\n        optimizer.step()\n        total_loss += loss.item()\n\n    avg_train_loss = total_loss / len(train_dataloader)\n    print(f'Epoch {epoch+1}/{num_epochs}, Training Loss: {avg_train_loss:.4f}')\n\n    autoencoder.eval()\n    with torch.no_grad():\n        total_val_loss = 0\n        for batch_idx, (inputs, _) in enumerate(val_dataloader):\n            # Move inputs to the GPU\n            inputs = inputs.to(device)\n\n            outputs = autoencoder(inputs)\n            val_loss = criterion(outputs, inputs)\n            total_val_loss += val_loss.item()\n\n        avg_val_loss = total_val_loss / len(val_dataloader)\n        print(f'Epoch {epoch+1}/{num_epochs}, Validation Loss: {avg_val_loss:.4f}')\n\nautoencoder.eval()\ntest_loss = 0\nwith torch.no_grad():\n    for batch_idx, (inputs, _) in enumerate(test_dataloader):\n        # Move inputs to the GPU\n        inputs = inputs.to(device)\n\n        outputs = autoencoder(inputs)\n        test_loss += criterion(outputs, inputs).item()\n\navg_test_loss = test_loss / len(test_dataloader)\nprint(f'Test Loss: {avg_test_loss:.4f}')\n","metadata":{"execution":{"iopub.status.busy":"2023-10-05T04:47:08.927557Z","iopub.execute_input":"2023-10-05T04:47:08.928347Z","iopub.status.idle":"2023-10-05T04:49:35.829018Z","shell.execute_reply.started":"2023-10-05T04:47:08.928317Z","shell.execute_reply":"2023-10-05T04:49:35.827953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}