昏暗的 pytorch 张量猫 =0 对我不起作用
pytorch tensors cat on dim =0 not worked for me
我在 pytorch 中遇到 cat
的问题。我想在 dim=0 上连接张量,例如,我想要这样的东西
>>> x = torch.randn(2, 3)
>>> x
tensor([[ 0.6580, -1.0969, -0.4614],
[-0.1034, -0.5790, 0.1497]])
>>> torch.cat((x, x, x), 0)
tensor([[ 0.6580, -1.0969, -0.4614],
[-0.1034, -0.5790, 0.1497],
[ 0.6580, -1.0969, -0.4614],
[-0.1034, -0.5790, 0.1497],
[ 0.6580, -1.0969, -0.4614],
[-0.1034, -0.5790, 0.1497]])
但是,当我尝试在我的程序中这样做时,我有
def create_batches_to_device(train_df, test_df, device,batch_size=2):
train_tensor = torch.tensor([])
for i in range (batch_size):
rand_2_strs = train_df.sample(2)
tmp_tensor = torch.tensor([rand_2_strs.get('Sigma').iloc[0],rand_2_strs.get('Sigma').iloc[1],
rand_2_strs.get('mu').iloc[0],rand_2_strs.get('mu').iloc[1],
rand_2_strs.get('th').iloc[0],rand_2_strs.get('th').iloc[1],
np.log(weighted_mse(np.array(rand_2_strs.get('Decay').iloc[0]),np.array(rand_2_strs.get('Decay').iloc[1]),t)[0])])
print("it is tmp tensor")
print(tmp_tensor)
train_tensor = torch.cat((train_tensor,tmp_tensor),dim=0)
print("this is after cat")
print(train_tensor)
create_batches_to_device(train_data, test_data, device)
我有结果
it is tmp tensor
tensor([ 0.3244, -0.6401, -0.7959, 0.9019, 0.1468, -1.7093, -6.4419],
dtype=torch.float64)
this is after cat
tensor([ 0.3244, -0.6401, -0.7959, 0.9019, 0.1468, -1.7093, -6.4419],
dtype=torch.float64)
it is tmp tensor
tensor([ 1.2923, -0.3088, -0.1275, 0.6417, -1.3383, 1.4020, 28.9065],
dtype=torch.float64)
this is after cat
tensor([ 0.3244, -0.6401, -0.7959, 0.9019, 0.1468, -1.7093, -6.4419, 1.2923,
-0.3088, -0.1275, 0.6417, -1.3383, 1.4020, 28.9065],
dtype=torch.float64)
无论是dim=0还是dim=-1,结果都是一样的
这是例子(看看 dim=-1)
def create_batches_to_device(train_df, test_df, device,batch_size=2):
train_tensor = torch.tensor([])
for i in range (batch_size):
rand_2_strs = train_df.sample(2)
tmp_tensor = torch.tensor([rand_2_strs.get('Sigma').iloc[0],rand_2_strs.get('Sigma').iloc[1],
rand_2_strs.get('mu').iloc[0],rand_2_strs.get('mu').iloc[1],
rand_2_strs.get('th').iloc[0],rand_2_strs.get('th').iloc[1],
np.log(weighted_mse(np.array(rand_2_strs.get('Decay').iloc[0]),np.array(rand_2_strs.get('Decay').iloc[1]),t)[0])])
print("it is tmp tensor")
print(tmp_tensor)
train_tensor = torch.cat((train_tensor,tmp_tensor),dim=-1)
print("this is after cat")
print(train_tensor)
create_batches_to_device(train_data, test_data, device)
结果是一样的
it is tmp tensor
tensor([ 1.0183, 0.2162, 0.4987, -0.0165, 0.2094, 0.9425, -14.4564],
dtype=torch.float64)
this is after cat
tensor([ 1.0183, 0.2162, 0.4987, -0.0165, 0.2094, 0.9425, -14.4564],
dtype=torch.float64)
it is tmp tensor
tensor([ 0.2389, -1.0108, -0.2350, 0.7105, -0.9200, 0.3282, 7.5456],
dtype=torch.float64)
this is after cat
tensor([ 1.0183, 0.2162, 0.4987, -0.0165, 0.2094, 0.9425, -14.4564,
0.2389, -1.0108, -0.2350, 0.7105, -0.9200, 0.3282, 7.5456],
dtype=torch.float64)
问题是 tmp_tensor 的形状 ([7]) 所以我只能在一维上连接。解决方案是我应该添加一个新字符串 tmp_tensor = torch.unsqueeze(tmp_tensor, 0)
现在 tmp_tensor([1,7]) 我可以毫无问题地使用 torch.cat
def create_batches_to_device(train_df, test_df, device,batch_size=3):
train_tensor = torch.tensor([])
for i in range (batch_size):
rand_2_strs = train_df.sample(2)
tmp_tensor = torch.tensor([rand_2_strs.get('Sigma').iloc[0],rand_2_strs.get('Sigma').iloc[1],
rand_2_strs.get('mu').iloc[0],rand_2_strs.get('mu').iloc[1],
rand_2_strs.get('th').iloc[0],rand_2_strs.get('th').iloc[1],
np.log(weighted_mse(np.array(rand_2_strs.get('Decay').iloc[0]),np.array(rand_2_strs.get('Decay').iloc[1]),t)[0])])
print("it is tmp tensor")
tmp_tensor = torch.unsqueeze(tmp_tensor, 0)
print(tmp_tensor.shape)
train_tensor = torch.cat((train_tensor,tmp_tensor),dim=0)
print("this is after cat")
print(train_tensor)
create_batches_to_device(train_data, test_data, device)
结果是
it is tmp tensor
torch.Size([1, 7])
this is after cat
tensor([[ 0.9207, -0.9658, 0.0492, 1.6959, 0.4620, -0.2433, -6.4764]],
dtype=torch.float64)
it is tmp tensor
torch.Size([1, 7])
this is after cat
tensor([[ 0.9207, -0.9658, 0.0492, 1.6959, 0.4620, -0.2433, -6.4764],
[-0.5921, -0.1198, 0.6192, -0.0977, -0.1704, 1.2384, 9.4497]],
dtype=torch.float64)
it is tmp tensor
torch.Size([1, 7])
this is after cat
tensor([[ 0.9207, -0.9658, 0.0492, 1.6959, 0.4620, -0.2433, -6.4764],
[-0.5921, -0.1198, 0.6192, -0.0977, -0.1704, 1.2384, 9.4497],
[ 0.3839, -0.3153, 0.6467, -0.9995, -0.7415, -0.5487, -6.5500]],
dtype=torch.float64)
我在 pytorch 中遇到 cat
的问题。我想在 dim=0 上连接张量,例如,我想要这样的东西
>>> x = torch.randn(2, 3)
>>> x
tensor([[ 0.6580, -1.0969, -0.4614],
[-0.1034, -0.5790, 0.1497]])
>>> torch.cat((x, x, x), 0)
tensor([[ 0.6580, -1.0969, -0.4614],
[-0.1034, -0.5790, 0.1497],
[ 0.6580, -1.0969, -0.4614],
[-0.1034, -0.5790, 0.1497],
[ 0.6580, -1.0969, -0.4614],
[-0.1034, -0.5790, 0.1497]])
但是,当我尝试在我的程序中这样做时,我有
def create_batches_to_device(train_df, test_df, device,batch_size=2):
train_tensor = torch.tensor([])
for i in range (batch_size):
rand_2_strs = train_df.sample(2)
tmp_tensor = torch.tensor([rand_2_strs.get('Sigma').iloc[0],rand_2_strs.get('Sigma').iloc[1],
rand_2_strs.get('mu').iloc[0],rand_2_strs.get('mu').iloc[1],
rand_2_strs.get('th').iloc[0],rand_2_strs.get('th').iloc[1],
np.log(weighted_mse(np.array(rand_2_strs.get('Decay').iloc[0]),np.array(rand_2_strs.get('Decay').iloc[1]),t)[0])])
print("it is tmp tensor")
print(tmp_tensor)
train_tensor = torch.cat((train_tensor,tmp_tensor),dim=0)
print("this is after cat")
print(train_tensor)
create_batches_to_device(train_data, test_data, device)
我有结果
it is tmp tensor
tensor([ 0.3244, -0.6401, -0.7959, 0.9019, 0.1468, -1.7093, -6.4419],
dtype=torch.float64)
this is after cat
tensor([ 0.3244, -0.6401, -0.7959, 0.9019, 0.1468, -1.7093, -6.4419],
dtype=torch.float64)
it is tmp tensor
tensor([ 1.2923, -0.3088, -0.1275, 0.6417, -1.3383, 1.4020, 28.9065],
dtype=torch.float64)
this is after cat
tensor([ 0.3244, -0.6401, -0.7959, 0.9019, 0.1468, -1.7093, -6.4419, 1.2923,
-0.3088, -0.1275, 0.6417, -1.3383, 1.4020, 28.9065],
dtype=torch.float64)
无论是dim=0还是dim=-1,结果都是一样的 这是例子(看看 dim=-1)
def create_batches_to_device(train_df, test_df, device,batch_size=2):
train_tensor = torch.tensor([])
for i in range (batch_size):
rand_2_strs = train_df.sample(2)
tmp_tensor = torch.tensor([rand_2_strs.get('Sigma').iloc[0],rand_2_strs.get('Sigma').iloc[1],
rand_2_strs.get('mu').iloc[0],rand_2_strs.get('mu').iloc[1],
rand_2_strs.get('th').iloc[0],rand_2_strs.get('th').iloc[1],
np.log(weighted_mse(np.array(rand_2_strs.get('Decay').iloc[0]),np.array(rand_2_strs.get('Decay').iloc[1]),t)[0])])
print("it is tmp tensor")
print(tmp_tensor)
train_tensor = torch.cat((train_tensor,tmp_tensor),dim=-1)
print("this is after cat")
print(train_tensor)
create_batches_to_device(train_data, test_data, device)
结果是一样的
it is tmp tensor
tensor([ 1.0183, 0.2162, 0.4987, -0.0165, 0.2094, 0.9425, -14.4564],
dtype=torch.float64)
this is after cat
tensor([ 1.0183, 0.2162, 0.4987, -0.0165, 0.2094, 0.9425, -14.4564],
dtype=torch.float64)
it is tmp tensor
tensor([ 0.2389, -1.0108, -0.2350, 0.7105, -0.9200, 0.3282, 7.5456],
dtype=torch.float64)
this is after cat
tensor([ 1.0183, 0.2162, 0.4987, -0.0165, 0.2094, 0.9425, -14.4564,
0.2389, -1.0108, -0.2350, 0.7105, -0.9200, 0.3282, 7.5456],
dtype=torch.float64)
问题是 tmp_tensor 的形状 ([7]) 所以我只能在一维上连接。解决方案是我应该添加一个新字符串 tmp_tensor = torch.unsqueeze(tmp_tensor, 0)
现在 tmp_tensor([1,7]) 我可以毫无问题地使用 torch.cat
def create_batches_to_device(train_df, test_df, device,batch_size=3):
train_tensor = torch.tensor([])
for i in range (batch_size):
rand_2_strs = train_df.sample(2)
tmp_tensor = torch.tensor([rand_2_strs.get('Sigma').iloc[0],rand_2_strs.get('Sigma').iloc[1],
rand_2_strs.get('mu').iloc[0],rand_2_strs.get('mu').iloc[1],
rand_2_strs.get('th').iloc[0],rand_2_strs.get('th').iloc[1],
np.log(weighted_mse(np.array(rand_2_strs.get('Decay').iloc[0]),np.array(rand_2_strs.get('Decay').iloc[1]),t)[0])])
print("it is tmp tensor")
tmp_tensor = torch.unsqueeze(tmp_tensor, 0)
print(tmp_tensor.shape)
train_tensor = torch.cat((train_tensor,tmp_tensor),dim=0)
print("this is after cat")
print(train_tensor)
create_batches_to_device(train_data, test_data, device)
结果是
it is tmp tensor
torch.Size([1, 7])
this is after cat
tensor([[ 0.9207, -0.9658, 0.0492, 1.6959, 0.4620, -0.2433, -6.4764]],
dtype=torch.float64)
it is tmp tensor
torch.Size([1, 7])
this is after cat
tensor([[ 0.9207, -0.9658, 0.0492, 1.6959, 0.4620, -0.2433, -6.4764],
[-0.5921, -0.1198, 0.6192, -0.0977, -0.1704, 1.2384, 9.4497]],
dtype=torch.float64)
it is tmp tensor
torch.Size([1, 7])
this is after cat
tensor([[ 0.9207, -0.9658, 0.0492, 1.6959, 0.4620, -0.2433, -6.4764],
[-0.5921, -0.1198, 0.6192, -0.0977, -0.1704, 1.2384, 9.4497],
[ 0.3839, -0.3153, 0.6467, -0.9995, -0.7415, -0.5487, -6.5500]],
dtype=torch.float64)