pytorch有用但不常用的API汇总(就先整这么多)

最近发现自己的代码能力属实不太行,所以打算每天抽点时间来汇总一下pytorch的API文档。

创建新的Tensor:

tensor.numel() #返回tensor中element的数量,直接就是int格式,不需要item()啥的

torch.sparse_coo_tensor #先mark一下,感觉以后可能会用得着,到时候再查

>>> torch.arange(0,10,3)
[0,3,6,9] #[step, end)

>>> torch.linspace(0,10,5)
[0, 2.5, 5, 7.5, 10] #start和end都包含,相邻数据的间隔是(end-start) / (steps-1)
#steps是几,tensor就包含几个元素

torch.logspace(start, end, steps, base=10.0)
>>> torch.logspace(0, 10, 3, base = 2)
[1, 2^5, 2^10] #linspace的指数版

torch.eye(m,n) #返回一个m行n列的tensor,对角元素是1

>>> torch.full((2,3),3.14)
tensor([[3.1400, 3.1400, 3.1400],
        [3.1400, 3.1400, 3.1400]])
torch.full_like(tensor,value)

torch.heaviside(input,value) 
# input和value都是tensor,value可以是一个值或者和input size相同
# input[i]<0, out[i] = 0; input[i] == 0, out[i] = value[i]; input[i]>0, out[i]=1
# 强行把小于0的元素变成0,把大于0的元素变成1,把等于0的元素变成value,感觉有点用又用处不大

Tensor的各种处理:索引,拼接等等:

torch.chunk(input, chunks(int), dim=0) → List of Tensors
# 将tensor分块,返回[Tensors],最后一个元素可能size会比较小,如果不能正好整除的话
>>> a = torch.randn(3,2)
>>> torch.chunk(a,2)
(tensor([[ 1.2660, -1.0006],
        [-0.5908, -0.0418]]), tensor([[0.2641, 0.8713]]))

torch.gather(input, dim, index) → Tensor
# index是一个下标Tensor,这个函数可以从input取出一个size和index一样的Tensor
# out[i][j][k] = input[index[i][j][k]][j][k]  # if dim == 0
# out[i][j][k] = input[i][index[i][j][k]][k]  # if dim == 1
# out[i][j][k] = input[i][j][index[i][j][k]]  # if dim == 2

Tensor.scatter_(dim, index, src, reduce=None) → Tensor
Tensor.scatter_add_(dim, index, src) → Tensor
# 这个完全相反,不是从input中往外取,而是修改input里面的值
# self[index[i][j][k]][j][k] = src[i][j][k]  # if dim == 0
# self[i][index[i][j][k]][k] = src[i][j][k]  # if dim == 1
# self[i][j][index[i][j][k]] = src[i][j][k]  # if dim == 2

torch.index_select(input, dim, index) → Tensor
# 在dim维度上,根据index来选出input中的tensor
>>> x = torch.randn(3, 4)
>>> x
tensor([[ 0.1427,  0.0231, -0.5414, -1.0009],
        [-0.4664,  0.2647, -0.1228, -1.1068],
        [-1.1734, -0.6571,  0.7230, -0.6004]])
>>> indices = torch.tensor([0, 2])
>>> torch.index_select(x, 0, indices)
tensor([[ 0.1427,  0.0231, -0.5414, -1.0009],
        [-1.1734, -0.6571,  0.7230, -0.6004]])
>>> torch.index_select(x, 1, indices)
tensor([[ 0.1427, -0.5414],
        [-0.4664, -0.1228],
        [-1.1734,  0.7230]])

torch.masked_select(input, mask) → Tensor
# mask是个Bool类型的Tensor,返回1个1D Tensor

torch.narrow(input, dim, start, length) → Tensor
# 前面是根据index来抽取,这个是顺序抽取,在dim维度上,从start开始,抽length条Tensor
>>> x = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> torch.narrow(x, 0, 0, 2)
tensor([[ 1,  2,  3],
        [ 4,  5,  6]])
>>> torch.narrow(x, 1, 1, 2)
tensor([[ 2,  3],
        [ 5,  6],
        [ 8,  9]])

torch.split(tensor, split_size_or_sections, dim=0)
# 这个是分块函数,split_size_or_sections是int或者list[int]
# 如果是int,每个chunk大小都是int
# 如果是list[int],每个chunk的大小对应list[int]
# torch.chunk的升级版

torch.take(input, index) → Tensor
# input按行展开成1D Tensor,然后取index得到output,output size与index相同

torch.tile(input, reps) → Tensor
# 把input重复teps次,得到新的tensor
>>> x = torch.tensor([1, 2, 3])
>>> x.tile((2,))
tensor([1, 2, 3, 1, 2, 3])
>>> y = torch.tensor([[1, 2], [3, 4]])
>>> torch.tile(y, (2, 2))
tensor([[1, 2, 1, 2],
        [3, 4, 3, 4],
        [1, 2, 1, 2],
        [3, 4, 3, 4]])

torch.unbind(input, dim=0) → seq
# 根据dim,把高维的tensor变成n个降1维的tensor

torch.where(condition, x, y) → Tensor
# 如果condition成立,则输出x,不成立则输出y
# x,y size相同或者可广播
>>> x
tensor([[-0.4620,  0.3139],
        [ 0.3898, -0.7197],
        [ 0.0478, -0.1657]])
>>> torch.where(x > 0, x, y)
tensor([[ 1.0000,  0.3139],
        [ 0.3898,  1.0000],
        [ 0.0478,  1.0000]])

Tensor的各种属性

torch(Tensor).argmax,argmin,amax,amin
# 实际上就是torch.max,torch.max返回amax, argmax,用法相同

torch(Tensor).all()
# 如果整个tensor的bool指都是true,返回true,否则返回false

torch(Tensor).any()
# 和all一样,如果整个Tensor包含一个bool值是true的element,则返回true,否则返回false

torch.dist(input, other, p=2) → Tensor
# 这个应该会很常用,求input-other的p范数,p范数是啥应该就不用解释了吧

torch.logsumexp(input, dim)
# 这个感觉可能用得到又不常用,插个眼先

torch.nansum(input) → Tensor
# 将Nan替换成0求和

torch.prod(input, dim) → Tensor
# 元素相乘

torch.count_nonzero(input, dim=None) → Tensor
# 可能会有用的函数,输出tensor中非0值的数量

torch.maximum(input, other, *, out=None) → Tensor
torch.minimum(input, other, *, out=None) → Tensor
# 输出两个tensor的最大值或最小值
>>> a = torch.tensor((1, 2, -1))
>>> b = torch.tensor((3, 0, 4))
>>> torch.maximum(a, b)
tensor([3, 2, 4])

torch.sotr torch.topk
# 插眼

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值