-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b9a78e8
commit a0265f5
Showing
1 changed file
with
364 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,364 @@ | ||
{ | ||
"nbformat": 4, | ||
"nbformat_minor": 0, | ||
"metadata": { | ||
"colab": { | ||
"provenance": [], | ||
"authorship_tag": "ABX9TyNDIhO1Yr+PmVAezfwSxCn9" | ||
}, | ||
"kernelspec": { | ||
"name": "python3", | ||
"display_name": "Python 3" | ||
}, | ||
"language_info": { | ||
"name": "python" | ||
} | ||
}, | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": { | ||
"id": "YlOlFH1_1PDK" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import numpy as np\n", | ||
"import torch\n", | ||
"import torch.nn as nn\n", | ||
"import torch.optim as optim\n", | ||
"from torch.utils.data import DataLoader, TensorDataset" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"# Set random seed for reproducibility\n", | ||
"np.random.seed(0)\n", | ||
"torch.manual_seed(0)\n" | ||
], | ||
"metadata": { | ||
"colab": { | ||
"base_uri": "https://localhost:8080/" | ||
}, | ||
"id": "tmqSTDd01gLR", | ||
"outputId": "e8be9557-ef67-4758-8463-271f69d62caf" | ||
}, | ||
"execution_count": 2, | ||
"outputs": [ | ||
{ | ||
"output_type": "execute_result", | ||
"data": { | ||
"text/plain": [ | ||
"<torch._C.Generator at 0x7d25b8b55130>" | ||
] | ||
}, | ||
"metadata": {}, | ||
"execution_count": 2 | ||
} | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"# Parameters\n", | ||
"num_classes = 3\n", | ||
"input_dim = 2 # Let's create 2D data for simplicity\n", | ||
"num_data_per_class = 1000\n", | ||
"hidden_dim = 100\n" | ||
], | ||
"metadata": { | ||
"id": "EWhAZEH71iI4" | ||
}, | ||
"execution_count": 4, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"# Generate random data\n", | ||
"data = []\n", | ||
"labels = []\n", | ||
"for class_number in range(num_classes):\n", | ||
" # Create data for each class\n", | ||
" class_data = np.random.randn(num_data_per_class, input_dim) + (class_number * 3)\n", | ||
" class_labels = np.full(num_data_per_class, class_number)\n", | ||
" data.append(class_data)\n", | ||
" labels.append(class_labels)" | ||
], | ||
"metadata": { | ||
"id": "EbhBVSw71kdA" | ||
}, | ||
"execution_count": 5, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"# Convert to single numpy arrays\n", | ||
"data = np.vstack(data)\n", | ||
"labels = np.hstack(labels)\n" | ||
], | ||
"metadata": { | ||
"id": "0Hj_HZU22oDI" | ||
}, | ||
"execution_count": 18, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"# Convert to PyTorch tensors\n", | ||
"data_tensor = torch.tensor(data, dtype=torch.float32)\n", | ||
"labels_tensor = torch.tensor(labels, dtype=torch.long)\n", | ||
"\n", | ||
"# Create a TensorDataset and DataLoader\n", | ||
"dataset = TensorDataset(data_tensor, labels_tensor)\n", | ||
"dataloader = DataLoader(dataset, batch_size=64, shuffle=True)\n" | ||
], | ||
"metadata": { | ||
"id": "HXo20AgC2zfR" | ||
}, | ||
"execution_count": 23, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"# Neural network\n", | ||
"class SimpleNN(nn.Module):\n", | ||
" def __init__(self):\n", | ||
" super(SimpleNN, self).__init__()\n", | ||
" self.fc1 = nn.Linear(input_dim, hidden_dim)\n", | ||
" self.fc2 = nn.Linear(hidden_dim, num_classes)\n", | ||
"\n", | ||
" def forward(self, x):\n", | ||
" x = torch.relu(self.fc1(x))\n", | ||
" x = self.fc2(x)\n", | ||
" return x\n", | ||
"\n", | ||
"model = SimpleNN()" | ||
], | ||
"metadata": { | ||
"id": "-LPCukJs3Nxq" | ||
}, | ||
"execution_count": 24, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"# Loss and optimizer\n", | ||
"criterion = nn.CrossEntropyLoss()\n", | ||
"optimizer = optim.Adam(model.parameters(), lr=0.001)\n" | ||
], | ||
"metadata": { | ||
"id": "AphIpfTz3Tl6" | ||
}, | ||
"execution_count": 25, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"# Training loop\n", | ||
"num_epochs = 100\n", | ||
"for epoch in range(num_epochs):\n", | ||
" for inputs, targets in dataloader:\n", | ||
" # Forward pass\n", | ||
" outputs = model(inputs)\n", | ||
" loss = criterion(outputs, targets)\n", | ||
"\n", | ||
" # Backward and optimize\n", | ||
" optimizer.zero_grad()\n", | ||
" loss.backward()\n", | ||
" optimizer.step()\n", | ||
"\n", | ||
" print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}')\n" | ||
], | ||
"metadata": { | ||
"colab": { | ||
"base_uri": "https://localhost:8080/" | ||
}, | ||
"id": "vugQZjOY3YCw", | ||
"outputId": "c9364454-2c45-4bd9-8d4b-6de46b787cc9" | ||
}, | ||
"execution_count": 29, | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"name": "stdout", | ||
"text": [ | ||
"Epoch [1/100], Loss: 0.7061\n", | ||
"Epoch [2/100], Loss: 0.5184\n", | ||
"Epoch [3/100], Loss: 0.3409\n", | ||
"Epoch [4/100], Loss: 0.3404\n", | ||
"Epoch [5/100], Loss: 0.3013\n", | ||
"Epoch [6/100], Loss: 0.2666\n", | ||
"Epoch [7/100], Loss: 0.1944\n", | ||
"Epoch [8/100], Loss: 0.1365\n", | ||
"Epoch [9/100], Loss: 0.2394\n", | ||
"Epoch [10/100], Loss: 0.1571\n", | ||
"Epoch [11/100], Loss: 0.1054\n", | ||
"Epoch [12/100], Loss: 0.1271\n", | ||
"Epoch [13/100], Loss: 0.0823\n", | ||
"Epoch [14/100], Loss: 0.0629\n", | ||
"Epoch [15/100], Loss: 0.0954\n", | ||
"Epoch [16/100], Loss: 0.1026\n", | ||
"Epoch [17/100], Loss: 0.0657\n", | ||
"Epoch [18/100], Loss: 0.0571\n", | ||
"Epoch [19/100], Loss: 0.0781\n", | ||
"Epoch [20/100], Loss: 0.1290\n", | ||
"Epoch [21/100], Loss: 0.1111\n", | ||
"Epoch [22/100], Loss: 0.0277\n", | ||
"Epoch [23/100], Loss: 0.0666\n", | ||
"Epoch [24/100], Loss: 0.0677\n", | ||
"Epoch [25/100], Loss: 0.0634\n", | ||
"Epoch [26/100], Loss: 0.1145\n", | ||
"Epoch [27/100], Loss: 0.0605\n", | ||
"Epoch [28/100], Loss: 0.0545\n", | ||
"Epoch [29/100], Loss: 0.0649\n", | ||
"Epoch [30/100], Loss: 0.0454\n", | ||
"Epoch [31/100], Loss: 0.0644\n", | ||
"Epoch [32/100], Loss: 0.0344\n", | ||
"Epoch [33/100], Loss: 0.0289\n", | ||
"Epoch [34/100], Loss: 0.1207\n", | ||
"Epoch [35/100], Loss: 0.0529\n", | ||
"Epoch [36/100], Loss: 0.0273\n", | ||
"Epoch [37/100], Loss: 0.0446\n", | ||
"Epoch [38/100], Loss: 0.1538\n", | ||
"Epoch [39/100], Loss: 0.0500\n", | ||
"Epoch [40/100], Loss: 0.0764\n", | ||
"Epoch [41/100], Loss: 0.0608\n", | ||
"Epoch [42/100], Loss: 0.0551\n", | ||
"Epoch [43/100], Loss: 0.0282\n", | ||
"Epoch [44/100], Loss: 0.0652\n", | ||
"Epoch [45/100], Loss: 0.0698\n", | ||
"Epoch [46/100], Loss: 0.1298\n", | ||
"Epoch [47/100], Loss: 0.1606\n", | ||
"Epoch [48/100], Loss: 0.0512\n", | ||
"Epoch [49/100], Loss: 0.0657\n", | ||
"Epoch [50/100], Loss: 0.0276\n", | ||
"Epoch [51/100], Loss: 0.0652\n", | ||
"Epoch [52/100], Loss: 0.0865\n", | ||
"Epoch [53/100], Loss: 0.0498\n", | ||
"Epoch [54/100], Loss: 0.0831\n", | ||
"Epoch [55/100], Loss: 0.0339\n", | ||
"Epoch [56/100], Loss: 0.0284\n", | ||
"Epoch [57/100], Loss: 0.0929\n", | ||
"Epoch [58/100], Loss: 0.0434\n", | ||
"Epoch [59/100], Loss: 0.0197\n", | ||
"Epoch [60/100], Loss: 0.0400\n", | ||
"Epoch [61/100], Loss: 0.0241\n", | ||
"Epoch [62/100], Loss: 0.0732\n", | ||
"Epoch [63/100], Loss: 0.0176\n", | ||
"Epoch [64/100], Loss: 0.0970\n", | ||
"Epoch [65/100], Loss: 0.0605\n", | ||
"Epoch [66/100], Loss: 0.0340\n", | ||
"Epoch [67/100], Loss: 0.0252\n", | ||
"Epoch [68/100], Loss: 0.0528\n", | ||
"Epoch [69/100], Loss: 0.0499\n", | ||
"Epoch [70/100], Loss: 0.0312\n", | ||
"Epoch [71/100], Loss: 0.0168\n", | ||
"Epoch [72/100], Loss: 0.0136\n", | ||
"Epoch [73/100], Loss: 0.0375\n", | ||
"Epoch [74/100], Loss: 0.0872\n", | ||
"Epoch [75/100], Loss: 0.0491\n", | ||
"Epoch [76/100], Loss: 0.0209\n", | ||
"Epoch [77/100], Loss: 0.1018\n", | ||
"Epoch [78/100], Loss: 0.0953\n", | ||
"Epoch [79/100], Loss: 0.0132\n", | ||
"Epoch [80/100], Loss: 0.0776\n", | ||
"Epoch [81/100], Loss: 0.0260\n", | ||
"Epoch [82/100], Loss: 0.0133\n", | ||
"Epoch [83/100], Loss: 0.0261\n", | ||
"Epoch [84/100], Loss: 0.1178\n", | ||
"Epoch [85/100], Loss: 0.0251\n", | ||
"Epoch [86/100], Loss: 0.0174\n", | ||
"Epoch [87/100], Loss: 0.0227\n", | ||
"Epoch [88/100], Loss: 0.0441\n", | ||
"Epoch [89/100], Loss: 0.1605\n", | ||
"Epoch [90/100], Loss: 0.0686\n", | ||
"Epoch [91/100], Loss: 0.0928\n", | ||
"Epoch [92/100], Loss: 0.0176\n", | ||
"Epoch [93/100], Loss: 0.0114\n", | ||
"Epoch [94/100], Loss: 0.0748\n", | ||
"Epoch [95/100], Loss: 0.0560\n", | ||
"Epoch [96/100], Loss: 0.0120\n", | ||
"Epoch [97/100], Loss: 0.0267\n", | ||
"Epoch [98/100], Loss: 0.0381\n", | ||
"Epoch [99/100], Loss: 0.0307\n", | ||
"Epoch [100/100], Loss: 0.0050\n" | ||
] | ||
} | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"# Test the model\n", | ||
"model.eval()\n", | ||
"with torch.no_grad():\n", | ||
" correct = 0\n", | ||
" total = 0\n", | ||
" for inputs, targets in dataloader:\n", | ||
" outputs = model(inputs)\n", | ||
" _, predicted = torch.max(outputs.data, 1)\n", | ||
" total += targets.size(0)\n", | ||
" correct += (predicted == targets).sum().item()\n", | ||
"\n", | ||
"print(f'Accuracy: {100 * correct / total} %')" | ||
], | ||
"metadata": { | ||
"colab": { | ||
"base_uri": "https://localhost:8080/" | ||
}, | ||
"id": "xmMrr4sq3xsJ", | ||
"outputId": "40449874-ee32-4791-fe23-f3c0c5c92396" | ||
}, | ||
"execution_count": 30, | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"name": "stdout", | ||
"text": [ | ||
"Accuracy: 98.06666666666666 %\n" | ||
] | ||
} | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [], | ||
"metadata": { | ||
"colab": { | ||
"base_uri": "https://localhost:8080/" | ||
}, | ||
"id": "hXvHLhcT4L5r", | ||
"outputId": "cd23a7bb-3710-48b3-d499-06fb8e2a0793" | ||
}, | ||
"execution_count": 34, | ||
"outputs": [ | ||
{ | ||
"output_type": "execute_result", | ||
"data": { | ||
"text/plain": [ | ||
"1" | ||
] | ||
}, | ||
"metadata": {}, | ||
"execution_count": 34 | ||
} | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [], | ||
"metadata": { | ||
"id": "iFzQWI8J4N5n" | ||
}, | ||
"execution_count": null, | ||
"outputs": [] | ||
} | ||
] | ||
} |