Procedure

  1. Install Jupyter Notebook and PyTorch
    • 1.1 Open a terminal or command prompt and run:
    • pip install notebook
    • 1.2 Install PyTorch by running:
    • pip install torch torchvision torchaudio
    • 1.3 Check the official PyTorch website for system-specific installation commands.
  2. Launch Jupyter Notebook
    • 2.1 Open a terminal or command prompt and type:
    • jupyter notebook
    • 2.2 This will open the Jupyter Notebook interface in your default web browser.
  3. Create a New Notebook
    • 3.1 Click on “New” → “Python 3” to create a new notebook.
    • 3.2 A new notebook interface will appear where you can write and execute Python code.
  4. Verify Installation
    • 4.1 In a new Jupyter Notebook cell, type:
    • import torch
      print(torch.__version__)
    • 4.2 If the output displays the PyTorch version without any errors, the installation was successful.
  5. Perform Basic Tensor Operations in PyTorch
    • 5.1 Create a simple tensor in PyTorch:
    • x = torch.tensor([[1, 2], [3, 4]])
      print(x)
    • 5.2 Perform a basic mathematical operation:
    • y = x + 5
      print(y)
    • 5.3 Check if GPU support is available:
    • print(torch.cuda.is_available())
  6. Train a Basic AI Model in PyTorch
    • 6.1 Import necessary libraries:
    • import torch.nn as nn
      import torch.optim as optim
    • 6.2 Define a simple neural network model:
    • class SimpleModel(nn.Module):
          def __init__(self):
              super(SimpleModel, self).__init__()
              self.layer = nn.Linear(2, 1)

          def forward(self, x):
              return self.layer(x)
    • 6.3 Create the model and print its structure:
    • model = SimpleModel()
      print(model)
    • 6.4 Train the model with dummy data:
    • optimizer = optim.SGD(model.parameters(), lr=0.01)
      loss_fn = nn.MSELoss()

      # Dummy input and target
      inputs = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
      targets = torch.tensor([[1.0], [2.0]])

      for epoch in range(100):
          optimizer.zero_grad()
          outputs = model(inputs)
          loss = loss_fn(outputs, targets)
          loss.backward()
          optimizer.step()

      print("Training completed!")