torch包是一个专门用于多维张量计算的包。
张量 Tensors- torch.is_tensor(obj)
- torch.is_storage(obj):tensor数据结构将数据存储在storage区域
- torch.set_default_tensor_type(t):可以设置CPU和GPU上的tensor数据类型,一般不用
- torch.numel(input)->int
- torch.set_printoptions(precision=None, threshold=None, edgeitems=None, linewidth=None, profile=None):只需懂得使用前三个参数就行
- precision – 浮点数输出的精度位数 (默认为8 )
- threshold – 阈值,若tensor的元素个数超过这个阈值的话,就会折叠显示tensor(默认为1000)
- edgeitems – 当发生折叠显示时,tensor的前后两端打印的行数。
- torch.eye(n, m=None, out=None):返回n行m列的矩阵,对角线位置全1,其它位置全0
- torch.from_numpy(ndarray) → Tensor:将numpy.ndarray 转换为pytorch的 Tensor。两者共享内存。返回的张量不能改变大小。
- torch.linspace(start, end, steps=100, out=None) → Tensor:生成一个 从start 到 end 的tensor。tensor的长度为steps。包括start和end。
- torch.logspace(start, end, steps=100, out=None) → Tensor:返回一个1维张量,包含在区间 1 0 s t a r t 10^{start} 10start 和 1 0 e n d 10^{end} 10end上以对数刻度均匀间隔的steps个点。 输出1维张量的长度为steps。
- torch.ones(*sizes, out=None) → Tensor:参数sizes (int...)
- torch.rand(*sizes, out=None) → Tensor:[0,1)的均匀分布的一组随机数
- torch.randn(*sizes, out=None) → Tensor:标准正泰分布(均值为0,方差为 1)的一组随机数。
- torch.randperm(n, out=None) → LongTensor:从0 到n -1 的随机整数排列。
- torch.arange(start, end, step=1, out=None) → Tensor:不包含end
- torch.zeros(*sizes, out=None) → Tensor
- torch.cat(inputs, dimension=0) → Tensor:在给定维度上对输入的张量序列seq 进行连接操作。
- torch.chunk(tensor, chunks, dim=0):在给定维度(轴)上将输入张量进行分块儿。chunks (int) – 分块的个数
- torch.gather(input, dim, index, out=None) → Tensor:沿给定轴dim,将输入索引张量index指定位置的值进行聚合。
- torch.index_select(input, dim, index, out=None) → Tensor:沿着指定维度对输入进行切片,取index中指定的相应项,然后返回到一个新的张量。index (LongTensor) – 包含索引下标的一维张量
- torch.masked_select(input, mask, out=None) → Tensor:根据张量mask中的索引值,取输入张量中的指定项( mask为一个 ByteTensor),将取值返回到一个新的1D张量。
- torch.nonzero(input, out=None) → LongTensor:输出张量中的每行包含输入中非零元素的索引。
- torch.split(tensor, split_size, dim=0):按指定维度将输入张量进行分割。split_size (int) – 单个分块的形状大小,最后一个块可能小于前面的块。
- torch.squeeze(input, dim=None, out=None):将输入张量形状中的1 去除并返回。返回张量与输入张量共享内存
- torch.stack(sequence, dim=0):增加新的维度进行堆叠。 dim (int) – 插入的维度。0<=dim
- torch.t(input, out=None) → Tensor:input是2维张量,转置0, 1维。
- torch.transpose(input, dim0, dim1, out=None) → Tensor:交换维度dim0和dim1。
- torch.unbind(tensor, dim=0):移除指定维后,返回一个元组,包含了沿着指定维切片后的各个切片
- torch.unsqueeze(input, dim, out=None):对输入的指定位置插入维度 1
- torch.manual_seed(seed):设定生成随机数的种子。 保证每次随机初始化时都一样。
- torch.cuda.manual_seed(int.seed):为当前GPU设置随机种子
- torch.cuda.manual_seed_all(int.seed):为所有的GPU设置种子
- torch.initial_seed():返回生成随机数的原始种子值(python long)。如果用torch.manual_seed(seed)设置了种子,则返回该种子。
- torch.bernoulli(input, out=None) → Tensor:从伯努利分布中抽取二元随机数(0 或者 1)。输入张量中的每个值都是概率,返回值将会是与输入相同大小的张量,每个值为0或者1 参数。
- torch.multinomial(input, num_samples,replacement=False, out=None) → LongTensor:作用是对input的每一行做n_samples次取值,输出的张量是每一次取值时input张量对应行的下标。replacement (bool, optional) – 布尔值,决定是否能重复抽取。
- torch.normal(means, std, out=None):返回正态分布的张量。均值和标准差的形状不须匹配,但每个张量的元素个数须相同。
- torch.normal(mean=0.0, std, out=None):与上面函数类似,所有抽取的样本共享均值。
- torch.normal(means, std=1.0, out=None):与上面函数类似,所有抽取的样本共享标准差。
- torch.save(obj, f, pickle_module=
, pickle_protocol=2):只需关注前两个参数。将obj保存到f路径。 - torch.load(f, map_location=None, pickle_module=
):加载torch.save()保存的对象。
>>> torch.load('tensors.pt')
# Load all tensors onto the CPU
>>> torch.load('tensors.pt', map_location=torch.device('cpu'))
# Load all tensors onto the CPU, using a function
>>> torch.load('tensors.pt', map_location=lambda storage, loc: storage)
# Load all tensors onto GPU 1
>>> torch.load('tensors.pt', map_location=lambda storage, loc: storage.cuda(1))
# Map tensors from GPU 1 to GPU 0
>>> torch.load('tensors.pt', map_location={'cuda:1':'cuda:0'})
# Load tensor from io.BytesIO object
>>> with open('tensor.pt', 'rb') as f:
buffer = io.BytesIO(f.read())
>>> torch.load(buffer)
# Load a module with 'ascii' encoding for unpickling
>>> torch.load('module.pt', encoding='ascii')
并行化 Parallelism
- torch.get_num_threads() → int:获得用于并行化CPU操作的OpenMP线程数
- torch.set_num_threads(int):设定用于并行化CPU操作的OpenMP线程数
数学操作Math operations上面两个函数是跟C++底层相关的,其实就是设置和获取CPU使用的线程数,线程数越多,速度越快。



