{ "cells": [ { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "from torch import nn\n", "import torch\n", "from torch.utils.data import DataLoader\n", "import torch.optim as optim\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.preprocessing import LabelEncoder\n", "from sklearn.preprocessing import StandardScaler\n" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
raritysizecolorspeciescountry
0Common4.5GreenTreeFrogUSA
1Uncommon3.2BrownBullfrogCanada
2Common5.1BlueTreeFrogMexico
3Rare6.7RedRedEyedTreeFrogBrazil
4Common2.8YellowBananaFrogAustralia
\n", "
" ], "text/plain": [ " rarity size color species country\n", "0 Common 4.5 Green TreeFrog USA\n", "1 Uncommon 3.2 Brown Bullfrog Canada\n", "2 Common 5.1 Blue TreeFrog Mexico\n", "3 Rare 6.7 Red RedEyedTreeFrog Brazil\n", "4 Common 2.8 Yellow BananaFrog Australia" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = pd.read_csv('data.csv')\n", "data.head()" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [], "source": [ "\n", "# Preprocess data\n", "label_encoder = LabelEncoder()\n", "data['species'] = label_encoder.fit_transform(data['species'])\n", "data['rarity'] = label_encoder.fit_transform(data['rarity']) # Encode the 'rarity' column\n", "data['color'] = label_encoder.fit_transform(data['color'])\n", "data['country'] = label_encoder.fit_transform(data['country'])\n", "\n", "\n", "X = data.drop(['species'], axis=1).values\n", "y = data['species'].values\n", "\n", "# Split data into training and testing sets\n", "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n", "\n" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "X_train_tensor = torch.tensor(X_train, dtype=torch.float32)\n", "y_train_tensor = torch.tensor(y_train, dtype=torch.int64)\n", "X_test_tensor = torch.tensor(X_test, dtype=torch.float32)\n", "y_test_tensor = torch.tensor(y_test, dtype=torch.int64)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [], "source": [ "# deep nn\n", "\n", "class SimpleNN(nn.Module):\n", " def __init__(self,input_size,hidden_size,output_size):\n", " super(SimpleNN,self).__init__()\n", " self.fc1 = nn.Linear(input_size,hidden_size)\n", " self.relu = nn.ReLU()\n", " self.fc2 = nn.Linear(hidden_size,output_size)\n", "\n", " def forward(self,x):\n", " x = self.relu(self.fc1(x))\n", " x = self.fc2(x)\n", " return x\n" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "input_size = 4\n", "hidden_size = 64 \n", "output_size = len(label_encoder.classes_)\n", "\n", "model = SimpleNN(input_size=input_size,hidden_size=hidden_size,output_size=output_size)\n", "optimizer = optim.Adam(model.parameters(),lr=0.001)\n", "loss_fn = nn.CrossEntropyLoss()\n", "\n", "output_size\n", "\n" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Epoch [1/10], Loss: 1.5100\n", "Epoch [2/10], Loss: 1.4834\n", "Epoch [3/10], Loss: 1.4557\n", "Epoch [4/10], Loss: 1.4273\n", "Epoch [5/10], Loss: 1.3985\n", "Epoch [6/10], Loss: 1.3693\n", "Epoch [7/10], Loss: 1.3398\n", "Epoch [8/10], Loss: 1.3105\n", "Epoch [9/10], Loss: 1.2812\n", "Epoch [10/10], Loss: 1.2522\n" ] } ], "source": [ "num_epochs = 10\n", "batch_size = 32\n", "\n", "for epoch in range(num_epochs):\n", " for i in range(0, len(X_train_tensor), batch_size):\n", " inputs = X_train_tensor[i:i + batch_size]\n", " labels = y_train_tensor[i:i + batch_size]\n", "\n", " optimizer.zero_grad()\n", " outputs = model(inputs)\n", " loss = loss_fn(outputs, labels)\n", " loss.backward()\n", " optimizer.step()\n", "\n", " print(f'Epoch [{epoch + 1}/{num_epochs}], Loss: {loss.item():.4f}')\n" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [], "source": [ "# Save the model\n", "torch.save(model.state_dict(), 'your_model.pth')\n" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [], "source": [ "loaded_model = SimpleNN(input_size=input_size, hidden_size=hidden_size, output_size=output_size)" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "loaded_model.load_state_dict(torch.load('your_model.pth'))" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "SimpleNN(\n", " (fc1): Linear(in_features=4, out_features=64, bias=True)\n", " (relu): ReLU()\n", " (fc2): Linear(in_features=64, out_features=5, bias=True)\n", ")" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "loaded_model.eval()" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Test Accuracy: 80.00%\n" ] } ], "source": [ "with torch.no_grad():\n", " # Make predictions on the test set\n", " loaded_outputs = loaded_model(X_test_tensor)\n", "\n", " # Convert the model outputs to class predictions\n", " _, predicted_labels = torch.max(loaded_outputs, 1)\n", "\n", " # Evaluate accuracy\n", " accuracy = (predicted_labels == y_test_tensor).sum().item() / len(y_test_tensor)\n", " print(f'Test Accuracy: {accuracy * 100:.2f}%')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "ai_env", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.13" } }, "nbformat": 4, "nbformat_minor": 2 }