返回学习路线
8模型进阶

第 8 周:PyTorch 实战

Dataset/DataLoader、训练循环、模型部署

阅读指南与约定

  • 所有数学公式使用 LaTeX(行内 \( ... \) 或块级 $$ ... $$)。
  • 所有代码示例使用 PyTorch,并尽量给出可直接运行的最小脚本(可在 CPU 上运行;GPU/分布式部分给出可选分支)。
  • 配图引用使用 Markdown 图片语法,路径形如 /sec08_xxx.png(本文提供占位引用)。
  • 架构图优先使用 Mermaid(易复制到笔记/文档系统),并配合图片占位引用。

0. 环境准备(10分钟)

0.1 版本建议

  • Python >= 3.10(推荐 3.11/3.12)
  • PyTorch >= 2.x(含 torch.compile 可选)
  • torchvision / torchaudio(按需)
  • datasets(HuggingFace)
  • albumentations(图像增强)

0.2 安装命令(示例)

pip install -U torch torchvision torchaudio
# 可选
pip install -U datasets albumentations opencv-python
pip install -U tensorboard wandb optuna

0.3 可复现性:随机种子与确定性

seed(PRNGpython,PRNGnumpy,PRNGtorch,CuDNN)\text{seed} \Rightarrow (\text{PRNG}_{\text{python}},\text{PRNG}_{\text{numpy}},\text{PRNG}_{\text{torch}},\text{CuDNN})

import os, random
import numpy as np
import torch

def seed_everything(seed: int = 42):
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    os.environ['PYTHONHASHSEED'] = str(seed)
    # 确定性(可能降低性能)
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False

seed_everything(42)

可复现性影响因素示意:数据顺序、算子非确定性、并行线程


8.1 PyTorch 数据管道

数据管道解决的问题:

  1. 把原始数据映射为模型可消费的张量(dtype、shape、device、normalize)。
  2. 在训练时高吞吐读取(多进程 worker、pin_memory、prefetch)。
  3. 把样本组织成 batch(padding、对齐、变长序列)。

数据管道架构:Storage→Dataset→Sampler→DataLoader→Batch→Model

8.1.1 Dataset 与 DataLoader 深入

8.1.1.1 Dataset 抽象基类与契约

  • __len__():返回样本数量。
  • __getitem__(idx):返回第 idx 个样本。

形式化地,把数据集看作映射:

D:i(xi,yi),i{0,1,,N1}\mathcal{D}: i \mapsto (x_i, y_i),\quad i\in\{0,1,\dots,N-1\}

from torch.utils.data import Dataset

class MyDataset(Dataset):
    def __len__(self):
        raise NotImplementedError

    def __getitem__(self, idx):
        raise NotImplementedError

8.1.1.2 自定义Dataset完整实现(图片)

from pathlib import Path
from PIL import Image
import numpy as np
import torch
from torch.utils.data import Dataset

class ImageFolderDataset(Dataset):
    """
    目录:
    root/
      class0/*.png
      class1/*.png
    """
    def __init__(self, root: str, transform=None):
        self.root = Path(root)
        self.transform = transform
        classes = sorted([p.name for p in self.root.iterdir() if p.is_dir()])
        self.class_to_idx = {c:i for i,c in enumerate(classes)}
        self.samples = []
        for c in classes:
            for fp in (self.root/c).glob('*'):
                if fp.suffix.lower() in {'.png','.jpg','.jpeg','.bmp'}:
                    self.samples.append((fp, self.class_to_idx[c]))

    def __len__(self):
        return len(self.samples)

    def __getitem__(self, idx: int):
        fp, label = self.samples[idx]
        img = Image.open(fp).convert('RGB')
        if self.transform is not None:
            x = self.transform(img)
        else:
            x = torch.from_numpy(np.array(img)).permute(2,0,1).float() / 255.0
        y = torch.tensor(label, dtype=torch.long)
        return x, y

自定义Image Dataset:索引→路径→解码→Transform→Tensor

8.1.1.3 自定义Dataset完整实现(文本)

文本工程要点:变长序列通常在 collate_fn 里 padding。

import torch
from torch.utils.data import Dataset

class CharTokenizer:
    def __init__(self):
        self.vocab = {ch:i+1 for i,ch in enumerate('abcdefghijklmnopqrstuvwxyz')}
        self.pad_id = 0
    def __call__(self, text: str):
        return [self.vocab.get(ch, 0) for ch in text.lower() if ch.isalpha()]

class ToyTextDataset(Dataset):
    def __init__(self, texts, labels, tokenizer):
        self.texts = list(texts)
        self.labels = list(labels)
        self.tok = tokenizer
    def __len__(self):
        return len(self.texts)
    def __getitem__(self, idx):
        ids = self.tok(self.texts[idx])
        return {
            'input_ids': torch.tensor(ids, dtype=torch.long),
            'label': torch.tensor(self.labels[idx], dtype=torch.long)
        }

padding 的数学描述:若 batch 内第 k 个序列长度 LkL_k,padding 到 Lmax=maxkLkL_{\max}=\max_k L_k

x~k=[xk,0,0,,0]ZLmax\tilde{x}_k=[x_k,0,0,\dots,0]\in\mathbb{Z}^{L_{\max}}

8.1.1.4 DataLoader参数详解与性能要点

DataLoader:(D,sampler,collate){(Xb,Yb)}b=1B\text{DataLoader}:(\mathcal{D},\text{sampler},\text{collate})\mapsto\{(X_b,Y_b)\}_{b=1}^B

  • batch_size:吞吐与收敛的工程折中点。
  • shuffle:训练集通常 True(或显式 sampler)。
  • num_workers:IO/解码瓶颈时提升明显。
  • pin_memory:GPU 训练推荐 True(加速 H2D)。
  • drop_last:BN/对齐需求时常用 True。
from torch.utils.data import DataLoader

loader = DataLoader(
    dataset=ImageFolderDataset('/path/to/data'),
    batch_size=64,
    shuffle=True,
    num_workers=4,
    pin_memory=True,
    drop_last=False,
)

for x, y in loader:
    pass

8.1.1.5 collate_fn:自定义批处理(变长padding)

import torch

def pad_collate(batch, pad_id: int = 0):
    input_ids = [b['input_ids'] for b in batch]
    labels = torch.stack([b['label'] for b in batch])
    lengths = torch.tensor([len(x) for x in input_ids], dtype=torch.long)
    max_len = int(lengths.max().item())

    padded = torch.full((len(batch), max_len), pad_id, dtype=torch.long)
    for i, ids in enumerate(input_ids):
        padded[i, :len(ids)] = ids

    attention_mask = (padded != pad_id).long()
    return {
        'input_ids': padded,
        'attention_mask': attention_mask,
        'lengths': lengths,
        'labels': labels,
    }

8.1.1.6 sampler策略:WeightedRandomSampler(类别不平衡)

采样概率:P(i)=wijwjP(i)=\frac{w_i}{ \sum_j w_j}

import torch
from torch.utils.data import DataLoader
from torch.utils.data.sampler import WeightedRandomSampler

labels = torch.tensor([0,0,0,0,1,1])
class_counts = torch.bincount(labels)
class_weights = 1.0 / class_counts.float()
weights = class_weights[labels]

sampler = WeightedRandomSampler(weights=weights, num_samples=len(weights), replacement=True)

# 这里只是演示index采样
dataset = list(range(len(labels)))
loader = DataLoader(dataset, batch_size=2, sampler=sampler)
for batch in loader:
    print(batch)

Sampler在DataLoader中的位置:决定 index 序列


8.1.2 数据预处理与增强

增强可以视为随机变换族 TTT\sim\mathcal{T}x~=T(x)\tilde{x}=T(x)

8.1.2.1 torchvision.transforms

from torchvision import transforms

train_tf = transforms.Compose([
    transforms.RandomResizedCrop(32, scale=(0.8, 1.0)),
    transforms.RandomHorizontalFlip(p=0.5),
    transforms.ColorJitter(0.2, 0.2, 0.2, 0.05),
    transforms.RandomRotation(15),
    transforms.ToTensor(),
    transforms.Normalize((0.4914,0.4822,0.4465), (0.247,0.243,0.261)),
])

val_tf = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.4914,0.4822,0.4465), (0.247,0.243,0.261)),
])

常用增强链路:随机裁剪/翻转/颜色抖动/旋转→normalize

8.1.2.2 Albumentations

import albumentations as A
from albumentations.pytorch import ToTensorV2

train_aug = A.Compose([
    A.RandomResizedCrop(32, 32, scale=(0.8, 1.0)),
    A.HorizontalFlip(p=0.5),
    A.ColorJitter(p=0.5),
    A.ShiftScaleRotate(0.05, 0.1, 15, p=0.5),
    A.Normalize((0.4914,0.4822,0.4465), (0.247,0.243,0.261)),
    ToTensorV2(),
])

8.1.2.3 自定义Transform

import torch

class RandomGaussianNoise:
    def __init__(self, sigma=0.05, p=0.5):
        self.sigma = sigma
        self.p = p
    def __call__(self, x: torch.Tensor):
        if torch.rand(()) < self.p:
            x = torch.clamp(x + torch.randn_like(x) * self.sigma, 0.0, 1.0)
        return x

8.1.2.4 测试时增强(TTA)

分类常见:对 logits 的 softmax 概率平均:

