将数字添加到张量的最后一个维度
Adding a number to the last dimension of a tensor
我正在尝试将一个数字添加到张量中,以将此整数添加为新维度的方式。
张量为2行7列:
x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]
x = torch.tensor(x)
x = x.reshape(-1,7)
print(x.shape)
print(x)
结果是:
torch.Size([2, 7])
tensor([[ 1, 2, 3, 4, 5, 6, 7],
[ 8, 9, 10, 11, 12, 13, 14]])
数字是浮点数:
a= 0.19
b= torch.tensor([a])
b.reshape(-1,1)
b= b.unsqueeze(dim=1)
print(b.shape)
b
即:
torch.Size([1, 1])
tensor([[0.1900]])
我要生成的是一个[2,8]
张量:
tensor([[1, 2, 3, 4, 5, 6, 7,0.1900],
[8, 9, 10, 11, 12, 13, 14,0.1900]])
所以,我想我可以torch.stack拥有一个新的维度:
c= torch.stack((x, b), dim=-1)
给出错误:RuntimeError: stack expects each tensor to be equal size, but got [2, 7] at entry 0 and [1, 1] at entry 1
PS:我试图将 x 重塑为 [14,1] 的形状并添加 [1,1] 浮点张量来制作 [15,1],但它只添加了一次所以我不能再做一个新的[2,8]。
x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]
x = torch.tensor(x)
x = x.reshape(-1,1)
print(x.shape)
print(x)
torch.Size([14, 1])
tensor([[ 1],
[ 2],
[ 3],
[ 4],
[ 5],
[ 6],
[ 7],
[ 8],
[ 9],
[10],
[11],
[12],
[13],
[14]])
print('b',b)
c= torch.cat((x, b), dim=-2)
print(c.shape)
b tensor([[0.1900]])
torch.Size([15, 1])
我很乐意得到一些帮助!
在连接它们之前需要展开张量 b
:
import torch
x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]
x = torch.tensor(x)
x = x.reshape(-1,7)
a=0.19
b= torch.tensor([a])
torch.cat((x,b.expand((2,1))),dim=1)
将给予:
tensor([[ 1.0000, 2.0000, 3.0000, 4.0000, 5.0000, 6.0000, 7.0000, 0.1900],
[ 8.0000, 9.0000, 10.0000, 11.0000, 12.0000, 13.0000, 14.0000, 0.1900]])
这是我运行重现的初始化代码的一部分:
x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]
x = torch.tensor(x)
x = x.reshape(-1,7)
a = 0.19
b = torch.tensor([a])
b.reshape(-1,1)
b = b.unsqueeze(dim=1)
我运行此代码之后:
b = torch.tile(b, (2, 1))
torch.cat((x, b), dim=1)
输出:
tensor([[ 1.0000, 2.0000, 3.0000, 4.0000, 5.0000, 6.0000, 7.0000, 0.1900],
[ 8.0000, 9.0000, 10.0000, 11.0000, 12.0000, 13.0000, 14.0000, 0.1900]])
我正在尝试将一个数字添加到张量中,以将此整数添加为新维度的方式。 张量为2行7列:
x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]
x = torch.tensor(x)
x = x.reshape(-1,7)
print(x.shape)
print(x)
结果是:
torch.Size([2, 7])
tensor([[ 1, 2, 3, 4, 5, 6, 7],
[ 8, 9, 10, 11, 12, 13, 14]])
数字是浮点数:
a= 0.19
b= torch.tensor([a])
b.reshape(-1,1)
b= b.unsqueeze(dim=1)
print(b.shape)
b
即:
torch.Size([1, 1])
tensor([[0.1900]])
我要生成的是一个[2,8]
张量:
tensor([[1, 2, 3, 4, 5, 6, 7,0.1900],
[8, 9, 10, 11, 12, 13, 14,0.1900]])
所以,我想我可以torch.stack拥有一个新的维度:
c= torch.stack((x, b), dim=-1)
给出错误:RuntimeError: stack expects each tensor to be equal size, but got [2, 7] at entry 0 and [1, 1] at entry 1
PS:我试图将 x 重塑为 [14,1] 的形状并添加 [1,1] 浮点张量来制作 [15,1],但它只添加了一次所以我不能再做一个新的[2,8]。
x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]
x = torch.tensor(x)
x = x.reshape(-1,1)
print(x.shape)
print(x)
torch.Size([14, 1])
tensor([[ 1],
[ 2],
[ 3],
[ 4],
[ 5],
[ 6],
[ 7],
[ 8],
[ 9],
[10],
[11],
[12],
[13],
[14]])
print('b',b)
c= torch.cat((x, b), dim=-2)
print(c.shape)
b tensor([[0.1900]])
torch.Size([15, 1])
我很乐意得到一些帮助!
在连接它们之前需要展开张量 b
:
import torch
x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]
x = torch.tensor(x)
x = x.reshape(-1,7)
a=0.19
b= torch.tensor([a])
torch.cat((x,b.expand((2,1))),dim=1)
将给予:
tensor([[ 1.0000, 2.0000, 3.0000, 4.0000, 5.0000, 6.0000, 7.0000, 0.1900],
[ 8.0000, 9.0000, 10.0000, 11.0000, 12.0000, 13.0000, 14.0000, 0.1900]])
这是我运行重现的初始化代码的一部分:
x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]
x = torch.tensor(x)
x = x.reshape(-1,7)
a = 0.19
b = torch.tensor([a])
b.reshape(-1,1)
b = b.unsqueeze(dim=1)
我运行此代码之后:
b = torch.tile(b, (2, 1))
torch.cat((x, b), dim=1)
输出:
tensor([[ 1.0000, 2.0000, 3.0000, 4.0000, 5.0000, 6.0000, 7.0000, 0.1900],
[ 8.0000, 9.0000, 10.0000, 11.0000, 12.0000, 13.0000, 14.0000, 0.1900]])