本文共 4909 字,大约阅读时间需要 16 分钟。
LibTorch 张量操作与线性回归
在LibTorch中,张量操作与PyTorch相比,主要体现在基础操作的写法上。值得注意的首要事务是理解LibTorch与PyTorch之间的语法差异,尤其是变量命名方式的变化。
1. torch::zeros
#includeusing namespace std;int main() { torch::Tensor b = torch::zeros({2, 3}); cout << b << endl; system("pause"); return 0;}
위키百科
2. torch::ones
#includeusing namespace std;int main() { torch::Tensor b = torch::ones({2, 3}); cout << b << endl; system("pause"); return 0;}
위키百科
3. torch::eye
#includeusing namespace std;int main() { torch::Tensor b = torch::eye(3); cout << b << endl; system("pause"); return 0;}
위키百科
4. torch::full
#includeusing namespace std;int main() { torch::Tensor b = torch::full({2, 3}, 999); cout << b << endl; system("pause"); return 0;}
위키百科
5. torch::tensor
#includeusing 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
#includeusing namespace std;int main() { torch::Tensor b = torch::rand({2, 3}); cout << b << endl; system("pause"); return 0;}
위키百科
7. torch::randn
#includeusing namespace std;int main() { torch::Tensor b = torch::randn({2, 3}); cout << b << endl; system("pause"); return 0;}
위키百科
8. torch::randint
#includeusing namespace std;int main() { torch::Tensor b = torch::randint(1, 10, {2, 3}); cout << b << endl; system("pause"); return 0;}
1. 浅拷贝
#includeusing namespace std;int main() { torch::Tensor b1 = torch::zeros({2, 3}); torch::Tensor b2 = b1; // 浅拷贝 auto b3 = torch::Tensor(b1);}
2. 深拷贝
#includeusing namespace std;int main() { torch::Tensor b1 = torch::zeros({2, 3}); torch::Tensor b2 = b1.clone(); // 测试深拷贝后的独立性 b2[0][0] = 888; cout << "b2: " << b2 << endl; cout << "b1: " << b1 << endl;}
3. 张量大小一致的深拷贝
#includeusing namespace std;int main() { torch::Tensor b1 = torch::zeros({2, 3}); torch::Tensor b2 = torch::zeros_like(b1); // 测试形状一致性 cout << "b2(ones-like): " << b2 << endl; b2 = torch::ones_like(b1); cout << "b2(ones-like):" << b2 << endl; b2 = torch::rand_like(b1, torch::kFloat); cout << "b2(random-like):" << b2 << endl;}
1. 使用from_blob方法从数组转换Tensor
#includeusing namespace std;int main() { int a[] = {1, 2, 3, 4, 5, 6}; torch::Tensor b = torch::from_blob(a, {6}, torch::kInt); cout << b << endl;}
2. 使用from_blob方法从浮点数数组转换Tensor
#includeusing namespace std;int main() { float data[] = {1, 2, 3, 4, 5, 6}; torch::Tensor f = torch::from_blob(data, {2, 3});}
3. 从std::vector
#includeusing namespace std;int main() { vector a = {1, 2, 3, 4, 5, 6}; // 方法一:直接使用 tensorSYSTEMrepr torch::Tensor b = torch::tensor(a); // 方法二:使用from_blob torch::Tensor b = torch::from_blob(a.data(), {2}, torch::kFloat);}
1. 拼接操作:torch::cat
#includeusing namespace std;int main() { auto a = torch::ones({2, 3}); auto a1 = torch::cat({a, a}, 0); auto a2 = torch::cat({a, a}, 1); cout << "a1: " << a1 << endl; cout << "a2: " << a2 << endl;}
2. 堆叠操作:torch::stack
#includeusing namespace std;int main() { auto a = torch::ones({2, 3}); auto a1 = torch::stack({a, a}, 2); cout << a1.shape() << endl;}
3. 切分操作:torch::chunk
#includeusing namespace std;int main() { auto a = torch::ones({2, 5}); auto chunks = torch::chunk(a, 2, 1); for (int idx, t : chunks) { cout << "idx: " << idx << " tensor: " << t << endl; }}
4. unsqueeze操作(无维度展开):torch::unsqueeze
#includeusing namespace std;int main() { auto a = torch::rand({2, 3}); // 查看原始形状 cout << a << endl; // 增加一个维度(默认在第一个维度) auto b = a.unsqueeze(1); cout << b << endl; // 测试形状变化 cout << b.shape() << endl;}
1. 截取张量的特定部分
#includeusing namespace std;int main() { auto a = torch::rand({2, 3, 5, 4}); // 截取最后两个元素 auto b = a.slice(2, -2, -1); cout << b << endl;}
1. 加法运算
#includeusing namespace std;int main() { auto a = torch::rand({2, 3}); auto b = torch::rand({3, 2}); auto c = a + b; cout << c << endl;}
2. 乘法运算
#includeusing namespace std;int main() { auto a = torch::rand({2, 3}); auto b = torch::rand({3, 2}); auto c = torch::matmul(a, b); cout << c << endl;}
3. 绝对值
#includeusing namespace std;int main() { auto a = torch::rand({2, 3}); auto b = torch::abs(a); cout << b << endl;}
#includeusing namespace std;int main() { auto a = torch::rand({2, 3, 6, 6}); cout << a << endl; cout << "Tensor size: " << a.sizes() << endl;}
以上内容涵盖了LibTorch中常用操作的实现,对于线性回归部分,可参考标准教程或进一步优化模型结构。但请注意,在实际应用中,需根据需求选择适合的框架和模型结构。
转载地址:http://nwwfk.baihongyu.com/