{ "cells": [ { "cell_type": "markdown", "id": "a11ebdff", "metadata": {}, "source": [ "## Manual approach" ] }, { "cell_type": "code", "execution_count": 22, "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": 23, "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": 24, "id": "35200e59", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Epoch 0: Loss = 10481.2871\n", "Epoch 400: Loss = 207.9038\n", "Epoch 800: Loss = 122.0165\n", "Epoch 1200: Loss = 71.6102\n", "Epoch 1600: Loss = 42.0273\n", "Epoch 2000: Loss = 24.6654\n", "Epoch 2400: Loss = 14.4759\n", "Epoch 2800: Loss = 8.4958\n", "Epoch 3200: Loss = 4.9861\n", "Epoch 3600: Loss = 2.9263\n", "Epoch 4000: Loss = 1.7174\n", "Epoch 4400: Loss = 1.0079\n", "Epoch 4800: Loss = 0.5916\n", "Epoch 5200: Loss = 0.3472\n", "Epoch 5600: Loss = 0.2038\n", "Epoch 6000: Loss = 0.1196\n", "Epoch 6400: Loss = 0.0702\n", "Epoch 6800: Loss = 0.0412\n", "Epoch 7200: Loss = 0.0242\n", "Epoch 7600: Loss = 0.0142\n", "Epoch 8000: Loss = 0.0083\n", "Epoch 8400: Loss = 0.0049\n", "Epoch 8800: Loss = 0.0029\n", "Epoch 9200: Loss = 0.0017\n", "Epoch 9600: Loss = 0.0010\n", "Epoch 10000: Loss = 0.0006\n", "Epoch 10400: Loss = 0.0003\n", "Epoch 10800: Loss = 0.0002\n", "Epoch 11200: Loss = 0.0001\n", "Epoch 11600: Loss = 0.0001\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": 25, "id": "be1eb1bd", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "--- Результаты обучения ---\n", "Предсказание для 100°C: 212.00°F (Ожидалось: 212.00)\n", "Итоговый вес W: 1.8000 (Ожидалось: 1.8)\n", "Итоговое смещение B: 31.9986 (Ожидалось: 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": 26, "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": 27, "id": "8860a027", "metadata": {}, "outputs": [], "source": [ "model = nn.Linear(in_features=1, out_features=1)" ] }, { "cell_type": "code", "execution_count": 28, "id": "b1213a8a", "metadata": {}, "outputs": [], "source": [ "criterion = nn.MSELoss()\n", "optimizer = optim.SGD(model.parameters(), lr=0.001)" ] }, { "cell_type": "code", "execution_count": 29, "id": "27d203c4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Epoch 0: loss = 5640.71875\n", "Epoch 400: loss = 197.63983154296875\n", "Epoch 800: loss = 115.99267578125\n", "Epoch 1200: loss = 68.0748291015625\n", "Epoch 1600: loss = 39.95244598388672\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": 30, "id": "024b4276", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "--- Результаты обучения ---\n", "Предсказание для 100°C: 231.59°F\n", "Итоговый вес W: 2.0797\n", "Итоговое смещение B: 23.6129\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": 31, "id": "df8942cd", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--- MLP Model Architecture ---\n", "SimpleMLP(\n", " (fc1): Linear(in_features=784, out_features=128, bias=True)\n", " (fc2): Linear(in_features=128, out_features=10, bias=True)\n", ")\n", "\n", "--- Starting Training Simulation ---\n", "Epoch [1/5], Loss: 2.3642\n", "Epoch [2/5], Loss: 2.0382\n", "Epoch [3/5], Loss: 1.7581\n", "Epoch [4/5], Loss: 1.5167\n", "Epoch [5/5], Loss: 1.3047\n", "\n", "MLP Example successfully defined and simulated training steps.\n" ] } ], "source": [ "import torch\n", "import torch.nn as nn\n", "import torch.optim as optim\n", "\n", "# 1. Define the MLP model\n", "class SimpleMLP(nn.Module):\n", " def __init__(self, input_size, hidden_size, output_size):\n", " super(SimpleMLP, self).__init__()\n", " # First linear layer: input to hidden layer\n", " self.fc1 = nn.Linear(input_size, hidden_size)\n", " # Second linear layer: hidden layer to output layer\n", " self.fc2 = nn.Linear(hidden_size, output_size)\n", "\n", " def forward(self, x):\n", " # Apply ReLU activation function\n", " x = torch.relu(self.fc1(x))\n", " # Output layer (no activation for regression or logits)\n", " x = self.fc2(x)\n", " return x\n", "\n", "# 2. Setup parameters\n", "input_size = 784 # Example: Flattened MNIST image (28*28)\n", "hidden_size = 128\n", "output_size = 10 # Example: Number of classes for classification\n", "\n", "# Initialize the model\n", "model = SimpleMLP(input_size, hidden_size, output_size)\n", "print(\"--- MLP Model Architecture ---\")\n", "print(model)\n", "\n", "# 3. Create dummy data for demonstration (In a real scenario, you would load a dataset like MNIST)\n", "# Batch size and input features\n", "batch_size = 64\n", "dummy_input = torch.randn(batch_size, input_size)\n", "# Dummy labels\n", "dummy_target = torch.randint(0, output_size, (batch_size,))\n", "\n", "# 4. Setup Loss Function and Optimizer\n", "criterion = nn.CrossEntropyLoss() # Suitable for multi-class classification\n", "optimizer = optim.Adam(model.parameters(), lr=0.001)\n", "\n", "print(\"\\n--- Starting Training Simulation ---\")\n", "\n", "# 5. Training Loop (Simulation)\n", "num_epochs = 5\n", "\n", "for epoch in range(num_epochs):\n", " # Forward pass\n", " outputs = model(dummy_input)\n", " \n", " # Calculate loss\n", " loss = criterion(outputs, dummy_target)\n", " \n", " # Backward pass and optimization\n", " optimizer.zero_grad()\n", " loss.backward()\n", " optimizer.step()\n", " \n", " print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}')\n", "\n", "print(\"\\nMLP Example successfully defined and simulated training steps.\")" ] }, { "cell_type": "code", "execution_count": 32, "id": "98166324", "metadata": {}, "outputs": [], "source": [ "import torch\n", "from torch import nn\n", "from matplotlib import pyplot as plt" ] }, { "cell_type": "code", "execution_count": null, "id": "ef1351a0", "metadata": {}, "outputs": [], "source": [ "X_test = torch.tensor(" ] }, { "cell_type": "code", "execution_count": null, "id": "b9f7783b", "metadata": {}, "outputs": [], "source": [ "\n", "\n", "class Model(nn.Module):\n", " def __init__(self):\n", " super().__init__()\n", " " ] }, { "cell_type": "code", "execution_count": null, "id": "90874c3d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "7b808533", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": ".venv (3.11.15.final.0)", "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 }