配置环境

这里使用 uv 配置环境

# 终端中使用代理
$env:HTTPS_PROXY = "http://127.0.0.1:7897"
$env:HTTPS_PROXY = "http://127.0.0.1:7897"
# 创建虚拟环境
uv venv --python=3.8
# 激活虚拟环境
.venv\Scripts\activate
# 安装torch
uv pip install torch==1.10.1+cpu torchvision==0.11.2+cpu torchaudio==0.10.1 -f https://download.pytorch.org/whl/cpu/torch_stable.html
uv pip install torchsummary==1.5.1
uv pip install numpy==1.23.2 pandas==1.3.4 matplotlib==3.5.0 scikit-learn==1.3.0
torch==1.10.1+cpu
torchvision==0.11.2+cpu
torchaudio==0.10.1
torchsummary==1.5.1
numpy==1.23.2 
pandas==1.3.4
matplotlib==3.5.0
scikit-learn==1.3.0

torch 常见函数

tensor.permute

tensor.permute(*dims) dims: 维度索引的新顺序。比如原张量形状是 ,你想变成 ,就需要传入索引顺序 (2, 0, 1)

torch.unsqueeze

给数据添加一个维度

torch.nn.ReflectionPad2d(padding) 的参数遵循 (左, 右, 上, 下) 的顺序:

tensor.view

在不改变张量数据的前提下,改变其维度(Shape)。你可以把它想象成对同一块内存数据进行“重新解释”。