-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathutils.py
29 lines (25 loc) · 962 Bytes
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import torch
import numpy as np
from torch.distributions.categorical import Categorical
# select - actions
def select_actions(pi, deterministic=False):
cate_dist = Categorical(pi)
if deterministic:
return torch.argmax(pi, dim=1).item()
else:
return cate_dist.sample().unsqueeze(-1)
# get the action log prob and entropy...
def evaluate_actions(pi, actions):
cate_dist = Categorical(pi)
return cate_dist.log_prob(actions.squeeze(-1)).unsqueeze(-1), cate_dist.entropy().mean()
# get the action log prob and entropy...
def evaluate_actions_sil(pi, actions):
cate_dist = Categorical(pi)
return cate_dist.log_prob(actions.squeeze(-1)).unsqueeze(-1), cate_dist.entropy().unsqueeze(-1)
def discount_with_dones(rewards, dones, gamma):
discounted = []
r = 0
for reward, done in zip(rewards[::-1], dones[::-1]):
r = reward + gamma * r * (1.-done)
discounted.append(r)
return discounted[::-1]