-
Notifications
You must be signed in to change notification settings - Fork 64
/
model.py
104 lines (78 loc) · 2.76 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
EPS = 0.003
def fanin_init(size, fanin=None):
fanin = fanin or size[0]
v = 1. / np.sqrt(fanin)
return torch.Tensor(size).uniform_(-v, v)
class Critic(nn.Module):
def __init__(self, state_dim, action_dim):
"""
:param state_dim: Dimension of input state (int)
:param action_dim: Dimension of input action (int)
:return:
"""
super(Critic, self).__init__()
self.state_dim = state_dim
self.action_dim = action_dim
self.fcs1 = nn.Linear(state_dim,256)
self.fcs1.weight.data = fanin_init(self.fcs1.weight.data.size())
self.fcs2 = nn.Linear(256,128)
self.fcs2.weight.data = fanin_init(self.fcs2.weight.data.size())
self.fca1 = nn.Linear(action_dim,128)
self.fca1.weight.data = fanin_init(self.fca1.weight.data.size())
self.fc2 = nn.Linear(256,128)
self.fc2.weight.data = fanin_init(self.fc2.weight.data.size())
self.fc3 = nn.Linear(128,1)
self.fc3.weight.data.uniform_(-EPS,EPS)
def forward(self, state, action):
"""
returns Value function Q(s,a) obtained from critic network
:param state: Input state (Torch Variable : [n,state_dim] )
:param action: Input Action (Torch Variable : [n,action_dim] )
:return: Value function : Q(S,a) (Torch Variable : [n,1] )
"""
s1 = F.relu(self.fcs1(state))
s2 = F.relu(self.fcs2(s1))
a1 = F.relu(self.fca1(action))
x = torch.cat((s2,a1),dim=1)
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
class Actor(nn.Module):
def __init__(self, state_dim, action_dim, action_lim):
"""
:param state_dim: Dimension of input state (int)
:param action_dim: Dimension of output action (int)
:param action_lim: Used to limit action in [-action_lim,action_lim]
:return:
"""
super(Actor, self).__init__()
self.state_dim = state_dim
self.action_dim = action_dim
self.action_lim = action_lim
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.fc3 = nn.Linear(128,64)
self.fc3.weight.data = fanin_init(self.fc3.weight.data.size())
self.fc4 = nn.Linear(64,action_dim)
self.fc4.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.relu(self.fc2(x))
x = F.relu(self.fc3(x))
action = F.tanh(self.fc4(x))
action = action * self.action_lim
return action