Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: visualize parametric biases #185

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions mohou/script/experimental/visualize_pblstm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import argparse
from typing import Optional

import matplotlib.pyplot as plt
import numpy as np
from sklearn.decomposition import PCA

from mohou.file import get_project_path
from mohou.model.lstm import PBLSTM
from mohou.trainer import TrainCache
from mohou.types import EpisodeBundle

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-pn", type=str, help="project name")
parser.add_argument("-n_pb_dim", type=int, help="pb dimension")

args = parser.parse_args()
project_name: Optional[str] = args.pn
n_pb_dim: Optional[str] = args.n_pb_dim

project_path = get_project_path(project_name)

tcache_search_kwargs = {}
if n_pb_dim is not None:
tcache_search_kwargs["n_pb_dim"] = n_pb_dim

tcache = TrainCache.load(project_path, PBLSTM, **tcache_search_kwargs)
model = tcache.best_model
assert model.config.n_pb_dim > 1, "(future plan) implement line plot?"

pb_list = [pb_tensor.detach().numpy() for pb_tensor in model.parametric_bias_list]
pb_arr = np.array(pb_list)

pca = PCA(2)
pca.fit(pb_arr)
pb_reduced_arr = pca.transform(pb_arr)

bundle = EpisodeBundle.load(project_path)
fig, ax = plt.subplots()
ax.scatter(pb_reduced_arr[:, 0], pb_reduced_arr[:, 1])
for idx_episode, pb in enumerate(pb_reduced_arr):
episode = bundle[idx_episode]
print(idx_episode)
print(episode.metadata)
ax.annotate(str(idx_episode), (pb[0], pb[1]))

plt.show()