[pytorch]基础函数笔记

这篇博客详细介绍了PyTorch中的基础函数用法,包括广播机制、pdb调试中画掩码以及torch.nn.CrossEntropyLoss的使用。特别强调了在使用CrossEntropyLoss时,输入和目标的维度匹配规则。
摘要由CSDN通过智能技术生成

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)→ Tensortorch.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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值