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!