Comprehensive Guide to Python and Deep Learning Development: From NumPy to GPU Acceleration

Want to learn AI but discouraged by the complex development environment? Installed a bunch of libraries but the code won’t run? The GPU is right there, but you don’t know how to use it?

This article helps you solve all environmental issues. From Python’s basic libraries to deep learning frameworks, from CPU to GPU acceleration, it teaches you step by step how to set up a professional AI development environment.

1. NumPy: The Foundation of AI Computation

You might ask: Doesn’t Python have lists? Why do we need NumPy?

Let’s look at an experiment: multiplying one million numbers.

import time
import numpy as np

# Native Python list
python_list = list(range(1000000))
start = time.time()
result = [x * 2 for x in python_list]
print(f"Python list time: {time.time() - start:.4f} seconds")
# Output: 0.0856 seconds

# NumPy array
numpy_array = np.arange(1000000)
start = time.time()
result = numpy_array * 2
print(f"NumPy array time: {time.time() - start:.4f} seconds")
# Output: 0.0021 seconds

NumPy is 40 times faster!

Why is it so fast? Because NumPy is implemented in C at the core and fully utilizes vectorized operations. In deep learning, models can have hundreds of millions of parameters, and massive computations are required during each training session; at this point, the performance difference is enormous.

Core Capabilities of NumPy

1. Creating Arrays – Much More Powerful than Lists

import numpy as np

# Create from a list
arr = np.array([1, 2, 3, 4, 5])

# Multi-dimensional array (matrix)
matrix = np.array([[1, 2, 3],
                   [4, 5, 6]])
print(matrix.shape)  # (2, 3) - 2 rows and 3 columns

# Quickly create special arrays
zeros = np.zeros((3, 4))       # 3×4 zero matrix
ones = np.ones((2, 3))          # 2×3 matrix of ones
identity = np.eye(4)            # 4×4 identity matrix
random = np.random.randn(3, 3)  # 3×3 standard normal distribution random numbers

# Create sequences
arange = np.arange(0, 10, 2)    # [0, 2, 4, 6, 8]
linspace = np.linspace(0, 1, 5)  # [0, 0.25, 0.5, 0.75, 1.0]

2. Reshaping Arrays – As Flexible as Molding Clay

In deep learning, it is often necessary to adjust the shape of the data. For example, a 28×28 image needs to be flattened into a 784-dimensional vector to input into a neural network.

# Create an array with 12 elements
arr = np.arange(12)
print(arr.shape)  # (12,)

# Reshape to a 3×4 matrix
matrix = arr.reshape(3, 4)
print(matrix.shape)  # (3, 4)

# Transpose
transposed = matrix.T
print(transposed.shape)  # (4, 3)

# Flatten
flattened = matrix.flatten()
print(flattened.shape)  # (12,)

3. Array Indexing – Precise Data Retrieval

# 1D array indexing
arr = np.arange(10)
print(arr[0])      # 0 (first element)
print(arr[-1])     # 9 (last element)
print(arr[2:5])    # [2, 3, 4] (slicing)
print(arr[::2])    # [0, 2, 4, 6, 8] (every other element)
print(arr[::-1])   # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] (reversed)

# 2D array indexing
arr2d = np.array([[1, 2, 3],
                  [4, 5, 6],
                  [7, 8, 9]])
print(arr2d[1, 2])   # 6 (2nd row, 3rd column)
print(arr2d[:2, 1:]) # First two rows, starting from the 2nd column

# Boolean indexing (super useful!)
arr = np.array([1, 2, 3, 4, 5])
mask = arr > 3
print(arr[mask])  # [4, 5] (select elements greater than 3)

Boolean indexing is particularly useful in data preprocessing, such as filtering outliers or selecting specific categories of samples.

4. Vectorized Operations – The Secret Weapon for Performance

a = np.array([1, 2, 3, 4])
b = np.array([10, 20, 30, 40])

# Element-wise operations (corresponding positions)
print(a + b)   # [11, 22, 33, 44]
print(a * b)   # [10, 40, 90, 160]
print(a ** 2)  # [1, 4, 9, 16]

