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
相关推荐
人道领域6 分钟前
SSM框架从入门到入土(AOP面向切面编程)
java·开发语言
铅笔侠_小龙虾6 分钟前
Flutter 实战: 计算器
开发语言·javascript·flutter
全栈老石23 分钟前
Python 异步生存手册:给被 JS async/await 宠坏的全栈工程师
后端·python
2的n次方_24 分钟前
Runtime 执行提交机制:NPU 硬件队列的管理与任务原子化下发
c语言·开发语言
梨落秋霜31 分钟前
Python入门篇【模块/包】
python
2501_9447114342 分钟前
JS 对象遍历全解析
开发语言·前端·javascript
凡人叶枫1 小时前
C++中智能指针详解(Linux实战版)| 彻底解决内存泄漏,新手也能吃透
java·linux·c语言·开发语言·c++·嵌入式开发
Tony Bai1 小时前
再见,丑陋的 container/heap!Go 泛型堆 heap/v2 提案解析
开发语言·后端·golang
小糯米6011 小时前
C++顺序表和vector
开发语言·c++·算法
froginwe112 小时前
JavaScript 函数调用
开发语言