week2

目标:

尝试完成一个多分类任务的训练:一个随机向量,哪一维数字最大就属于第几类。

内容:

python 复制代码
import torch
import torch.nn as nn
import torch.optim as optim

# 1. 固定随机种子
torch.manual_seed(42)

# 2. 定义参数
input_dim = 5
num_classes = 5
num_samples = 10000
epochs = 100
batch_size = 128

# 3. 生成训练数据
X_train = torch.rand(num_samples, input_dim)

# 找出每个向量中最大值的位置
y_train = torch.argmax(X_train, dim=1)

# 生成独立测试数据
X_test = torch.rand(2000, input_dim)
y_test = torch.argmax(X_test, dim=1)

# 4. 定义神经网络
class Model(nn.Module):
    def __init__(self):
        super().__init__()

        self.fc = nn.Linear(5, 5)

    def forward(self, x):
        return self.fc(x)

model = Model()

# 5. 定义损失函数和优化器
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.01)

# 6. 训练模型
for epoch in range(epochs):
    model.train()

    indices = torch.randperm(num_samples)
    total_loss = 0
    total_correct = 0

    for start in range(0, num_samples, batch_size):
        idx = indices[start:start + batch_size]

        X_batch = X_train[idx]
        y_batch = y_train[idx]

        # 前向传播
        outputs = model(X_batch)

        # 计算损失
        loss = criterion(outputs, y_batch)

        # 梯度清零
        optimizer.zero_grad()

        # 反向传播
        loss.backward()

        # 更新参数
        optimizer.step()

        total_loss += loss.item() * len(idx)

        preds = torch.argmax(outputs, dim=1)
        total_correct += (preds == y_batch).sum().item()

    if (epoch + 1) % 10 == 0:
        print(
            f"Epoch {epoch + 1}, "
            f"Loss: {total_loss / num_samples:.4f}, "
            f"Accuracy: {total_correct / num_samples:.2%}"
        )

# 7. 测试模型
model.eval()

with torch.no_grad():
    outputs = model(X_test)
    predictions = torch.argmax(outputs, dim=1)

    accuracy = (predictions == y_test).float().mean()

    print(f"\n测试准确率: {accuracy.item():.2%}")

# 8. 预测新的随机向量
x = torch.tensor([
    [0.12, 0.35, 0.91, 0.43, 0.28]
])

with torch.no_grad():
    output = model(x)
    prediction = torch.argmax(output, dim=1)

print(f"预测类别:第 {prediction.item() + 1} 类")
print(f"真实类别:第 {torch.argmax(x).item() + 1} 类")
相关推荐
小oo呆1 小时前
【自然语言处理与大模型】如何编写优质的Skill?
人工智能
非优秀程序员9 小时前
第12章 烧钱无底洞!!!不烧就死,烧了也不知道啥时候死|《互联网产品恩仇录》
人工智能
Zelman94510 小时前
第06章-交互与观察动作空间
人工智能
冬奇Lab10 小时前
开源项目第258期:Harness² —— Google Research 的「递归式 Agent Harness 优化」,模型权重不动,只改 Agent 的外壳
人工智能·开源
字节跳动视频云技术团队11 小时前
每两家头部手机厂商就有一家选它:火山引擎如何成为 AI 手机助手的“隐形底座”?
人工智能
泯泷12 小时前
像项目经理一样指挥 AI 团队:管理者模式
人工智能·llm·agent
京东云开发者12 小时前
双11囤Token火了:京东搜索量暴涨7倍,AI生产力成新刚需
人工智能
ahccqw12 小时前
OpenCV 车辆数量统计
人工智能·opencv·计算机视觉
人工智能AI技术12 小时前
LangGraph 本地 build 频频翻车,最终在服务器跑通流式对话
人工智能