浏览代码

[Bug-fix] RND would not be saved correctly. Added tests (#4514)

/MLA-1734-demo-provider
GitHub 4 年前
当前提交
400e14cb
共有 2 个文件被更改,包括 26 次插入0 次删除
  1. 20
      ml-agents/mlagents/trainers/tests/torch/saver/test_saver_reward_providers.py
  2. 6
      ml-agents/mlagents/trainers/torch/components/reward_providers/rnd_reward_provider.py

20
ml-agents/mlagents/trainers/tests/torch/saver/test_saver_reward_providers.py


import pytest
import os
import numpy as np
from mlagents.trainers.ppo.optimizer_torch import TorchPPOOptimizer
from mlagents.trainers.sac.optimizer_torch import TorchSACOptimizer
from mlagents.trainers.model_saver.torch_model_saver import TorchModelSaver

CuriositySettings,
GAILSettings,
RNDSettings,
from mlagents.trainers.tests.torch.test_reward_providers.utils import (
create_agent_buffer,
)
DEMO_PATH = (
os.path.join(os.path.dirname(os.path.abspath(__file__)), os.pardir, os.pardir)

trainer_settings.reward_signals = {
RewardSignalType.CURIOSITY: CuriositySettings(),
RewardSignalType.GAIL: GAILSettings(demo_path=DEMO_PATH),
RewardSignalType.RND: RNDSettings(),
}
policy = create_policy_mock(trainer_settings, use_discrete=False)
optimizer = OptimizerClass(policy, trainer_settings)

module_dict_2 = optimizer2.get_modules()
assert "Module:GAIL" in module_dict_1
assert "Module:GAIL" in module_dict_2
assert "Module:Curiosity" in module_dict_1
assert "Module:Curiosity" in module_dict_2
assert "Module:RND-pred" in module_dict_1
assert "Module:RND-pred" in module_dict_2
assert "Module:RND-target" in module_dict_1
assert "Module:RND-target" in module_dict_2
for name, module1 in module_dict_1.items():
assert name in module_dict_2
module2 = module_dict_2[name]

# Run some rewards
data = create_agent_buffer(policy.behavior_spec, 1)
for reward_name in optimizer.reward_signals.keys():
rp_1 = optimizer.reward_signals[reward_name]
rp_2 = optimizer2.reward_signals[reward_name]
assert np.array_equal(rp_1.evaluate(data), rp_2.evaluate(data))

6
ml-agents/mlagents/trainers/torch/components/reward_providers/rnd_reward_provider.py


self.optimizer.step()
return {"Losses/RND Loss": loss.detach().cpu().numpy()}
def get_modules(self):
return {
f"Module:{self.name}-pred": self._training_network,
f"Module:{self.name}-target": self._random_network,
}
class RNDNetwork(torch.nn.Module):
EPSILON = 1e-10

正在加载...
取消
保存