前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python|张量创建操作[5]

Python|张量创建操作[5]

作者头像
福贵
发布2020-05-27 23:49:08
5960
发布2020-05-27 23:49:08
举报
文章被收录于专栏:菜鸟致敬菜鸟致敬

创建操作续3

torch.quantizeperchannel(input, scales, zero_points, axis, dtype) → Tensor

根据给定的scale和zero points,把每个通道量化,返回张量 参数

  • input(Tensor):需要量化的浮点张量
  • scales(Tensor):一维张量,给每个通道指定scale,大小是input.size(axis)
  • zero_points(Tensor):size是input.size(axis)
  • dtype

例子

代码语言:javascript
复制
    >>> x = torch.tensor([[-1.0, 0.0], [1.0, 2.0]])

    >>> torch.quantize_per_channel(x, torch.tensor([0.1, 0.01]), torch.tensor([10, 0]), 0, torch.quint8)

    tensor([[-1.,  0.],

            [ 1.,  2.]], size=(2, 2), dtype=torch.quint8,

           quantization_scheme=torch.per_channel_affine,

           scale=tensor([0.1000, 0.0100], dtype=torch.float64),

           zero_point=tensor([10,  0]), axis=0)

    >>> torch.quantize_per_channel(x, torch.tensor([0.1, 0.01]), torch.tensor([10, 0]), 0, torch.quint8).int_repr()

    tensor([[  0,  10],

            [100, 200]], dtype=torch.uint8)

索引,切片,连接和转换操作

torch.cat(tensors, dim=0, out=None) → Tensor

指定维度,连接给定的张量,张量需要有相同的形状,或者为空也可以 参数

  • Tensors(Tensor序列):需要连接的张量序列
  • dim(int,可选参数):张量连接的维度
  • out:输出张量

例子

代码语言:javascript
复制
        >>> x = torch.randn(2, 3)

    >>> x

    tensor([[ 0.6580, -1.0969, -0.4614],

            [-0.1034, -0.5790,  0.1497]])

    >>> torch.cat((x, x, x), 0)

    tensor([[ 0.6580, -1.0969, -0.4614],

            [-0.1034, -0.5790,  0.1497],

            [ 0.6580, -1.0969, -0.4614],

            [-0.1034, -0.5790,  0.1497],

            [ 0.6580, -1.0969, -0.4614],

            [-0.1034, -0.5790,  0.1497]])

    >>> torch.cat((x, x, x), 1)

    tensor([[ 0.6580, -1.0969, -0.4614,  0.6580, -1.0969, -0.4614,  0.6580,

             -1.0969, -0.4614],

            [-0.1034, -0.5790,  0.1497, -0.1034, -0.5790,  0.1497, -0.1034,

             -0.5790,  0.1497]])

torch.chunk(input, chunks, dim=0) → List of Tensors

把张量分割成指定数量的块,每个块是输入张量的view 最后一个块如果张量沿着指定的维度不可分割成指定形状的块,那么最后一个块形状更小 参数

  • input(Tensor):需要分割的张量
  • chunks(int):需要返回的块数量
  • dim(int):沿分割的维度

torch.gather(input, dim, index, out=None, sparse_grad=False) → Tensor

沿给定的维度轴,收集值 对于一个三维张量:

代码语言:javascript
复制
    out[i][j][k] = input[index[i][j][k]][j][k]  # 如果 dim == 0

    out[i][j][k] = input[i][index[i][j][k]][k]  # 如果 dim == 1

    out[i][j][k] = input[i][j][index[i][j][k]]  # 如果 dim == 2

如果输入张量是n维,大小为(x0,x1,x2,...,xn-1),指定的dim为i,那么index必须是n维张量,在y>=1的地方,其大小为(x0,x1,...,xn-1),输出张量out则和index一样的size 参数

  • input(Tensor):源张量
  • dim(int):索引的轴
  • index:需要收集元素的索引
  • out
  • sparse_grad(bool,可选参数):如果是True,那么输入input是一个稀疏张量

例子

代码语言:javascript
复制
    >>> t = torch.tensor([[1,2],[3,4]])

    >>> torch.gather(t, 1, torch.tensor([[0,0],[1,0]]))

    tensor([[ 1,  1],

            [ 4,  3]])torch.index_select(input, dim, index, out=None) → Tensor

返回一个新的张量,沿input指定的dim索引,index是一个长张量 返回的张量和源张量维度相同,指定dim的这个维度和index一样长度,其他的维度和源张量一样 返回张量开辟新的内存,如果输出张量out的shape不适合,会自动纠正,并且必要时重新开辟内存 参数

  • input(Tensor):输入张量
  • dim(int):我们需要索引的维度
  • index(LongTensor):包含需要索引的序列
  • out

例子

代码语言:javascript
复制
    >>> x = torch.randn(3, 4)

    >>> x

    tensor([[ 0.1427,  0.0231, -0.5414, -1.0009],

            [-0.4664,  0.2647, -0.1228, -1.1068],

            [-1.1734, -0.6571,  0.7230, -0.6004]])

    >>> indices = torch.tensor([0, 2])

    >>> torch.index_select(x, 0, indices)

    tensor([[ 0.1427,  0.0231, -0.5414, -1.0009],

            [-1.1734, -0.6571,  0.7230, -0.6004]])

    >>> torch.index_select(x, 1, indices)

    tensor([[ 0.1427, -0.5414],

            [-0.4664, -0.1228],

            [-1.1734,  0.7230]])
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-05-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python与MySQL 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 创建操作续3
    • torch.quantizeperchannel(input, scales, zero_points, axis, dtype) → Tensor
      • 例子
      • 索引,切片,连接和转换操作
        • torch.cat(tensors, dim=0, out=None) → Tensor
          • 例子
            • torch.chunk(input, chunks, dim=0) → List of Tensors
              • torch.gather(input, dim, index, out=None, sparse_grad=False) → Tensor
                • 例子
                  • 例子
                  领券
                  问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档