Skip to content

Commit

Permalink
Allow for publishing of reward network in discrete CRR (#588)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #588

Allow for publishing of reward network in discrete_crr.py

Differential Revision: D32711991

fbshipit-source-id: 5dcd8bc4ee9e5fb9922a6b47563b0c12a0908fa9
  • Loading branch information
David Vengerov authored and facebook-github-bot committed Dec 1, 2021
1 parent 4ab19c5 commit 528c8f1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
20 changes: 19 additions & 1 deletion reagent/model_managers/discrete/discrete_crr.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def get_reporter(self):
# in utils.py

def serving_module_names(self):
module_names = ["default_model", "dqn", "actor_dqn"]
module_names = ["default_model", "dqn", "actor_dqn", "reward"]
if len(self.action_names) == 2:
module_names.append("binary_difference_scorer")
return module_names
Expand All @@ -219,6 +219,7 @@ def build_serving_modules(
"dqn": self._build_dqn_module(
trainer_module.q1_network, normalization_data_map
),
"reward": self.build_reward_module(trainer_module, normalization_data_map),
"actor_dqn": self._build_dqn_module(
ActorDQN(trainer_module.actor_network), normalization_data_map
),
Expand Down Expand Up @@ -286,6 +287,23 @@ def build_actor_module(
action_feature_ids=list(range(len(self.action_names))),
)

def build_reward_module(
self,
trainer_module: DiscreteCRRTrainer,
normalization_data_map: Dict[str, NormalizationData],
) -> torch.nn.Module:
"""
Returns a TorchScript predictor module
"""
net_builder = self.cpe_net_builder.value
assert trainer_module.reward_network is not None
return net_builder.build_serving_module(
trainer_module.reward_network,
normalization_data_map[NormalizationKey.STATE],
action_names=self.action_names,
state_feature_config=self.state_feature_config,
)


class ActorDQN(ModelBase):
def __init__(self, actor):
Expand Down
2 changes: 2 additions & 0 deletions reagent/training/discrete_crr_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ def __init__(
# pyre-fixme[16]: Optional type has no attribute `__getitem__`.
self.reward_boosts[0, i] = rl.reward_boost[k]

# The function below adds reward_network as a member object to DQNTrainerBaseLightning,
# from which DiscreteCRRTrainer is derived.
self._initialize_cpe(
reward_network,
q_network_cpe,
Expand Down

0 comments on commit 528c8f1

Please sign in to comment.