如何使用 protobuf 序列化 tuple(std::string, torch::Tensor) 的 libtorch 向量?

How to serialize a libtorch vector of tuple(std::string, torch::Tensor) using protobuf?

我应该如何定义 ProtoBuf 模式以便我可以序列化这个形状向量?

std::vector<std::tuple(std::string, torch::Tensor)>

我能找到的都是简单琐碎的例子。我不知道我应该如何在 ProtoBuf 模式中定义 torch::Tensortuple

您可以在 ProtoBuf 架构中使用 string 表示 torch::Tensor

示例:

syntax = "proto3";

package tensor;

message Lookup {
    message Tuple {
        string key = 1;
        string tensor = 2;
    }

    repeated Tuple tuples = 1;
}

使用 torch::save()torch::load() API 通过 std::stringstream 转换 to/from std::string,如前所述 here