# Libtorch 张量基础详解

include 目录中包含了各种头文件，每个头文件对一类操作。
python 目录中有对应的 python 脚本。

## 1. 创建张量

torch::Tensor a = torch::zeros({2, 3});
torch::Tensor b = torch::ones({2, 3});
torch::Tensor c = torch::eye(3);
torch::Tensor d = torch::full_like(a, 10);
torch::Tensor e = torch::randn({2, 3});
torch::Tensor f = torch::arange(10);
torch::Tensor g = torch::tensor({{1, 2}, {3, 4}});

## 2. 张量索引和切片

torch::Tensor a = torch::randn({2, 3, 4});
torch::Tensor b = a[1];
torch::Tensor c = a.index({1, 2, 3});        // 等同 a[1][2][3]
torch::Tensor d = a.index({Slice(None), 2}); // 等同 a.index({"...", 2})
torch::Tensor e = a.index({Slice(None), Slice(None), Slice(None, None, 2)});
torch::Tensor f = a.index_select(-1, torch::tensor({1, 1, 0}));

## 3. 张量属性

torch::Tensor a = torch::randn({2, 3});
std::cout << a.size(1) << std::endl;
std::cout << a.sizes() << std::endl;
std::cout << a[0].sizes() << std::endl;
std::cout << a[0][0].item<float>() << std::endl;
std::cout << a.data() << std::endl;
std::cout << a.dtype() << std::endl;
std::cout << a.device() << std::endl;

## 4. 张量变换

torch::Tensor a = torch::randn({2, 3});
torch::Tensor b = a.transpose(0, 1);
torch::Tensor c = a.reshape({-1, 1});
torch::Tensor d = a.view({3, 2});
torch::Tensor e = a.toType(torch::kFloat32);

## 5. 张量计算

torch::Tensor a = torch::ones({3, 3});
torch::Tensor b = torch::randn({3, 3});
torch::Tensor c = a.matmul(b);
torch::Tensor d = a.mul(b);
torch::Tensor e = torch::cat({a, b}, 0);
torch::Tensor f = torch::stack({a, b});

LibTorch 大部分 API 和 PyTorch 命名和使用逻辑都基本一致，稍加熟悉就可以了。

Spring Boot与Kubernetes云原生微服务实践 -〔杨波〕

Service Mesh实战 -〔马若飞〕