-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet.py
45 lines (36 loc) · 1.28 KB
/
net.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.distributions import Categorical
class policy_net(nn.Module):
def __init__(self, input_dim, output_dim):
super(policy_net, self).__init__()
self.input_dim = input_dim
self.output_dim = output_dim
self.fc1 = nn.Linear(self.input_dim, 128)
self.fc2 = nn.Linear(128, 128)
self.fc3 = nn.Linear(128, self.output_dim)
def forward(self, input):
x = F.relu(self.fc1(input))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return F.softmax(x, 1)
def act(self, input):
probs = self.forward(input)
dist = Categorical(probs)
action = dist.sample()
action = action.detach().item()
return action
class value_net(nn.Module):
def __init__(self, input_dim, output_dim):
super(value_net, self).__init__()
self.input_dim = input_dim
self.output_dim = output_dim
self.fc1 = nn.Linear(self.input_dim, 128)
self.fc2 = nn.Linear(128, 128)
self.fc3 = nn.Linear(128, self.output_dim)
def forward(self, input):
x = F.relu(self.fc1(input))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x