本文主要是介绍libtorch学习第二,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
张量创建的几种方式
估计大多数人都是第3种用的多
#include<torch/torch.h>
#include<iostream>using std::cout; using std::endl;int main()
{auto b = torch::zeros({ 3,4 });//cout << b << endl;b = torch::ones({ 3,4 });//cout << b << endl;b = torch::eye(4); // 单位阵//cout << b << endl;b = torch::full({ 3,4 }, 10);//cout << b << endl;b = torch::tensor({ 33,22,11 });//cout << b << endl;auto r = torch::rand({ 3,4 }); //0-1//cout << r << endl;r = torch::randn({ 3,4 });// 正态分布//cout << r << endl;r = torch::randint(0, 4, { 3,3 }); // [min,max) ,int//cout << r << endl;///float aa[10] = { 3,4,6 };std::vector<float> aaaa{ 5,6,7 };auto aaTensor = torch::from_blob(aa, { 5 }, torch::kFloat); // 类型要与原类型一致auto aaaaTensor = torch::from_blob(aaaa.data(), { 3 }, torch::kFloat);/*cout << aaTensor << endl;cout << aaaaTensor << endl;*//b = torch::zeros({ 3,4 });auto d = torch::Tensor(b);//cout << d << endl;d = torch::zeros_like(b);//cout << d << endl;d = torch::ones_like(b);//cout << d << endl;d = torch::rand_like(b, torch::kFloat);//cout << d << endl;d = b.clone(); // 深拷贝//cout << d << endl;return 0;
}
这篇关于libtorch学习第二的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!