Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix negative probability values #18

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions agent/asyncrl/policy_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ def _sample_discrete_actions(batch_probs):
List consisting of sampled actions
"""
action_indices = []

# Subtract a tiny value from probabilities in order to avoid
# "ValueError: sum(pvals[:-1]) > 1.0" in numpy.multinomial
batch_probs = batch_probs - np.finfo(np.float32).epsneg

# Prevent having a vector which sum is not in [0, 1]
while not 0 < np.sum(batch_probs) < 1:
# Subtract a tiny value from probabilities in order to avoid
# "ValueError: sum(pvals[:-1]) > 1.0" in numpy.multinomial
batch_probs = batch_probs - np.finfo(np.float32).epsneg
# Apply abs function to keep probability values positive to avoid
# "ValueError: pvals < 0, pvals > 1 or pvals contains NaNs" in numpy.multinomial
batch_probs = np.absolute(batch_probs)

for i in range(batch_probs.shape[0]):
histogram = np.random.multinomial(1, batch_probs[i])
action_indices.append(int(np.nonzero(histogram)[0]))
Expand Down