Skip to content

Commit aff2313

Browse files
fix: corrected NeuralBandit and adjusted action processing
1 parent ef90a90 commit aff2313

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

pearl/policy_learners/contextual_bandits/neural_bandit.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def act(
132132
"""
133133
Args:
134134
subjective_state: state will be applied to different action vectors in action_space
135-
action_space: contains a list of action vector, currenly only support static space
135+
action_space: contains a list of action vector, currently only support static space
136136
Return:
137137
action index chosen given state and action vectors
138138
"""
@@ -145,7 +145,14 @@ def act(
145145
state_features_only=self._state_features_only,
146146
action_representation_module=self.action_representation_module,
147147
)
148-
values = self.model(new_feature).squeeze(-1)
148+
batch_size = new_feature.shape[0]
149+
feature_dim = new_feature.shape[-1]
150+
# Flatten the action dimension before model evaluation
151+
values = (
152+
self.model(new_feature.reshape(-1, feature_dim))
153+
.reshape(batch_size, -1)
154+
.squeeze(-1)
155+
)
149156
# batch_size * action_count
150157
assert values.numel() == new_feature.shape[0] * action_count
151158
return self.exploration_module.act(

pearl/replay_buffers/tensor_based_replay_buffer.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,12 @@ def _process_non_optional_single_state(
161161

162162
def _process_single_action(self, action: Action) -> torch.Tensor:
163163
if isinstance(action, torch.Tensor):
164-
return action.to(get_default_device()).clone().detach().unsqueeze(0)
164+
tensor = action.to(get_default_device()).clone().detach()
165165
else:
166-
return torch.tensor(action).unsqueeze(0)
166+
tensor = torch.tensor(action)
167+
if tensor.ndim == 0:
168+
tensor = tensor.unsqueeze(0)
169+
return tensor
167170

168171
def _process_single_reward(self, reward: Reward) -> torch.Tensor:
169172
return torch.tensor([reward])

0 commit comments

Comments
 (0)