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]]]))
相关推荐
爱喝热水的呀哈喽38 分钟前
Java collection实例电影尝试
java·windows·python
python_chai2 小时前
Python核心数据结构详解:元组、集合与字典
java·数据结构·python
程序员小续3 小时前
React 多个 HOC 嵌套太深,会带来哪些隐患?
java·前端·javascript·vue.js·python·react.js·webpack
jndingxin3 小时前
OpenCV 图形API(21)逐像素操作
人工智能·opencv·计算机视觉
程序员小杰@4 小时前
AI前端组件库Ant DesIgn X
开发语言·前端·人工智能
九转成圣4 小时前
windows10安装配置并使用Miniconda3
python·conda
Aerkui4 小时前
Python高阶函数-eval深入解析
开发语言·python
浩哥的技术博客5 小时前
使用MetaGPT 创建智能体(1)入门
人工智能·大模型·智能体
胖哥真不错5 小时前
数据分享:汽车测评数据
python·机器学习·数据分享·汽车测评数据·car evaluation