# Matrix operations
A = np.array([[1, 2],
              [3, 4]])
B = np.array([[5, 6],
              [7, 8]])

# Matrix multiplication (linear algebra)
C = A @ B  # Syntax for Python 3.5+
# Result: [[19, 22],
#          [43, 50]]

# Broadcasting: allows operations on arrays of different shapes
arr = np.array([[1, 2, 3],
                [4, 5, 6]])
print(arr + 10)  # Add 10 to each element
# [[11, 12, 13],
#  [14, 15, 16]]

5. Statistical Functions – Tools for Data Analysis

data = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

print(f"Mean: {data.mean()}")      # 5.5
print(f"Median: {np.median(data)}") # 5.5
print(f"Standard Deviation: {data.std()}")     # 2.87
print(f"Variance: {data.var()}")       # 8.25
print(f"Max: {data.max()}")     # 10
print(f"Min: {data.min()}")     # 1
print(f"Sum: {data.sum()}")       # 55

# Statistics along a specified axis
matrix = np.array([[1, 2, 3],
                   [4, 5, 6]])
print(matrix.mean(axis=0))  # Mean by column: [2.5, 3.5, 4.5]
print(matrix.mean(axis=1))  # Mean by row: [2., 5.]

Practical Applications of NumPy in AI

Example 1: Implementing the Softmax Function

Softmax is a commonly used activation function in the output layer of neural networks, converting any real number into a probability distribution.

def softmax(x):
    """
    Convert logits to probability distribution
    Subtracting the maximum value is for numerical stability, avoiding exponential overflow
    """
    exp_x = np.exp(x - np.max(x))
    return exp_x / np.sum(exp_x)

logits = np.array([2.0, 1.0, 0.1])
probs = softmax(logits)
print(f"Softmax output: {probs}")
# [0.659, 0.242, 0.099]
print(f"Sum of probabilities: {probs.sum()}")
# 1.0 (indeed a probability distribution)

Example 2: Vectorized Euclidean Distance Calculation

The K-nearest neighbors algorithm requires calculating the distance from each sample to all training samples. If done with loops, 100 samples × 1000 training samples = 100,000 calculations, which is too slow. NumPy’s vectorization can compute it all at once.

def euclidean_distance(X, Y):
    """
    Calculate the Euclidean distance from each point in X to each point in Y
    X: (n, d) - n d-dimensional vectors
    Y: (m, d) - m d-dimensional vectors
    Returns: (n, m) - distance matrix
    """
    # Using broadcasting, one line of code does it
    return np.sqrt(np.sum((X[:, np.newaxis, :] - Y[np.newaxis, :, :]) ** 2, axis=2))

X = np.random.randn(100, 50)  # 100 samples, each 50-dimensional
Y = np.random.randn(20, 50)   # 20 samples

distances = euclidean_distance(X, Y)
print(f"Distance matrix shape: {distances.shape}")  # (100, 20)

Example 3: Forward Propagation of a Simple Neural Network

def relu(x):
    """ReLU activation function: f(x) = max(0, x)"""
    return np.maximum(0, x)

# Simulating a layer of a neural network
batch_size = 32
input_dim = 784  # Flattened 28×28 image
hidden_dim = 128

# Input data (32 samples)
X = np.random.randn(batch_size, input_dim)

# Weights and biases (randomly initialized)
W = np.random.randn(input_dim, hidden_dim) / np.sqrt(input_dim)  # Xavier initialization
b = np.zeros(hidden_dim)

# Forward propagation
z = X @ W + b     # Linear transformation
a = relu(z)       # Activation

print(f"Input shape: {X.shape}")   # (32, 784)
print(f"Output shape: {a.shape}")   # (32, 128)
print(f"ReLU activation rate: {(a > 0).mean():.2%}")  # About 50% (ReLU will deactivate half of the neurons)

2. Pandas: The Swiss Army Knife for Data Processing

NumPy excels at numerical computation, but handling tabular data (like CSV files) is not very convenient. That’s where Pandas comes in.

