当前位置: 首页 > article >正文

torch_unbindtorch_chunk

文章目录

  • 1. torch.unbind
  • 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 代码
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=
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
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 结果
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]]]))

http://www.kler.cn/a/538672.html

相关文章:

  • kafka服务端之副本
  • [笔记] 汇编杂记(持续更新)
  • centos7 curl#6 - Could not resolve host mirrorlist.centos.org; 未知的错误 解决方案
  • 版本更新|OpenCSG AutoHub v0.2.8
  • YouBIP 项目
  • 五十天精通硬件设计第20天-CAN协议及电路设计介绍
  • 0207作业
  • DeepSeek 和 ChatGPT 的商业化发展前景对比
  • 深入浅出谈VR(虚拟现实、VR镜头)
  • 1、http介绍
  • 深入探究 C++17 std::is_invocable
  • LeetCode--300. 最长递增子序列【DP+二分】
  • 自动化测试工具selenium的安装踩坑
  • android隐藏虚拟按键recents button
  • Android的MQTT客户端实现
  • Qt实现简易视频播放器
  • Spring Boot 自动装配原理与优化实践
  • 算法与数据结构(合并K个升序链表)
  • C#面试常考随笔14: 方法如何传递不定数量的参数?params关键字怎么使用?
  • kafak最新安装教程【kafka_2.13-3.9.0】双语版
  • 电商行业的新篇章:3D和AR技术助力销售转化率提升!
  • 基于html和vue.js以及其他编程技术打造一个仿京东购物网站平台
  • c++学习笔记——c++基础
  • 【DeepSeek】DeepSeek概述 | 本地部署deepseek
  • Day81:数据的保存
  • Thinkpad T480s/X1c 2018 Manjaro Sway(ArchLinux)安装指纹(ID 06cb:009a)