最近发现自己的代码能力属实不太行,所以打算每天抽点时间来汇总一下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]])
|