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
相关推荐
Ulyanov9 小时前
从桌面到云端:构建Web三维战场指挥系统
开发语言·前端·python·tkinter·pyvista·gui开发
星火开发设计9 小时前
C++ 函数定义与调用:程序模块化的第一步
java·开发语言·c++·学习·函数·知识
cypking9 小时前
二、前端Java后端对比指南
java·开发语言·前端
钟离墨笺10 小时前
Go语言--2go基础-->map
开发语言·后端·golang
lsx20240610 小时前
DOM CDATA
开发语言
Tony Bai10 小时前
Go 语言的“魔法”时刻:如何用 -toolexec 实现零侵入式自动插桩?
开发语言·后端·golang
CCPC不拿奖不改名10 小时前
两种完整的 Git 分支协作流程
大数据·人工智能·git·python·elasticsearch·搜索引擎·自然语言处理
Coding茶水间10 小时前
基于深度学习的交通标志检测系统演示与介绍(YOLOv12/v11/v8/v5模型+Pyqt5界面+训练代码+数据集)
开发语言·人工智能·深度学习·yolo·目标检测·机器学习
a努力。11 小时前
字节Java面试被问:TCP的BBR拥塞控制算法原理
java·开发语言·python·tcp/ip·elasticsearch·面试·职场和发展
费弗里11 小时前
一个小技巧轻松提升Dash应用debug效率
python·dash