[英]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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.