How to put a list of tensor into a new tensor in pytorch?
How can I merge a list of tensor into a single tensor?
I have a list of 64 images (128x128, RGB), and I want to create a batch of images with this size: torch.Size([64, 3, 128, 128])
I tried with torch.stack() but I can't get all elements of the list without iterating it.
How can I do it?
lfw_dataset = ImageFolder(os.path.join(root_dir), transform=test_transform)
lfw_dataset_train, lfw_dataset_val = torch.utils.data.random_split(lfw_dataset_train1, [train_values, validation_values])
train_loader = DataLoader(lfw_dataset_train, batch_size, num_workers=4, shuffle=True)
val_loader   = DataLoader(lfw_dataset_val,   batch_size, num_workers=4, shuffle=False)
# Define dictionary of loaders
loaders = {&quot;train&quot;: train_loader,
           &quot;val&quot;: val_loader}
 
positive_list= []
negative_list= []
positive_img = []
negative_img = []
for i, (input, labels) in enumerate(loaders[&quot;train&quot;]):
  for num, x in enumerate(labels):
    target = x.item()
    k = [i for i, (imgs, label_pos) in enumerate(lfw_dataset.imgs) if label_pos==target]
    group_pos = (target, k)
    positive_list.append(group_pos)
    for i, (imgs, label_neg) in enumerate(lfw_dataset.imgs):
       if label_neg!=target:
         j = [i]
       break
    group_neg = (target, j)
    negative_list.append(group_neg)
    anchor_img=input[num]
    positive = random.choice(positive_list[num][1])
    negative = random.choice(negative_list[num][1])
    positive_img.append(lfw_dataset[positive][0])
    negative_img.append(lfw_dataset[negative][0])


You have to use torch.cat(input1, input2, dim)
