LibTorch之张量操作与线性回归
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
#include using namespace std;int main(){ torch::Tensor b = torch::ones({ 2,3 }); cout << b << endl; system("pause"); return 0;}
3.torch::eye
#include using namespace std;int main(){ torch::Tensor b = torch::eye(3); cout << b << endl; system("pause"); return 0;}
4.torch::full
#include using namespace std;int main(){ torch::Tensor b = torch::full({ 2,3 }, 999); cout << b << endl; system("pause"); return 0;}
5.torch::tensor
#include using namespace std;int main(){ torch::Tensor b = torch::tensor({ {1,2,3},{4,5,6} }); cout << b << endl; system("pause"); return 0;}
6.torch::rand(产生0-1之间的随机值)
#include using namespace std;int main(){ torch::Tensor b = torch::rand({ 2,3 }); cout << b << endl; system("pause"); return 0;}
7.torch::randn(取正态分布服从N(0,1)的随机值)
#include using namespace std;int main(){ torch::Tensor b = torch::randn({ 2,3 }); cout << b << endl; system("pause"); return 0;}
8.torch::randint(取[min,max)的随机整型数值)
#include using 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;}
同大小张量
= torch::zeros_like(b1);b2 = torch::ones_like(b1);b2 = torch::rand_like(b1,torch::kFloat);
#include using 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.向量
#include using 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;}
四 张量维度变换
1.拼接:torch::cat
2.堆叠:torch::stack
3.unsqueeze
::Tensor a = torch::rand({2,3}); std::cout<五 截取张量
六 张量的四则运算
七 获取张量的大小
#include using namespace std;int main(){ torch::Tensor rgb = torch::randn({ 1, 3, 6, 6 }); cout << rgb << endl; cout << rgb.sizes() << endl; system("pause"); return 0;}
https://nmxjp.com/w/59/54216/0.html
https://pytorch.org/cppdocs/notes/tensor_creation.html
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~