pytorch到libtorch,一般就是[]到{}的变化
- 一 张量初始化
- 二 深浅拷贝
- 三 C++常用数据类型转换Tensor型变量
- 四 张量维度变换
- 五 截取张量
- 六 张量的四则运算
- 七 获取张量的大小
- 8.
- 9.
- 0.
一 张量初始化
1.torch::zeros
#include
using namespace std;
int main()
{
torch::Tensor b = torch::zeros({ 2,3 });
cout << b << endl;
system("pause");
return 0;
}
2.torch::ones
#include3.torch::eyeusing namespace std; int main() { torch::Tensor b = torch::ones({ 2,3 }); cout << b << endl; system("pause"); return 0; }
#include4.torch::fullusing namespace std; int main() { torch::Tensor b = torch::eye(3); cout << b << endl; system("pause"); return 0; }
#include5.torch::tensorusing namespace std; int main() { torch::Tensor b = torch::full({ 2,3 }, 999); cout << b << endl; system("pause"); return 0; }
#include6.torch::rand(产生0-1之间的随机值)using namespace std; int main() { torch::Tensor b = torch::tensor({ {1,2,3},{4,5,6} }); cout << b << endl; system("pause"); return 0; }
#include7.torch::randn(取正态分布服从N(0,1)的随机值)using namespace std; int main() { torch::Tensor b = torch::rand({ 2,3 }); cout << b << endl; system("pause"); return 0; }
#include8.torch::randint(取[min,max)的随机整型数值)using namespace std; int main() { torch::Tensor b = torch::randn({ 2,3 }); cout << b << endl; system("pause"); return 0; }
#includeusing namespace std; int main() { torch::Tensor b = torch::randint(1, 10, { 2,3 }); cout << b << endl; system("pause"); return 0; }
二 深浅拷贝
1.浅拷贝
torch::Tensor b1 = torch::zeros({3,4});
// 浅拷贝
torch::Tensor b2 =b1;
torch::Tensor b2 = torch::Tensor(b1);
#include
using namespace std;
int main()
{
torch::Tensor b1 = torch::zeros({ 2,3 });
cout << b1 << endl;
torch::Tensor b2 = b1;
//cout << b2 << endl;
//torch::Tensor b2 = torch::Tensor(b1);
cout << b2 << endl;
//
b2[0][0] = 888;
cout << "b1:" << endl;
cout << b1 << endl;
system("pause");
return 0;
}
2.深拷贝
torch::Tensor b1 = torch::zeros({3,4}); torch::Tensor b2 = b.clone();
#include同大小张量using namespace std; int main() { torch::Tensor b1 = torch::zeros({ 2,3 }); cout << "b1:" << endl; cout << b1 << endl; torch::Tensor b2 = b1.clone();; b2[0][0] = 888; cout << "b2:" << endl; cout << b2 << endl; cout << "b1:" << endl; cout << b1 << endl; system("pause"); return 0; }
b2 = torch::zeros_like(b1); b2 = torch::ones_like(b1); b2 = torch::rand_like(b1,torch::kFloat);
#includeusing namespace std; int main() { torch::Tensor b1 = torch::zeros({ 2,3 }); torch::Tensor b2 = torch::zeros_like(b1); cout << b2 << endl; b2 = torch::ones_like(b1); cout << b2 << endl; b2 = torch::rand_like(b1, torch::kFloat); cout << b2 << endl; system("pause"); return 0; }
三 C++常用数据类型转换Tensor型变量
1.数组
#include
using namespace std;
int main()
{
int a[10] = { 1,2,3,4,5,6 };
b = torch::from_blob(a, { 6 }, torch::kInt);
cout << b << endl;
system("pause");
return 0;
}
float data[] = { 1, 2, 3, 4, 5, 6 }; torch::Tensor f = torch::from_blob(data, {2, 3});2.向量
#includeusing namespace std; int main() { std::vector a = { 1,2,3,4,5,6 }; // 方式1 torch::Tensor b= torch::tensor(a); cout << b << endl; // 方式2 b = torch::from_blob(a.data(), { 2 }, torch::kFloat); cout << b << endl; system("pause"); return 0; }