Note that this tutorial requires bitrl to be configured with PyTorch support.
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>
{
}
{
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;
}
torch::Tensor t = torch::tensor(2.0);
auto val = t.item();
std::cout << "Tensor t is: " << val.to<float>() << std::endl;
std::cout << " Dimension of tensor: " << t.dim() << std::endl;
torch::Tensor tensor = torch::eye(3);
std::cout << "Tensor is: " << tensor << std::endl;
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;
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;
std::vector<int_t> uint_data = {1, 2, 3, 4};
auto tensor_from_data_3 = torch::tensor(uint_data);
auto unsqueezed_tensor = tensor_from_data_3.unsqueeze(0);
auto sum = tensor_from_data_2 + tensor_from_data_1;
std::cout <<
sum << std::endl;
auto diff = tensor_from_data_2 - tensor_from_data_1;
std::cout <<
sum << std::endl;
auto tensor1 = torch::tensor({1.0, 2.0, 3.0});
auto product = tensor1 * tensor1;
std::cout << product << std::endl;
if (torch::cuda::is_available())
{
auto cuda_tensor = torch::tensor({1.0, 2.0, 3.0}).to("cuda");
}
std::cout << torch::ones({2, 4}) << std::endl;
return 0;
}
#else
#include <iostream>
{
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