-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddpg.py
66 lines (55 loc) · 1.76 KB
/
ddpg.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
import torch
import torch.nn as nn
import torch.nn.functional as F
class ActorNet(nn.Module):
def __init__(self, state_dim, action_dim):
super(ActorNet, self).__init__()
self.fc = nn.Sequential(
nn.Linear(state_dim, 64),
# nn.BatchNorm1d(1024),
nn.ReLU(),
nn.Linear(64, 32),
# nn.BatchNorm1d(512),
nn.ReLU(),
nn.Linear(32, action_dim),
nn.Tanh()
)
def forward(self, state):
action = self.fc(state)
return action
def get_weights(self):
return {k: v.cpu() for k, v in self.state_dict().items()}
def set_weights(self, weights):
self.load_state_dict(weights)
class CriticNet(nn.Module):
def __init__(self, state_dim, action_dim):
super(CriticNet, self).__init__()
# self.fcs = nn.Sequential(
# nn.Linear(state_dim, 64),
# # nn.BatchNorm1d(512),
# nn.ReLU()
# )
# self.fca = nn.Sequential(
# nn.Linear(action_dim + 64, 64),
# # nn.BatchNorm1d(256),
# nn.ReLU()
# )
# self.out = nn.Sequential(
# nn.Linear(64, 1)
# )
self.fc = nn.Sequential(
nn.Linear(state_dim + action_dim, 64),
nn.ReLU(),
nn.Linear(64, 32),
nn.ReLU(),
nn.Linear(32, 1)
)
def forward(self, state, action):
# x = self.fcs(state)
action_value = self.fc(torch.cat([state, action], dim=-1))
# action_value = self.out(x)
return action_value
def get_weights(self):
return {k: v.cpu() for k, v in self.state_dict().items()}
def set_weights(self, weights):
self.load_state_dict(weights)