pytorch到libtorch,一般就是
[]
到{}
的变化
#include
using namespace std;
int main()
{
torch::Tensor b = torch::zeros({ 2,3 });
cout << b << endl;
system("pause");
return 0;
}
#include
using namespace std;
int main()
{
torch::Tensor b = torch::ones({ 2,3 });
cout << b << endl;
system("pause");
return 0;
}
#include
using namespace std;
int main()
{
torch::Tensor b = torch::eye(3);
cout << b << endl;
system("pause");
return 0;
}
#include
using namespace std;
int main()
{
torch::Tensor b = torch::full({ 2,3 }, 999);
cout << b << endl;
system("pause");
return 0;
}
#include
using namespace std;
int main()
{
torch::Tensor b = torch::tensor({ {1,2,3},{4,5,6} });
cout << b << endl;
system("pause");
return 0;
}
#include
using namespace std;
int main()
{
torch::Tensor b = torch::rand({ 2,3 });
cout << b << endl;
system("pause");
return 0;
}
#include
using namespace std;
int main()
{
torch::Tensor b = torch::randn({ 2,3 });
cout << b << endl;
system("pause");
return 0;
}
#include
using namespace std;
int main()
{
torch::Tensor b = torch::randint(1, 10, { 2,3 });
cout << b << endl;
system("pause");
return 0;
}
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;
}
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;
}
#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});
#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;
}
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))
#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;
}
import torch
a = torch.ones((2,3))
a1 = torch.stack([a,a],dim=2)
print("a1:{} a1's shape:{} \n".format(a1,a1.shape))
#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;
}
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))
#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;
}
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;
}
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( 2 和 5 就是最完美的模型参数,是训练模型参数的目标)
y = 2*x + (5 + torch.randn(50,1))
# 构建线性回归模型 y = wx +b
# 通过训练,确定w和b接近2和5
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
#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;
}