p^(yx)=1Mm=1Msoftmax(f(Tm(x)))\hat{p}(y\mid x)=\frac{1}{M}\sum_{m=1}^M\text{softmax}(f(T_m(x)))

import torch

@torch.no_grad()
def tta_predict(model, x, tta_transforms):
    model.eval()
    probs = []
    for tf in tta_transforms:
        logits = model(tf(x))
        probs.append(torch.softmax(logits, dim=-1))
    return torch.stack(probs).mean(0)

8.1.3 常用数据集加载

8.1.3.1 torchvision.datasets

from torchvision.datasets import CIFAR10, MNIST
from torchvision import transforms

root = './data'
train_ds = CIFAR10(root, train=True, download=True, transform=transforms.ToTensor())
val_ds = CIFAR10(root, train=False, download=True, transform=transforms.ToTensor())
mnist = MNIST(root, train=True, download=True, transform=transforms.ToTensor())

8.1.3.2 HuggingFace datasets

from datasets import load_dataset

ds = load_dataset('imdb')
print(ds['train'][0].keys())

8.1.3.3 自定义数据集组织方式(工程建议)

  • raw/(只读原始数据)
  • processed/(可再生预处理产物)
  • splits/(train/val/test 切分清单)
  • metadata/(label映射、统计量、hash等)

数据集工程目录结构示意:raw/processed/splits/metadata


8.2 PyTorch 模型构建

模型构建流程:定义Module→forward→loss→backward→step

8.2.1 nn.Module 深入

8.2.1.1 init 与 forward

import torch
import torch.nn as nn

class MLP(nn.Module):
    def __init__(self, in_dim: int, hidden: int, out_dim: int):
        super().__init__()
        self.net = nn.Sequential(
            nn.Linear(in_dim, hidden),
            nn.ReLU(),
            nn.Linear(hidden, out_dim),
        )
    def forward(self, x):
        return self.net(x)

model = MLP(128, 256, 10)
print(model(torch.randn(4,128)).shape)

8.2.1.2 parameters / named_parameters / buffers

import torch
import torch.nn as nn

class WithBuffer(nn.Module):
    def __init__(self):
        super().__init__()
        self.linear = nn.Linear(4, 4)
        self.register_buffer('scale', torch.tensor(0.5))
    def forward(self, x):
        return self.linear(x) * self.scale

m = WithBuffer()
print('buffers:', [n for n,_ in m.named_buffers()])
print('params:', [n for n,_ in m.named_parameters()])

8.2.1.3 子模块管理:add_module / children / modules

import torch
import torch.nn as nn

class Stack(nn.Module):
    def __init__(self, dims):
        super().__init__()
        for i, (a,b) in enumerate(zip(dims[:-1], dims[1:])):
            self.add_module(f'layer_{i}', nn.Linear(a,b))
    def forward(self, x):
        for layer in self.children():
            x = layer(x)
        return x

net = Stack([8,16,16,4])
print(net(torch.randn(2,8)).shape)

8.2.1.4 Hook:register_forward_hook / register_full_backward_hook

import torch
import torch.nn as nn

model = nn.Sequential(nn.Linear(8,16), nn.ReLU(), nn.Linear(16,4))
acts, grads = {}, {}

handle_f = model[0].register_forward_hook(lambda m,i,o: acts.setdefault('fc1', o.detach().cpu()))

# backward hook 推荐用 full backward hook
handle_b = model[0].register_full_backward_hook(lambda m, gin, gout: grads.setdefault('fc1', gout[0].detach().cpu()))

x = torch.randn(2,8)
logits = model(x)
loss = logits.pow(2).mean()
loss.backward()

print('act:', acts['fc1'].shape)
print('grad:', grads['fc1'].shape)

handle_f.remove(); handle_b.remove()

Hook捕获激活/梯度:调试与可视化管线


8.2.2 常用网络层

8.2.2.1 卷积层:Conv1d/2d/3d

2D 卷积输出尺寸:

