[英]Concat tensors in PyTorch
I have a tensor called data
of the shape [128, 4, 150, 150]
where 128 is the batch size, 4 is the number of channels, and the last 2 dimensions are height and width.我有一个名为
[128, 4, 150, 150]
形状data
的张量[128, 4, 150, 150]
其中 128 是批量大小,4 是通道数,最后两个维度是高度和宽度。 I have another tensor called fake
of the shape [128, 1, 150, 150]
.我有另一个称为
[128, 1, 150, 150]
形状的fake
张量。
I want to drop the last list/array
from the 2nd dimension of data
;我想从
data
的第二维中删除最后一个list/array
; the shape of data would now be [128, 3, 150, 150]
;数据的形状现在是
[128, 3, 150, 150]
; and concatenate it with fake
giving the output dimension of the concatenation as [128, 4, 150, 150]
.并将其与
fake
连接起来,给出连接的输出维度为[128, 4, 150, 150]
。
Basically, in other words, I want to concatenate the first 3 dimensions of data
with fake
to give a 4-dimensional tensor.基本上,换句话说,我想将
data
的前 3 个维度与fake
以给出一个 4 维张量。
I am using PyTorch and came across the functions torch.cat()
and torch.stack()
我正在使用 PyTorch 并遇到了函数
torch.cat()
和torch.stack()
Here is a sample code I've written:这是我编写的示例代码:
fake_combined = []
for j in range(batch_size):
fake_combined.append(torch.stack((data[j][0].to(device), data[j][1].to(device), data[j][2].to(device), fake[j][0].to(device))))
fake_combined = torch.tensor(fake_combined, dtype=torch.float32)
fake_combined = fake_combined.to(device)
But I am getting an error in the line:但是我在行中遇到错误:
fake_combined = torch.tensor(fake_combined, dtype=torch.float32)
The error is:错误是:
ValueError: only one element tensors can be converted to Python scalars
Also, if I print the shape of fake_combined
, I get the output as [128,]
instead of [128, 4, 150, 150]
另外,如果我打印
fake_combined
的形状,我得到的输出为[128,]
而不是[128, 4, 150, 150]
And when I print the shape of fake_combined[0]
, I get the output as [4, 150, 150]
, which is as expected.当我打印
fake_combined[0]
的形状时,我得到的输出为[4, 150, 150]
,这是预期的。
So my question is, why am I not able to convert the list to tensor using torch.tensor()
.所以我的问题是,为什么我不能使用
torch.tensor()
将列表转换为张量。 Am I missing something?我错过了什么吗? Is there any better way to do what I intend to do?
有没有更好的方法来做我打算做的事情?
Any help will be appreciated!任何帮助将不胜感激! Thanks!
谢谢!
@rollthedice32 's answer works perfectly fine. @rollthedice32 的答案非常好。 For educational purposes, here's using
torch.cat
出于教育目的,这里使用
torch.cat
a = torch.rand(128, 4, 150, 150)
b = torch.rand(128, 1, 150, 150)
# Cut out last dimension
a = a[:, :3, :, :]
# Concatenate in 2nd dimension
result = torch.cat([a, b], dim=1)
print(result.shape)
# => torch.Size([128, 4, 150, 150])
You could also just assign to that particular dimension.您也可以只分配给该特定维度。
orig = torch.randint(low=0, high=10, size=(2,3,2,2))
fake = torch.randint(low=111, high=119, size=(2,1,2,2))
orig[:,[2],:,:] = fake
Original Before原版之前
tensor([[[[0, 1],
[8, 0]],
[[4, 9],
[6, 1]],
[[8, 2],
[7, 6]]],
[[[1, 1],
[8, 5]],
[[5, 0],
[8, 6]],
[[5, 5],
[2, 8]]]])
Fake伪造的
tensor([[[[117, 115],
[114, 111]]],
[[[115, 115],
[118, 115]]]])
Original After原版之后
tensor([[[[ 0, 1],
[ 8, 0]],
[[ 4, 9],
[ 6, 1]],
[[117, 115],
[114, 111]]],
[[[ 1, 1],
[ 8, 5]],
[[ 5, 0],
[ 8, 6]],
[[115, 115],
[118, 115]]]])
Hope this helps!希望这可以帮助! :)
:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.