libtorch操作

libtorch操作

带stride数组与tensor之间的转化
int16_t src0[64 * 64] = { 0 };
int16_t *  Psrc0 = src0;
int stride0 = 64;

for (int i = 0; i < 64 * 64; ++i)
    src0[i] = i % 64;

torch::Tensor Tsrc0 = torch::from_blob(Psrc0, { 1, 64 }, at::kShort);
Psrc0 += stride0;

for (int j = 1; j < 64; ++j)
{
    Tsrc0 = torch::cat({Tsrc0, torch::from_blob(Psrc0, { 1, 64 }, at::kShort) }, 0);
    Psrc0 += stride0;
}


int16_t src1[64 * 64] = { 0 };
int16_t *  Psrc1 = src1;
int stride1 = 64;


for (int i = 0; i < 64 * 64; ++i)
    src1[i] = (i % 64) << 1;

torch::Tensor Tsrc1 = torch::from_blob(Psrc1, { 1, 64 }, at::kShort);
Psrc1 += stride1;

for (int j = 1; j < 64; ++j)
{
    Tsrc1 = torch::cat({ Tsrc1, torch::from_blob(Psrc1, { 1, 64 }, at::kShort) }, 0);
    Psrc1 += stride1;
}

// 转换为float类型
torch::Tensor curLuma = torch::cat({ Tsrc0.unsqueeze(0), Tsrc1.unsqueeze(0) }, 0).toType(c10::ScalarType(6));

std::cout << curLuma << std::endl;
std::cout << curLuma.sizes() << std::endl; 
分片操作

https://pytorch.org/cppdocs/notes/tensor_indexing.html

int tempArr[9] = { 0 };
for (int i = 0; i < 9; ++i)
{
    tempArr[i] = i + 1;
}
torch::Tensor TArr = torch::from_blob(tempArr, { 3, 3 }, at::kInt);
std::cout << TArr << std::endl;
std::cout << TArr.index({ torch::indexing::Slice(0, 2), torch::indexing::Slice(0, 2) }) << std::endl;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值