{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Fitting a straight line using Feed Forward Neural Network" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import torch\n", "import torch.nn as nn\n", "import torch.optim as optim\n", "import matplotlib.pyplot as plt\n", "from torch.utils.tensorboard import SummaryWriter" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Define the Linear Regression Model\n", "class LinearRegressionModel(nn.Module):\n", " def __init__(self, input_dim, output_dim):\n", " super(LinearRegressionModel, self).__init__()\n", " self.linear = nn.Linear(input_dim, output_dim)\n", " \n", " def forward(self, x):\n", " return self.linear(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Generate dummy data\n", "torch.manual_seed(42)\n", "X = torch.randn(100, 1)\n", "y = 3 * X + 2 + 0.1 * torch.randn(100, 1) # y = 3x + 2 with noise" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Initialize model, loss function, and optimizer\n", "model = LinearRegressionModel(input_dim=1, output_dim=1)\n", "criterion = nn.MSELoss()\n", "optimizer = optim.SGD(model.parameters(), lr=0.01)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Training loop\n", "epochs = 1000\n", "for epoch in range(epochs):\n", " optimizer.zero_grad()\n", " outputs = model(X)\n", " loss = criterion(outputs, y)\n", " loss.backward()\n", " optimizer.step()\n", " if (epoch + 1) % 100 == 0:\n", " print(f'Epoch [{epoch+1}/{epochs}], Loss: {loss.item():.4f}')\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Print final parameters\n", "for name, param in model.named_parameters():\n", " print(f'{name}: {param.data}')\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Plot data, predictions, and learned parameters\n", "with torch.no_grad():\n", " predicted = model(X)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.scatter(X.numpy(), y.numpy(), label='Original Data')\n", "plt.plot(X.numpy(), predicted.numpy(), color='red', label='Fitted Line')\n", "plt.legend()\n", "plt.xlabel('X')\n", "plt.ylabel('y')\n", "plt.title('Linear Regression Fit')\n", "plt.show()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.9" } }, "nbformat": 4, "nbformat_minor": 4 }