How-to forward a batch of type vector<Example<>>?

I’ve my own BatchDataset.
The DataLoader returns type vector<Example<>>.

The tutorials and topics I’ve found do something like…

for (auto &batch : *data_loader)
{
    Tensor t = batch.data;
}

But here batch is a vector and batch.data makes no sense. At least it doesn’t return a Tensor.

I could do something like

for (auto &batch : *data_loader)
{
    for (auto example : batch)
    {
        Tensor t = example.data;
    }
}

But I’d like to forward the whole batch and not only a single tensor from the batch.

How does this work?

Hello @arlecchino,

that is because in most tutorials there is the torch::data::transforms::Stack<>() transform applied to the dataset. It converts your std::vector<torch::data::Example> into one Tensor. Check out the MNIST example, there you will find this line

auto test_dataset = torch::data::datasets::MNIST(
                        kDataRoot, torch::data::datasets::MNIST::Mode::kTest)
                        .map(torch::data::transforms::Normalize<>(0.1307, 0.3081))
                        .map(torch::data::transforms::Stack<>()); // this is where the magic happens. It internally 
                                                                  // calls torch::stack on a
                                                                  // std::vector<torch::Tensor>

You could also loop over your std::vector<torch::data::Example> and access your data of every torch::data::Example as you mentioned. Does this solve your problem?

2 Likes

Thanks @mhubii - I didn’t get that this map to the Stack<>() transforms the type.

1 Like