1.常见固定值的初始化方式
//zeros产生值全为0的张量
auto b = torch::zeros({ 3,4 });
//ones产生值全为1的张量
b = torch::ones({ 3,4 });
//eye产生单位矩阵张量。
b = torch::eye(4);
//full产生指定值和尺寸的张量。
b = torch::full({ 3,4 }, 10);
b = torch::tensor({ 33,22,11 });
2.随机初始化
auto r = torch::rand({3,4});
r = torch::randn({3, 4});
r = torch::randint(0, 4,{3,3});
rand产生0-1之间的随机值,randn取正态分布N(0,1)的随机值,randint取[min,max)的随机整型数值。
3.从其他类似转换为张量
int a[10] = {3,4,6};
std::vector<float> b= {3,4,6};
auto c= torch::tensor(b);
auto d= torch::from_blob(a,{3},torch::kFloat);
auto e= torch::from_blob(b.data(),{3},torch::kFloat);