r""" Generate actions according to a softmax policy. Notes ----- The softmax policy assumes that the pmf over actions in state :math:`x_t` is given by: .. math:: \pi(a | x^{(t)}) = \text{softmax}( \text{obs}^{(t)} \cdot \
(self, obs)
| 193 | self.episode_history = {"rewards": [], "state_actions": []} |
| 194 | |
| 195 | def act(self, obs): |
| 196 | r""" |
| 197 | Generate actions according to a softmax policy. |
| 198 | |
| 199 | Notes |
| 200 | ----- |
| 201 | The softmax policy assumes that the pmf over actions in state :math:`x_t` is |
| 202 | given by: |
| 203 | |
| 204 | .. math:: |
| 205 | |
| 206 | \pi(a | x^{(t)}) = \text{softmax}( |
| 207 | \text{obs}^{(t)} \cdot \mathbf{W}_i^{(t)} + \mathbf{b}_i^{(t)} ) |
| 208 | |
| 209 | where :math:`\mathbf{W}` is a learned weight matrix, `obs` is the observation |
| 210 | at timestep `t`, and **b** is a learned bias vector. |
| 211 | |
| 212 | Parameters |
| 213 | ---------- |
| 214 | obs : int or :py:class:`ndarray <numpy.ndarray>` |
| 215 | An observation from the environment. |
| 216 | |
| 217 | Returns |
| 218 | ------- |
| 219 | action : int, float, or :py:class:`ndarray <numpy.ndarray>` |
| 220 | An action sampled from the distribution over actions defined by the |
| 221 | softmax policy. |
| 222 | """ |
| 223 | E, P = self.env_info, self.parameters |
| 224 | W, b = P["W"], P["b"] |
| 225 | |
| 226 | s = self._obs2num[obs] |
| 227 | s = np.array([s]) if E["obs_dim"] == 1 else s |
| 228 | |
| 229 | # compute softmax |
| 230 | Z = s.T @ W + b |
| 231 | e_Z = np.exp(Z - np.max(Z, axis=-1, keepdims=True)) |
| 232 | action_probs = e_Z / e_Z.sum(axis=-1, keepdims=True) |
| 233 | |
| 234 | # sample action |
| 235 | a = np.random.multinomial(1, action_probs).argmax() |
| 236 | return self._num2action[a] |
| 237 | |
| 238 | def run_episode(self, max_steps, render=False): |
| 239 | """ |