How to load python tensor in C++?

So I copied the code from but I got exception: c10::Error at memory location at torch::pickle_load. Anyone know how to fix it ?
My python code:

    x = torch.arange(10)
    f = io.BytesIO(), f, _use_new_zipfile_serialization=True)
    # send f wherever
    with open('', "wb") as outfile:
        # Copy the BytesIO stream to the output file

My C++ code:

#include <iostream>
#include <torch/torch.h>

std::vector<char> get_the_bytes(std::string filename) {
    std::ifstream input(filename, std::ios::binary);
    std::vector<char> bytes(

    return bytes;

int main()
    std::vector<char> f = get_the_bytes("path\\to\\");
    torch::IValue x = torch::pickle_load(f);
    torch::Tensor tensor = x.toTensor();
    std::cout << tensor;

1 Like

Check your libtorch version and pytorch version. It might because you have different versions.