Skip to content
Open

111 #256

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
*.pkl
*.zip
*.pth
*.txt
*.ckpt
*.pyc
*.onnx
*.data
*.lock
*/__pycache__/
*.DS_Store
*.idea/
*.pytest_cache/
*.ruff_cache/
data/
.ipynb_checkpoints

15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: 当前文件",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${workspaceFolder}",
"justMyCode": true
}
]
}

14 changes: 14 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"python.defaultInterpreterPath": "${workspaceFolder}/pytorch-tutorial/bin/python",
"python.terminal.activateEnvironment": true,
"python.analysis.typeCheckingMode": "basic",
"[python]": {
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll": "explicit",
"source.organizeImports": "explicit"
}
}
}

9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@

## uv
```bash
uv venv --python 3.11
uv venv
source .venv/bin/activate
uv add xxx
uv sync
```
<p align="center"><img width="40%" src="logo/pytorch_logo_2018.svg" /></p>

--------------------------------------------------------------------------------
Expand Down
59 changes: 59 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
[project]
name = "tutorials"
version = "0.1.0"
description = "Add your description here"
requires-python = ">=3.9"
authors = [
{ name = "lihanghang", email = "[email protected]" } # 对象格式,符合新版规范
]
dependencies = [
"matplotlib>=3.9.4",
"onnx>=1.19.1",
"onnxruntime>=1.20.1",
"onnxscript>=0.5.7",
"pandas>=2.3.3",
"setuptools>=80.9.0",
"tabulate>=0.9.0",
"tensordict>=0.10.0",
"torchvision>=0.23.0",
]

[tool.poetry.dependencies]
aiohttp = "3.12.14" # 异步HTTP客户端/服务器库
urllib3 = "2.6.2" # HTTP客户端库,提供连接池和线程安全
orjson = ">=3.9.14,<4.0.0" # 高性能JSON序列化/反序列化库
uuid = "^1.30" # 用于生成和操作UUID的库
torch = "2.8.0" # PyTorch核心库,深度学习框架
contourpy = "1.3.0" # 用于绘制等高线的Python库,matplotlib依赖
cycler = "0.12.1" # 用于生成循环样式的工具库,matplotlib依赖
fonttools = "4.60.2" # 用于处理字体文件的库,matplotlib依赖
kiwisolver = "1.4.7" # 用于约束求解的库,matplotlib依赖
pyparsing = "3.3.1" # 用于解析字符串的库,matplotlib依赖
importlib-resources = "6.5.2" # 用于访问Python包资源的库
matplotlib = "3.9.4" # 数据可视化库,用于创建图表和图形
python-dateutil = "2.9.0.post0" # 日期时间处理扩展库,提供更强大的日期操作
six = "1.17.0" # Python 2和Python 3兼容性库
click = "8.1.8" # 命令行界面开发库,用于创建命令行工具
joblib = "1.5.3" # 用于并行计算和任务调度的库,常用于机器学习
nltk = "3.9.2" # 自然语言处理工具包,包含语料库和算法
regex = "2025.11.3" # 正则表达式扩展库,提供比标准re更强大的功能
tqdm = "4.67.1" # 进度条库,用于显示循环和任务的进度
pycocotools = "2.0.11" # COCO数据集工具库,用于目标检测、分割等任务
argparse = "1.4.0" # 命令行参数解析库,用于处理命令行输入
pandas = "2.3.3"
pytz = "2025.2"
tzdata = "2025.3"
tensordict = "0.10.0"
cloudpickle = "3.1.2"
importlib-metadata = "8.7.1"
pyvers = "0.1.0"
pillow = "11.3.0"
setuptools = "80.9.0"
[[tool.poetry.source]]
name = "aliyun"
url = "https://mirrors.huaweicloud.com/repository/pypi/simple/"
priority = "primary"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
186 changes: 107 additions & 79 deletions tutorials/01-basics/feedforward_neural_network/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,93 +2,121 @@
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
import ssl

# 前馈神经网络
"""
前馈神经网络是网络结构,反向传播是训练这个网络的核心算法。
神经网络的「骨架」—— 定义了「神经元如何分层、层与层如何连接、每层神经元数量、用什么激活函数」的整体框架,
决定了信息在网络中如何传递,是模型能拟合数据的基础,和 “反向传播(训练算法)” 是 “骨架” 和 “打磨骨架的方法” 的关系。

# Device configuration
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
"""
ssl._create_default_https_context = ssl._create_unverified_context

# Hyper-parameters
input_size = 784
hidden_size = 500
num_classes = 10
num_epochs = 5
batch_size = 100
learning_rate = 0.001

