pytorch入门之tensor基础操作

    科技2022-07-11  96

    创建tensor

    创建指定元素的tensor: torch.tensor(data, dtype=None, device=None, requires_grad=False, pin_memory=False) 创建随机元素的tensor: torch.rand(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) 创建服从正态分布N(0,1)的tensor: torch.randn(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) 创建在0—(n-1)之间随机整数的tensor: torch.randperm(n, out=None, dtype=torch.int64, layout=torch.strided, device=None, requires_grad=False) 创建指定区间low到high的tensor: torch.randint(low=0, high, size, *, generator=None, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)

    操作tensor

    拼接tensor,dim=0表示按行拼接,dim=1表示列拼接 torch.cat(tensors, dim=0, out=None) 裁剪tensor torch.split(tensor, split_size_or_sections, dim=0) ps: split_size_or_sections的详细介绍 将tensor的维度的数据更换位置 .permute(a,b,c,...)

    example: input = torch.randn(32, 35, 256) input = input.permute(0, 2, 1) input的大小为(32,256,35)

    Processed: 0.008, SQL: 8