day37简单的神经网络@浙大疏锦行

day37简单的神经网络@浙大疏锦行

使用 sklearn 的 load_digits 数据集 (8x8 像素的手写数字) 进行 MLP 训练。

python 复制代码
import torch
import torch.nn as nn
import torch.optim as optim
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
import numpy as np
import matplotlib.pyplot as plt

# 1. 加载数据
digits = load_digits()
X = digits.data
y = digits.target

print(f"数据形状: {X.shape}")
print(f"标签形状: {y.shape}")

# 查看一张图片
plt.imshow(digits.images[0], cmap='gray')
plt.title(f"Label: {y[0]}")
plt.show()

数据形状: (1797, 64) 标签形状: (1797,)

python 复制代码
# 2. 数据预处理
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# 归一化
scaler = MinMaxScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

# 转换为 Tensor
X_train = torch.FloatTensor(X_train)
y_train = torch.LongTensor(y_train)
X_test = torch.FloatTensor(X_test)
y_test = torch.LongTensor(y_test)

print("训练集 Tensor 形状:", X_train.shape)
print("测试集 Tensor 形状:", X_test.shape)

训练集 Tensor 形状: torch.Size(1437, 64)

测试集 Tensor 形状: torch.Size(360, 64)

python 复制代码
# 3. 定义模型
class MLP(nn.Module):
    def __init__(self):
        super(MLP, self).__init__()
        # 输入层 64 (8*8像素) -> 隐藏层 32 -> 输出层 10 (0-9数字)
        self.fc1 = nn.Linear(64, 32)
        self.relu = nn.ReLU()
        self.fc2 = nn.Linear(32, 10) 
    
    def forward(self, x):
        out = self.fc1(x)
        out = self.relu(out)
        out = self.fc2(out)
        return out

model = MLP()
print(model)

MLP(

(fc1): Linear(in_features=64, out_features=32, bias=True) (relu): ReLU()

(fc2): Linear(in_features=32, out_features=10, bias=True)

)

python 复制代码
# 4. 定义损失函数和优化器
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.1) # 学习率稍微调大一点,或者增加epoch
python 复制代码
# 5. 训练模型
num_epochs = 2000
losses = []

for epoch in range(num_epochs):
    # 前向传播
    outputs = model(X_train)
    loss = criterion(outputs, y_train)
    
    # 反向传播和优化
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    
    losses.append(loss.item())
    
    if (epoch + 1) % 100 == 0:
        print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}')
python 复制代码
# 6. 可视化损失
plt.plot(range(num_epochs), losses)
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.title('Training Loss')
plt.show()
python 复制代码
# 7. 模型评估
with torch.no_grad():
    # 训练集准确率
    outputs_train = model(X_train)
    _, predicted_train = torch.max(outputs_train, 1)
    accuracy_train = (predicted_train == y_train).sum().item() / y_train.size(0)
    
    # 测试集准确率
    outputs_test = model(X_test)
    _, predicted_test = torch.max(outputs_test, 1)
    accuracy_test = (predicted_test == y_test).sum().item() / y_test.size(0)
    
    print(f'训练集准确率: {accuracy_train:.4f}')
    print(f'测试集准确率: {accuracy_test:.4f}')

@浙大疏锦行

相关推荐
兵慌码乱7 小时前
基于 MediaPipe 与 PySide2 的手势交互音乐控制系统实现:轻量化视觉交互全流程解析
python·opencv·计算机视觉·人机交互·手势识别·mediapipe·pyside2
luckdewei9 小时前
FastAPI 资产管理系统实战:复杂 ORM 关联、Alembic 迁移与 N+1 查询优化
python
aqi0016 小时前
15天学会AI应用开发(八)使用向量数据库实现RAG功能
人工智能·python·大模型·ai编程·ai应用
Csvn17 小时前
`functools.lru_cache` —— 一行代码搞定缓存加速
后端·python
金銀銅鐵1 天前
[Python] 从《千字文》中随机挑选汉字
后端·python
cup112 天前
[技术复盘] Windows Python 打包实战:Nuitka 环境踩坑总结与 CI 自动化构建全指南
python·ai·环境变量·ci·nuitka·skill
aqi002 天前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
金銀銅鐵2 天前
用 Python 实现 Take-Away 游戏
python·游戏