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]]]))
相关推荐
幻想趾于现实1 小时前
视觉硬件选型和算法选择(CNN)
人工智能·深度学习·计算机视觉
Jackson@ML2 小时前
Django开发入门 – 1.搭建基于Python Web框架Django的IDE开发环境
后端·python·django
初学者↑2 小时前
知识图谱可视化系统python+neo4j+vue3
python·知识图谱·neo4j
一念&4 小时前
python爬虫--简单登录
开发语言·爬虫·python
明月看潮生4 小时前
青少年编程与数学 02-008 Pyhon语言编程基础 26课题、常见框架
python·青少年编程·框架·编程语言·编程与数学
明月看潮生4 小时前
青少年编程与数学 02-008 Pyhon语言编程基础 25课题、文件操作
开发语言·python·青少年编程·文件操作·编程与数学
LittleNyima5 小时前
【笔记】扩散模型(一〇):Dreambooth 理论与实现|主题驱动生成
人工智能·笔记·深度学习·aigc·扩散模型
pitch_dark5 小时前
python学习笔记
python
谢眠5 小时前
CNN-day5-经典神经网络LeNets5
人工智能·深度学习·计算机视觉
听吉米讲故事5 小时前
ChatGPT搜索免费开放:AI搜索引擎挑战谷歌霸主地位全面分析
人工智能·搜索引擎·chatgpt