LibTorch之张量操作与线性回归

L i b T o r c h 之张量操作与线性回归 LibTorch之张量操作与线性回归 LibTorch之张量操作与线性回归

pytorch到libtorch,一般就是[]{}的变化

  • 一 张量初始化
  • 二 深浅拷贝
  • 三 C++常用数据类型转换Tensor型变量
  • 四 张量维度变换
  • 五 截取张量
  • 六 张量的四则运算
  • 七 获取张量的大小
  • 8.
  • 9.
  • 10.线性回归

一 张量初始化

1.torch::zeros

#include 
using namespace std;
int main()
{
	torch::Tensor b = torch::zeros({ 2,3 });
	cout << b << endl;

	system("pause");
	return 0;
}

LibTorch之张量操作与线性回归_第1张图片

2.torch::ones

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

LibTorch之张量操作与线性回归_第2张图片

3.torch::eye

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

LibTorch之张量操作与线性回归_第3张图片

4.torch::full

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

LibTorch之张量操作与线性回归_第4张图片

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;
}

LibTorch之张量操作与线性回归_第5张图片

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;
}

LibTorch之张量操作与线性回归_第6张图片

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;
}

LibTorch之张量操作与线性回归_第7张图片

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;
}

LibTorch之张量操作与线性回归_第8张图片

二 深浅拷贝

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;
}

LibTorch之张量操作与线性回归_第9张图片

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;
}

LibTorch之张量操作与线性回归_第10张图片

同大小张量


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;
}

LibTorch之张量操作与线性回归_第11张图片

三 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;
}

LibTorch之张量操作与线性回归_第12张图片

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<float> 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;
}

LibTorch之张量操作与线性回归_第13张图片

四 张量维度变换

1.拼接:torch::cat

import torch

a = torch.ones((2,3))
a1 = torch.cat((a,a),dim=0)
a2 = torch.cat((a,a),dim=1)

print("a1:{} a1's shape:{} \na2:{} a2'shape:{}".format(a1,a1.shape,a2,a2.shape))

LibTorch之张量操作与线性回归_第14张图片

#include 
#include 

int main()
{

    auto a = torch::ones({ 2,3 });
    auto a1 = torch::cat({a,a},0);
    auto a2 = torch::cat({ a,a }, 1);

    std::cout << a1<<a1.sizes() << std::endl;
    std::cout << a2<<a2.sizes() << std::endl;
    
}

2.堆叠:torch::stack

import torch

a = torch.ones((2,3))
a1 = torch.stack([a,a],dim=2)

print("a1:{} a1's shape:{} \n".format(a1,a1.shape))

LibTorch之张量操作与线性回归_第15张图片

#include 
using namespace std;
int main()
{

    auto a = torch::ones({ 2,3 });
    auto a1 = torch::stack({ a,a }, 2);

    std::cout << a1 << a1.sizes() << std::endl;

	system("pause");
	return 0;
}

切分:chunk

import torch

# 切分:chunk()

a = torch.ones((2,5))
sub_of_tensor = torch.chunk(a,dim=1,chunks=2)

for idx,t in enumerate(sub_of_tensor):
    print("id:{}  tensor: {} shape: {}".format(idx,t,t.shape))

LibTorch之张量操作与线性回归_第16张图片

#include 
using namespace std;
int main()
{

    auto a = torch::ones({ 2,5 });
    auto a1 = torch::chunk(a,2,1);
    std::cout << a1[0] << a1[0].sizes() << std::endl;
    std::cout << a1[1] << a1[1].sizes() << std::endl;
	system("pause");
	return 0;
}

LibTorch之张量操作与线性回归_第17张图片

3.unsqueeze


    torch::Tensor a = torch::rand({2,3});
    std::cout<<a<<std::endl;

    torch::Tensor b = a.unsqueeze(0);
    std::cout<<b<<std::endl;

    torch::Tensor bb = a.unsqueeze(1);
    std::cout<<bb<<std::endl;

    torch::Tensor bbb = a.unsqueeze(2);
    std::cout<<bbb<<std::endl;


五 截取张量

六 张量的四则运算

七 获取张量的大小

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

LibTorch之张量操作与线性回归_第18张图片

10.线性回归

import torch
from matplotlib import pyplot as plt
torch.manual_seed(10)

# 学习率
lr = 0.01

# 构建50个样本的训练数据集 单个样本示例为: (x:0.4581,y:6.2339)
# 确定输入
x = torch.rand(50,1)
# 确定label( 25 就是最完美的模型参数,是训练模型参数的目标)
y = 2*x + (5 + torch.randn(50,1))

# 构建线性回归模型  y = wx +b
# 通过训练,确定w和b接近25
w = torch.randn((1),requires_grad=True)
b = torch.zeros((1),requires_grad=True)

for iteration in range(100000):
    # 前向传播
    wx = torch.mul(w,x)
    y_pred = torch.add(wx,b)
    # 计算loss
    loss = (0.5*(y-y_pred)**2).mean()
    # 反向传播
    loss.backward()
    # 更新参数
    b.data.sub_(lr*b.grad)
    w.data.sub_(lr*w.grad)
    # 输出loss
    print("loss:{}".format(loss.data.numpy()))
    # 打印训练信息和结果
    if(iteration%20 == 0):
        print("Iteration:{}\n w:{} b:{}".format(iteration,w.data.numpy(),b.data.numpy()))
        # loss小于0.6,训练结束
        if(loss.data.numpy() <0.6):
            break

LibTorch之张量操作与线性回归_第19张图片

#include 
using namespace std;
int main()
{
    // 学习率
    int lr = 0.1;

    // 构建50个样本的训练数据集 单个样本示例为: (x : 0.4581, y : 6.2339)
    // 确定输入
    auto x = torch::rand({ 50,1 });
    // 确定label( 2 和 5 就是最完美的模型参数,是训练模型参数的目标)

    auto y = 2 * x + (5 + torch::randn({ 50,1 }));

    // 构建线性回归模型  y = wx + b
    // 通过训练,确定w和b接近2和5
    auto options = torch::TensorOptions()
        .requires_grad(true);

    torch::Tensor w = torch::randn({ 1 }, options);
    torch::Tensor b = torch::randn({ 1 }, options);

    for (int iteration = 0; iteration < 10000; iteration++) {
        // 前向传播
        auto wx = torch::mul(w,x);
        auto y_pred = torch::add(wx, b);
        // 计算loss
        auto loss = (0.5 * (y - y_pred) * (y - y_pred)) / 5;
       // cout << loss << endl;
        // 反向传播
        loss.backward();
        // 更新参数
        w.data().sub_(lr * w.grad());
        b.data().sub_(lr * b.grad());

        // 打印loss
        //cout << "loss :" << loss.data().numpy_T()<
        // 
        // 打印训练信息和结果
        cout << "Iteration:" << iteration << "w:" << w.data().numpy_T() << "b:" << b.data().numpy_T() << endl;

        if (iteration %20 == 0) {
            cout << "Iteration:"<< iteration << "w:"<< w.data().numpy_T() << "b:"<<b.data().numpy_T() << 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

LibTorch之张量操作与线性回归_第20张图片

你可能感兴趣的:(线性回归,算法,回归)