-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetworks.py
154 lines (122 loc) · 4.92 KB
/
networks.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import torch
import torch.nn as nn
import torch.nn.functional as F
device = (
torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
)
class MLP(nn.Module):
def __init__(self, input_dim, n_epochs, batch_size):
super(MLP, self).__init__()
self.fc = nn.Linear(input_dim, 1).to(device)
self.n_epochs = n_epochs
self.batch_size = batch_size
self.optimizer = torch.optim.Adam(self.fc.parameters())
self.loss_func = torch.nn.BCELoss()
def forward(self, x):
return torch.sigmoid(self.fc(x))
def train(self, training_features, train_labels):
for epoch in range(self.n_epochs):
order = torch.randperm(len(training_features))
for start_index in range(
0, len(training_features), self.batch_size
):
self.optimizer.zero_grad()
# fmt: off
batch_indexes = order[
start_index: start_index + self.batch_size
]
# fmt: on
X_batch = training_features[batch_indexes].to(device)
y_batch = train_labels[batch_indexes].to(device)
preds = torch.sigmoid(self.fc(X_batch))
loss_value = self.loss_func(preds.squeeze(), y_batch)
loss_value.backward()
self.optimizer.step()
class PolicyNNV2(nn.Module):
def __init__(self, state_dim, action_dim):
super(PolicyNNV2, self).__init__()
self.fc1 = nn.Linear(state_dim, 512, bias=True)
self.layernorm_1 = nn.LayerNorm(512)
self.fc2 = nn.Linear(512, 1024, bias=True)
self.layernorm_2 = nn.LayerNorm(1024)
self.fc3 = nn.Linear(1024, action_dim, bias=True)
self.dropout = nn.Dropout(0.25)
def forward(self, state):
y = self.fc1(state)
y = self.layernorm_1(y)
y = F.gelu(y)
y = self.dropout(y)
y = self.fc2(y)
y = self.layernorm_2(y)
y = F.gelu(y)
y = self.dropout(y)
y = self.fc3(y)
action_probs = F.softmax(y, dim=-1)
return action_probs
class Block(nn.Module):
def __init__(self, input_dim, output_dim, dropout_prob=0.25) -> None:
super().__init__()
self.fc = nn.Linear(input_dim, output_dim, bias=True)
self.layernorm = nn.LayerNorm(output_dim)
self.dropout = nn.Dropout(dropout_prob)
def forward(self, inputs):
return self.dropout(F.gelu(self.layernorm(self.fc(inputs))))
class OutputBlock(nn.Module):
def __init__(self, input_dim, action_dim) -> None:
super().__init__()
self.fc = nn.Linear(1024, action_dim, bias=True)
self.activation = nn.Softmax(dim=-1)
def forward(self, inputs):
return self.activation(self.fc(inputs))
class PolicyNNV3(nn.Module):
def __init__(self, state_dim, action_dim):
super(PolicyNNV3, self).__init__()
self.block_1 = Block(state_dim, 512)
self.residual_fc = nn.Linear(state_dim, 1024)
self.block_2 = Block(512, 1024)
self.block_3 = Block(1024, 1024)
self.output_block = OutputBlock(1024, action_dim)
def forward(self, state):
y = self.block_1(state)
residual = self.residual_fc(state)
y = self.block_2(y)
y = self.block_3(y)
y += residual
action_probs = self.output_block(y)
return action_probs
class PolicyNN(nn.Module):
def __init__(self, state_dim, action_dim, initializer=None):
super(PolicyNN, self).__init__()
self.fc1 = nn.Linear(state_dim, 512, bias=True)
self.fc2 = nn.Linear(512, 1024, bias=True)
self.fc3 = nn.Linear(1024, action_dim, bias=True)
self.softmax = nn.Softmax(dim=1)
self.dropout = nn.Dropout(0.25)
def forward(self, state):
y = F.relu(self.fc1(state))
y = self.dropout(y)
y = F.relu(self.fc2(y))
y = self.dropout(y)
y = F.relu(self.fc3(y))
action_probs = self.softmax(y)
return action_probs
class ValueNN(nn.Module):
def __init__(self, state_dim, initializer=None):
super(ValueNN, self).__init__()
self.fc1 = nn.Linear(state_dim, 64, bias=True)
self.fc2 = nn.Linear(64, 1, bias=True)
def forward(self, state):
y = F.relu(self.fc1(state))
value_estimated = self.fc2(y)
return torch.squeeze(value_estimated)
class QNetwork(nn.Module):
def __init__(self, state_dim, action_space):
super(QNetwork, self).__init__()
self.fc1 = nn.Linear(state_dim, 128)
self.fc2 = nn.Linear(128, 64)
self.fc3 = nn.Linear(64, action_space)
def forward(self, state):
y = F.relu(self.fc1(state))
y = F.relu(self.fc2(y))
action_values = self.fc3(y)
return action_values