文章目录
- [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]]]))