an additional course from freeCodeCamp started.
This commit is contained in:
File diff suppressed because it is too large
Load Diff
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,155 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "ed9e0a13",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import torch\n",
|
||||
"from torch import nn\n",
|
||||
"from torch.utils.data import DataLoader\n",
|
||||
"from torchvision import datasets\n",
|
||||
"from torchvision.transforms import v2"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "c838fd53",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"100.0%\n",
|
||||
"100.0%\n",
|
||||
"100.0%\n",
|
||||
"100.0%\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"training_data = datasets.FashionMNIST(\n",
|
||||
" root=\"data\",\n",
|
||||
" train=True,\n",
|
||||
" download=True,\n",
|
||||
" transform=v2.Compose([v2.ToImage(), v2.ToDtype(torch.float32, scale=True)]),\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"test_data = datasets.FashionMNIST(\n",
|
||||
" root=\"data\",\n",
|
||||
" train=False,\n",
|
||||
" download=True,\n",
|
||||
" transform=v2.Compose([v2.ToImage(), v2.ToDtype(torch.float32, scale=True)]),\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"id": "744a8ecb",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Shape of X [N, C, H, W]: torch.Size([64, 1, 28, 28])\n",
|
||||
"Shape of y: torch.Size([64]) torch.int64\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"batch_size = 64\n",
|
||||
"\n",
|
||||
"train_dataloader = DataLoader(training_data, batch_size=batch_size)\n",
|
||||
"test_dataloader = DataLoader(test_data, batch_size=batch_size)\n",
|
||||
"\n",
|
||||
"for X, y in test_dataloader:\n",
|
||||
" print(f\"Shape of X [N, C, H, W]: {X.shape}\")\n",
|
||||
" print(f\"Shape of y: {y.shape} {y.dtype}\")\n",
|
||||
" break"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"id": "25639067",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Using cuda device\n",
|
||||
"NeuralNetwork(\n",
|
||||
" (flatten): Flatten(start_dim=1, end_dim=-1)\n",
|
||||
" (linear_relu_stack): Sequential(\n",
|
||||
" (0): Linear(in_features=784, out_features=512, bias=True)\n",
|
||||
" (1): ReLU()\n",
|
||||
" (2): Linear(in_features=512, out_features=512, bias=True)\n",
|
||||
" (3): ReLU()\n",
|
||||
" (4): Linear(in_features=512, out_features=10, bias=True)\n",
|
||||
" )\n",
|
||||
")\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"device = torch.accelerator.current_accelerator().type if torch.accelerator.is_available() else \"cpu\"\n",
|
||||
"print(f\"Using {device} device\")\n",
|
||||
"\n",
|
||||
"class NeuralNetwork(nn.Module):\n",
|
||||
" def __init__(self):\n",
|
||||
" super().__init__()\n",
|
||||
" self.flatten = nn.Flatten()\n",
|
||||
" self.linear_relu_stack = nn.Sequential(\n",
|
||||
" nn.Linear(28*28, 512),\n",
|
||||
" nn.ReLU(),\n",
|
||||
" nn.Linear(512, 512),\n",
|
||||
" nn.ReLU(),\n",
|
||||
" nn.Linear(512, 10)\n",
|
||||
" )\n",
|
||||
" def forward(self, x):\n",
|
||||
" x = self.flatten(x)\n",
|
||||
" logits = self.linear_relu_stack(x)\n",
|
||||
" return logits\n",
|
||||
" \n",
|
||||
"model = NeuralNetwork().to(device)\n",
|
||||
"print(model)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "4d995f06",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": ".venv (3.11.15)",
|
||||
"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.11.15"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"id": "7af2ca72",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import torch"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "0684c0ff",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"X = torch.tensor([[1.0],[2.0],[3.0]])\n",
|
||||
"y = torch.tensor([[4.0],[5.0],[6.0]])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 19,
|
||||
"id": "1078a0d7",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"w = torch.randn(1, requires_grad=True)\n",
|
||||
"b = torch.randn(1, requires_grad=True)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 22,
|
||||
"id": "6d7c95c5",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"0.4485014081001282 3.9534966945648193\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"for _ in range(100):\n",
|
||||
" y_pred = X @ w + b\n",
|
||||
" loss = ((y_pred - y) ** 2).mean()\n",
|
||||
" loss.backward()\n",
|
||||
" with torch.no_grad():\n",
|
||||
" w -= 0.05 * w.grad\n",
|
||||
" b -= 0.05 * w.grad\n",
|
||||
" w.grad.zero_()\n",
|
||||
" b.grad.zero_()\n",
|
||||
"\n",
|
||||
"print(w.item(),b.item())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "b23bda3c",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "1b6fcc44",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": ".venv (3.11.15)",
|
||||
"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.11.15"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
@@ -0,0 +1,284 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "a11ebdff",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Manual approach"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "3163fb75",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import torch\n",
|
||||
"\n",
|
||||
"X = torch.tensor([[0.0],[10.0], [20.0], [30.0], [40.0]])\n",
|
||||
"Y = torch.tensor([[32.0], [50.0], [68.0], [86.0], [104.0]])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"id": "64a02514",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"W = torch.randn((1, 1), requires_grad=True)\n",
|
||||
"B = torch.randn((1, 1), requires_grad=True)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"id": "35200e59",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Epoch 0: Loss = 42.0600\n",
|
||||
"Epoch 400: Loss = 24.6847\n",
|
||||
"Epoch 800: Loss = 14.4873\n",
|
||||
"Epoch 1200: Loss = 8.5024\n",
|
||||
"Epoch 1600: Loss = 4.9900\n",
|
||||
"Epoch 2000: Loss = 2.9286\n",
|
||||
"Epoch 2400: Loss = 1.7188\n",
|
||||
"Epoch 2800: Loss = 1.0087\n",
|
||||
"Epoch 3200: Loss = 0.5920\n",
|
||||
"Epoch 3600: Loss = 0.3474\n",
|
||||
"Epoch 4000: Loss = 0.2039\n",
|
||||
"Epoch 4400: Loss = 0.1197\n",
|
||||
"Epoch 4800: Loss = 0.0702\n",
|
||||
"Epoch 5200: Loss = 0.0412\n",
|
||||
"Epoch 5600: Loss = 0.0242\n",
|
||||
"Epoch 6000: Loss = 0.0142\n",
|
||||
"Epoch 6400: Loss = 0.0083\n",
|
||||
"Epoch 6800: Loss = 0.0049\n",
|
||||
"Epoch 7200: Loss = 0.0029\n",
|
||||
"Epoch 7600: Loss = 0.0017\n",
|
||||
"Epoch 8000: Loss = 0.0010\n",
|
||||
"Epoch 8400: Loss = 0.0006\n",
|
||||
"Epoch 8800: Loss = 0.0003\n",
|
||||
"Epoch 9200: Loss = 0.0002\n",
|
||||
"Epoch 9600: Loss = 0.0001\n",
|
||||
"Epoch 10000: Loss = 0.0001\n",
|
||||
"Epoch 10400: Loss = 0.0000\n",
|
||||
"Epoch 10800: Loss = 0.0000\n",
|
||||
"Epoch 11200: Loss = 0.0000\n",
|
||||
"Epoch 11600: Loss = 0.0000\n",
|
||||
"Epoch 12000: Loss = 0.0000\n",
|
||||
"Epoch 12400: Loss = 0.0000\n",
|
||||
"Epoch 12800: Loss = 0.0000\n",
|
||||
"Epoch 13200: Loss = 0.0000\n",
|
||||
"Epoch 13600: Loss = 0.0000\n",
|
||||
"Epoch 14000: Loss = 0.0000\n",
|
||||
"Epoch 14400: Loss = 0.0000\n",
|
||||
"Epoch 14800: Loss = 0.0000\n",
|
||||
"Epoch 15200: Loss = 0.0000\n",
|
||||
"Epoch 15600: Loss = 0.0000\n",
|
||||
"Epoch 16000: Loss = 0.0000\n",
|
||||
"Epoch 16400: Loss = 0.0000\n",
|
||||
"Epoch 16800: Loss = 0.0000\n",
|
||||
"Epoch 17200: Loss = 0.0000\n",
|
||||
"Epoch 17600: Loss = 0.0000\n",
|
||||
"Epoch 18000: Loss = 0.0000\n",
|
||||
"Epoch 18400: Loss = 0.0000\n",
|
||||
"Epoch 18800: Loss = 0.0000\n",
|
||||
"Epoch 19200: Loss = 0.0000\n",
|
||||
"Epoch 19600: Loss = 0.0000\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"learning_rate = 0.001\n",
|
||||
"\n",
|
||||
"for epoch in range(20000):\n",
|
||||
" Y_pred = X.matmul(W) + B\n",
|
||||
"\n",
|
||||
" loss = ((Y_pred - Y)**2).mean()\n",
|
||||
"\n",
|
||||
" loss.backward()\n",
|
||||
" with torch.no_grad():\n",
|
||||
" W -= learning_rate * W.grad\n",
|
||||
" B -= learning_rate * B.grad\n",
|
||||
"\n",
|
||||
" W.grad.zero_()\n",
|
||||
" B.grad.zero_()\n",
|
||||
"\n",
|
||||
" if epoch % 400 == 0:\n",
|
||||
" print(f\"Epoch {epoch}: Loss = {loss.item():.4f}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"id": "be1eb1bd",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\n",
|
||||
"--- Результаты обучения ---\n",
|
||||
"Предсказание для 100°C: 211.99°F (Ожидалось: 212.00)\n",
|
||||
"Итоговый вес W: 1.7999 (Ожидалось: 1.8)\n",
|
||||
"Итоговое смещение B: 32.0029 (Ожидалось: 32.0)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"with torch.no_grad():\n",
|
||||
" X_test = torch.tensor([[100.0]])\n",
|
||||
" Y_test_pred = X_test.matmul(W) + B\n",
|
||||
" print(\"\\n--- Результаты обучения ---\")\n",
|
||||
" print(f\"Предсказание для 100°C: {Y_test_pred.item():.2f}°F (Ожидалось: 212.00)\")\n",
|
||||
" print(f\"Итоговый вес W: {W.item():.4f} (Ожидалось: 1.8)\")\n",
|
||||
" print(f\"Итоговое смещение B: {B.item():.4f} (Ожидалось: 32.0)\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "1a066894",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Professional approach"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"id": "fa3c1e81",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import torch\n",
|
||||
"import torch.nn as nn\n",
|
||||
"import torch.optim as optim\n",
|
||||
"\n",
|
||||
"X = torch.tensor([[0.0],[10.0], [20.0], [30.0], [40.0]])\n",
|
||||
"Y = torch.tensor([[32.0], [50.0], [68.0], [86.0], [104.0]])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"id": "8860a027",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"model = nn.Linear(in_features=1, out_features=1)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 19,
|
||||
"id": "b1213a8a",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"criterion = nn.MSELoss()\n",
|
||||
"optimizer = optim.SGD(model.parameters(), lr=0.001)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 20,
|
||||
"id": "27d203c4",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Epoch 0: loss = 6039.51318359375\n",
|
||||
"Epoch 400: loss = 199.59164428710938\n",
|
||||
"Epoch 800: loss = 117.13835144042969\n",
|
||||
"Epoch 1200: loss = 68.74734497070312\n",
|
||||
"Epoch 1600: loss = 40.34719467163086\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"for epoch in range(2000):\n",
|
||||
" Y_pred = model(X)\n",
|
||||
"\n",
|
||||
" loss = criterion(Y_pred, Y)\n",
|
||||
"\n",
|
||||
" optimizer.zero_grad()\n",
|
||||
"\n",
|
||||
" loss.backward()\n",
|
||||
"\n",
|
||||
" optimizer.step()\n",
|
||||
"\n",
|
||||
" if epoch % 400 == 0:\n",
|
||||
" print(f\"Epoch {epoch}: loss = {loss.item()}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 23,
|
||||
"id": "024b4276",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\n",
|
||||
"--- Результаты обучения ---\n",
|
||||
"Предсказание для 100°C: 231.68°F\n",
|
||||
"Итоговый вес W: 2.0811\n",
|
||||
"Итоговое смещение B: 23.5716\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"with torch.no_grad():\n",
|
||||
" X_test = torch.tensor([[100.0]])\n",
|
||||
" Y_test_pred = model(X_test)\n",
|
||||
" print(\"\\n--- Результаты обучения ---\")\n",
|
||||
" print(f\"Предсказание для 100°C: {Y_test_pred.item():.2f}°F\")\n",
|
||||
" \n",
|
||||
" # Доступ к обученным весам внутри слоя\n",
|
||||
" print(f\"Итоговый вес W: {model.weight.item():.4f}\")\n",
|
||||
" print(f\"Итоговое смещение B: {model.bias.item():.4f}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "df8942cd",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": ".venv (3.11.15)",
|
||||
"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.11.15"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
@@ -776,7 +776,7 @@
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": ".venv (3.11.15.final.0)",
|
||||
"display_name": ".venv (3.11.15)",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user