This repository has been archived by the owner on Dec 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel.py
73 lines (55 loc) · 2.13 KB
/
model.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
65
66
67
68
69
70
71
72
73
import torch
import torch.nn as nn
import torch.nn.functional as F
class ReacherActorCritic(nn.Module):
def __init__(self, state_size, action_dim):
super(ReacherActorCritic, self).__init__()
self.state_size = state_size
self.action_dim = action_dim
self.fc1 = nn.Linear(state_size, 100)
self.fc2 = nn.Linear(100, 100)
self.fc_actor = nn.Linear(100, self.action_dim)
self.fc_critic = nn.Linear(100, 1)
self.std = nn.Parameter(torch.zeros(1, action_dim))
def forward(self, x, action=None):
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
# Actor
mean = torch.tanh(self.fc_actor(x))
dist = torch.distributions.Normal(mean, F.softplus(self.std))
if action is None:
action = dist.sample()
log_prob = dist.log_prob(action)
# Critic
# State value V(s)
v = self.fc_critic(x)
return action, log_prob, dist.entropy(), v
class CrawlerActorCritic(nn.Module):
def __init__(self, state_size, action_dim):
super(CrawlerActorCritic, self).__init__()
self.state_size = state_size
self.action_dim = action_dim
self.fc1 = nn.Linear(state_size, 512)
#self.fc1_bn = nn.BatchNorm1d(300)
self.fc2 = nn.Linear(512, 256)
#self.fc2_bn = nn.BatchNorm1d(256)
#self.fc3 = nn.Linear(200, 100)
#self.fc3_bn = nn.BatchNorm1d(100)
self.fc_actor_mean = nn.Linear(256, self.action_dim)
self.fc_actor_std = nn.Linear(256, self.action_dim)
self.fc_critic = nn.Linear(256, 1)
self.std = nn.Parameter(torch.zeros(1, action_dim))
def forward(self, x, action=None):
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
# Actor
mean = torch.tanh(self.fc_actor_mean(x))
std = F.softplus(self.fc_actor_std(x))
dist = torch.distributions.Normal(mean, std)
if action is None:
action = dist.sample()
log_prob = dist.log_prob(action)
# Critic
# State value V(s)
v = self.fc_critic(x)
return action, log_prob, dist.entropy(), v