栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 软件开发 > 后端开发 > C/C++/C#

LibTorch之张量操作与线性回归

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

LibTorch之张量操作与线性回归

L i b T o r c h 之张量操作与线性回归 LibTorch之张量操作与线性回归 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;
}

同大小张量
b2 = 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
    torch::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://blog.csdn.net/Flag_ing/article/details/109628297 https://www.nmxjp.com/w/59/54216/0.html https://pytorch.org/cppdocs/notes/tensor_creation.html

转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/1037917.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号