-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDDPG_P.py
68 lines (53 loc) · 2.03 KB
/
DDPG_P.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
EPS = 0.003
def logrelu(x):
return torch.log(x*x+1)*x
def fanin_init(size, fanin=None):
fanin = fanin or size[0]
v = 1. / np.sqrt(fanin)
return torch.Tensor(size).uniform_(-v, v)
class Policy(nn.Module):
"""Actor (Policy) Model."""
def __init__(self, state_dim, action_dim):
"""
:param state_dim: Dimension of input state (int)
:param action_dim: Dimension of output action (int)
:return:
"""
super(Policy, self).__init__()
self.state_dim = state_dim
self.action_dim = action_dim
self.fc1 = nn.Linear(state_dim,256)
self.fc1.weight.data = fanin_init(self.fc1.weight.data.size())
self.fc2 = nn.Linear(256,128)
self.fc2.weight.data = fanin_init(self.fc2.weight.data.size())
self.mu1 = nn.Linear(128,64)
self.mu1.weight.data = fanin_init(self.mu1.weight.data.size())
self.mu2 = nn.Linear(64,action_dim)
self.mu2.weight.data.uniform_(-EPS,EPS)
self.sigma1 = nn.Linear(128,64)
self.sigma1.weight.data = fanin_init(self.sigma1.weight.data.size())
self.sigma2 = nn.Linear(64,action_dim)
self.sigma2.weight.data.uniform_(-EPS,EPS)
def forward(self, state):
"""
returns policy function Pi(s) obtained from actor network
this function is a gaussian prob distribution for all actions
with mean lying in (-1,1) and sigma lying in (0,1)
The sampled action can , then later be rescaled
:param state: Input state (Torch Variable : [n,state_dim] )
:return: Output action (Torch Variable: [n,action_dim] )
"""
x = F.relu(self.fc1(state))
#x = F.dropout(x, 0.1)
x = F.relu(self.fc2(x))
#x = F.dropout(x, 0.1)
mu = F.relu(self.mu1(x))
mu = self.mu2(mu)
sigma = F.relu(self.sigma1(x))
sigma = self.sigma2(sigma)
sigma = torch.clamp(sigma, -1, 1)
return mu, sigma*sigma