libtorch学习第二

张量创建的几种方式

估计大多数人都是第3种用的多

#include
#include

using std::cout; using std::endl;

int main()
{
	auto b = torch::zeros({ 3,4 });
	//cout << b << endl;

	b = torch::ones({ 3,4 });
	//cout << b << endl;

	b = torch::eye(4); // 单位阵
	//cout << b << endl;

	b = torch::full({ 3,4 }, 10);
	//cout << b << endl;

	b = torch::tensor({ 33,22,11 });
	//cout << b << endl;
	


	auto r = torch::rand({ 3,4 }); //0-1
	//cout << r << endl;

	r = torch::randn({ 3,4 });// 正态分布
	//cout << r << endl;

	r = torch::randint(0, 4, { 3,3 }); // [min,max) ,int
	//cout << r << endl;
	///

	float aa[10] = { 3,4,6 };
	std::vector<float> aaaa{ 5,6,7 };
	auto aaTensor = torch::from_blob(aa, { 5 }, torch::kFloat); // 类型要与原类型一致
	auto aaaaTensor = torch::from_blob(aaaa.data(), { 3 }, torch::kFloat);
	/*cout << aaTensor << endl;
	cout << aaaaTensor << endl;*/
	/

	b = torch::zeros({ 3,4 });
	auto d = torch::Tensor(b);
	//cout << d << endl;
	d = torch::zeros_like(b);
	//cout << d << endl;
	d = torch::ones_like(b);
	//cout << d << endl;
	d = torch::rand_like(b, torch::kFloat);
	//cout << d << endl;
	d = b.clone(); // 深拷贝
	//cout << d << endl;

	return 0;
}

你可能感兴趣的:(pytorch,人工智能)