Files
Karpathy_ZtH_Learning/My_Experiments/pytorch3.ipynb
T

285 lines
7.5 KiB
Plaintext

{
"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
}