gpt4 book ai didi

c++ - 在 C++ API 中将一个张量的一大块复制到另一个张量中

转载 作者:太空宇宙 更新时间:2023-11-04 12:34:45 25 4
gpt4 key购买 nike

我需要将一个张量的一行(在 c++ API 中)复制到另一个张量的某个部分,开始和结束索引可用的形式。在 C++ 中,我们可以使用类似的东西:

int myints[] = {10, 20, 30, 40, 50, 60, 70};
std::vector<int> myvector(18);

std::copy(myints, myints + 3, myvector.begin() + 4);

将三个值从 myints 复制到 myvector,从第四个索引开始。我想知道 libtorch(即 C++)中是否有类似的 API?

最佳答案

C++ API 提供了 Python slice equivalent 函数

at::Tensor at::Tensor::slice(int64_t dim, int64_t start, int64_t end, int64_t step);

因此你可以这样做:

auto myints = torch::Tensor({10, 20, 30, 40, 50, 60, 70});
auto myvector = torch::ones({18});

myvector.slice(0, 3, 7) = myints.slice(0, 0, 3);

在你的例子中使用 dim=0 第一维

关于c++ - 在 C++ API 中将一个张量的一大块复制到另一个张量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56908893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com