-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcritic.py
64 lines (53 loc) · 1.96 KB
/
critic.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
import torch
class CriticNetwork(torch.nn.Module):
def __init__(
self,
input_shape,
n_actions,
h1_size=400,
h2_size=300,
lr=1e-3,
decay=1e-2,
chkpt_path="weights/critic.pt",
):
super(CriticNetwork, self).__init__()
self.input_shape = input_shape
self.n_actions = n_actions
self.h1_size = h1_size
self.h2_size = h2_size
self.lr = lr
self.decay = decay
self.chkpt_path = chkpt_path
input_features = input_shape[0] + self.n_actions[0]
self.h1_layer = torch.nn.Linear(input_features, self.h1_size)
self.h2_layer = torch.nn.Linear(self.h1_size, self.h2_size)
self.out_layer = torch.nn.Linear(self.h2_size, 1)
self.optimizer = torch.optim.Adam(
self.parameters(), lr=self.lr, weight_decay=self.decay
)
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.to(self.device)
def forward(self, state, action):
x = torch.concatenate((state, action), dim=1)
x = torch.nn.functional.relu(self.h1_layer(x))
x = torch.nn.functional.relu(self.h2_layer(x))
return self.out_layer(x)
def save_checkpoint(self, epoch=None, loss=None):
torch.save(self.state_dict(), self.chkpt_path)
# torch.save(
# {
# "epoch": epoch,
# "model_state_dict": self.state_dict(),
# "optimizer_state_dict": self.optimizer.state_dict(),
# "loss": loss,
# },
# self.chkpt_path,
# )
def load_checkpoint(self):
self.load_state_dict(torch.load(self.chkpt_path))
# chkpt = torch.load(self.chkpt_path)
# self.load_state_dict(chkpt["model_state_dict"])
# self.optimizer.load_state_dict(chkpt["optimizer_state_dict"])
# epoch = chkpt["epoch"]
# loss = chkpt["loss"]
# return epoch, loss