1#ifndef BITRL_RL_ALGORITHMS_ES_MLP_POLICY_H
2#define BITRL_RL_ALGORITHMS_ES_MLP_POLICY_H
39 const std::vector<uint_t> &
layer_sizes() const noexcept {
return sizes_; }
49 std::vector<real_t>
forward(
const std::vector<real_t> ¶meters,
50 const std::vector<real_t> &x)
const;
55 uint_t act_discrete(
const std::vector<real_t> ¶meters,
const std::vector<real_t> &x)
const;
60 std::vector<real_t>
act_continuous(
const std::vector<real_t> ¶meters,
61 const std::vector<real_t> &x,
real_t scale = 1.0)
const;
64 std::vector<uint_t> sizes_;
70 if (sizes_.size() < 2 ||
71 std::any_of(sizes_.begin(), sizes_.end(), [](
uint_t s) { return s == 0; }))
73 throw std::invalid_argument(
"MLPPolicy: needs at least two layers, none empty");
75 for (std::size_t l = 0; l + 1 < sizes_.size(); ++l)
77 n_parameters_ += sizes_[l + 1] * (sizes_[l] + 1);
84 std::vector<real_t> p;
85 p.reserve(n_parameters_);
86 for (std::size_t l = 0; l + 1 < sizes_.size(); ++l)
88 const real_t scale = 1.0 / std::sqrt(
static_cast<real_t>(sizes_[l]));
89 for (
uint_t i = 0; i < sizes_[l + 1] * sizes_[l]; ++i)
91 p.push_back(scale * normal());
93 p.insert(p.end(), sizes_[l + 1], 0.0);
99 const std::vector<real_t> &x)
const
101 if (parameters.size() != n_parameters_ || x.size() != sizes_.front())
103 throw std::invalid_argument(
"MLPPolicy: wrong number of parameters or inputs");
105 std::vector<real_t> in = x;
107 for (std::size_t l = 0; l + 1 < sizes_.size(); ++l)
109 const uint_t n_in = sizes_[l];
110 const uint_t n_out = sizes_[l + 1];
111 const real_t *w = parameters.data() + at;
112 const real_t *b = w + n_out * n_in;
113 std::vector<real_t> out(n_out);
114 for (
uint_t o = 0; o < n_out; ++o)
117 for (
uint_t i = 0; i < n_in; ++i)
119 s += w[o * n_in + i] * in[i];
121 out[o] = (l + 2 < sizes_.size()) ? std::tanh(s) : s;
123 at += n_out * (n_in + 1);
130 const std::vector<real_t> &x)
const
132 const auto out =
forward(parameters, x);
133 return static_cast<uint_t>(std::max_element(out.begin(), out.end()) - out.begin());
137 const std::vector<real_t> &x,
140 auto out =
forward(parameters, x);
143 o = scale * std::tanh(o);
152template <
typename EnvType,
typename Act>
155 auto step = env.reset(seed);
157 for (
uint_t t = 0; t < max_steps && !step.done(); ++t)
159 step = env.step(act(step.observation()));
160 total += step.reward();
uint_t act_discrete(const std::vector< real_t > ¶meters, const std::vector< real_t > &x) const
The output with the largest value: the action of a discrete action space.
Definition mlp_policy.h:129
std::vector< real_t > act_continuous(const std::vector< real_t > ¶meters, const std::vector< real_t > &x, real_t scale=1.0) const
scale * tanh(outputs): the action of a bounded continuous action space
Definition mlp_policy.h:136
const std::vector< uint_t > & layer_sizes() const noexcept
Definition mlp_policy.h:39
uint_t n_parameters() const noexcept
How many parameters the network has.
Definition mlp_policy.h:37
MLPPolicy(std::vector< uint_t > layer_sizes)
Definition mlp_policy.h:68
std::vector< real_t > forward(const std::vector< real_t > ¶meters, const std::vector< real_t > &x) const
The outputs of the network for observation x with parameters.
Definition mlp_policy.h:98
std::vector< real_t > initial_parameters(uint_t seed) const
Weights drawn from N(0, 1 / fan_in), biases 0.
Definition mlp_policy.h:81
Normal random numbers that are the same on every machine.
Definition evolution_strategy.h:33
real_t episode_return(EnvType &env, Act &&act, uint_t seed, uint_t max_steps)
The undiscounted return of one episode of env, reset with seed, acting with act, at most max_steps st...
Definition mlp_policy.h:153
Various utilities used when working with RL problems.
Definition bitrl_log.h:25
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
double real_t
real_t
Definition bitsim_types.h:20