-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
36 lines (32 loc) · 1015 Bytes
/
utils.py
File metadata and controls
36 lines (32 loc) · 1015 Bytes
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
import os
import random
import numpy as np
import torch
import matplotlib.pyplot as plt
def set_seed(seed=42):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
def save_checkpoint(agent, path, meta=None):
os.makedirs(os.path.dirname(path) or '.', exist_ok=True)
payload = {
"state_dict": agent.online.state_dict(),
"meta": meta or {}
}
torch.save(payload, path)
def load_checkpoint(agent, path, map_location=None):
if not os.path.exists(path): return False
payload = torch.load(path, map_location=map_location)
agent.online.load_state_dict(payload["state_dict"])
agent.target.load_state_dict(agent.online.state_dict())
return True
def plot_metrics(scores, losses, fname=None):
fig, ax = plt.subplots(2,1, figsize=(6,6))
ax[0].plot(scores); ax[0].set_title("Scores")
ax[1].plot(losses); ax[1].set_title("Loss")
plt.tight_layout()
if fname:
fig.savefig(fname)
else:
plt.show()
plt.close(fig)