1#ifndef TORCH_TENSOR_UTILS_H
2#define TORCH_TENSOR_UTILS_H
4#include "bitrl/base/bitrl_config.h"
9#include <torch/torch.h>
27 std::vector<std::int64_t> values;
28 values.reserve(container.size());
29 for (
const auto &value : container)
31 values.push_back(
static_cast<bool>(value) ? 1 : 0);
33 return torch::tensor(values, torch::dtype(torch::kInt64)).to(torch::kBool);
41template <
typename TorchNetType>
void copy_parameters(
const TorchNetType &from, TorchNetType &to)
43 torch::NoGradGuard no_grad;
45 auto params = to->named_parameters(
true );
46 for (
const auto &item : from->named_parameters(
true ))
48 auto *param = params.find(item.key());
51 throw std::invalid_argument(
"copy_parameters: no parameter " + item.key());
53 param->copy_(item.value());
56 auto buffers = to->named_buffers(
true );
57 for (
const auto &item : from->named_buffers(
true ))
59 auto *buffer = buffers.find(item.key());
60 if (buffer ==
nullptr)
62 throw std::invalid_argument(
"copy_parameters: no buffer " + item.key());
64 buffer->copy_(item.value());
73template <
typename TorchNetType>
77 torch::load(from, params_path);
Definition torch_tensor_utils.h:19
void copy_parameters(const TorchNetType &from, TorchNetType &to)
copy_parameters. Copy the parameters and buffers of one network into another of the same type,...
Definition torch_tensor_utils.h:41
torch_tensor_t create_mask(const ContainerType &container)
create_mask. Create a boolean mask from the given container. The container should hold values convert...
Definition torch_tensor_utils.h:25
void copy_parameters_to(TorchNetType &to, const std::string ¶ms_path)
copy_parameters_to. Copy into to the parameters and buffers of a network of the same type that was sa...
Definition torch_tensor_utils.h:74
Various utilities used when working with RL problems.
Definition bitrl_log.h:25
torch::Tensor torch_tensor_t
torch_tensor_t
Definition bitrl_types.h:95