DataFrame: Manipulating Data Like Excel

import pandas as pd

# Create DataFrame from a dictionary
data = {
'name': ['Alice', 'Bob', 'Charlie', 'David'],
'age': [25, 30, 35, 28],
'city': ['Beijing', 'Shanghai', 'Shenzhen', 'Hangzhou'],
'salary': [15000, 20000, 18000, 22000]
}
df = pd.DataFrame(data)
print(df)
#       name  age city  salary
# 0    Alice   25   北京   15000
# 1      Bob   30   上海   20000
# 2  Charlie   35   深圳   18000
# 3    David   28   杭州   22000

# Quickly view data
print(df.head(2))      # First 2 rows
print(df.describe())   # Statistical summary
print(df.info())       # Data types and missing value information

Data Selection and Filtering

# Select columns
print(df['name'])            # Single column
print(df[['name', 'age']])   # Multiple columns

# Conditional filtering
high_salary = df[df['salary'] > 18000]
print(high_salary)
#     name  age city  salary
# 1    Bob   30   上海   20000
# 3  David   28   杭州   22000

# Multi-condition combination
young_rich = df[(df['age'] < 30) & (df['salary'] > 20000)]
print(young_rich)
#     name  age city  salary
# 3  David   28   杭州   22000

Data Processing

# Add new column
df['bonus'] = df['salary'] * 0.1

# Modify column
df['salary'] = df['salary'] * 1.05  # Everyone gets a 5% raise

# Handle missing values
df_with_nan = df.copy()
df_with_nan.loc[0, 'salary'] = np.nan  # Create a missing value
print(df_with_nan.isnull().sum())      # Count missing values

# Fill missing values (with mean)
df_filled = df_with_nan.fillna(df_with_nan['salary'].mean())

# Sort
df_sorted = df.sort_values('salary', ascending=False)

# Group statistics
city_stats = df.groupby('city')['salary'].agg(['mean', 'count'])
print(city_stats)

AI Data Preprocessing in Practice

from sklearn.datasets import load_iris
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split

# Load the Iris dataset
iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df['species'] = iris.target

# Data exploration
print(df.head())
print(df.groupby('species').mean())  # Average values of features for each species

# Feature standardization (make each feature have mean 0 and standard deviation 1)
scaler = StandardScaler()
feature_cols = iris.feature_names
df[feature_cols] = scaler.fit_transform(df[feature_cols])

# Data splitting
train_df, test_df = train_test_split(df, test_size=0.2, random_state=42)
print(f"Training set: {len(train_df)} samples, Test set: {len(test_df)} samples")
# Training set: 120 samples, Test set: 30 samples

3. PyTorch: The Tool for Deep Learning

TensorFlow and PyTorch are the two major frameworks for deep learning. PyTorch has become the preferred choice in academia and research due to its more Pythonic nature and easier debugging.

Why Choose PyTorch?

Feature PyTorch TensorFlow
Ease of Use Very friendly, like writing Python A bit complex
Debugging Easy (dynamic graph) More difficult (static graph)
Community Mainstream in academia Mainstream in industry
Deployment Relatively difficult Easy (TF Serving)
Learning Curve Gentle Steep

If you are learning AI, PyTorch is highly recommended. If it is for large-scale deployment in a production environment, consider TensorFlow.

Tensors: The Core of PyTorch

A tensor is a multi-dimensional array, similar to NumPy’s array, but can run on a GPU and supports automatic differentiation.

import torch

# Create a tensor
t1 = torch.tensor([1, 2, 3])
print(t1)  # tensor([1, 2, 3])

# Convert from NumPy
import numpy as np
np_array = np.array([1, 2, 3])
t2 = torch.from_numpy(np_array)

# Create special tensors
zeros = torch.zeros(2, 3)       # 2×3 zero tensor
ones = torch.ones(3, 4)          # 3×4 tensor of ones
rand = torch.rand(2, 2)          # 2×2 random tensor [0,1)
r.randn = torch.randn(3, 3)        # 3×3 standard normal distribution
eye = torch.eye(4)               # 4×4 identity matrix

