2023-02-17 22:29:12 -08:00
|
|
|
import torch
|
|
|
|
import numpy as np
|
2023-02-18 19:50:43 -08:00
|
|
|
from pathlib import Path
|
2023-02-17 22:29:12 -08:00
|
|
|
import matplotlib.pyplot as plt
|
2023-02-18 19:50:43 -08:00
|
|
|
from multiprocessing import Pool
|
2023-02-17 22:29:12 -08:00
|
|
|
|
2023-02-18 19:50:43 -08:00
|
|
|
from celeste import Celeste
|
|
|
|
from main import DQN
|
|
|
|
from main import Transition
|
2023-02-17 22:29:12 -08:00
|
|
|
|
2023-02-18 19:50:43 -08:00
|
|
|
# Use cpu, the script is faster in parallel.
|
|
|
|
compute_device = torch.device("cpu")
|
2023-02-17 22:29:12 -08:00
|
|
|
|
|
|
|
|
|
|
|
# Celeste env properties
|
|
|
|
n_observations = len(Celeste.state_number_map)
|
|
|
|
n_actions = len(Celeste.action_space)
|
|
|
|
|
|
|
|
|
2023-02-18 19:50:43 -08:00
|
|
|
out_dir = Path("out/plots")
|
|
|
|
out_dir.mkdir(parents = True, exist_ok = True)
|
2023-02-17 22:29:12 -08:00
|
|
|
|
2023-02-18 19:50:43 -08:00
|
|
|
src_dir = Path("model_data/model_archive")
|
2023-02-17 22:29:12 -08:00
|
|
|
|
|
|
|
policy_net = DQN(
|
|
|
|
n_observations,
|
|
|
|
n_actions
|
|
|
|
).to(compute_device)
|
|
|
|
|
|
|
|
target_net = DQN(
|
|
|
|
n_observations,
|
|
|
|
n_actions
|
|
|
|
).to(compute_device)
|
|
|
|
|
|
|
|
optimizer = torch.optim.AdamW(
|
|
|
|
policy_net.parameters(),
|
|
|
|
lr = 0.01, # Hyperparameter: learning rate
|
|
|
|
amsgrad = True
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def makeplt(i, net):
|
|
|
|
p = np.zeros((128, 128), dtype=np.float32)
|
|
|
|
|
|
|
|
for r in range(len(p)):
|
|
|
|
for c in range(len(p[r])):
|
|
|
|
with torch.no_grad():
|
|
|
|
k = net(
|
|
|
|
torch.tensor(
|
|
|
|
[c, r, 60, 80],
|
|
|
|
dtype = torch.float32,
|
|
|
|
device = compute_device
|
|
|
|
).unsqueeze(0)
|
|
|
|
)[0][i].item()
|
|
|
|
|
|
|
|
p[r][c] = k
|
|
|
|
return p
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-02-18 19:50:43 -08:00
|
|
|
def plot(src):
|
|
|
|
checkpoint = torch.load(src)
|
2023-02-17 22:29:12 -08:00
|
|
|
policy_net.load_state_dict(checkpoint["policy_state_dict"])
|
|
|
|
|
|
|
|
|
|
|
|
fig, axs = plt.subplots(2, 4, figsize = (15, 10))
|
|
|
|
|
|
|
|
for a in range(len(axs.ravel())):
|
|
|
|
ax = axs.ravel()[a]
|
|
|
|
ax.set(adjustable="box", aspect="equal")
|
|
|
|
plot = ax.pcolor(
|
|
|
|
makeplt(a, policy_net),
|
2023-02-18 19:50:43 -08:00
|
|
|
cmap = "Greens",
|
2023-02-17 22:29:12 -08:00
|
|
|
vmin = 0,
|
|
|
|
)
|
|
|
|
ax.set_title(Celeste.action_space[a])
|
|
|
|
ax.invert_yaxis()
|
|
|
|
fig.colorbar(plot)
|
2023-02-18 19:50:43 -08:00
|
|
|
print(src)
|
|
|
|
fig.savefig(out_dir / f"{src.stem}.png")
|
2023-02-17 22:29:12 -08:00
|
|
|
plt.close()
|
2023-02-18 19:50:43 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
with Pool(5) as p:
|
|
|
|
p.map(plot, list(src_dir.iterdir()))
|
|
|
|
|
|
|
|
|