Unpack tuple of tensors

I am working on pytorch code of time series forecasting using TFT method. Here

  1. First I have taken a csv file to read my data that has 4 columns with numeric values. (stock prices)
  2. I converted the data into TimeSeriesDataSet() format.
  3. Then converted the datasets to dataloaders for training val, and testing using to_dataloader() method.
  4. I am trying to extract the feature (x) and label (y) using below sample code

    for x, y in iter(val_dataloader)

Here y represent the target column for which I want my model to make predictions. I have options to make declare single and multiple target columns like this
a: target_colum = “colum1”
b: target_colum = [‘colum1’, colum2’] etc.

in case of “a” where I have only one column to predict I can extract the actual labels
like this
#actuals = torch.cat([y[0] for x, y in iter(test_dataloader)])

However in case of “b” where I have multiple columns I am not able to extract the actual labels

when I explored I found that “y” is a tuple and in my case there are two target columns then in the “y” data is stored like this

horizon = n
print(y) = ( [ tensor( [ [1], [2],…[n no. of predictions for column 1] ] ) , tensor( [ [1], [2],…[n no. of predictions for column 2] ] ) ] , none)

and if there are multiple test data like if n = 30 and I have 120 rows in the test data then this list continues with 4 such data.

my problem is I want to extract this data from “y”. Kindly help I am trapped in these square and small brackets. My intention is to store all the values in a numpy array with size (n, 2) .

Something like this should work:

class MyDataset(Dataset):
    def __init__(self):
        self.data = torch.randn(100, 1)
        self.target1 = torch.arange(100)
        self.target2 = torch.arange(100, 200)
        
    def __len__(self):
        return len(self.data)
    
    def __getitem__(self, index):
        x = self.data[index]
        y1 = self.target1[index]
        y2 = self.target2[index]
        return x, (y1, y2)
    

dataset = MyDataset()
loader = DataLoader(dataset, batch_size=10)

targets = [torch.stack((y1, y2), dim=1) for _, (y1, y2) in loader]
targets = torch.cat(targets, dim=0)
print(targets.shape)
# torch.Size([100, 2])