Procedure
- Install Jupyter Notebook and PyTorch
- 1.1 Open a terminal or command prompt and run:
- 1.2 Install PyTorch by running:
- 1.3 Check the official PyTorch website for system-specific installation commands.
- Launch Jupyter Notebook
- 2.1 Open a terminal or command prompt and type:
- 2.2 This will open the Jupyter Notebook interface in your default web browser.
- 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.
- Verify Installation
- 4.1 In a new Jupyter Notebook cell, type:
- 4.2 If the output displays the PyTorch version without any errors, the installation was successful.
- Perform Basic Tensor Operations in PyTorch
- 5.1 Create a simple tensor in PyTorch:
- 5.2 Perform a basic mathematical operation:
- 5.3 Check if GPU support is available:
- Train a Basic AI Model in PyTorch
- 6.1 Import necessary libraries:
- 6.2 Define a simple neural network model:
- 6.3 Create the model and print its structure:
- 6.4 Train the model with dummy data:
pip install notebookpip install torch torchvision torchaudiojupyter notebookimport torch
print(torch.__version__)x = torch.tensor([[1, 2], [3, 4]])
print(x)y = x + 5
print(y)print(torch.cuda.is_available())import torch.nn as nn
import torch.optim as optimclass SimpleModel(nn.Module):
def __init__(self):
super(SimpleModel, self).__init__()
self.layer = nn.Linear(2, 1)
def forward(self, x):
return self.layer(x)model = SimpleModel()
print(model)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!")