-
Notifications
You must be signed in to change notification settings - Fork 43
/
Model_common.py
64 lines (52 loc) · 2.09 KB
/
Model_common.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 as th
from torch import nn
class ActorNetwork(nn.Module):
"""
A network for actor
"""
def __init__(self, state_dim, hidden_size, output_size, output_act):
super(ActorNetwork, self).__init__()
self.fc1 = nn.Linear(state_dim, hidden_size)
self.fc2 = nn.Linear(hidden_size, hidden_size)
self.fc3 = nn.Linear(hidden_size, output_size)
# activation function for the output
self.output_act = output_act
def __call__(self, state):
out = nn.functional.relu(self.fc1(state))
out = nn.functional.relu(self.fc2(out))
out = self.output_act(self.fc3(out))
return out
class CriticNetwork(nn.Module):
"""
A network for critic
"""
def __init__(self, state_dim, action_dim, hidden_size, output_size=1):
super(CriticNetwork, self).__init__()
self.fc1 = nn.Linear(state_dim, hidden_size)
self.fc2 = nn.Linear(hidden_size + action_dim, hidden_size)
self.fc3 = nn.Linear(hidden_size, output_size)
def __call__(self, state, action):
out = nn.functional.relu(self.fc1(state))
out = th.cat([out, action], 1)
out = nn.functional.relu(self.fc2(out))
out = self.fc3(out)
return out
class ActorCriticNetwork(nn.Module):
"""
An actor-critic network that shared lower-layer representations but
have distinct output layers
"""
def __init__(self, state_dim, action_dim, hidden_size,
actor_output_act, critic_output_size=1):
super(ActorCriticNetwork, self).__init__()
self.fc1 = nn.Linear(state_dim, hidden_size)
self.fc2 = nn.Linear(hidden_size, hidden_size)
self.actor_linear = nn.Linear(hidden_size, action_dim)
self.critic_linear = nn.Linear(hidden_size, critic_output_size)
self.actor_output_act = actor_output_act
def __call__(self, state):
out = nn.functional.relu(self.fc1(state))
out = nn.functional.relu(self.fc2(out))
act = self.actor_output_act(self.actor_linear(out))
val = self.critic_linear(out)
return act, val