-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpred_model.py
More file actions
57 lines (45 loc) · 1.52 KB
/
pred_model.py
File metadata and controls
57 lines (45 loc) · 1.52 KB
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
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import pdb
from torch.autograd import Variable
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 PredModel(nn.Module):
def __init__(self, state_dim, action_dim,output_dim):
"""
:param state_dim: Dimension of input state (int)
:param action_dim: Dimension of input action (int)
:return:
"""
super(PredModel, 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,output_dim)
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