Create Vector in LibTorch

LibTorch makes it easy to work with vectors in C++. In this post, we'll walk through the process of creating and using vectors in LibTorch.
Picture of Maziar Mazaheri

Maziar Mazaheri

				
						const at::Tensor Vector = torch::tensor({ 506.56, 568.55 }).cuda();
	std::cout << "Vector: \n" << Vector << std::endl;
	std::cout << "Vector Dimension: " << Vector.ndimension() << std::endl;
	std::cout << "Vector item at index 0: " << Vector[0].cuda() << std::endl;
				
			

This line initializes a constant Tensor named Vector using the LibTorch library. It creates a 1-dimensional Tensor with values 506.56 and 568.55 and then assigns it to the Vector variable. The cuda() method is called on the Tensor to move it to the GPU (assuming that a GPU is available and configured for PyTorch).

				
					const at::Tensor Vector = torch::tensor({ 506.56, 568.55 }).cuda();

				
			

to print the value of the Vector Tensor to the console. The \n character creates a new line to make the output more readable.

				
					std::cout << "Vector: \n" << Vector << std::endl;

				
			

Now lets print the number of dimensions in the Vector Tensor to the console. The ndimension() method returns the number of dimensions in a Tensor.

				
						std::cout << "Vector Dimension: " << Vector.ndimension() << std::endl;
				
			

Next for print the value of the first element of the Vector Tensor to the console. The [0] indexing syntax is used to access the first element of the Tensor, and the cuda() method is called on the element to move it back to the GPU (assuming it was moved to the CPU for the previous output statement). The output should show the value 506.56, which is the first element of the Tensor.

				
					std::cout << "Vector item at index 0: " << Vector[0].cuda() << std::endl;
				
			

I’m happy to have shared this post with you and I truly hope you’ll find it helpful!

Best of luck to you!

Photo by vackground.com on Unsplash
Learning Playground

Create a tensor in LibTorch

LibTorch makes it easy to work with tensors in C++. In this post, we'll walk through the process of creating and using tensor in LibTorch. ...
Read More →
Learning Playground

Create Matrix in LibTorch

LibTorch makes it easy to work with vectors in C++. In this post, we'll walk through the process of creating and using Matrix in LibTorch. ...
Read More →
Learning Playground

Create Vector in LibTorch

LibTorch makes it easy to work with vectors in C++. In this post, we'll walk through the process of creating and using vectors in LibTorch.
Read More →
Learning Playground

Create a scalar tensor in LibTorch, Part 2

The provided code initializes a scalar value in LibTorch, which is a single numeric value used in computations.
Read More →
Learning Playground

Create a scalar tensor in LibTorch, Part 1

This is a short C++ code using the libtorch library to create a scalar tensor called my_scalar, which is stored in the GPU memory.
Read More →
Feature of Artificial intelligence in my eyes.
Blog

AGI: A Decade Away – My Personal Intuition

The rapid advancements in AI technology suggest that Artificial General Intelligence (AGI) could be possible in 10 years, thanks to significant progress in AI algorithms ...
Read More →
Game Engine

Default O3DE project folder not found

Fix O3DE error by editing "o3de_manifest.json" or deleting ".O3DE" folder for automatic resolution.
Read More →

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top