Hey Matej,
Thank you for your help. Your first suggestion (stacking) worked for forwarding but it failed for loss. I assumed I should do the same stacking for targets as you showed it for inputs.
        auto data_vec = std::vector<torch::Tensor>();
        auto target_vec = std::vector<torch::Tensor>();

        for (auto& data : samples)
        {
            data_vec.push_back(data.first);         // input - torch::tensor(c10::ArrayRef(data_x)); <-[112]
            target_vec.push_back(data.second); // target - torch::tensor(c10::ArrayRef(target_x)); <- [2]
        }

        auto data = torch::stack(data_vec);
        auto targets = torch::stack(target_vec);

        
        auto output = forward(data);   // <- forward accepts stacked inputs
        try {
            auto loss = torch::nll_loss(output, targets); <- exception thrown: 0D or 1D target tensor expected, multi-target not supported

            optimizer.zero_grad();
            loss.backward();
            optimizer.step();
        }
        catch (const std::exception& e) {
            std::cout << "Exception occured\n" << e.what() << std::endl;
            exit(-1);
        }

torch::nll_loss(output, targets) throws “0D or 1D target tensor expected, multi-target not supported”. Should I stack inputs belonging to the same class and pass a single target for such batch?
I will also try you second suggestion (custom dataset) and share the results.