bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
PyTorch tensors

Example intro_example_1 demonstrated some basic functionality that bitrl implements. In addition, bitrl is heavily based on the C++ API exposed by PyTorch. Therefore, this tutorial is meant to show you some basics on how to create and manipulate PyTorch tensors. There are many resources you can consult for further info:

Note that this tutorial requires bitrl to be configured with PyTorch support.

The driver code

The driver code for this tutorial is shown below.

#include "bitrl/base/bitrl_config.h"
#ifdef USE_PYTORCH
#include <torch/torch.h>
#include <iostream>
#include <string>
#include <typeinfo>
#include <vector>
namespace example2
{
} // namespace example2
int main()
{
using namespace example2;
if (torch::cuda::is_available())
{
std::cout << "CUDA is available on this machine" << std::endl;
}
else
{
std::cout << "CUDA is not available on this machine" << std::endl;
}
// create a torch::Tensor from a scalar value
torch::Tensor t = torch::tensor(2.0);
// the return value is c10::Scalar
auto val = t.item();
// use c10::Sclar.to<T>() to get the actual value
// T is the actual type we want
std::cout << "Tensor t is: " << val.to<float>() << std::endl;
std::cout << " Dimension of tensor: " << t.dim() << std::endl;
// various methods to create a tensor
torch::Tensor tensor = torch::eye(3);
std::cout << "Tensor is: " << tensor << std::endl;
// floating point tensor
std::vector<real_t> data(3, 2.0);
auto tensor_from_data_1 = torch::tensor(data);
std::cout << "Dimension of tensor: " << tensor_from_data_1.dim() << std::endl;
// loop over the values of the tensor
for (uint_t i = 0; i < static_cast<uint_t>(tensor_from_data_1.size(0)); ++i)
{
std::cout << tensor_from_data_1[i] << std::endl;
}
data[0] = data[1] = data[2] = 1.0;
auto tensor_from_data_2 = torch::tensor(data);
std::cout << tensor_from_data_2 << std::endl;
// integer tensor
std::vector<int_t> uint_data = {1, 2, 3, 4};
auto tensor_from_data_3 = torch::tensor(uint_data);
// unsqueeze operation
auto unsqueezed_tensor = tensor_from_data_3.unsqueeze(0);
// add two tensors together
auto sum = tensor_from_data_2 + tensor_from_data_1;
std::cout << sum << std::endl;
// difference of two tensors
auto diff = tensor_from_data_2 - tensor_from_data_1;
std::cout << sum << std::endl;
// compute element-wise product
auto tensor1 = torch::tensor({1.0, 2.0, 3.0});
auto product = tensor1 * tensor1;
std::cout << product << std::endl;
if (torch::cuda::is_available())
{
// create a tensor and send it to the GPU
auto cuda_tensor = torch::tensor({1.0, 2.0, 3.0}).to("cuda");
}
// create a tensor of shape 2x4 filled with ones
std::cout << torch::ones({2, 4}) << std::endl;
return 0;
}
#else
#include <iostream>
int main()
{
std::cout << "This example requires PyTorch and gymfcpp. Reconfigure bitrl with USE_PYTORCH "
"flags turned ON."
<< std::endl;
return 0;
}
#endif
bitrl's logging: the logger of bitsim, bitsim/utils/logger.h, with bitrl's messages going to the logg...
int main()
Definition discounted_returns.cpp:19
std::iterator_traits< IteratorType >::value_type sum(IteratorType begin, IteratorType end, bool parallel=true)
Definition vector_math.h:89
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
double real_t
real_t
Definition bitsim_types.h:20
int int_t
integer type
Definition bitsim_types.h:30
Definition pytorch_tensors.cpp:16