文章目录
- 1. `torch.randn((dim1, dim2))`
- 2. `torch.linspace(start, end, steps=100, out=None)`
- 3. `torch.meshgird(x, y)`
- 4. `torch.expand(*sizes) → Tensor`
- 5.`torch.cat(tensors,dim=0,out=None)→ Tensor` 和 `torch.stack(tensors,dim=0,out=None)→ Tensor`
- 6. `torch.nn.functional.interpolate(input_shape, scale_factor= , mode='')`
- 7. `torch.sum() , torch.ones()`
- 8. `torch.nonzero() 返回查找符合条件的索引`
- 9. `torch.clamp(input, min, max, *, out=None) → Tensor`
- 10. `torch.squeeze(input, dim) and torch.unsqueeze(input, dim)`
- 11. `torch.nn.functional.max_pool2d` 以及 `关于tensor之间的比较,bool值的转化 `
- 12. `torch.Tensor.view(*shape) → Tensor`
- 13. `torch.cumsum(input, dim, *, dtype=None, out=None) → Tensor`
- 14. `torch.argsort(input, dim=-1, descending=False) → LongTensor`
- 14. `torch.mm(input, mat2, *, out=None) → Tensor`
- 15. `torch.triu(input, diagonal=0, *, out=None) → Tensor`
- 16. `torch.nn.functional.interpolate(input, size=None, scale_factor=None, mode='nearest', align_corners=None, recompute_scale_factor=None)`
- 17. `torch.zeros(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor`
- 18. `pytorch通过索引直接扩大矩阵`
- 19. `torch.repeat(*sizes) → Tensor`
- 20. 广播以及在pdb调试中画掩码
- 21. torch.nn.CrossEntropyLoss()
1. torch.randn((dim1, dim2))
import torch
# 1.torch.randn
s = torch.randn((5,2,3))
2. torch.linspace(start, end, steps=100, out=None)
# 2.torch.linspace
a = torch.linspace(-1, 1, s.shape[-1]) # 均匀分割。 [-1, 1]分成s.shape[0]
b = torch.linspace(-1, 1, s.shape[-2])
print('a:', a)
print('b:', b)
print('-'* 100)
a: tensor([-1., 0., 1.])
b: tensor([-1., 1.])
3. torch.meshgird(x, y)
# a: tensor([-1., 0., 1.])
# b: tensor([-1., 1.])
# 3.torch.meshgrid
a, b = torch.meshgrid(a, b) # meshgrid后为[3, 2] 但是根据元素的不同 a,b也不同
# 也就是将a,b扩展为 a x b的大小,可用于将一维的坐标扩展为二维上的坐标。(x或者y在变换中其中有一个坐标是保持不变的)
print(a.size(), b.size())
print('meshgrid a:', a)
print('meshgrid b:', b)
print('-'* 100)
torch.Size([3, 2]) torch.Size([3, 2])
meshgrid a: tensor([[-1., -1.],
[ 0., 0.],
[ 1., 1.]])
meshgrid b: tensor([[-1., 1.],
[-1., 1.],
[-1., 1.]])
4. torch.expand(*sizes) → Tensor
# 4.torch.expand
a = a.expand(s.shape[0], 1, -1, -1) # 第三第四个参数为-1,-1 表示使用a.shape(3, 2)本身的2个维度
b = b.expand(s.shape[0], 1, -1, -1) # [5, 1, 3, 2] 上同
print(a.size())
print(b.size())
torch.Size([5, 1, 3, 2])
torch.Size([5, 1, 3, 2])
5.torch.cat(tensors,dim=0,out=None)→ Tensor
和 torch.stack(tensors,dim=0,out=None)→ Tensor
# 5.torch.cat()
cat_ab = torch.cat((a,b), dim=1) # 使用cat的前提是 其他维度必须相同。对dim=1上进行cat
print("cat_ab:", cat_ab.size()) # [5, 2, 3, 2]
>>> a = torch.rand((2, 3))
>>> b = torch.rand((2, 3))
>>> c = torch.cat((a, b))
>>> a.size(), b.size(), c.size()
(torch.Size([2, 3]), torch.Size([2, 3]), torch.Size([4, 3]))
>>> a = torch.rand((2, 3))
>>> b = torch.rand((2, 3))
>>> c = torch.stack((a, b))
>>> a.size(), b.size(), c.size()
(torch.Size([2, 3]), torch.Size([2, 3]), torch.Size([2, 2, 3]))
6. torch.nn.functional.interpolate(input_shape, scale_factor= , mode='')
# 6.torch.nn.functional.interpolate
cat_ab = torch.nn.functional.interpolate(cat_ab, scale_factor=2, mode="bilinear") # [N, C, H, W] --> [N, C, 2H, 2W]
print("interpolate cat_ab:", cat_ab.size()) # [5, 2, 6, 4]
cat_ab: torch.Size([5, 2, 3, 2])
interpolate cat_ab: torch.Size([5, 2, 6, 4])
7. torch.sum() , torch.ones()
import torch
a = torch.ones((3,2)) # torch.Size([3, 2])
b = a.sum(dim=0)
b.size() # torch.Size([2])
c = a.sum(dim=1)
b.size() # torch.Size([3])
8. torch.nonzero() 返回查找符合条件的索引
(x condition y) .nonzero()
x=torch.arange(12).view(3,4)
x
tensor([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
(x>4).nonzero()
tensor([[1, 1],
[1, 2],
[1, 3],
[2, 0],
[2, 1],
[2, 2],
[2, 3]])
9. torch.clamp(input, min, max, *, out=None) → Tensor
>>> a = torch.randn(4)
>>> a
tensor([-1.7120, 0.1734, -0.0478, -0.0922])
>>> torch.clamp(a, min=-0.5, max=0.5)
tensor([-0.5000, 0.1734, -0.0478, -0.0922])
>>> a = torch.randn(4)
>>> a
tensor([-0.0299, -2.3184, 2.1593, -0.8883])
>>> torch.clamp(a, min=0.5)
tensor([ 0.5000, 0.5000, 2.1593, 0.5000])
>>> a = torch.randn(4)
>>> a
tensor([ 0.7753, -0.4702, -0.4599, 1.1899])
>>> torch.clamp(a, max=0.5)
tensor([ 0.5000, -0.4702, -0.4599, 0.5000])
10. torch.squeeze(input, dim) and torch.unsqueeze(input, dim)
增加或者减少某维度为1的维数
>>> x = torch.zeros(2, 1