ight floor,\quad W_{out}=\left\lfloor rac{W_{in}+2p-d(k-1)-1}{s}+1 ight floor$$ ```python import torch import torch.nn as nn x = torch.randn(8, 3, 32, 32) conv = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1) y = conv(x) print(y.shape) ``` 参数量:$\#params=C_{out}\cdot(C_{in}\cdot k_h\cdot k_w + 1)$。 #### 8.2.2.2 池化层 ```python import torch import torch.nn as nn x = torch.randn(2, 16, 32, 32) mp = nn.MaxPool2d(2,2) ap = nn.AvgPool2d(2,2) aap = nn.AdaptiveAvgPool2d((1,1)) print(mp(x).shape, ap(x).shape, aap(x).shape) ``` #### 8.2.2.3 归一化层 BatchNorm:$\hat{x}=\frac{x-\mu_\mathcal{B}}{\sqrt{\sigma_\mathcal{B}^2+\epsilon}},\ y=\gamma\hat{x}+\beta$。 ```python import torch import torch.nn as nn x = torch.randn(4, 32, 16, 16) bn = nn.BatchNorm2d(32) ln = nn.LayerNorm([32,16,16]) gn = nn.GroupNorm(8, 32) inorm = nn.InstanceNorm2d(32) for layer in [bn, ln, gn, inorm]: y = layer(x) print(layer.__class__.__name__, y.shape) ``` #### 8.2.2.4 循环层:LSTM/GRU ```python import torch import torch.nn as nn seq_len, batch, feat = 12, 4, 32 x = torch.randn(seq_len, batch, feat) lstm = nn.LSTM(feat, 64, num_layers=2) out, (h, c) = lstm(x) print(out.shape, h.shape, c.shape) ``` #### 8.2.2.5 Transformer:MultiheadAttention 缩放点积注意力: $$\text{Attention}(Q,K,V)=\text{softmax}\left(\frac{QK^\top}{\sqrt{d_k}}\right)V$$ ```python import torch import torch.nn as nn B, T, D = 2, 5, 32 x = torch.randn(T, B, D) # [T,B,D] mha = nn.MultiheadAttention(embed_dim=D, num_heads=4) out, attn = mha(x, x, x, need_weights=True) print(out.shape, attn.shape) ``` #### 8.2.2.6 嵌入层:nn.Embedding ```python import torch import torch.nn as nn vocab_size, dim = 10000, 128 emb = nn.Embedding(vocab_size, dim, padding_idx=0) ids = torch.randint(0, vocab_size, (4, 12)) x = emb(ids) print(x.shape) # [4,12,128] ``` --- ### 8.2.3 模型设计模式 #### 8.2.3.1 Sequential vs ModuleList vs ModuleDict - `nn.Sequential`:适合纯顺序结构。 - `nn.ModuleList`:列表形式注册子模块(forward里自己写循环)。 - `nn.ModuleDict`:字典形式注册(动态选择子模块)。 ```python import torch import torch.nn as nn # ModuleList 示例:可变深度 class DeepMLP(nn.Module): def __init__(self, in_dim=128, hidden=256, depth=4, out_dim=10): super().__init__() self.inp = nn.Linear(in_dim, hidden) self.blocks = nn.ModuleList([nn.Sequential(nn.ReLU(), nn.Linear(hidden, hidden)) for _ in range(depth)]) self.out = nn.Linear(hidden, out_dim) def forward(self, x): x = self.inp(x) for blk in self.blocks: x = x + blk(x) # 这里顺便做残差 return self.out(x) model = DeepMLP(depth=3) print(model(torch.randn(2,128)).shape) ``` #### 8.2.3.2 残差连接实现(ResNet风格) ```python import torch import torch.nn as nn class ResidualBlock(nn.Module): def __init__(self, c): super().__init__() self.net = nn.Sequential( nn.Conv2d(c, c, 3, padding=1, bias=False), nn.BatchNorm2d(c), nn.ReLU(inplace=True), nn.Conv2d(c, c, 3, padding=1, bias=False), nn.BatchNorm2d(c), ) self.act = nn.ReLU(inplace=True) def forward(self, x): return self.act(x + self.net(x)) blk = ResidualBlock(16) print(blk(torch.randn(2,16,32,32)).shape) ``` ![残差块结构示意:x→F(x)→x+F(x)→ReLU](/sec08_010.png) #### 8.2.3.3 注意力模块实现(SE/通道注意力简化版) ```python import torch import torch.nn as nn class SEBlock(nn.Module): def __init__(self, c, r=16): super().__init__() self.pool = nn.AdaptiveAvgPool2d((1,1)) self.fc = nn.Sequential( nn.Linear(c, c//r), nn.ReLU(inplace=True), nn.Linear(c//r, c), nn.Sigmoid(), ) def forward(self, x): b,c,_,_ = x.shape s = self.pool(x).view(b,c) w = self.fc(s).view(b,c,1,1) return x * w se = SEBlock(32) print(se(torch.randn(2,32,16,16)).shape) ``` #### 8.2.3.4 预训练模型加载:torchvision.models 与 timm ```python import torch from torchvision.models import resnet18 model = resnet18(weights=None) # 若有网络可用:weights='DEFAULT' print(sum(p.numel() for p in model.parameters())/1e6, 'M params') ``` ```python # timm 示例(可选) # import timm # model = timm.create_model('vit_base_patch16_224', pretrained=True, num_classes=10) ``` ```mermaid flowchart LR A[输入图像] --> B[Backbone (预训练)] B --> C[特征向量] --> D[任务头 (分类/检测/分割)] --> E[输出] ``` ![预训练模型使用架构图:Backbone + Head](/sec08_011.png) --- ## 8.3 训练工程 ![训练工程全景:数据→前向→loss→反向→优化→日志→checkpoint](/sec08_012.png) ### 8.3.1 完整训练循环 训练循环的工程目标: - 结构清晰:train/val/test 分离 - 可扩展:支持 AMP、梯度累积、分布式 - 可观测:loss/metric/lr/时间/显存 #### 8.3.1.1 标准训练/验证循环(参考实现) ```python import time import torch import torch.nn as nn @torch.no_grad() def evaluate(model, loader, device): model.eval() total, correct, loss_sum = 0, 0, 0.0 criterion = nn.CrossEntropyLoss() for x, y in loader: x, y = x.to(device), y.to(device) logits = model(x) loss = criterion(logits, y) loss_sum += loss.item() * x.size(0) pred = logits.argmax(dim=1) correct += (pred == y).sum().item() total += x.size(0) return { 'loss': loss_sum / total, 'acc': correct / total, } def train_one_epoch(model, loader, optimizer, device, scaler=None, grad_clip=None, accum_steps=1): model.train() criterion = nn.CrossEntropyLoss() total, correct, loss_sum = 0, 0, 0.0 t0 = time.time() optimizer.zero_grad(set_to_none=True) for step, (x, y) in enumerate(loader): x, y = x.to(device), y.to(device) if scaler is None: logits = model(x) loss = criterion(logits, y) / accum_steps loss.backward() else: with torch.cuda.amp.autocast(): logits = model(x) loss = criterion(logits, y) / accum_steps scaler.scale(loss).backward() if (step + 1) % accum_steps == 0: if grad_clip is not None: if scaler is not None: scaler.unscale_(optimizer) torch.nn.utils.clip_grad_norm_(model.parameters(), grad_clip) if scaler is None: optimizer.step() else: scaler.step(optimizer) scaler.update() optimizer.zero_grad(set_to_none=True) loss_sum += loss.item() * x.size(0) * accum_steps pred = logits.argmax(1) correct += (pred == y).sum().item() total += x.size(0) return { 'loss': loss_sum / total, 'acc': correct / total, 'sec': time.time() - t0, } ``` #### 8.3.1.2 Early Stopping(早停) 早停本质是在验证集指标 $m_t$ 不再提升时停止: $$\text{stop if }\max_{k\le t} m_k - m_t < \epsilon \text{ for }p ext{ consecutive epochs}$$ ```python class EarlyStopping: def __init__(self, patience=10, mode='max', min_delta=0.0): self.patience = patience self.mode = mode self.min_delta = min_delta self.best = None self.bad_epochs = 0 def step(self, metric): if self.best is None: self.best = metric return False improve = (metric > self.best + self.min_delta) if self.mode == 'max' else (metric < self.best - self.min_delta) if improve: self.best = metric self.bad_epochs = 0 else: self.bad_epochs += 1 return self.bad_epochs >= self.patience ``` #### 8.3.1.3 梯度裁剪 裁剪 $\lVert g\rVert_2$ 到阈值 $c$: $$g \leftarrow g\cdot\min\left(1,\frac{c}{\lVert g\rVert_2+\epsilon} ight)$$ #### 8.3.1.4 学习率调度器 ```python import torch from torch.optim import SGD from torch.optim.lr_scheduler import StepLR, CosineAnnealingLR, OneCycleLR, ReduceLROnPlateau model = torch.nn.Linear(10, 2) opt = SGD(model.parameters(), lr=0.1, momentum=0.9) step_lr = StepLR(opt, step_size=30, gamma=0.1) cos_lr = CosineAnnealingLR(opt, T_max=100) # OneCycleLR 需要指定 steps_per_epoch 与 epochs # onecycle = OneCycleLR(opt, max_lr=0.1, steps_per_epoch=100, epochs=10) # plateau = ReduceLROnPlateau(opt, mode='max', factor=0.1, patience=5) ``` #### 8.3.1.5 混合精度训练(AMP) ```python import torch scaler = torch.cuda.amp.GradScaler(enabled=torch.cuda.is_available()) # 在 train_one_epoch 里传入 scaler 即可 ``` #### 8.3.1.6 梯度累积(模拟大batch) 若显存限制导致 batch_size 太小,可以设 `accum_steps=k`,等价于把 k 个小 batch 的梯度相加再 step。 --- ### 8.3.2 GPU训练 #### 8.3.2.1 设备管理 ```python import torch device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = torch.nn.Linear(10,2).to(device) x = torch.randn(4,10, device=device) y = model(x) print(device, y.device) ``` #### 8.3.2.2 DataParallel vs DistributedDataParallel - `DataParallel`:单进程多卡,使用方便但性能/可控性一般,不推荐用于严肃训练。 - `DistributedDataParallel (DDP)`:多进程多卡,通信高效,是工业标准。 ```mermaid flowchart LR subgraph P0[进程0] A0[GPU0 model] end subgraph P1[进程1] A1[GPU1 model] end A0 <-- allreduce gradients --> A1 ``` ![DDP梯度同步:all-reduce](/sec08_013.png) #### 8.3.2.3 分布式训练基础概念 - **world_size**:总进程数 - **rank**:当前进程编号 - **local_rank**:当前节点内 GPU 编号 - **backend**:通信后端(nccl/gloo) #### 8.3.2.4 显存优化技巧(工程清单) - AMP(fp16/bf16) - 梯度累积(减少激活峰值) - activation checkpointing(重算换显存) - 减小输入分辨率/网络宽度 - `torch.backends.cudnn.benchmark=True`(可带来速度提升,但可能影响确定性) --- ### 8.3.3 训练监控 #### 8.3.3.1 TensorBoard 集成 ```python from torch.utils.tensorboard import SummaryWriter writer = SummaryWriter('runs/week8') # 每个epoch记录 # writer.add_scalar('train/loss', train_loss, epoch) # writer.add_scalar('val/acc', val_acc, epoch) # writer.add_scalar('lr', optimizer.param_groups[0]['lr'], epoch) writer.close() ``` #### 8.3.3.2 Weights & Biases(wandb) ```python # import wandb # wandb.init(project='week8-cifar10') # wandb.log({'train/loss': loss, 'val/acc': acc}) # wandb.finish() ``` #### 8.3.3.3 自定义指标追踪(AverageMeter) ```python class AverageMeter: def __init__(self): self.reset() def reset(self): self.sum = 0.0 self.cnt = 0 def update(self, val, n=1): self.sum += float(val) * n self.cnt += int(n) @property def avg(self): return self.sum / max(1, self.cnt) ``` #### 8.3.3.4 学习率 finder 工具(思路) LR finder 通常做法:在一个 epoch 内指数提升学习率并记录 loss 曲线,选取 loss 开始下降且未发散的区间。 ![LR finder曲线:lr↑ vs loss](/sec08_014.png) --- ## 8.4 模型评估与调优 ### 8.4.1 模型验证策略 #### 8.4.1.1 K折交叉验证(K-fold) ```python import numpy as np from sklearn.model_selection import KFold # 假设你有 dataset 与训练函数 train_and_eval(fold_indices) N = 1000 indices = np.arange(N) kf = KFold(n_splits=5, shuffle=True, random_state=42) for fold, (tr, va) in enumerate(kf.split(indices)): print('fold', fold, 'train', len(tr), 'val', len(va)) ``` #### 8.4.1.2 超参数搜索:Grid/Random/Optuna ```python # Optuna最小示例(可选,需要 pip install optuna) # import optuna # def objective(trial): # lr = trial.suggest_float('lr', 1e-4, 1e-1, log=True) # wd = trial.suggest_float('wd', 1e-6, 1e-2, log=True) # # train... return val_acc # return 0.8 # study = optuna.create_study(direction='maximize') # study.optimize(objective, n_trials=30) ``` #### 8.4.1.3 模型集成(Ensemble) 分类常见:对概率平均:$\bar{p}=\frac{1}{M}\sum_m p_m$。 --- ### 8.4.2 正则化技术 #### 8.4.2.1 Dropout Dropout 可视为对激活做 Bernoulli 掩码:$\tilde{h}=m\odot h,\ m\sim\text{Bernoulli}(1-p)$。 ```python import torch import torch.nn as nn net = nn.Sequential(nn.Linear(128,256), nn.ReLU(), nn.Dropout(p=0.5), nn.Linear(256,10)) print(net(torch.randn(4,128)).shape) ``` #### 8.4.2.2 权重衰减(weight decay) 在 SGD/AdamW 中,权重衰减相当于在目标中加入 $\lambda\lVert w\rVert_2^2$(工程上推荐 AdamW)。 #### 8.4.2.3 数据增强作为正则化 增强让模型在一组邻域样本上保持一致性,可理解为对输入空间的局部平滑。 #### 8.4.2.4 标签平滑(Label Smoothing) 把 one-hot 标签 $y$ 变为:$y'=(1-\epsilon)y+\epsilon/K$。 #### 8.4.2.5 Mixup 与 CutMix(最小代码) ```python import torch def mixup(x, y, alpha=0.2): lam = torch.distributions.Beta(alpha, alpha).sample(()).item() idx = torch.randperm(x.size(0)) x2, y2 = x[idx], y[idx] x_mix = lam * x + (1-lam) * x2 return x_mix, y, y2, lam ``` ![Mixup示意:两张图线性混合](/sec08_015.png) --- ### 8.4.3 调试技巧 #### 8.4.3.1 梯度检查(gradient checking) 数值梯度:$\frac{\partial f}{\partial w}\approx\frac{f(w+\epsilon)-f(w-\epsilon)}{2\epsilon}$。 ```python import torch # torch.autograd.gradcheck 需要 double 精度 def fn(x): return (x * x).sum() x = torch.randn(3, 4, dtype=torch.double, requires_grad=True) print(torch.autograd.gradcheck(fn, (x,), eps=1e-6, atol=1e-4)) ``` #### 8.4.3.2 过拟合单batch测试(最强排错手段之一) 如果模型连一个 batch 都拟合不了,优先怀疑: - 数据/标签错位 - loss/activation 不匹配(如 logits 先做 softmax 又用 CE) - 学习率/初始化/正则化过强 #### 8.4.3.3 梯度/激活分布可视化(用hook) 可以把 hook 捕获的 tensor 画直方图(TensorBoard / Matplotlib)。 #### 8.4.3.4 常见训练问题诊断(速查表) - loss 不下降:检查 lr、数据、label、model输出维度 - acc 卡在随机:检查 label 编码、shuffle、loss是否对 - 过拟合:加增强、加wd、加dropout、早停 - 梯度爆炸:梯度裁剪、减小lr、检查BN --- ## 8.5 模型部署 ![部署路径:训练checkpoint→导出(TorchScript/ONNX)→服务/端侧](/sec08_016.png) ### 8.5.1 模型保存与加载 #### 8.5.1.1 state_dict vs 完整模型保存 ```python import torch # 推荐:保存state_dict(更稳定、更可移植) # torch.save({'model': model.state_dict(), 'optimizer': opt.state_dict(), 'epoch': epoch}, 'ckpt.pt') # 加载 # ckpt = torch.load('ckpt.pt', map_location='cpu') # model.load_state_dict(ckpt['model']) # opt.load_state_dict(ckpt['optimizer']) ``` #### 8.5.1.2 检查点管理与版本控制 建议:以 `run_id + config_hash` 组织输出目录,并保存 config.yaml + metrics.json。 ### 8.5.2 模型导出 #### 8.5.2.1 TorchScript:trace vs script ```python import torch import torch.nn as nn class Tiny(nn.Module): def __init__(self): super().__init__() self.fc = nn.Linear(4,2) def forward(self, x): return self.fc(x) m = Tiny().eval() x = torch.randn(1,4) traced = torch.jit.trace(m, x) traced.save('tiny_traced.pt') scripted = torch.jit.script(m) scripted.save('tiny_scripted.pt') ``` #### 8.5.2.2 ONNX 导出与验证 ```python import torch import torch.nn as nn model = nn.Linear(4,2).eval() x = torch.randn(1,4) torch.onnx.export( model, x, 'linear.onnx', input_names=['x'], output_names=['y'], opset_version=17, ) print('exported linear.onnx') ``` #### 8.5.2.3 TorchServe / 移动端(概念性) TorchServe:把 TorchScript/模型归档成可服务的 endpoint;端侧可用 PyTorch Mobile/ExecuTorch 等路线(按版本演进)。 ### 8.5.3 模型优化 #### 8.5.3.1 量化(Quantization) - 动态量化:权重量化,激活运行时量化 - 静态量化:需要校准数据 - QAT:训练时引入量化噪声 #### 8.5.3.2 剪枝(Pruning) 结构化/非结构化剪枝;工程上关注速度收益与硬件友好性。 #### 8.5.3.3 知识蒸馏(KD) 软标签蒸馏:$\mathcal{L}=\alpha\mathcal{L}_{CE}+(1-\alpha)T^2\text{KL}(p_T^\text{teacher}\Vert p_T^\text{student})$。 --- ## 8.6 实战项目:CIFAR10 完整训练(目标:90%+) 本节给出一个**可直接运行的端到端脚本**: - 数据加载 + 增强 pipeline - CNN 架构设计(小型 ResNet-like) - 训练循环(含验证、早停、LR调度、AMP、梯度裁剪、梯度累积) - 测试评估 - 模型导出(TorchScript / ONNX) ![CIFAR10实战流水线:dataset→augment→model→train→eval→export](/sec08_017.png) ### 8.6.1 一键训练脚本(cifar10_train.py) ```python import os import time from dataclasses import dataclass import torch import torch.nn as nn import torch.optim as optim from torch.utils.data import DataLoader from torchvision.datasets import CIFAR10 from torchvision import transforms @dataclass class Config: data_dir: str = './data' epochs: int = 50 batch_size: int = 128 lr: float = 0.1 weight_decay: float = 5e-4 momentum: float = 0.9 num_workers: int = 4 amp: bool = True grad_clip: float = 1.0 accum_steps: int = 1 patience: int = 10 device: str = 'cuda' if torch.cuda.is_available() else 'cpu' class ResidualBlock(nn.Module): def __init__(self, c, stride=1): super().__init__() self.conv1 = nn.Conv2d(c, c, 3, stride=stride, padding=1, bias=False) self.bn1 = nn.BatchNorm2d(c) self.conv2 = nn.Conv2d(c, c, 3, stride=1, padding=1, bias=False) self.bn2 = nn.BatchNorm2d(c) self.act = nn.ReLU(inplace=True) def forward(self, x): out = self.act(self.bn1(self.conv1(x))) out = self.bn2(self.conv2(out)) out = self.act(out + x) return out class SmallResNet(nn.Module): def __init__(self, num_classes=10): super().__init__() self.stem = nn.Sequential( nn.Conv2d(3, 64, 3, padding=1, bias=False), nn.BatchNorm2d(64), nn.ReLU(inplace=True), ) self.block1 = ResidualBlock(64) self.block2 = ResidualBlock(64) self.head = nn.Sequential( nn.AdaptiveAvgPool2d((1, 1)), nn.Flatten(), nn.Linear(64, num_classes) ) def forward(self, x): x = self.stem(x) x = self.block1(x) x = self.block2(x) return self.head(x) class EarlyStopping: def __init__(self, patience=10, mode='max', min_delta=0.0): self.patience = patience self.mode = mode self.min_delta = min_delta self.best = None self.bad = 0 def step(self, metric): if self.best is None: self.best = metric return False improve = metric > self.best + self.min_delta if self.mode == 'max' else metric < self.best - self.min_delta if improve: self.best = metric self.bad = 0 else: self.bad += 1 return self.bad >= self.patience @torch.no_grad() def evaluate(model, loader, device): model.eval() criterion = nn.CrossEntropyLoss() total, correct, loss_sum = 0, 0, 0.0 for x, y in loader: x, y = x.to(device), y.to(device) logits = model(x) loss = criterion(logits, y) loss_sum += loss.item() * x.size(0) correct += (logits.argmax(1) == y).sum().item() total += x.size(0) return loss_sum / total, correct / total def train_one_epoch(model, loader, optimizer, device, scaler=None, grad_clip=None, accum_steps=1): model.train() criterion = nn.CrossEntropyLoss() total, correct, loss_sum = 0, 0, 0.0 optimizer.zero_grad(set_to_none=True) for step, (x, y) in enumerate(loader): x, y = x.to(device), y.to(device) if scaler is None: logits = model(x) loss = criterion(logits, y) / accum_steps loss.backward() else: with torch.cuda.amp.autocast(): logits = model(x) loss = criterion(logits, y) / accum_steps scaler.scale(loss).backward() if (step + 1) % accum_steps == 0: if grad_clip is not None: if scaler is not None: scaler.unscale_(optimizer) torch.nn.utils.clip_grad_norm_(model.parameters(), grad_clip) if scaler is None: optimizer.step() else: scaler.step(optimizer) scaler.update() optimizer.zero_grad(set_to_none=True) loss_sum += loss.item() * x.size(0) * accum_steps correct += (logits.argmax(1) == y).sum().item() total += x.size(0) return loss_sum / total, correct / total def main(): cfg = Config() train_tf = transforms.Compose([ transforms.RandomCrop(32, padding=4), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize((0.4914,0.4822,0.4465), (0.247,0.243,0.261)), ]) test_tf = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.4914,0.4822,0.4465), (0.247,0.243,0.261)), ]) train_ds = CIFAR10(cfg.data_dir, train=True, download=True, transform=train_tf) test_ds = CIFAR10(cfg.data_dir, train=False, download=True, transform=test_tf) # 简单切分一部分做val val_size = 5000 train_size = len(train_ds) - val_size train_ds, val_ds = torch.utils.data.random_split(train_ds, [train_size, val_size], generator=torch.Generator().manual_seed(42)) train_loader = DataLoader(train_ds, batch_size=cfg.batch_size, shuffle=True, num_workers=cfg.num_workers, pin_memory=True) val_loader = DataLoader(val_ds, batch_size=cfg.batch_size, shuffle=False, num_workers=cfg.num_workers, pin_memory=True) test_loader = DataLoader(test_ds, batch_size=cfg.batch_size, shuffle=False, num_workers=cfg.num_workers, pin_memory=True) device = torch.device(cfg.device) model = SmallResNet(num_classes=10).to(device) optimizer = optim.SGD(model.parameters(), lr=cfg.lr, momentum=cfg.momentum, weight_decay=cfg.weight_decay) scheduler = optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=cfg.epochs) scaler = torch.cuda.amp.GradScaler(enabled=(cfg.amp and device.type == 'cuda')) es = EarlyStopping(patience=cfg.patience, mode='max') best_acc = 0.0 os.makedirs('outputs', exist_ok=True) for epoch in range(1, cfg.epochs + 1): t0 = time.time() tr_loss, tr_acc = train_one_epoch(model, train_loader, optimizer, device, scaler=scaler, grad_clip=cfg.grad_clip, accum_steps=cfg.accum_steps) va_loss, va_acc = evaluate(model, val_loader, device) scheduler.step() lr = optimizer.param_groups[0]['lr'] print(f"Epoch {epoch:03d} | lr={lr:.5f} | train loss={tr_loss:.4f} acc={tr_acc:.4f} | val loss={va_loss:.4f} acc={va_acc:.4f} | {time.time()-t0:.1f}s") # checkpoint if va_acc > best_acc: best_acc = va_acc torch.save({'model': model.state_dict(), 'epoch': epoch, 'val_acc': va_acc}, 'outputs/best.pt') if es.step(va_acc): print('Early stopping triggered') break # test ckpt = torch.load('outputs/best.pt', map_location=device) model.load_state_dict(ckpt['model']) te_loss, te_acc = evaluate(model, test_loader, device) print(f"Test loss={te_loss:.4f} acc={te_acc:.4f}") # export torchscript model.eval() dummy = torch.randn(1, 3, 32, 32, device=device) traced = torch.jit.trace(model, dummy) traced.save('outputs/model_ts.pt') print('Saved TorchScript to outputs/model_ts.pt') # export onnx (CPU导出更通用) model_cpu = model.cpu() dummy_cpu = torch.randn(1, 3, 32, 32) torch.onnx.export(model_cpu, dummy_cpu, 'outputs/model.onnx', input_names=['x'], output_names=['logits'], opset_version=17) print('Saved ONNX to outputs/model.onnx') if __name__ == '__main__': main() ``` ### 8.6.2 超参数调优建议(围绕90%+) - 优先保证增强与训练循环正确(先跑到 80%+)。 - 再尝试:更深/更宽模型、Cosine/OneCycle、label smoothing、Mixup/CutMix。 - 对小模型:提高 epochs(如 200)常常比复杂技巧更直接。 ![达到90%+的关键旋钮:增强、优化器、调度器、正则化](/sec08_018.png) --- ## 时间分配建议 - 2h Dataset/DataLoader - 3h 编写完整训练循环 - 2h CIFAR10实战 - 1h GPU训练、混合精度、模型导出 ## 里程碑 在 CIFAR10 数据集上训练 CNN 模型,达到 **90%+ 测试准确率**。 ## 学完你应该能... - 编写自定义Dataset处理任意格式的数据 - 编写包含完整功能的训练循环 - 使用GPU加速训练 - 保存和加载训练好的模型 --- ## 附录A:逐段练习(大量小练习,建议边写边跑) 说明:本附录用 *大量短练习* 把 API 细节练到肌肉记忆。每个练习都尽量是一个可运行的小片段或可验证的结论。 ### A.001 练习:DataLoader 小任务 **目标**:完成一个最小可复现的小实验(编号 1)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 1 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.002 练习:Transform 小任务 **目标**:完成一个最小可复现的小实验(编号 2)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 2 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.003 练习:Module 小任务 **目标**:完成一个最小可复现的小实验(编号 3)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 3 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.004 练习:Optimizer 小任务 **目标**:完成一个最小可复现的小实验(编号 4)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 4 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.005 练习:Scheduler 小任务 **目标**:完成一个最小可复现的小实验(编号 5)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 5 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.006 练习:AMP 小任务 **目标**:完成一个最小可复现的小实验(编号 6)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 6 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.007 练习:DDP 小任务 **目标**:完成一个最小可复现的小实验(编号 7)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 7 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.008 练习:Debug 小任务 **目标**:完成一个最小可复现的小实验(编号 8)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 8 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.009 练习:Export 小任务 **目标**:完成一个最小可复现的小实验(编号 9)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 9 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.010 练习:Dataset 小任务 **目标**:完成一个最小可复现的小实验(编号 10)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 10 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.011 练习:DataLoader 小任务 **目标**:完成一个最小可复现的小实验(编号 11)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 11 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.012 练习:Transform 小任务 **目标**:完成一个最小可复现的小实验(编号 12)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 12 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.013 练习:Module 小任务 **目标**:完成一个最小可复现的小实验(编号 13)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 13 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.014 练习:Optimizer 小任务 **目标**:完成一个最小可复现的小实验(编号 14)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 14 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.015 练习:Scheduler 小任务 **目标**:完成一个最小可复现的小实验(编号 15)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 15 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.016 练习:AMP 小任务 **目标**:完成一个最小可复现的小实验(编号 16)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 16 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.017 练习:DDP 小任务 **目标**:完成一个最小可复现的小实验(编号 17)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 17 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.018 练习:Debug 小任务 **目标**:完成一个最小可复现的小实验(编号 18)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 18 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.019 练习:Export 小任务 **目标**:完成一个最小可复现的小实验(编号 19)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 19 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.020 练习:Dataset 小任务 **目标**:完成一个最小可复现的小实验(编号 20)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 20 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.021 练习:DataLoader 小任务 **目标**:完成一个最小可复现的小实验(编号 21)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 21 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.022 练习:Transform 小任务 **目标**:完成一个最小可复现的小实验(编号 22)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 22 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.023 练习:Module 小任务 **目标**:完成一个最小可复现的小实验(编号 23)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 23 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.024 练习:Optimizer 小任务 **目标**:完成一个最小可复现的小实验(编号 24)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 24 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.025 练习:Scheduler 小任务 **目标**:完成一个最小可复现的小实验(编号 25)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 25 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.026 练习:AMP 小任务 **目标**:完成一个最小可复现的小实验(编号 26)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 26 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.027 练习:DDP 小任务 **目标**:完成一个最小可复现的小实验(编号 27)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 27 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.028 练习:Debug 小任务 **目标**:完成一个最小可复现的小实验(编号 28)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 28 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.029 练习:Export 小任务 **目标**:完成一个最小可复现的小实验(编号 29)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 29 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.030 练习:Dataset 小任务 **目标**:完成一个最小可复现的小实验(编号 30)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 30 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.031 练习:DataLoader 小任务 **目标**:完成一个最小可复现的小实验(编号 31)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 31 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.032 练习:Transform 小任务 **目标**:完成一个最小可复现的小实验(编号 32)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 32 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.033 练习:Module 小任务 **目标**:完成一个最小可复现的小实验(编号 33)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 33 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.034 练习:Optimizer 小任务 **目标**:完成一个最小可复现的小实验(编号 34)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 34 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.035 练习:Scheduler 小任务 **目标**:完成一个最小可复现的小实验(编号 35)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 35 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.036 练习:AMP 小任务 **目标**:完成一个最小可复现的小实验(编号 36)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 36 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.037 练习:DDP 小任务 **目标**:完成一个最小可复现的小实验(编号 37)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 37 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.038 练习:Debug 小任务 **目标**:完成一个最小可复现的小实验(编号 38)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 38 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.039 练习:Export 小任务 **目标**:完成一个最小可复现的小实验(编号 39)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 39 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.040 练习:Dataset 小任务 **目标**:完成一个最小可复现的小实验(编号 40)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 40 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.041 练习:DataLoader 小任务 **目标**:完成一个最小可复现的小实验(编号 41)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 41 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.042 练习:Transform 小任务 **目标**:完成一个最小可复现的小实验(编号 42)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 42 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.043 练习:Module 小任务 **目标**:完成一个最小可复现的小实验(编号 43)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 43 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.044 练习:Optimizer 小任务 **目标**:完成一个最小可复现的小实验(编号 44)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 44 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.045 练习:Scheduler 小任务 **目标**:完成一个最小可复现的小实验(编号 45)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 45 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.046 练习:AMP 小任务 **目标**:完成一个最小可复现的小实验(编号 46)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 46 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.047 练习:DDP 小任务 **目标**:完成一个最小可复现的小实验(编号 47)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 47 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.048 练习:Debug 小任务 **目标**:完成一个最小可复现的小实验(编号 48)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 48 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.049 练习:Export 小任务 **目标**:完成一个最小可复现的小实验(编号 49)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 49 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.050 练习:Dataset 小任务 **目标**:完成一个最小可复现的小实验(编号 50)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 50 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.051 练习:DataLoader 小任务 **目标**:完成一个最小可复现的小实验(编号 51)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 51 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.052 练习:Transform 小任务 **目标**:完成一个最小可复现的小实验(编号 52)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 52 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.053 练习:Module 小任务 **目标**:完成一个最小可复现的小实验(编号 53)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 53 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.054 练习:Optimizer 小任务 **目标**:完成一个最小可复现的小实验(编号 54)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 54 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.055 练习:Scheduler 小任务 **目标**:完成一个最小可复现的小实验(编号 55)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 55 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.056 练习:AMP 小任务 **目标**:完成一个最小可复现的小实验(编号 56)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 56 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.057 练习:DDP 小任务 **目标**:完成一个最小可复现的小实验(编号 57)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 57 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.058 练习:Debug 小任务 **目标**:完成一个最小可复现的小实验(编号 58)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 58 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.059 练习:Export 小任务 **目标**:完成一个最小可复现的小实验(编号 59)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 59 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.060 练习:Dataset 小任务 **目标**:完成一个最小可复现的小实验(编号 60)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 60 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.061 练习:DataLoader 小任务 **目标**:完成一个最小可复现的小实验(编号 61)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 61 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.062 练习:Transform 小任务 **目标**:完成一个最小可复现的小实验(编号 62)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 62 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.063 练习:Module 小任务 **目标**:完成一个最小可复现的小实验(编号 63)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 63 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.064 练习:Optimizer 小任务 **目标**:完成一个最小可复现的小实验(编号 64)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 64 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.065 练习:Scheduler 小任务 **目标**:完成一个最小可复现的小实验(编号 65)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 65 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.066 练习:AMP 小任务 **目标**:完成一个最小可复现的小实验(编号 66)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 66 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.067 练习:DDP 小任务 **目标**:完成一个最小可复现的小实验(编号 67)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 67 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.068 练习:Debug 小任务 **目标**:完成一个最小可复现的小实验(编号 68)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 68 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.069 练习:Export 小任务 **目标**:完成一个最小可复现的小实验(编号 69)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 69 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.070 练习:Dataset 小任务 **目标**:完成一个最小可复现的小实验(编号 70)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 70 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.071 练习:DataLoader 小任务 **目标**:完成一个最小可复现的小实验(编号 71)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 71 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.072 练习:Transform 小任务 **目标**:完成一个最小可复现的小实验(编号 72)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 72 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.073 练习:Module 小任务 **目标**:完成一个最小可复现的小实验(编号 73)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 73 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.074 练习:Optimizer 小任务 **目标**:完成一个最小可复现的小实验(编号 74)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 74 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.075 练习:Scheduler 小任务 **目标**:完成一个最小可复现的小实验(编号 75)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 75 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.076 练习:AMP 小任务 **目标**:完成一个最小可复现的小实验(编号 76)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 76 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.077 练习:DDP 小任务 **目标**:完成一个最小可复现的小实验(编号 77)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 77 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.078 练习:Debug 小任务 **目标**:完成一个最小可复现的小实验(编号 78)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 78 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.079 练习:Export 小任务 **目标**:完成一个最小可复现的小实验(编号 79)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 79 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.080 练习:Dataset 小任务 **目标**:完成一个最小可复现的小实验(编号 80)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 80 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.081 练习:DataLoader 小任务 **目标**:完成一个最小可复现的小实验(编号 81)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 81 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.082 练习:Transform 小任务 **目标**:完成一个最小可复现的小实验(编号 82)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 82 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.083 练习:Module 小任务 **目标**:完成一个最小可复现的小实验(编号 83)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 83 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.084 练习:Optimizer 小任务 **目标**:完成一个最小可复现的小实验(编号 84)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 84 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.085 练习:Scheduler 小任务 **目标**:完成一个最小可复现的小实验(编号 85)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 85 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.086 练习:AMP 小任务 **目标**:完成一个最小可复现的小实验(编号 86)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 86 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.087 练习:DDP 小任务 **目标**:完成一个最小可复现的小实验(编号 87)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 87 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.088 练习:Debug 小任务 **目标**:完成一个最小可复现的小实验(编号 88)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 88 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.089 练习:Export 小任务 **目标**:完成一个最小可复现的小实验(编号 89)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 89 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.090 练习:Dataset 小任务 **目标**:完成一个最小可复现的小实验(编号 90)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 90 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.091 练习:DataLoader 小任务 **目标**:完成一个最小可复现的小实验(编号 91)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 91 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.092 练习:Transform 小任务 **目标**:完成一个最小可复现的小实验(编号 92)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 92 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.093 练习:Module 小任务 **目标**:完成一个最小可复现的小实验(编号 93)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 93 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.094 练习:Optimizer 小任务 **目标**:完成一个最小可复现的小实验(编号 94)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 94 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.095 练习:Scheduler 小任务 **目标**:完成一个最小可复现的小实验(编号 95)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 95 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.096 练习:AMP 小任务 **目标**:完成一个最小可复现的小实验(编号 96)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 96 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.097 练习:DDP 小任务 **目标**:完成一个最小可复现的小实验(编号 97)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 97 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.098 练习:Debug 小任务 **目标**:完成一个最小可复现的小实验(编号 98)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 98 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.099 练习:Export 小任务 **目标**:完成一个最小可复现的小实验(编号 99)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 99 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.100 练习:Dataset 小任务 **目标**:完成一个最小可复现的小实验(编号 100)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 100 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.101 练习:DataLoader 小任务 **目标**:完成一个最小可复现的小实验(编号 101)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 101 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.102 练习:Transform 小任务 **目标**:完成一个最小可复现的小实验(编号 102)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 102 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.103 练习:Module 小任务 **目标**:完成一个最小可复现的小实验(编号 103)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 103 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.104 练习:Optimizer 小任务 **目标**:完成一个最小可复现的小实验(编号 104)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 104 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.105 练习:Scheduler 小任务 **目标**:完成一个最小可复现的小实验(编号 105)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 105 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.106 练习:AMP 小任务 **目标**:完成一个最小可复现的小实验(编号 106)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 106 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.107 练习:DDP 小任务 **目标**:完成一个最小可复现的小实验(编号 107)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 107 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.108 练习:Debug 小任务 **目标**:完成一个最小可复现的小实验(编号 108)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 108 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.109 练习:Export 小任务 **目标**:完成一个最小可复现的小实验(编号 109)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 109 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.110 练习:Dataset 小任务 **目标**:完成一个最小可复现的小实验(编号 110)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 110 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.111 练习:DataLoader 小任务 **目标**:完成一个最小可复现的小实验(编号 111)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 111 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.112 练习:Transform 小任务 **目标**:完成一个最小可复现的小实验(编号 112)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 112 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.113 练习:Module 小任务 **目标**:完成一个最小可复现的小实验(编号 113)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 113 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.114 练习:Optimizer 小任务 **目标**:完成一个最小可复现的小实验(编号 114)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 114 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.115 练习:Scheduler 小任务 **目标**:完成一个最小可复现的小实验(编号 115)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 115 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.116 练习:AMP 小任务 **目标**:完成一个最小可复现的小实验(编号 116)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 116 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.117 练习:DDP 小任务 **目标**:完成一个最小可复现的小实验(编号 117)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 117 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.118 练习:Debug 小任务 **目标**:完成一个最小可复现的小实验(编号 118)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 118 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.119 练习:Export 小任务 **目标**:完成一个最小可复现的小实验(编号 119)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 119 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.120 练习:Dataset 小任务 **目标**:完成一个最小可复现的小实验(编号 120)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 120 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.121 练习:DataLoader 小任务 **目标**:完成一个最小可复现的小实验(编号 121)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 121 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.122 练习:Transform 小任务 **目标**:完成一个最小可复现的小实验(编号 122)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 122 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.123 练习:Module 小任务 **目标**:完成一个最小可复现的小实验(编号 123)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 123 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.124 练习:Optimizer 小任务 **目标**:完成一个最小可复现的小实验(编号 124)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 124 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.125 练习:Scheduler 小任务 **目标**:完成一个最小可复现的小实验(编号 125)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 125 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.126 练习:AMP 小任务 **目标**:完成一个最小可复现的小实验(编号 126)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 126 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.127 练习:DDP 小任务 **目标**:完成一个最小可复现的小实验(编号 127)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 127 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.128 练习:Debug 小任务 **目标**:完成一个最小可复现的小实验(编号 128)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 128 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.129 练习:Export 小任务 **目标**:完成一个最小可复现的小实验(编号 129)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 129 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.130 练习:Dataset 小任务 **目标**:完成一个最小可复现的小实验(编号 130)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 130 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.131 练习:DataLoader 小任务 **目标**:完成一个最小可复现的小实验(编号 131)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 131 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.132 练习:Transform 小任务 **目标**:完成一个最小可复现的小实验(编号 132)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 132 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.133 练习:Module 小任务 **目标**:完成一个最小可复现的小实验(编号 133)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 133 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.134 练习:Optimizer 小任务 **目标**:完成一个最小可复现的小实验(编号 134)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 134 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.135 练习:Scheduler 小任务 **目标**:完成一个最小可复现的小实验(编号 135)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 135 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.136 练习:AMP 小任务 **目标**:完成一个最小可复现的小实验(编号 136)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 136 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.137 练习:DDP 小任务 **目标**:完成一个最小可复现的小实验(编号 137)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 137 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.138 练习:Debug 小任务 **目标**:完成一个最小可复现的小实验(编号 138)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 138 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.139 练习:Export 小任务 **目标**:完成一个最小可复现的小实验(编号 139)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 139 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.140 练习:Dataset 小任务 **目标**:完成一个最小可复现的小实验(编号 140)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 140 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.141 练习:DataLoader 小任务 **目标**:完成一个最小可复现的小实验(编号 141)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 141 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.142 练习:Transform 小任务 **目标**:完成一个最小可复现的小实验(编号 142)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 142 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.143 练习:Module 小任务 **目标**:完成一个最小可复现的小实验(编号 143)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 143 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.144 练习:Optimizer 小任务 **目标**:完成一个最小可复现的小实验(编号 144)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 144 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.145 练习:Scheduler 小任务 **目标**:完成一个最小可复现的小实验(编号 145)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 145 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.146 练习:AMP 小任务 **目标**:完成一个最小可复现的小实验(编号 146)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 146 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.147 练习:DDP 小任务 **目标**:完成一个最小可复现的小实验(编号 147)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 147 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.148 练习:Debug 小任务 **目标**:完成一个最小可复现的小实验(编号 148)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 148 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.149 练习:Export 小任务 **目标**:完成一个最小可复现的小实验(编号 149)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 149 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.150 练习:Dataset 小任务 **目标**:完成一个最小可复现的小实验(编号 150)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 150 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.151 练习:DataLoader 小任务 **目标**:完成一个最小可复现的小实验(编号 151)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 151 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.152 练习:Transform 小任务 **目标**:完成一个最小可复现的小实验(编号 152)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 152 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.153 练习:Module 小任务 **目标**:完成一个最小可复现的小实验(编号 153)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 153 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.154 练习:Optimizer 小任务 **目标**:完成一个最小可复现的小实验(编号 154)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 154 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.155 练习:Scheduler 小任务 **目标**:完成一个最小可复现的小实验(编号 155)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 155 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.156 练习:AMP 小任务 **目标**:完成一个最小可复现的小实验(编号 156)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 156 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.157 练习:DDP 小任务 **目标**:完成一个最小可复现的小实验(编号 157)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 157 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.158 练习:Debug 小任务 **目标**:完成一个最小可复现的小实验(编号 158)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 158 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.159 练习:Export 小任务 **目标**:完成一个最小可复现的小实验(编号 159)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 159 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.160 练习:Dataset 小任务 **目标**:完成一个最小可复现的小实验(编号 160)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 160 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.161 练习:DataLoader 小任务 **目标**:完成一个最小可复现的小实验(编号 161)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 161 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.162 练习:Transform 小任务 **目标**:完成一个最小可复现的小实验(编号 162)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 162 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.163 练习:Module 小任务 **目标**:完成一个最小可复现的小实验(编号 163)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 163 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.164 练习:Optimizer 小任务 **目标**:完成一个最小可复现的小实验(编号 164)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 164 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.165 练习:Scheduler 小任务 **目标**:完成一个最小可复现的小实验(编号 165)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 165 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.166 练习:AMP 小任务 **目标**:完成一个最小可复现的小实验(编号 166)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 166 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.167 练习:DDP 小任务 **目标**:完成一个最小可复现的小实验(编号 167)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 167 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.168 练习:Debug 小任务 **目标**:完成一个最小可复现的小实验(编号 168)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 168 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.169 练习:Export 小任务 **目标**:完成一个最小可复现的小实验(编号 169)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 169 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.170 练习:Dataset 小任务 **目标**:完成一个最小可复现的小实验(编号 170)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 170 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.171 练习:DataLoader 小任务 **目标**:完成一个最小可复现的小实验(编号 171)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 171 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.172 练习:Transform 小任务 **目标**:完成一个最小可复现的小实验(编号 172)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 172 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.173 练习:Module 小任务 **目标**:完成一个最小可复现的小实验(编号 173)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 173 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.174 练习:Optimizer 小任务 **目标**:完成一个最小可复现的小实验(编号 174)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 174 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.175 练习:Scheduler 小任务 **目标**:完成一个最小可复现的小实验(编号 175)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 175 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.176 练习:AMP 小任务 **目标**:完成一个最小可复现的小实验(编号 176)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 176 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.177 练习:DDP 小任务 **目标**:完成一个最小可复现的小实验(编号 177)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 177 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.178 练习:Debug 小任务 **目标**:完成一个最小可复现的小实验(编号 178)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 178 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.179 练习:Export 小任务 **目标**:完成一个最小可复现的小实验(编号 179)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 179 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.180 练习:Dataset 小任务 **目标**:完成一个最小可复现的小实验(编号 180)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 180 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.181 练习:DataLoader 小任务 **目标**:完成一个最小可复现的小实验(编号 181)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 181 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.182 练习:Transform 小任务 **目标**:完成一个最小可复现的小实验(编号 182)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 182 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.183 练习:Module 小任务 **目标**:完成一个最小可复现的小实验(编号 183)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 183 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.184 练习:Optimizer 小任务 **目标**:完成一个最小可复现的小实验(编号 184)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 184 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.185 练习:Scheduler 小任务 **目标**:完成一个最小可复现的小实验(编号 185)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 185 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.186 练习:AMP 小任务 **目标**:完成一个最小可复现的小实验(编号 186)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 186 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.187 练习:DDP 小任务 **目标**:完成一个最小可复现的小实验(编号 187)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 187 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.188 练习:Debug 小任务 **目标**:完成一个最小可复现的小实验(编号 188)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 188 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.189 练习:Export 小任务 **目标**:完成一个最小可复现的小实验(编号 189)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 189 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.190 练习:Dataset 小任务 **目标**:完成一个最小可复现的小实验(编号 190)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 190 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.191 练习:DataLoader 小任务 **目标**:完成一个最小可复现的小实验(编号 191)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 191 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.192 练习:Transform 小任务 **目标**:完成一个最小可复现的小实验(编号 192)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 192 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.193 练习:Module 小任务 **目标**:完成一个最小可复现的小实验(编号 193)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 193 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.194 练习:Optimizer 小任务 **目标**:完成一个最小可复现的小实验(编号 194)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 194 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.195 练习:Scheduler 小任务 **目标**:完成一个最小可复现的小实验(编号 195)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 195 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.196 练习:AMP 小任务 **目标**:完成一个最小可复现的小实验(编号 196)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 196 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.197 练习:DDP 小任务 **目标**:完成一个最小可复现的小实验(编号 197)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 197 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.198 练习:Debug 小任务 **目标**:完成一个最小可复现的小实验(编号 198)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 198 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.199 练习:Export 小任务 **目标**:完成一个最小可复现的小实验(编号 199)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 199 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.200 练习:Dataset 小任务 **目标**:完成一个最小可复现的小实验(编号 200)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 200 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.201 练习:DataLoader 小任务 **目标**:完成一个最小可复现的小实验(编号 201)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 201 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.202 练习:Transform 小任务 **目标**:完成一个最小可复现的小实验(编号 202)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 202 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.203 练习:Module 小任务 **目标**:完成一个最小可复现的小实验(编号 203)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 203 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.204 练习:Optimizer 小任务 **目标**:完成一个最小可复现的小实验(编号 204)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 204 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.205 练习:Scheduler 小任务 **目标**:完成一个最小可复现的小实验(编号 205)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 205 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.206 练习:AMP 小任务 **目标**:完成一个最小可复现的小实验(编号 206)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 206 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.207 练习:DDP 小任务 **目标**:完成一个最小可复现的小实验(编号 207)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 207 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.208 练习:Debug 小任务 **目标**:完成一个最小可复现的小实验(编号 208)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 208 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.209 练习:Export 小任务 **目标**:完成一个最小可复现的小实验(编号 209)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 209 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.210 练习:Dataset 小任务 **目标**:完成一个最小可复现的小实验(编号 210)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 210 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.211 练习:DataLoader 小任务 **目标**:完成一个最小可复现的小实验(编号 211)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 211 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.212 练习:Transform 小任务 **目标**:完成一个最小可复现的小实验(编号 212)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 212 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.213 练习:Module 小任务 **目标**:完成一个最小可复现的小实验(编号 213)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 213 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.214 练习:Optimizer 小任务 **目标**:完成一个最小可复现的小实验(编号 214)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 214 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.215 练习:Scheduler 小任务 **目标**:完成一个最小可复现的小实验(编号 215)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 215 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.216 练习:AMP 小任务 **目标**:完成一个最小可复现的小实验(编号 216)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 216 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.217 练习:DDP 小任务 **目标**:完成一个最小可复现的小实验(编号 217)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 217 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.218 练习:Debug 小任务 **目标**:完成一个最小可复现的小实验(编号 218)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 218 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.219 练习:Export 小任务 **目标**:完成一个最小可复现的小实验(编号 219)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 219 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.220 练习:Dataset 小任务 **目标**:完成一个最小可复现的小实验(编号 220)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 220 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.221 练习:DataLoader 小任务 **目标**:完成一个最小可复现的小实验(编号 221)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 221 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.222 练习:Transform 小任务 **目标**:完成一个最小可复现的小实验(编号 222)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 222 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.223 练习:Module 小任务 **目标**:完成一个最小可复现的小实验(编号 223)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 223 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.224 练习:Optimizer 小任务 **目标**:完成一个最小可复现的小实验(编号 224)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 224 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.225 练习:Scheduler 小任务 **目标**:完成一个最小可复现的小实验(编号 225)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 225 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.226 练习:AMP 小任务 **目标**:完成一个最小可复现的小实验(编号 226)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 226 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.227 练习:DDP 小任务 **目标**:完成一个最小可复现的小实验(编号 227)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 227 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.228 练习:Debug 小任务 **目标**:完成一个最小可复现的小实验(编号 228)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 228 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.229 练习:Export 小任务 **目标**:完成一个最小可复现的小实验(编号 229)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 229 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.230 练习:Dataset 小任务 **目标**:完成一个最小可复现的小实验(编号 230)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 230 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.231 练习:DataLoader 小任务 **目标**:完成一个最小可复现的小实验(编号 231)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 231 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.232 练习:Transform 小任务 **目标**:完成一个最小可复现的小实验(编号 232)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 232 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.233 练习:Module 小任务 **目标**:完成一个最小可复现的小实验(编号 233)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 233 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.234 练习:Optimizer 小任务 **目标**:完成一个最小可复现的小实验(编号 234)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 234 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.235 练习:Scheduler 小任务 **目标**:完成一个最小可复现的小实验(编号 235)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 235 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.236 练习:AMP 小任务 **目标**:完成一个最小可复现的小实验(编号 236)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 236 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.237 练习:DDP 小任务 **目标**:完成一个最小可复现的小实验(编号 237)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 237 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.238 练习:Debug 小任务 **目标**:完成一个最小可复现的小实验(编号 238)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 238 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.239 练习:Export 小任务 **目标**:完成一个最小可复现的小实验(编号 239)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 239 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.240 练习:Dataset 小任务 **目标**:完成一个最小可复现的小实验(编号 240)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 240 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.241 练习:DataLoader 小任务 **目标**:完成一个最小可复现的小实验(编号 241)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 241 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.242 练习:Transform 小任务 **目标**:完成一个最小可复现的小实验(编号 242)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 242 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.243 练习:Module 小任务 **目标**:完成一个最小可复现的小实验(编号 243)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 243 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.244 练习:Optimizer 小任务 **目标**:完成一个最小可复现的小实验(编号 244)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 244 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.245 练习:Scheduler 小任务 **目标**:完成一个最小可复现的小实验(编号 245)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 245 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.246 练习:AMP 小任务 **目标**:完成一个最小可复现的小实验(编号 246)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 246 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.247 练习:DDP 小任务 **目标**:完成一个最小可复现的小实验(编号 247)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 247 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.248 练习:Debug 小任务 **目标**:完成一个最小可复现的小实验(编号 248)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 248 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.249 练习:Export 小任务 **目标**:完成一个最小可复现的小实验(编号 249)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 249 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.250 练习:Dataset 小任务 **目标**:完成一个最小可复现的小实验(编号 250)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 250 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.251 练习:DataLoader 小任务 **目标**:完成一个最小可复现的小实验(编号 251)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 251 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.252 练习:Transform 小任务 **目标**:完成一个最小可复现的小实验(编号 252)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 252 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.253 练习:Module 小任务 **目标**:完成一个最小可复现的小实验(编号 253)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 253 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.254 练习:Optimizer 小任务 **目标**:完成一个最小可复现的小实验(编号 254)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 254 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.255 练习:Scheduler 小任务 **目标**:完成一个最小可复现的小实验(编号 255)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 255 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.256 练习:AMP 小任务 **目标**:完成一个最小可复现的小实验(编号 256)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 256 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.257 练习:DDP 小任务 **目标**:完成一个最小可复现的小实验(编号 257)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 257 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.258 练习:Debug 小任务 **目标**:完成一个最小可复现的小实验(编号 258)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 258 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.259 练习:Export 小任务 **目标**:完成一个最小可复现的小实验(编号 259)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 259 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.260 练习:Dataset 小任务 **目标**:完成一个最小可复现的小实验(编号 260)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 260 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.261 练习:DataLoader 小任务 **目标**:完成一个最小可复现的小实验(编号 261)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 261 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.262 练习:Transform 小任务 **目标**:完成一个最小可复现的小实验(编号 262)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 262 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.263 练习:Module 小任务 **目标**:完成一个最小可复现的小实验(编号 263)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 263 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.264 练习:Optimizer 小任务 **目标**:完成一个最小可复现的小实验(编号 264)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 264 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.265 练习:Scheduler 小任务 **目标**:完成一个最小可复现的小实验(编号 265)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 265 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.266 练习:AMP 小任务 **目标**:完成一个最小可复现的小实验(编号 266)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 266 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.267 练习:DDP 小任务 **目标**:完成一个最小可复现的小实验(编号 267)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 267 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.268 练习:Debug 小任务 **目标**:完成一个最小可复现的小实验(编号 268)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 268 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.269 练习:Export 小任务 **目标**:完成一个最小可复现的小实验(编号 269)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 269 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.270 练习:Dataset 小任务 **目标**:完成一个最小可复现的小实验(编号 270)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 270 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.271 练习:DataLoader 小任务 **目标**:完成一个最小可复现的小实验(编号 271)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 271 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.272 练习:Transform 小任务 **目标**:完成一个最小可复现的小实验(编号 272)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 272 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.273 练习:Module 小任务 **目标**:完成一个最小可复现的小实验(编号 273)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 273 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.274 练习:Optimizer 小任务 **目标**:完成一个最小可复现的小实验(编号 274)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 274 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.275 练习:Scheduler 小任务 **目标**:完成一个最小可复现的小实验(编号 275)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 275 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.276 练习:AMP 小任务 **目标**:完成一个最小可复现的小实验(编号 276)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 276 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.277 练习:DDP 小任务 **目标**:完成一个最小可复现的小实验(编号 277)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 277 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.278 练习:Debug 小任务 **目标**:完成一个最小可复现的小实验(编号 278)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 278 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.279 练习:Export 小任务 **目标**:完成一个最小可复现的小实验(编号 279)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 279 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.280 练习:Dataset 小任务 **目标**:完成一个最小可复现的小实验(编号 280)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 280 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.281 练习:DataLoader 小任务 **目标**:完成一个最小可复现的小实验(编号 281)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 281 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.282 练习:Transform 小任务 **目标**:完成一个最小可复现的小实验(编号 282)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 282 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.283 练习:Module 小任务 **目标**:完成一个最小可复现的小实验(编号 283)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 283 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.284 练习:Optimizer 小任务 **目标**:完成一个最小可复现的小实验(编号 284)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 284 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.285 练习:Scheduler 小任务 **目标**:完成一个最小可复现的小实验(编号 285)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 285 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.286 练习:AMP 小任务 **目标**:完成一个最小可复现的小实验(编号 286)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 286 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.287 练习:DDP 小任务 **目标**:完成一个最小可复现的小实验(编号 287)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 287 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.288 练习:Debug 小任务 **目标**:完成一个最小可复现的小实验(编号 288)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 288 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.289 练习:Export 小任务 **目标**:完成一个最小可复现的小实验(编号 289)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 289 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.290 练习:Dataset 小任务 **目标**:完成一个最小可复现的小实验(编号 290)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 290 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.291 练习:DataLoader 小任务 **目标**:完成一个最小可复现的小实验(编号 291)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 291 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.292 练习:Transform 小任务 **目标**:完成一个最小可复现的小实验(编号 292)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 292 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.293 练习:Module 小任务 **目标**:完成一个最小可复现的小实验(编号 293)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 293 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.294 练习:Optimizer 小任务 **目标**:完成一个最小可复现的小实验(编号 294)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 294 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.295 练习:Scheduler 小任务 **目标**:完成一个最小可复现的小实验(编号 295)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 295 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.296 练习:AMP 小任务 **目标**:完成一个最小可复现的小实验(编号 296)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 296 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.297 练习:DDP 小任务 **目标**:完成一个最小可复现的小实验(编号 297)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 297 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.298 练习:Debug 小任务 **目标**:完成一个最小可复现的小实验(编号 298)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 298 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.299 练习:Export 小任务 **目标**:完成一个最小可复现的小实验(编号 299)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 299 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 ### A.300 练习:Dataset 小任务 **目标**:完成一个最小可复现的小实验(编号 300)。 **提示**(你应该能独立完成): - 明确输入张量 shape - 打印 dtype/device - 用断言确保输出符合预期 **参考代码骨架**: ```python # Exercise 300 import torch # TODO: 替换为你的实现 x = torch.randn(2, 3) print('shape', x.shape, 'dtype', x.dtype, 'device', x.device) ``` **自检**: - 程序可运行 - 输出与你预期一致 --- ## 附录B:PyTorch工程速查表(Cheat Sheet) ### B.1 张量形状与常用操作 - **reshape/view**:`x.view(B,-1)` - **permute**:`x.permute(0,2,3,1)` - **contiguous**:`x = x.contiguous()` - **stack**:`torch.stack(list_of_tensors, dim=0)` - **cat**:`torch.cat(list_of_tensors, dim=1)` - **gather**:`torch.gather(x, dim=1, index=idx)` - **where**:`torch.where(mask, a, b)` - **einsum**:`torch.einsum('btd,bdh->bth', a, b)` - **no_grad**:`with torch.no_grad(): ...` - **inference_mode**:`with torch.inference_mode(): ...` ### B.2 训练循环检查清单 1. `model.train()` / `model.eval()` 是否正确切换? 2. loss 是否对 logits 直接用 `CrossEntropyLoss`(不要先 softmax)? 3. optimizer 是否 `zero_grad`?是否使用 `set_to_none=True` 提速? 4. 是否有梯度累积导致 loss 需要除以 accum_steps? 5. 是否记录并打印 lr? 6. checkpoint 是否保存 best 模型而不是 last? 7. 验证集是否关闭增强与随机性? 8. seed 是否固定(如需可复现)? ![训练循环检查清单可视化](/sec08_019.png)