pytorch: Tensor.new_zeros使用场景,type_as函数, torch.stack()使用方法

本文介绍了Tensor库中的new_zeros函数用于创建全零张量,type_as函数实现类型转换,以及torch.stack函数如何沿新维度堆叠张量。通过实例演示了如何在实际项目中运用这些技术来处理多维数据和类型一致性。
摘要由CSDN通过智能技术生成

1. Tensor.new_zeros(size, dtype=None, device=None, requires_grad=False) → Tensor

创建一个tensor基于另一个tensor,目的是什么呢?
答:创建与原tensor 相同类型,相同device的tensor.

Tensor.new_zeros(size, dtype=None, device=None, requires_grad=False) → Tensor
Returns a Tensor of size size filled with 0. 
By default, the returned Tensor has the same torch.dtype and torch.device as this tensor.

12 type_as函数

torch.Tensor的类型转换函数.

def type_as(self, tensor): # real signature unknown; restored from __doc__
    """
    type_as(tensor) -> Tensor
    
    Returns this tensor cast to the type of the given tensor.
    
    This is a no-op if the tensor is already of the correct type. This is
    equivalent to ``self.type(tensor.type())``
    
    Args:
        tensor (Tensor): the tensor which has the desired type
    """
    return _te.Tensor(*(), **{})

一般情况下常常使用tensor后调用的方法:

比如 .double()
.float()
.byte()
.int()

3. torch.stack()

torch.cat() 函数连接两个tensor, 沿着设定的维度增加尺寸。 参考 文章

torch.stack() 沿着一个新维度对输入张量序列进行连接。 序列中所有的张量都应该为相同形状。
浅显说法:把多个2维的张量凑成一个3维的张量;多个3维的凑成一个4维的张量…以此类推,也就是在增加新的维度进行堆叠。

# 假设是时间步T1的输出
T1 = torch.tensor([[1, 2, 3],
          [4, 5, 6],
          [7, 8, 9]])
# 假设是时间步T2的输出
T2 = torch.tensor([[10, 20, 30],
          [40, 50, 60],
          [70, 80, 90]])

print(torch.stack((T1,T2),dim=0).shape)
print(torch.stack((T1,T2),dim=1).shape)
print(torch.stack((T1,T2),dim=2).shape)
print(torch.stack((T1,T2),dim=3).shape)
# outputs:
torch.Size([2, 3, 3])
torch.Size([3, 2, 3])
torch.Size([3, 3, 2])
'选择的dim>len(outputs),所以报错'
IndexError: Dimension out of range (expected to be in range of [-3, 2], but got 3)

比如一种使用方法,增加一个维度时一个列表的tensor连接在一起

dir = Path('/home/ww/dataset/REDS/val_sharp_bicubic/X4/012/')
frames = [read_image(os.path.join(dir, f'{i:08d}.png')) for i in range(10)]

input = torch.stack(frames)

每个图像是 3 * h * w
stack后 是 10 * 3 * h * w

[1]https://blog.csdn.net/xinjieyuan/article/details/105205326

class Detect(nn.Module): stride = None # strides computed during build onnx_dynamic = False # ONNX export parameter def __init__(self, nc=80, anchors=(), ch=(), inplace=True): # detection layer super().__init__() self.nc = nc # number of classes self.no = nc + 5 # number of outputs per anchor self.nl = len(anchors) # number of detection layers self.na = len(anchors[0]) // 2 # number of anchors self.grid = [torch.zeros(1)] * self.nl # init grid a = torch.tensor(anchors).float().view(self.nl, -1, 2) self.register_buffer('anchors', a) # shape(nl,na,2) self.register_buffer('anchor_grid', a.clone().view(self.nl, 1, -1, 1, 1, 2)) # shape(nl,1,na,1,1,2) self.m = nn.ModuleList(nn.Conv2d(x, self.no * self.na, 1) for x in ch) # output conv self.inplace = inplace # use in-place ops (e.g. slice assignment) def forward(self, x): z = [] # inference output for i in range(self.nl): x[i] = self.m[i](x[i]) # conv bs, _, ny, nx = x[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85) x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous() if not self.training: # inference if self.grid[i].shape[2:4] != x[i].shape[2:4] or self.onnx_dynamic: self.grid[i] = self._make_grid(nx, ny).to(x[i].device) y = x[i].sigmoid() if self.inplace: y[..., 0:2] = (y[..., 0:2] * 2. - 0.5 + self.grid[i]) * self.stride[i] # xy y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh else: # for YOLOv5 on AWS Inferentia https://github.com/ultralytics/yolov5/pull/2953 xy = (y[..., 0:2] * 2. - 0.5 + self.grid[i]) * self.stride[i] # xy wh = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i].view(1, self.na, 1, 1, 2) # wh y = torch.cat((xy, wh, y[..., 4:]), -1) z.append(y.view(bs, -1, self.no)) return x if self.training else (torch.cat(z, 1), x) @staticmethod def _make_grid(nx=20, ny=20): yv, xv = torch.meshgrid([torch.arange(ny), torch.arange(nx)]) return torch.stack((xv, yv), 2).view((1, 1, ny, nx, 2)).float()
05-30
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值