The correct way to have split input to a model

I am using a DataLoader and then

for input_batch, input_labels in data_loader:

as is torch-onic.

However, my model needs to use “split input”, ie it’s forward function needs to do something like this:

def forward(x):
   resnet_input, fc_input = x

   resnet_out = self.resnet(resnet_input)
   out = self.fc_layer(torch.concat(resnet_out, fc_input))

   return out

But torch.utils.data.Dataset::getitem is expected to return tensor, tensor, so I don’t know how to do this.

Maybe I should be constructing an input tensor which has another channel which has the extra information in it? And then in forward doing something like:

def forward(x):
   resnet_input = x[:,:,:,:3]
   fc_input = x[:,:,:,3][0:4,0,0]

That’s pretty ugly though (and I’m just assuming the backpropogation all still works when you’re slicing the input like that…)

This feels like something which has a standard way of doing things, can you please educate me?

You can return a tuple or list, too:

class MyDataset(Dataset):
    def __init__(self):
        self.data1 = torch.randn(10, 1)
        self.data2 = torch.randn(10, 1)
        self.target = torch.randn(10, 1)
        
    def __len__(self):
        return len(self.data1)
    
    def __getitem__(self, index):
        x1 = self.data1[index]
        x2 = self.data2[index]
        y = self.target[index]
        return (x1, x2), y

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

for (input_batch1, input_batch_2), input_labels in loader:
    print(type(input_batch1))
    print(type(input_labels))
# <class 'torch.Tensor'>
# <class 'torch.Tensor'>
# <class 'torch.Tensor'>
# <class 'torch.Tensor'>
    
for input_batch, input_labels in loader:
    print(type(input_batch))
    print(type(input_labels))
# <class 'list'>
# <class 'torch.Tensor'>
# <class 'list'>
# <class 'torch.Tensor'>

Thanks Ptr. I thought I was having a problem with sending a tuple to module::forward (which I’d need to do), but I’ll try again.