# MNIST dataset
train_dataset = torchvision.datasets.MNIST(root='../../data',
train=True,
transform=transforms.ToTensor(),
download=True)

test_dataset = torchvision.datasets.MNIST(root='../../data',
train=False,
transform=transforms.ToTensor())

# Data loader
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
batch_size=batch_size,
shuffle=True)

test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
batch_size=batch_size,
shuffle=False)

# Fully connected neural network with one hidden layer
class NeuralNet(nn.Module):
def __init__(self, input_size, hidden_size, num_classes):
super(NeuralNet, self).__init__()
self.fc1 = nn.Linear(input_size, hidden_size)
self.relu = nn.ReLU()
self.fc2 = nn.Linear(hidden_size, num_classes)
self.fc1 = nn.Linear(input_size, hidden_size) # 输入层→隐藏层
self.relu = nn.ReLU() # 激活函数
self.fc2 = nn.Linear(hidden_size, num_classes) # 隐藏层→输出层

def forward(self, x):
out = self.fc1(x)
out = self.relu(out)
out = self.fc2(out)
out = self.fc1(x) # 线性变换
out = self.relu(out) # 非线性激活
out = self.fc2(out) # 线性变换
return out

model = NeuralNet(input_size, hidden_size, num_classes).to(device)

# Loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)

# Train the model
total_step = len(train_loader)
for epoch in range(num_epochs):
for i, (images, labels) in enumerate(train_loader):
# Move tensors to the configured device
images = images.reshape(-1, 28*28).to(device)
labels = labels.to(device)

# Forward pass
outputs = model(images)
loss = criterion(outputs, labels)

# Backward and optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()

if (i+1) % 100 == 0:
print ('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'
.format(epoch+1, num_epochs, i+1, total_step, loss.item()))

# Test the model
# In test phase, we don't need to compute gradients (for memory efficiency)
with torch.no_grad():
correct = 0
total = 0
for images, labels in test_loader:
images = images.reshape(-1, 28*28).to(device)
labels = labels.to(device)
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()

print('Accuracy of the network on the 10000 test images: {} %'.format(100 * correct / total))

# Save the model checkpoint
torch.save(model.state_dict(), 'model.ckpt')
"""
FNN与之前学习的模型对比
模型 结构复杂度 学习能力 应用场景
线性回归 简单(单层) 只能学习线性关系 简单回归任务
逻辑回归 简单(单层+激活) 只能学习线性可分的分类 简单分类任务
前馈神经网络 复杂(多层+激活) 可学习复杂非线性关系 复杂分类/回归任务

"""
"""
前馈神经网络的作用
FNN的核心作用是学习输入与输出之间的复杂映射关系,主要用于两类任务:

分类任务:将输入数据分为不同类别(如代码中的MNIST数字分类)
回归任务:预测连续数值(如房价预测、股票价格预测)
其强大之处在于:通过多层结构和非线性激活,能够拟合几乎任何复杂的函数关系(这是神经网络的"万能近似定理")。
"""
if __name__ == '__main__':

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

# Hyper-parameters
input_size = 784
hidden_size = 500
num_classes = 10
num_epochs = 5
batch_size = 100
learning_rate = 0.001

# MNIST dataset
train_dataset = torchvision.datasets.MNIST(root='../../data',
train=True,
transform=transforms.ToTensor(),
download=True)

test_dataset = torchvision.datasets.MNIST(root='../../data',
train=False,
transform=transforms.ToTensor())

# Data loader
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
batch_size=batch_size,
shuffle=True)

test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
batch_size=batch_size,
shuffle=False)

model = NeuralNet(input_size, hidden_size, num_classes).to(device)

# Loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)

# Train the model
total_step = len(train_loader)
for epoch in range(num_epochs):
for i, (images, labels) in enumerate(train_loader):
# Move tensors to the configured device
images = images.reshape(-1, 28 * 28).to(device)
labels = labels.to(device)

# Forward pass
outputs = model(images)
loss = criterion(outputs, labels)

# Backward and optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()

if (i + 1) % 100 == 0:
print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'
.format(epoch + 1, num_epochs, i + 1, total_step, loss.item()))

# Test the model
# In test phase, we don't need to compute gradients (for memory efficiency)
with torch.no_grad():
correct = 0
total = 0
for images, labels in test_loader:
images = images.reshape(-1, 28 * 28).to(device)
labels = labels.to(device)
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()

print('Accuracy of the network on the 10000 test images: {} %'.format(100 * correct / total))

# Save the model checkpoint
torch.save(model.state_dict(), 'model.ckpt')
Loading