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]]]))
相关推荐
星辰大海的精灵3 分钟前
基于Dify+MCP实现通过微信发送天气信息给好友
人工智能·后端·python
ReturnOfMars5 分钟前
AI本地批量生图Agent-Jaaz体验,确实强
人工智能
柠檬味拥抱6 分钟前
人工智能在教育中的角色-AI Agent助力个性化学习与学生辅导
人工智能
精灵vector8 分钟前
Agent短期记忆的几种持久化存储方式
人工智能·python
大模型之路13 分钟前
基于本地LLM与MCP架构构建AI智能体全指南
人工智能·架构
大霸王龙22 分钟前
系统模块与功能设计框架
人工智能·wpf
北京_宏哥23 分钟前
🔥Python零基础从入门到精通详细教程4-数据类型的转换- 上篇
前端·python·面试
乾巫宇宙国监察特使34 分钟前
Python的设计模式
python·测试
Hockor42 分钟前
写给前端的 Python 教程四(列表/元组)
前端·后端·python
Se7en25843 分钟前
Prefix Caching 详解:实现 KV Cache 的跨请求高效复用
人工智能