-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathutils.py
64 lines (53 loc) · 1.83 KB
/
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import torch.nn.functional as F
import torch.nn as nn
import torch
class Actor(nn.Module):
def __init__(self, state_dim, action_dim, net_width):
super(Actor, self).__init__()
self.l1 = nn.Linear(state_dim, net_width)
self.l2 = nn.Linear(net_width, net_width)
self.l3 = nn.Linear(net_width, action_dim)
def forward(self, state):
n = torch.tanh(self.l1(state))
n = torch.tanh(self.l2(n))
return n
def pi(self, state, softmax_dim = 0):
n = self.forward(state)
prob = F.softmax(self.l3(n), dim=softmax_dim)
return prob
class Critic(nn.Module):
def __init__(self, state_dim,net_width):
super(Critic, self).__init__()
self.C1 = nn.Linear(state_dim, net_width)
self.C2 = nn.Linear(net_width, net_width)
self.C3 = nn.Linear(net_width, 1)
def forward(self, state):
v = torch.relu(self.C1(state))
v = torch.relu(self.C2(v))
v = self.C3(v)
return v
def evaluate_policy(env, agent, turns = 3):
total_scores = 0
for j in range(turns):
s, info = env.reset()
done = False
while not done:
# Take deterministic actions at test time
a, logprob_a = agent.select_action(s, deterministic=True)
s_next, r, dw, tr, info = env.step(a)
done = (dw or tr)
total_scores += r
s = s_next
return int(total_scores/turns)
#You can just ignore this funciton. Is not related to the RL.
def str2bool(v):
'''transfer str to bool for argparse'''
if isinstance(v, bool):
return v
if v.lower() in ('yes', 'True','true','TRUE', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'False','false','FALSE', 'f', 'n', '0'):
return False
else:
print('Wrong Input.')
raise