Python day43

@浙大疏锦行 Python day43

python 复制代码
import torch
import numpy as np
import pandas as pd
import torchvision 
import torchvision.transforms as transforms
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F 
from torch.utils.data import DataLoader, Dataset

transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])

test_dataset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform)
train_dataset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)

train_dataloader = DataLoader(
    train_dataset,
    batch_size=32,
    shuffle=True,
)

test_dataloader = DataLoader(
    test_dataset,
    batch_size=32,
    shuffle=False,
)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(f"Using device: {device}")

class Net(nn.Module):
    def __init__(self):
        super(Net,self).__init__()
        # Convolutional Layer 1
        self.conv1 = nn.Conv2d(
            in_channels =3,
            out_channels=32,
            padding=2,
            kernel_size=3,
            stride=1
        )
        # Batch Normlization
        self.bn1 = nn.BatchNorm2d(num_features=32)
        # ReLU Activation
        self.relu1 = nn.ReLU()

        # CNN Layer 2
        self.conv2 = nn.Conv2d(
            in_channels=32,
            out_channels=64,
            padding=2,
            kernel_size=3,
            stride=1
        )
        self.bn2 = nn.BatchNorm2d(num_features=64)
        self.relu2 = nn.ReLU()


        # MLP
        self.fc1 = nn.Linear(in_features=64*8*8, out_features=128)

        # Dropout
        self.dropout = nn.Dropout(p=0.5)

        # Output Layer
        self.fc2 = nn.Linear(in_features=128, out_features=10)

    def forward(self,x):
        # CNN layer 1
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu1(x)

        # CNN layer 2
        x = self.conv2(x)
        x = self.bn2(x)
        x = self.relu2(x)

        # MLP
        x = x.view(-1, 64*8*8)
        x = self.fc1(x)         # MLP
        x = self.dropout(x)     # Dropout 随机丢弃神经元
        x = self.fc2(x)         # Output Layer
        return x            # 这里的x是未经过softmax的结果

model = Net()
model.to(device)
print(model)

criterion = nn.CrossEntropyLoss()                     # 交叉熵损失函数
optimizer = optim.Adam(model.parameters(), lr=0.001)  # Adam优化器

# 引入学习率调度器,在训练过程中动态调整学习率--训练初期使用较大的 LR 快速降低损失,训练后期使用较小的 LR 更精细地逼近全局最优解。
# 在每个 epoch 结束后,需要手动调用调度器来更新学习率,可以在训练过程中调用 scheduler.step()
scheduler = optim.lr_scheduler.ReduceLROnPlateau(
    optimizer,        # 指定要控制的优化器(这里是Adam)
    mode='min',       # 监测的指标是"最小化"(如损失函数)
    patience=3,       # 如果连续3个epoch指标没有改善,才降低LR
    factor=0.5        # 降低LR的比例(新LR = 旧LR × 0.5)
)

def train():
    pass
相关推荐
默_笙1 天前
🍙 给每个请求过安检:FastAPI 是怎么把校验写进类型注解的
python
小羊没烦恼!1 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
qq_426003961 天前
启动playwright录制codegen生成自动化测试脚本
python·自动化
虎头金猫1 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
长沙三为智能科技1 天前
家政小程序开发从0到上线:五阶段交付流程与验收清单
python
伞伞悦读1 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
只睡四小时1 天前
Canvas 弹道联机实战:700 行 + 固定时间步长
python·websocket·html5·游戏开发·canvas
C语言小火车1 天前
C/C++ 为什么需要编译器?
开发语言·c++
奇思妙想聪明勤奋的小羊1 天前
DeepAgents第5章:子Agent 与上下文隔离—让 Agent学会委派
人工智能·python·学习·语言模型
lpfasd1231 天前
2026年第38周GitHub趋势周报
python·科技·github