-
Notifications
You must be signed in to change notification settings - Fork 6
/
NN.py
52 lines (40 loc) · 1.4 KB
/
NN.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
import torch
import torch.nn.functional as F
import torchvision.transforms as T
from PIL import Image
from torch import nn
def weights_init_(m):
if isinstance(m, nn.Linear):
torch.nn.init.xavier_uniform_(m.weight, gain=1)
torch.nn.init.constant_(m.bias, 0)
class CriticNN(nn.Module):
"""
Value Network
"""
def __init__(self, num_inputs, num_actions, hidden_size=256):
super(CriticNN, self).__init__()
self.linear1 = nn.Linear(num_inputs + num_actions, hidden_size)
self.linear2 = nn.Linear(hidden_size, hidden_size)
self.linear3 = nn.Linear(hidden_size, 1)
self.apply(weights_init_)
def forward(self, state, action):
x = torch.cat((state, action), 1)
x = F.relu(self.linear1(x))
x = F.relu(self.linear2(x))
x = self.linear3(x)
return x
class ActorNN(nn.Module):
"""
Policy Network
"""
def __init__(self, num_inputs, num_actions, hidden_size=256):
super(ActorNN, self).__init__()
self.linear1 = nn.Linear(num_inputs, hidden_size)
self.linear2 = nn.Linear(hidden_size, hidden_size)
self.linear3 = nn.Linear(hidden_size, num_actions)
self.apply(weights_init_)
def forward(self, state):
x = F.relu(self.linear1(state))
x = F.relu(self.linear2(x))
x = torch.tanh(self.linear3(x))
return x