| import pytest |
| import torch as th |
| from torch.distributions import Normal |
|
|
| from stable_baselines3 import A2C, PPO, SAC |
|
|
|
|
| def test_state_dependent_exploration_grad(): |
| """ |
| Check that the gradient correspond to the expected one |
| """ |
| n_states = 2 |
| state_dim = 3 |
| action_dim = 10 |
| sigma_hat = th.ones(state_dim, action_dim, requires_grad=True) |
| |
| |
| |
| th.manual_seed(2) |
| weights_dist = Normal(th.zeros_like(sigma_hat), sigma_hat) |
| weights = weights_dist.rsample() |
|
|
| state = th.rand(n_states, state_dim) |
| mu = th.ones(action_dim) |
| noise = th.mm(state, weights) |
|
|
| action = mu + noise |
|
|
| variance = th.mm(state ** 2, sigma_hat ** 2) |
| action_dist = Normal(mu, th.sqrt(variance)) |
|
|
| |
| loss = action_dist.log_prob(action.detach()).sum(dim=-1).mean() |
| loss.backward() |
|
|
| |
| |
| grad = th.zeros_like(sigma_hat) |
| for j in range(action_dim): |
| |
| |
| |
| sigma_j = th.sqrt(variance[:, j]) |
| for i in range(state_dim): |
| |
| |
| d_log_policy_j = (noise[:, j] ** 2 - sigma_j ** 2) / sigma_j ** 3 |
| |
| d_log_sigma_j = (state[:, i] ** 2 * sigma_hat[i, j]) / sigma_j |
| |
| grad[i, j] = (d_log_policy_j * d_log_sigma_j).mean() |
|
|
| |
| assert sigma_hat.grad.allclose(grad) |
|
|
|
|
| def test_sde_check(): |
| with pytest.raises(ValueError): |
| PPO("MlpPolicy", "CartPole-v1", use_sde=True) |
|
|
|
|
| @pytest.mark.parametrize("model_class", [SAC, A2C, PPO]) |
| @pytest.mark.parametrize("sde_net_arch", [None, [32, 16], []]) |
| @pytest.mark.parametrize("use_expln", [False, True]) |
| def test_state_dependent_offpolicy_noise(model_class, sde_net_arch, use_expln): |
| model = model_class( |
| "MlpPolicy", |
| "Pendulum-v0", |
| use_sde=True, |
| seed=None, |
| create_eval_env=True, |
| verbose=1, |
| policy_kwargs=dict(log_std_init=-2, sde_net_arch=sde_net_arch, use_expln=use_expln, net_arch=[64]), |
| ) |
| model.learn(total_timesteps=int(300), eval_freq=250) |
| model.policy.reset_noise() |
| if model_class == SAC: |
| model.policy.actor.get_std() |
|
|