Vector of torch tensors to torch tensor

I have a vector of torch tensors and wanted to know how to convert this to a torch tensor.
The vector represents a batch of torch tensors.

std::vector<std::tuple<torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor>> batch =
                buffer.sample_queue(batch_size);

        std::vector<torch::Tensor> states;
        std::vector<torch::Tensor> new_states;
        std::vector<torch::Tensor> actions;
        std::vector<torch::Tensor> rewards;
        std::vector<torch::Tensor> dones;

        for (auto i : batch){
            states.push_back(std::get<0>(i));
            new_states.push_back(std::get<1>(i));
            actions.push_back(std::get<2>(i));
            rewards.push_back(std::get<3>(i));
            dones.push_back(std::get<4>(i));
        }

Thanks!

Just like you would in Python, use torch::cat to concatenate along a single axis and torch::stack to create a new axis. Note that dimensions (except possible the cat dimension) must match for this to succeed.

Best regards

Thomas

1 Like