本文档记录了一些pytorch常用操作以及概念
1. 构造数据 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 import torchtorch.zeros(3 , 4 ) torch.ones(2 , 3 ) torch.full((2 , 2 ), 7.0 ) x = torch.randn(10 ,8 ,4 ) y = torch.ones_like(x) torch.randn(5 , 10 ) torch.rand(3 , 3 ) torch.randint(0 , 10 , (2 , 4 )) torch.arange(0 , 10 , 2 ) torch.linspace(0 , 1 , 5 ) import numpy as nptorch.from_numpy(np.array([[1 , 2 ], [3 , 4 ]]))
2. 常用输入数据 1 2 3 x = torch.randn(10 , 3 , 32 , 32 ) y = torch.randint(0 , 5 , (10 ,))
3. 向量化操作 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 a = torch.randn(3 , 4 ) b = torch.randn(4 , 5 ) out = a @ b out = torch.matmul(a, b) x = torch.randn(10 , 5 ) bias = torch.randn(5 ) x = x + bias x = torch.arange(0 , 10 , 1 ).reshape(2 , 5 ) bias = torch.tensor(1.0 ) y = x + bias torch.cat([a, a], dim=0 ) torch.cat([a, a], dim=1 ) x = torch.randn(2 , 3 , 4 ) x.view(6 , 4 ) x.permute(1 , 0 , 2 ) x = torch.randn(10 ) mask = x > 0 x[mask]
eval 和 train的切换 eval和train类似现场保护功能,开关切换switch off model.train() 将模型设置为“训练模式”。这会启用诸如 Dropout 和 BatchNorm 这样的层的训练行为(如参数更新、随机失活等)。通常在训练阶段调用。
model.eval() 将模型设置为“评估/推理模式”。这会关闭 Dropout、BatchNorm 等层的训练特性,使用固定参数进行推理。通常在验证或测试阶段调用1 2 3 4 5 6 7 8 9 10 model.eval () for split in ['train' , 'val' ]: losses = torch.zeros(eval_iters) for k in range (eval_iters): X, Y = get_batch(split) logits, loss = model(X, Y) losses[k] = loss.item() out[split] = losses.mean() model.train() return out