PyTorch笔记06----拼接与拆分
Tensor的拼接与拆分操作
- Cat
- Stack
- Split
- Chunk
cat
Statisics about scores - [class1-4, students, scores] - [class5-9, students, scores]
a = torch.rand(4, 32, 8) |
stack
create new dim 例如一个老师统计了一个班的成绩:
[students, scores],另一个老师也是,那么使用stack得到[2, students,
scores],而不是把students那一维度拼接起来a = torch.rand(32, 8)
b = torch.rand(32, 8)
torch.stack([a, b], dim = 0).shape
#torch.Size([2, 32, 8])
torch.stack([a, b], dim = 1).shape
#torch.Size([32, 2, 8])
stack必须维度一致 a = torch.rand(30, 8)
b = torch.rand(32, 8)
torch.stack([a, b], dim = 0)
--------ERROR--------
split: by len
b = torch.rand(32, 8) |
chunk: by num
b = torch.rand(32, 8) |