博客
关于我
LibTorch之张量操作与线性回归
阅读量:802 次
发布时间:2023-01-31

本文共 4909 字,大约阅读时间需要 16 分钟。

LibTorch 张量操作与线性回归

在LibTorch中,张量操作与PyTorch相比,主要体现在基础操作的写法上。值得注意的首要事务是理解LibTorch与PyTorch之间的语法差异,尤其是变量命名方式的变化。

一 张量初始化

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

#include 
using namespace std;int main() { torch::Tensor b = torch::rand({2, 3}); cout << b << endl; system("pause"); return 0;}

위키百科

7. torch::randn

#include 
using namespace std;int main() { torch::Tensor b = torch::randn({2, 3}); cout << b << endl; system("pause"); return 0;}

위키百科

8. torch::randint

#include 
using namespace std;int main() { torch::Tensor b = torch::randint(1, 10, {2, 3}); cout << b << endl; system("pause"); return 0;}

二 深浅拷贝

1. 浅拷贝

#include 
using namespace std;int main() { torch::Tensor b1 = torch::zeros({2, 3}); torch::Tensor b2 = b1; // 浅拷贝 auto b3 = torch::Tensor(b1);}

2. 深拷贝

#include 
using 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. 张量大小一致的深拷贝

#include 
using 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;}

三 C++ 常用数据类型转换为Tensor变量

1. 使用from_blob方法从数组转换Tensor

#include 
using 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

#include 
using namespace std;int main() { float data[] = {1, 2, 3, 4, 5, 6}; torch::Tensor f = torch::from_blob(data, {2, 3});}

3. 从std::vector

转换Tensor

#include 
using 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

#include 
using 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

#include 
using namespace std;int main() { auto a = torch::ones({2, 3}); auto a1 = torch::stack({a, a}, 2); cout << a1.shape() << endl;}

3. 切分操作:torch::chunk

#include 
using 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

#include 
using 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. 截取张量的特定部分

#include 
using namespace std;int main() { auto a = torch::rand({2, 3, 5, 4}); // 截取最后两个元素 auto b = a.slice(2, -2, -1); cout << b << endl;}

六 张量的四则运算

1. 加法运算

#include 
using namespace std;int main() { auto a = torch::rand({2, 3}); auto b = torch::rand({3, 2}); auto c = a + b; cout << c << endl;}

2. 乘法运算

#include 
using 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. 绝对值

#include 
using namespace std;int main() { auto a = torch::rand({2, 3}); auto b = torch::abs(a); cout << b << endl;}

七 获取张量的大小

#include 
using namespace std;int main() { auto a = torch::rand({2, 3, 6, 6}); cout << a << endl; cout << "Tensor size: " << a.sizes() << endl;}

八 其他操作(需cmd+T>Code查看完整代码)

以上内容涵盖了LibTorch中常用操作的实现,对于线性回归部分,可参考标准教程或进一步优化模型结构。但请注意,在实际应用中,需根据需求选择适合的框架和模型结构。

转载地址:http://nwwfk.baihongyu.com/

你可能感兴趣的文章
Network Sniffer and Connection Analyzer
查看>>
Network 灰鸽宝典【目录】
查看>>
Networkx写入Shape文件
查看>>
NetworkX系列教程(11)-graph和其他数据格式转换
查看>>
Networkx读取军械调查-ITN综合传输网络?/读取GML文件
查看>>
network小学习
查看>>
Netwox网络工具使用详解
查看>>
Net与Flex入门
查看>>
net包之IPConn
查看>>
net发布的dll方法和类显示注释信息(字段说明信息)[图解]
查看>>
Net操作配置文件(Web.config|App.config)通用类
查看>>
NeurIPS(神经信息处理系统大会)-ChatGPT4o作答
查看>>
neuroph轻量级神经网络框架
查看>>
Neutron系列 : Neutron OVS OpenFlow 流表 和 L2 Population(7)
查看>>
NEW DATE()之参数传递
查看>>
New Relic——手机应用app开发达人的福利立即就到啦!
查看>>
new 一个button 然后dispose,最后这个button是null吗???
查看>>
next项目部署到服务器pm2进程守护
查看>>
nexus上传jar
查看>>
Nexus指南中的更新强调集成和透明度的重要性
查看>>