# Tensor properties
print(f"Shape: {rand.shape}")         # torch.Size([2, 2])
print(f"Data Type: {rand.dtype}")     # torch.float32
print(f"Device: {rand.device}")        # cpu
print(f"Total Elements: {rand.numel()}")   # 4

Tensor Operations

# Basic operations
a = torch.tensor([1.0, 2.0, 3.0])
b = torch.tensor([4.0, 5.0, 6.0])

print(a + b)          # tensor([5., 7., 9.])
print(a * b)          # tensor([4., 10., 18.])
print(a ** 2)         # tensor([1., 4., 9.])
print(torch.dot(a, b))  # tensor(32.) - dot product

# Matrix multiplication
A = torch.randn(3, 4)
B = torch.randn(4, 5)
C = A @ B  # Matrix multiplication
print(C.shape)  # torch.Size([3, 5])

# Change shape
x = torch.arange(12)
x = x.view(3, 4)  # or x.reshape(3, 4)
print(x.shape)  # torch.Size([3, 4])

Automatic Differentiation: PyTorch’s Secret Weapon

Training neural networks requires calculating gradients (partial derivatives), which is too complex to do manually. PyTorch’s automatic differentiation can handle this process automatically.

# Simple example: differentiation
x = torch.tensor(2.0, requires_grad=True)  # Need to calculate gradient
y = x ** 2 + 3 * x + 1  # y = x² + 3x + 1

# Backpropagation, automatically calculate gradient
y.backward()

# Gradient dy/dx = 2x + 3 = 2*2 + 3 = 7
print(f"dy/dx = {x.grad}")  # 7.0

Linear Regression in Practice: Implementing Gradient Descent from Scratch

torch.manual_seed(42)  # Fix random seed for reproducibility

# Generate data: y = 3x + 2 + noise
X = torch.randn(100, 1)
y_true = 3 * X + 2 + torch.randn(100, 1) * 0.1

# Initialize parameters (random guess)
w = torch.randn(1, requires_grad=True)
b = torch.zeros(1, requires_grad=True)

# Training
learning_rate = 0.1
for epoch in range(100):
    # Forward propagation: calculate predicted values
    y_pred = w * X + b

    # Calculate loss (mean squared error)
    loss = ((y_pred - y_true) ** 2).mean()

    # Backpropagation: automatically calculate gradients
    loss.backward()

    # Update parameters (gradient descent)
    with torch.no_grad():  # No gradients needed when updating parameters
        w -= learning_rate * w.grad
        b -= learning_rate * b.grad

    # Zero gradients (important! Otherwise gradients will accumulate)
    w.grad.zero_()
    b.grad.zero_()

    if (epoch + 1) % 20 == 0:
        print(f"Epoch {epoch+1}: Loss={loss.item():.4f}, w={w.item():.2f}, b={b.item():.2f}")

# Output:
# Epoch 20: Loss=0.0123, w=2.95, b=1.98
# Epoch 40: Loss=0.0101, w=2.98, b=2.00
# Epoch 60: Loss=0.0100, w=2.99, b=2.01
# Epoch 80: Loss=0.0100, w=3.00, b=2.01
# Epoch 100: Loss=0.0100, w=3.00, b=2.01

Look, the model successfully learned the true parameters w=3, b=2!

Building Neural Networks

PyTorch provides the <span><span>nn.Module</span></span> base class, making it easy to define complex neural networks.

import torch.nn as nn
import torch.nn.functional as F

# Method 1: Using Sequential (suitable for simple sequential structures)
model = nn.Sequential(
    nn.Linear(784, 256),   # Fully connected layer: 784 → 256
    nn.ReLU(),              # Activation function
    nn.Linear(256, 128),   # 256 → 128
    nn.ReLU(),
    nn.Linear(128, 10)     # 128 → 10 (10 categories)
)

