help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • Blog
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
pytorch-neuralnetworks
/
training
RSK World
pytorch-neuralnetworks
Neural networks with PyTorch
training
  • __pycache__
  • __init__.py917 B
  • advanced_trainer.py5.2 KB
  • callbacks.py5.6 KB
  • metrics.py3.9 KB
  • trainer.py5.4 KB
  • utils.py3.9 KB
02_automatic_differentiation.ipynb
notebooks/02_automatic_differentiation.ipynb
Raw Download
Find: Go to:
{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Automatic Differentiation in PyTorch\n",
        "\n",
        "<!--\n",
        "Project: PyTorch Neural Networks\n",
        "Author: RSK World\n",
        "Website: https://rskworld.in\n",
        "Email: help@rskworld.in\n",
        "Phone: +91 93305 39277\n",
        "-->\n",
        "\n",
        "This notebook demonstrates PyTorch's automatic differentiation feature, which is essential for training neural networks.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Automatic Differentiation in PyTorch\n",
        "# Project: PyTorch Neural Networks\n",
        "# Author: RSK World\n",
        "# Website: https://rskworld.in\n",
        "# Email: help@rskworld.in\n",
        "# Phone: +91 93305 39277\n",
        "\n",
        "import torch\n",
        "\n",
        "# Enable gradient tracking\n",
        "x = torch.tensor(2.0, requires_grad=True)\n",
        "y = torch.tensor(3.0, requires_grad=True)\n",
        "\n",
        "# Define a function\n",
        "z = x**2 + y**2 + x*y\n",
        "\n",
        "print(f\"x = {x.item()}\")\n",
        "print(f\"y = {y.item()}\")\n",
        "print(f\"z = x² + y² + xy = {z.item()}\")\n",
        "\n",
        "# Compute gradients\n",
        "z.backward()\n",
        "\n",
        "print(f\"\\n∂z/∂x = {x.grad.item()}\")\n",
        "print(f\"∂z/∂y = {y.grad.item()}\")\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Gradient Descent Example\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Simple gradient descent example\n",
        "# Find the minimum of f(x) = x²\n",
        "\n",
        "x = torch.tensor(5.0, requires_grad=True)\n",
        "learning_rate = 0.1\n",
        "\n",
        "print(\"Gradient Descent to minimize f(x) = x²\")\n",
        "print(f\"Initial x: {x.item()}\")\n",
        "\n",
        "for i in range(10):\n",
        "    # Forward pass\n",
        "    loss = x**2\n",
        "    \n",
        "    # Backward pass\n",
        "    loss.backward()\n",
        "    \n",
        "    # Update x (gradient descent)\n",
        "    with torch.no_grad():\n",
        "        x -= learning_rate * x.grad\n",
        "        x.grad.zero_()\n",
        "    \n",
        "    print(f\"Iteration {i+1}: x = {x.item():.4f}, f(x) = {loss.item():.4f}\")\n",
        "\n",
        "print(f\"\\nFinal x: {x.item():.4f} (should be close to 0)\")\n",
        "\n"
      ]
    }
  ],
  "metadata": {
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}
105 lines•2.8 KB
json
🚀 Support RSK World

Subscribe to our YouTube channel for latest tutorials & updates!



Click subscribe & support our work ❤️

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer