Skip to content

Understanding Tensors in PyTorch: A Beginner's Guide with a Practical Example

Hello everyone! Today, we're going to dive into the world of PyTorch and explore one of its fundamental concepts - tensors. If you're just starting out with machine learning or deep learning, understanding tensors is crucial as they are the backbone of these fields, especially in frameworks like PyTorch. By the end of this guide, you'll not only understand what tensors are but also see them in action through a simple stock market prediction example.

What is a Tensor?

In the simplest terms, a tensor is a container that can house data in N dimensions. You can think of: - A scalar (a single number) as a 0-dimensional tensor. - A vector (a list of numbers) as a 1-dimensional tensor. - A matrix (a table of numbers) as a 2-dimensional tensor. - Higher dimensions (3D matrices and more) are just extensions of these concepts.

PyTorch uses tensors to encode the inputs and outputs of a model, as well as the model’s parameters.

Tensors in PyTorch

PyTorch tensors are similar to NumPy arrays with the addition that they can also be used on a GPU to accelerate computing. Here's how you can think about them:

  1. Device-Agnostic: Tensors can be moved onto any device, be it a CPU or a GPU, which makes your model scalable and fast.
  2. Mutable: Once created, you can change the values of a tensor, unlike other immutable Python objects.

Creating Tensors in PyTorch

Here's how you can create tensors in PyTorch:

import torch

# Vector of zeros
zero_vector = torch.zeros(3)  # Tensor: [0., 0., 0.]

# Matrix of ones
one_matrix = torch.ones(2, 3)  # Tensor: [[1., 1., 1.], [1., 1., 1.]]

# Random tensor
random_tensor = torch.rand(2, 2)  # Random numbers

A Practical Example: Stock Market Prediction

Let's put our knowledge into practice by predicting stock prices using a very simple linear model in PyTorch. We'll use a dummy dataset to keep things straightforward.

Step 1: Import Required Libraries

import torch
import torch.nn as nn
import numpy as np

Step 2: Prepare the Dataset

Imagine we have stock prices for 5 days and we want to predict prices for the next day.

# Days:    1, 2, 3, 4, 5
prices = np.array([120, 121, 122, 123, 124])  # Dummy prices
prices_tensor = torch.from_numpy(prices).float()  # Convert to tensor

Step 3: Create a Model

We will use a simple linear regression model for this prediction.

model = nn.Linear(in_features=1, out_features=1)  # Simple linear model

Step 4: Define the Loss Function and Optimizer

criterion = torch.nn.MSELoss()  # Mean Squared Error Loss
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)  # Stochastic Gradient Descent

Step 5: Train the Model

For simplicity, let’s assume our inputs are the day numbers and outputs are the prices.

days = torch.tensor([[1], [2], [3], [4], [5]]).float()
prices = torch.tensor([[120], [121], [122], [123], [124]]).float()

# Training loop
for epoch in range(200):
    model.train()
    optimizer.zero_grad()  # Clear gradients
    outputs = model(days)  # Forward pass
    loss = criterion(outputs, prices)  # Calculate loss
    loss.backward()  # Calculate gradients
    optimizer.step()  # Update weights

    if epoch % 20 == 0:
        print(f'Epoch {epoch+1}, Loss: {loss.item()}')

Step 6: Make Predictions

model.eval()  # Set the model to evaluation mode
with torch.no_grad():
    predicted = model(torch.tensor([[6.0]]))  # Predict for day 6
print(f'Predicted price for day 6: {predicted.item()}')

Conclusion

Through this example, you can see how tensors operate as the core structure for storing data and how they are used in models. PyTorch provides a powerful, yet flexible platform for all kinds of predictive models, including ones for the stock market.

Remember, the key to getting good at using PyTorch is practice and experimentation. So, take this basic example and try to expand on it, perhaps using real stock market data and more complex models!

Happy Learning!