|
| 1 | +from unittest.mock import MagicMock |
| 2 | +import pytest |
| 3 | +import mlagents.trainers.tests.mock_brain as mb |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +import os |
| 7 | + |
| 8 | +from mlagents.trainers.policy.torch_policy import TorchPolicy |
| 9 | +from mlagents.trainers.torch.components.bc.module import BCModule |
| 10 | +from mlagents.trainers.settings import ( |
| 11 | + TrainerSettings, |
| 12 | + BehavioralCloningSettings, |
| 13 | + NetworkSettings, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +def create_bc_module(mock_behavior_specs, bc_settings, use_rnn, tanhresample): |
| 18 | + # model_path = env.external_brain_names[0] |
| 19 | + trainer_config = TrainerSettings() |
| 20 | + trainer_config.network_settings.memory = ( |
| 21 | + NetworkSettings.MemorySettings() if use_rnn else None |
| 22 | + ) |
| 23 | + policy = TorchPolicy( |
| 24 | + 0, |
| 25 | + mock_behavior_specs, |
| 26 | + trainer_config, |
| 27 | + "test", |
| 28 | + False, |
| 29 | + tanhresample, |
| 30 | + tanhresample, |
| 31 | + ) |
| 32 | + bc_module = BCModule( |
| 33 | + policy, |
| 34 | + settings=bc_settings, |
| 35 | + policy_learning_rate=trainer_config.hyperparameters.learning_rate, |
| 36 | + default_batch_size=trainer_config.hyperparameters.batch_size, |
| 37 | + default_num_epoch=3, |
| 38 | + ) |
| 39 | + return bc_module |
| 40 | + |
| 41 | + |
| 42 | +# Test default values |
| 43 | +def test_bcmodule_defaults(): |
| 44 | + # See if default values match |
| 45 | + mock_specs = mb.create_mock_3dball_behavior_specs() |
| 46 | + bc_settings = BehavioralCloningSettings( |
| 47 | + demo_path=os.path.dirname(os.path.abspath(__file__)) + "/" + "test.demo" |
| 48 | + ) |
| 49 | + bc_module = create_bc_module(mock_specs, bc_settings, False, False) |
| 50 | + assert bc_module.num_epoch == 3 |
| 51 | + assert bc_module.batch_size == TrainerSettings().hyperparameters.batch_size |
| 52 | + # Assign strange values and see if it overrides properly |
| 53 | + bc_settings = BehavioralCloningSettings( |
| 54 | + demo_path=os.path.dirname(os.path.abspath(__file__)) + "/" + "test.demo", |
| 55 | + num_epoch=100, |
| 56 | + batch_size=10000, |
| 57 | + ) |
| 58 | + bc_module = create_bc_module(mock_specs, bc_settings, False, False) |
| 59 | + assert bc_module.num_epoch == 100 |
| 60 | + assert bc_module.batch_size == 10000 |
| 61 | + |
| 62 | + |
| 63 | +# Test with continuous control env and vector actions |
| 64 | +@pytest.mark.parametrize("is_sac", [True, False], ids=["sac", "ppo"]) |
| 65 | +def test_bcmodule_update(is_sac): |
| 66 | + mock_specs = mb.create_mock_3dball_behavior_specs() |
| 67 | + bc_settings = BehavioralCloningSettings( |
| 68 | + demo_path=os.path.dirname(os.path.abspath(__file__)) + "/" + "test.demo" |
| 69 | + ) |
| 70 | + bc_module = create_bc_module(mock_specs, bc_settings, False, is_sac) |
| 71 | + stats = bc_module.update() |
| 72 | + for _, item in stats.items(): |
| 73 | + assert isinstance(item, np.float32) |
| 74 | + |
| 75 | + |
| 76 | +# Test with constant pretraining learning rate |
| 77 | +@pytest.mark.parametrize("is_sac", [True, False], ids=["sac", "ppo"]) |
| 78 | +def test_bcmodule_constant_lr_update(is_sac): |
| 79 | + mock_specs = mb.create_mock_3dball_behavior_specs() |
| 80 | + bc_settings = BehavioralCloningSettings( |
| 81 | + demo_path=os.path.dirname(os.path.abspath(__file__)) + "/" + "test.demo", |
| 82 | + steps=0, |
| 83 | + ) |
| 84 | + bc_module = create_bc_module(mock_specs, bc_settings, False, is_sac) |
| 85 | + stats = bc_module.update() |
| 86 | + for _, item in stats.items(): |
| 87 | + assert isinstance(item, np.float32) |
| 88 | + old_learning_rate = bc_module.current_lr |
| 89 | + |
| 90 | + _ = bc_module.update() |
| 91 | + assert old_learning_rate == bc_module.current_lr |
| 92 | + |
| 93 | + |
| 94 | +# Test with constant pretraining learning rate |
| 95 | +@pytest.mark.parametrize("is_sac", [True, False], ids=["sac", "ppo"]) |
| 96 | +def test_bcmodule_linear_lr_update(is_sac): |
| 97 | + mock_specs = mb.create_mock_3dball_behavior_specs() |
| 98 | + bc_settings = BehavioralCloningSettings( |
| 99 | + demo_path=os.path.dirname(os.path.abspath(__file__)) + "/" + "test.demo", |
| 100 | + steps=100, |
| 101 | + ) |
| 102 | + bc_module = create_bc_module(mock_specs, bc_settings, False, is_sac) |
| 103 | + # Should decay by 10/100 * 0.0003 = 0.00003 |
| 104 | + bc_module.policy.get_current_step = MagicMock(return_value=10) |
| 105 | + old_learning_rate = bc_module.current_lr |
| 106 | + _ = bc_module.update() |
| 107 | + assert old_learning_rate - 0.00003 == pytest.approx(bc_module.current_lr, abs=0.01) |
| 108 | + |
| 109 | + |
| 110 | +# Test with RNN |
| 111 | +@pytest.mark.parametrize("is_sac", [True, False], ids=["sac", "ppo"]) |
| 112 | +def test_bcmodule_rnn_update(is_sac): |
| 113 | + mock_specs = mb.create_mock_3dball_behavior_specs() |
| 114 | + bc_settings = BehavioralCloningSettings( |
| 115 | + demo_path=os.path.dirname(os.path.abspath(__file__)) + "/" + "test.demo" |
| 116 | + ) |
| 117 | + bc_module = create_bc_module(mock_specs, bc_settings, True, is_sac) |
| 118 | + stats = bc_module.update() |
| 119 | + for _, item in stats.items(): |
| 120 | + assert isinstance(item, np.float32) |
| 121 | + |
| 122 | + |
| 123 | +# Test with discrete control and visual observations |
| 124 | +@pytest.mark.parametrize("is_sac", [True, False], ids=["sac", "ppo"]) |
| 125 | +def test_bcmodule_dc_visual_update(is_sac): |
| 126 | + mock_specs = mb.create_mock_banana_behavior_specs() |
| 127 | + bc_settings = BehavioralCloningSettings( |
| 128 | + demo_path=os.path.dirname(os.path.abspath(__file__)) + "/" + "testdcvis.demo" |
| 129 | + ) |
| 130 | + bc_module = create_bc_module(mock_specs, bc_settings, False, is_sac) |
| 131 | + stats = bc_module.update() |
| 132 | + for _, item in stats.items(): |
| 133 | + assert isinstance(item, np.float32) |
| 134 | + |
| 135 | + |
| 136 | +# Test with discrete control, visual observations and RNN |
| 137 | +@pytest.mark.parametrize("is_sac", [True, False], ids=["sac", "ppo"]) |
| 138 | +def test_bcmodule_rnn_dc_update(is_sac): |
| 139 | + mock_specs = mb.create_mock_banana_behavior_specs() |
| 140 | + bc_settings = BehavioralCloningSettings( |
| 141 | + demo_path=os.path.dirname(os.path.abspath(__file__)) + "/" + "testdcvis.demo" |
| 142 | + ) |
| 143 | + bc_module = create_bc_module(mock_specs, bc_settings, True, is_sac) |
| 144 | + stats = bc_module.update() |
| 145 | + for _, item in stats.items(): |
| 146 | + assert isinstance(item, np.float32) |
| 147 | + |
| 148 | + |
| 149 | +if __name__ == "__main__": |
| 150 | + pytest.main() |
0 commit comments