美文网首页
torch.cat()使用方法

torch.cat()使用方法

作者: 一位学有余力的同学 | 来源:发表于2021-03-21 18:56 被阅读0次

    torch.cat()可以对tensor进行上下或左右拼接,其传入的参数为:

    torch.cat(tensors, dim=0)

    dim控制拼接的维度,dim=0为上下拼接,dim=1为左右拼接[1]
    下面是实例详解:

    >>> import torch
    >>> a = torch.rand((2,3))
    >>> a
    tensor([[0.7515, 0.1021, 0.0726],
            [0.0575, 0.1666, 0.2763]])
    >>> b = torch.rand((2,3))
    >>> b
    tensor([[0.7485, 0.8340, 0.2617],
            [0.7847, 0.2847, 0.3445]])
    >>> c =torch.cat((a,b),dim=0)
    >>> c
    tensor([[0.7515, 0.1021, 0.0726],
            [0.0575, 0.1666, 0.2763],
            [0.7485, 0.8340, 0.2617],
            [0.7847, 0.2847, 0.3445]])
    >>> d = torch.cat((a,b),dim=1)
    >>> d
    tensor([[0.7515, 0.1021, 0.0726, 0.7485, 0.8340, 0.2617],
            [0.0575, 0.1666, 0.2763, 0.7847, 0.2847, 0.3445]])
    

    1. torch.cat()官方文档

    相关文章

      网友评论

          本文标题:torch.cat()使用方法

          本文链接:https://www.haomeiwen.com/subject/hlrocltx.html