torch_unbind&torch_chunk

文章目录

  • [1. torch.unbind](#1. torch.unbind)
  • [2. torch.chunk](#2. torch.chunk)

1. torch.unbind

torch.unbind的作用是将矩阵沿着指定维度进行解耦分割成一个

  • 输入矩阵A = [2,3,4]
  • torch.unbind(input=A,dim=0] , 按照第0维分割,形成2个[3,4],[3,4]矩阵
  • torch.unbind(input=A,dim=1] , 按照第1维分割,形成3个[2,4],[2,4],[2,4]矩阵
  • torch.unbind(input=A,dim=2] , 按照第2维分割,形成4个[2,3],[2,3],[2,3],[2,3]矩阵
  • python 代码
python 复制代码
import torch
import torch.nn as nn

torch.set_printoptions(precision=3, sci_mode=False)

if __name__ == "__main__":
    run_code = 0
    batch_size = 2
    image_w = 3
    image_h = 4
    image_total = batch_size * image_w * image_h
    image = torch.arange(image_total).reshape(batch_size, image_w, image_h)
    image_unbind0 = torch.unbind(input=image, dim=0)
    image_unbind1 = torch.unbind(input=image, dim=1)
    image_unbind2 = torch.unbind(input=image, dim=2)
    print(f"image=\n{image}")
    print(f"image_unbind0=\n{image_unbind0}")
    print(f"image_unbind1=\n{image_unbind1}")
    print(f"image_unbind2=\n{image_unbind2}")
  • 结果:
python 复制代码
image=
tensor([[[ 0,  1,  2,  3],
         [ 4,  5,  6,  7],
         [ 8,  9, 10, 11]],

        [[12, 13, 14, 15],
         [16, 17, 18, 19],
         [20, 21, 22, 23]]])
image_unbind0=
(tensor([[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]]), tensor([[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]))
image_unbind1=
(tensor([[ 0,  1,  2,  3],
        [12, 13, 14, 15]]), tensor([[ 4,  5,  6,  7],
        [16, 17, 18, 19]]), tensor([[ 8,  9, 10, 11],
        [20, 21, 22, 23]]))
image_unbind2=
(tensor([[ 0,  4,  8],
        [12, 16, 20]]), tensor([[ 1,  5,  9],
        [13, 17, 21]]), tensor([[ 2,  6, 10],
        [14, 18, 22]]), tensor([[ 3,  7, 11],
        [15, 19, 23]]))

2. torch.chunk

torch.chunk 的作用是将矩阵按照指定维度分割成指定份数,先按照份数来均匀切割,最后的不够就单独保留

  • python
python 复制代码
import torch
import torch.nn as nn

torch.set_printoptions(precision=3, sci_mode=False)

if __name__ == "__main__":
    run_code = 0
    batch_size = 2
    image_w = 3
    image_h = 4
    image_total = batch_size * image_w * image_h
    image = torch.arange(image_total).reshape(batch_size, image_w, image_h)
    image_unbind0 = torch.unbind(input=image, dim=0)
    image_unbind1 = torch.unbind(input=image, dim=1)
    image_unbind2 = torch.unbind(input=image, dim=2)
    print(f"image=\n{image}")
    print(f"image_unbind0=\n{image_unbind0}")
    print(f"image_unbind1=\n{image_unbind1}")
    print(f"image_unbind2=\n{image_unbind2}")
    image_chunk0 = torch.chunk(input=image,dim=0,chunks=2)
    image_chunk1 = torch.chunk(input=image,dim=1,chunks=2)
    image_chunk2 = torch.chunk(input=image,dim=2,chunks=2)
    print(f"image_chunk0=\n{image_chunk0}")
    print(f"image_chunk1=\n{image_chunk1}")
    print(f"image_chunk2=\n{image_chunk2}")
  • python 结果
python 复制代码
image=
tensor([[[ 0,  1,  2,  3],
         [ 4,  5,  6,  7],
         [ 8,  9, 10, 11]],

        [[12, 13, 14, 15],
         [16, 17, 18, 19],
         [20, 21, 22, 23]]])
image_unbind0=
(tensor([[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]]), tensor([[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]))
image_unbind1=
(tensor([[ 0,  1,  2,  3],
        [12, 13, 14, 15]]), tensor([[ 4,  5,  6,  7],
        [16, 17, 18, 19]]), tensor([[ 8,  9, 10, 11],
        [20, 21, 22, 23]]))
image_unbind2=
(tensor([[ 0,  4,  8],
        [12, 16, 20]]), tensor([[ 1,  5,  9],
        [13, 17, 21]]), tensor([[ 2,  6, 10],
        [14, 18, 22]]), tensor([[ 3,  7, 11],
        [15, 19, 23]]))
image_chunk0=
(tensor([[[ 0,  1,  2,  3],
         [ 4,  5,  6,  7],
         [ 8,  9, 10, 11]]]), tensor([[[12, 13, 14, 15],
         [16, 17, 18, 19],
         [20, 21, 22, 23]]]))
image_chunk1=
(tensor([[[ 0,  1,  2,  3],
         [ 4,  5,  6,  7]],

        [[12, 13, 14, 15],
         [16, 17, 18, 19]]]), tensor([[[ 8,  9, 10, 11]],

        [[20, 21, 22, 23]]]))
image_chunk2=
(tensor([[[ 0,  1],
         [ 4,  5],
         [ 8,  9]],

        [[12, 13],
         [16, 17],
         [20, 21]]]), tensor([[[ 2,  3],
         [ 6,  7],
         [10, 11]],

        [[14, 15],
         [18, 19],
         [22, 23]]]))
相关推荐
pianmian13 分钟前
【无标题】
python
yangmf204016 分钟前
私有知识库 Coco AI 实战(四):打造 ES 索引参数小助手
大数据·人工智能·elasticsearch·coco ai
IT北辰36 分钟前
Python数据处理:文件的自动化重命名与整合
数据库·python·自动化
大数据在线40 分钟前
当向量数据库与云计算相遇:AI应用全面提速
人工智能·云计算·向量数据库·亚马逊云科技·zilliz
stevenzqzq42 分钟前
编程中如何与AI交互-结构化输入和理解确认机制
人工智能·交互
高峰君主1 小时前
生成式AI全栈入侵:当GPT-4开始自动编写你的Next.js路由时,人类开发者该如何重新定义存在价值?
人工智能
J先生x1 小时前
【开源项目】基于sherpa-onnx的实时语音识别系统 - LiveASR
人工智能·语音识别
沉到海底去吧Go2 小时前
【图片识别改名】批量读取图片区域文字识别后批量改名,基于Python和腾讯云的实现方案
开发语言·python·腾讯云
火星资讯2 小时前
“兴火·燎原”总冠军诞生,云宏信息《金融高算力轻量云平台》登顶
人工智能·科技
百锦再2 小时前
Python深度挖掘:openpyxl和pandas的使用详细
java·开发语言·python·框架·pandas·压力测试·idea