-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
49 lines (40 loc) · 1.59 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
"""
This module implements a QNetwork using PyTorch.
Author: Kai Waelti
"""
import torch
import torch.nn as nn
import torch.nn.functional as F
class QNetwork(nn.Module):
"""Module that maps states to action values."""
def __init__(self, state_size: int,
action_size: int,
seed: int = 42,
fully_connected_units_1: int = 64,
fully_connected_units_2: int = 64):
"""
Initialize parameters and build model.
Params
======
state_size (int): Dimension of each state
action_size (int): Dimension of possible actions
seed (int): Random seed
fully_connected_1 (int): # Nodes in 1st FC hidden layer
fully_connected_2 (int): # Nodes in 2nd FC hidden layer
"""
super(QNetwork, self).__init__()
self.seed = torch.manual_seed(seed)
self.fully_connected_1 = nn.Linear(state_size,
fully_connected_units_1)
self.fully_connected_2 = nn.Linear(fully_connected_units_1,
fully_connected_units_2)
self.fully_connected_3 = nn.Linear(fully_connected_units_2,
action_size)
def forward(self, state):
"""Build a network that maps state -> action values."""
x_1 = self.fully_connected_1(state)
x_relu_1 = F.relu(x_1)
x_2 = self.fully_connected_2(x_relu_1)
x_relu_2 = F.relu(x_2)
action_values = self.fully_connected_3(x_relu_2)
return action_values