pytorch 相关操作
查看NVIDIA驱动版本
nvidia-smi
conda相关
创建conda环境
conda create -n pytorch-xxx python=3.10
进入/退出conda环境
conda activate pytorch-xxx
conda deactivate
安装pytorch
版本windows python的 cuda 11.8
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia
pytorch数据类型等基础编程
import torch
a = torch.Tensor([[1, 2], [3, 4]])
b = torch.Tensor(2, 4) # 2x4, 值随机
c = torch.ones(2, 2) # 全1
d = torch.eye(2, 3) # 对角线
e = torch.normal(mean=0, std=torch.rand(5)) # 正态分布
f = torch.normal(mean=torch.rand(5), std=torch.rand(5))
g = torch.Tensor(2, 2).uniform_(-1, 1) # 2x2的tensor,值为-1到1
h = torch.linspace(1, 20, 6) # 从1到20线性等分,等差数列,个数为2
i = torch.randperm(10) # 0到9随机排列的1维tensor
print(h)
# value type device layout
aa = torch.tensor([1, 2, 3], dtype=torch.int8, device=torch.device('cpu'))
print(aa)
print(aa.dtype, aa.layout)
# 稀疏tensor
indices = torch.tensor([[0, 1, 1], [2, 0, 2]]) # [[x1, x2, x3], [y1, y2, y3]]
values = torch.tensor([3, 4, 5], dtype=torch.float32) # [val(x1, y1), val(x2, y2), val(x3, y3)],其余为0
bb = torch.sparse_coo_tensor(indices, values, [2, 4]) # 坐标, 值, 形状
bb_stride = bb.to_dense() # 转成稠密张量
print(bb)
print(bb.dtype, bb.device, bb.layout)
print(bb_stride)

浙公网安备 33010602011771号