# Method 2: Custom module (more flexible)
class MyNet(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(784, 256)
        self.fc2 = nn.Linear(256, 128)
        self.fc3 = nn.Linear(128, 10)

    def forward(self, x):
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

model = MyNet()

# View model structure
print(model)

# Count parameters
total_params = sum(p.numel() for p in model.parameters())
print(f"Total parameters: {total_params:,}")  # 235,146

# Forward propagation
x = torch.randn(32, 784)  # 32 samples
output = model(x)
print(f"Output shape: {output.shape}")  # torch.Size([32, 10])

Complete Training Process

import torch
import torch.nn as nn
import torch.optim as optim

# 1. Define model
class MNISTNet(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(28*28, 512)
        self.fc2 = nn.Linear(512, 256)
        self.fc3 = nn.Linear(256, 10)
        self.dropout = nn.Dropout(0.2)  # Prevent overfitting

    def forward(self, x):
        x = x.view(-1, 28*28)  # Flatten the image
        x = F.relu(self.fc1(x))
        x = self.dropout(x)
        x = F.relu(self.fc2(x))
        x = self.dropout(x)
        x = self.fc3(x)
        return F.log_softmax(x, dim=1)

model = MNISTNet()

# 2. Define loss function and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)

# 3. Training function
def train(model, train_loader, optimizer, epoch):
    model.train()  # Set to training mode
    for batch_idx, (data, target) in enumerate(train_loader):
        # Zero gradients
        optimizer.zero_grad()

        # Forward propagation
        output = model(data)

        # Calculate loss
        loss = criterion(output, target)

        # Backpropagation
        loss.backward()

        # Update parameters
        optimizer.step()

        if batch_idx % 100 == 0:
            print(f'Epoch {epoch} [{batch_idx * len(data)}/{len(train_loader.dataset)}]'
                  f' Loss: {loss.item():.6f}')

# 4. Testing function
def test(model, test_loader):
    model.eval()  # Set to evaluation mode
    test_loss = 0
    correct = 0

    with torch.no_grad():  # No gradients needed during testing
        for data, target in test_loader:
            output = model(data)
            test_loss += criterion(output, target).item()
            pred = output.argmax(dim=1)
            correct += pred.eq(target).sum().item()

    test_loss /= len(test_loader)
    accuracy = 100. * correct / len(test_loader.dataset)
    print(f'Test set: Average loss={test_loss:.4f}, '
          f'Accuracy={correct}/{len(test_loader.dataset)} ({accuracy:.2f}%)')

# 5. Training loop
# for epoch in range(1, 11):
#     train(model, train_loader, optimizer, epoch)
#     test(model, test_loader)

# 6. Save model
# torch.save(model.state_dict(), 'mnist_model.pth')

# 7. Load model
# model = MNISTNet()
# model.load_state_dict(torch.load('mnist_model.pth'))

4. GPU Acceleration: Speeding Up Training by 10 Times

Deep learning involves massive computations; a task that takes a day on a CPU can be completed in just one hour on a GPU.

Check if GPU is Available

import torch

print(f"CUDA available: {torch.cuda.is_available()}")
print(f"CUDA version: {torch.version.cuda}")
print(f"Number of GPUs: {torch.cuda.device_count()}")
if torch.cuda.is_available():
    print(f"GPU model: {torch.cuda.get_device_name(0)}")

Using GPU

# Set device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using device: {device}")

# Method 1: Move tensor to GPU
x = torch.randn(1000, 1000)
x_gpu = x.to(device)  # Recommended
# or x_gpu = x.cuda()

# Method 2: Create directly on GPU
a = torch.randn(1000, 1000, device=device)

# Method 3: Move model to GPU
model = MyNet()
model.to(device)

# Operations on GPU
a = torch.randn(1000, 1000, device=device)
b = torch.randn(1000, 1000, device=device)
c = a @ b  # Execute on GPU, much faster

# Move results back to CPU (for visualization or saving)
c_cpu = c.cpu()

Complete GPU Training Example

# Device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# Move model to GPU
model = MNISTNet().to(device)

# Training

def train_gpu(model, train_loader, optimizer, epoch):
    model.train()
    for batch_idx, (data, target) in enumerate(train_loader):
        # Move data to GPU
        data, target = data.to(device), target.to(device)

        optimizer.zero_grad()
        output = model(data)
        loss = criterion(output, target)
        loss.backward()
        optimizer.step()

        if batch_idx % 100 == 0:
            print(f'Epoch {epoch} Loss: {loss.item():.6f}')

Monitor GPU Usage

# Terminal command
nvidia-smi  # Check GPU status

# Real-time monitoring (refresh every second)
watch -n 1 nvidia-smi

# Use specific GPU only
export CUDA_VISIBLE_DEVICES=0    # Use only GPU 0
export CUDA_VISIBLE_DEVICES=0,1  # Use GPU 0 and 1

5. Environment Setup: One-Stop Configuration Guide

1. Conda Environment Management

Why use virtual environments?

Imagine you have two projects:

  • Project A requires PyTorch 1.12 + Python 3.8
  • Project B requires PyTorch 2.0 + Python 3.10

If you don’t use virtual environments, these two projects will conflict. Conda can create independent environments for each project.

# Create environment
conda create -n ai-env python=3.10

# Activate environment
conda activate ai-env

# Install packages
conda install numpy pandas matplotlib
conda install pytorch torchvision -c pytorch

# View installed packages
conda list

# Export environment (for easy sharing and reproduction)
conda env export > environment.yml

# Create environment from configuration file
conda env create -f environment.yml

# Delete environment
conda deactivate
conda env remove -n ai-env

2. Install PyTorch (GPU Version)

Visit https://pytorch.org to select the appropriate version:

# CUDA 11.8
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia

# CUDA 12.1
conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia

# Verification
python -c "import torch; print(torch.cuda.is_available())"
# Output: True

3. Jupyter Notebook: Interactive Development

Jupyter is great for data exploration and experimentation:

# Install
conda install jupyter notebook

# Start
jupyter notebook
# Browser automatically opens http://localhost:8888

Common Shortcuts:

  • <span><span>Shift + Enter</span></span>: Run the current cell and move to the next
  • <span><span>Ctrl + Enter</span></span>: Run the current cell
  • <span><span>A</span></span>: Insert a cell above
  • <span><span>B</span></span>: Insert a cell below
  • <span><span>DD</span></span>: Delete the cell
  • <span><span>Tab</span></span>: Code completion
  • <span><span>Shift + Tab</span></span>: View documentation

Magic Commands (especially useful in Jupyter):

# Measure execution time
%timeit some_function()

# View all variables
%whos

# Display matplotlib figures in the notebook
%matplotlib inline

# Run shell commands
!pip install transformers
!nvidia-smi

# Automatically reload modules (apply changes after modifying code)
%load_ext autoreload
%autoreload 2

4. Docker Containerization (Optional)

Docker can package the entire environment, ensuring it runs on any machine.

# Pull the official PyTorch image
docker pull pytorch/pytorch:2.0.1-cuda11.7-cudnn8-runtime

# Run container
docker run -it --gpus all -v $(pwd):/workspace pytorch/pytorch:2.0.1-cuda11.7-cudnn8-runtime

6. Essential AI Libraries

Scikit-learn: Traditional Machine Learning

from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

# Load data
from sklearn.datasets import load_iris
X, y = load_iris(return_X_y=True)

# Split data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

# Train model
clf = RandomForestClassifier(n_estimators=100)
clf.fit(X_train, y_train)

# Predict and evaluate
y_pred = clf.predict(X_test)
print(f"Accuracy: {accuracy_score(y_test, y_pred):.4f}")

Hugging Face Transformers: Pre-trained Model Library

The most powerful NLP library, allowing you to use models like BERT and GPT with just a few lines of code.

from transformers import pipeline

# Sentiment analysis
classifier = pipeline("sentiment-analysis")
result = classifier("I love this movie!")
print(result)
# [{'label': 'POSITIVE', 'score': 0.9998}]

# Text generation
generator = pipeline("text-generation", model="gpt2")
result = generator("Once upon a time", max_length=50)

# Question answering
qa = pipeline("question-answering")
context = "PyTorch is a deep learning framework developed by Meta"
question = "Who developed PyTorch?"
answer = qa(question=question, context=context)
print(answer['answer'])  # Meta

Matplotlib: Data Visualization

import matplotlib.pyplot as plt
import numpy as np

# Visualize training process
epochs = 50
train_loss = np.exp(-np.linspace(0, 3, epochs)) + np.random.randn(epochs) * 0.05
val_loss = np.exp(-np.linspace(0, 2.5, epochs)) + np.random.randn(epochs) * 0.08

plt.figure(figsize=(10, 6))
plt.plot(range(epochs), train_loss, label='Training Loss', linewidth=2)
plt.plot(range(epochs), val_loss, label='Validation Loss', linewidth=2)
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.title('Model Training Process')
plt.legend()
plt.grid(True, alpha=0.3)
plt.savefig('training_curve.png')

7. Best Practices for Project Structure

A clear project structure can improve development efficiency:

my-ai-project/
├── data/                    # Data directory
│   ├── raw/                # Raw data
│   ├── processed/          # Processed data
│   └── external/           # External data
├── notebooks/              # Jupyter notebooks
│   ├── 01-exploration.ipynb
│   └── 02-modeling.ipynb
├── src/                    # Source code
│   ├── __init__.py
│   ├── data/              # Data processing
│   │   └── dataset.py
│   ├── models/            # Model definitions
│   │   └── network.py
│   ├── training/          # Training code
│   │   └── train.py
│   └── utils/             # Utility functions
│       └── helpers.py
├── tests/                 # Test code
│   └── test_models.py
├── configs/               # Configuration files
│   └── config.yaml
├── requirements.txt       # Dependency list
├── environment.yml        # Conda environment configuration
├── README.md
└── .gitignore

8. Common Problem Solutions

Q1: What to do if GPU memory is insufficient?

  1. Reduce batch size
  2. Use gradient accumulation
  3. Mixed precision training (FP16)
  4. Use gradient checkpointing

Q2: Training speed too slow?

  1. Use GPU
  2. Increase batch size (if memory allows)
  3. Use the num_workers parameter in DataLoader (multi-process data loading)
  4. Use pin_memory=True (accelerate data transfer from CPU to GPU)

Q3: Model not converging?

  1. Check learning rate (it may be too high or too low)
  2. Check data preprocessing (normalization/standardization)
  3. Use learning rate schedulers
  4. Check for exploding/vanishing gradients (print gradient norms)

Q4: Conda environment conflicts?

  1. Delete the environment and recreate it
  2. Use mamba (much faster than conda):<span><span>conda install mamba -c conda-forge</span></span>
  3. Fix version numbers to avoid conflicts

In Conclusion

To do a good job, one must first sharpen their tools. Mastering these tools gives you the key to enter the world of AI:

NumPy: The foundation of numerical computation, the underlying basis for all calculations

Pandas: The tool for data processing, easily handling various tabular data

PyTorch: The deep learning framework, essential from beginner to research

GPU Acceleration: A 10x speed boost, essential for training large models

Development Environment: Conda + Jupyter, an efficient development process

Tool Ecosystem: Scikit-learn, Transformers, etc., standing on the shoulders of giants

These are not things to learn once and forget; they will be repeatedly used throughout your AI learning and working process. Recommendations:

  1. Learn by doing: Write code after finishing each part
  2. Check documentation: Look up official documentation for any APIs you don’t understand
  3. Take notes: Record commonly used code snippets to form your own toolbox
  4. Work on projects: Use these tools to complete a small project (like MNIST handwritten digit recognition)

Now, open Jupyter Notebook and start your first line of AI code!

Core Tools Quick Reference:

# NumPy
import numpy as np
arr = np.array([1, 2, 3])
arr.mean(), arr.std()

# Pandas
import pandas as pd
df = pd.DataFrame({'col': [1, 2, 3]})
df[df['col'] > 1]

# PyTorch
import torch
x = torch.randn(3, 3, requires_grad=True)
y = x.sum()
y.backward()

# GPU
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)

Leave a Comment