-
Notifications
You must be signed in to change notification settings - Fork 1
/
vis_util.py
76 lines (76 loc) · 2.74 KB
/
vis_util.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
import matplotlib.pyplot as plt
import json
from bandit_alg import Exp3
import fire, glob
import ast
def draw_bandit_distribution(weight_history, idx, prefix="none"):
weight_history = [[w / sum(weights) for w in weights] for weights in weight_history]
plt.figure()
colors = [ "red", "blue", "orange", "green"]
if len(weight_history[0]) == 4:
colors = ['red', '#1f77b4', '#ff7f0e', '#2ca02c']
else:
colors = [ '#1f77b4', '#ff7f0e', '#2ca02c']
for i in range(len(weight_history[0])):
plt.plot([w[i] for w in weight_history], color=colors[i])
plt.xlabel("Number of Rounds")
plt.ylabel("Probability")
if len(weight_history[0]) == 4:
plt.title("Bandit Arm Distribution History")
plt.legend(["Do Nothing", "Reflection", "Fluency", "Coherence", "MI"])
plt.savefig(f"{prefix}_bandit_arm_distribution_{idx}.png")
else:
plt.title("Reward Weight History")
plt.legend(["Reflection", "Fluency", "Coherence"])
# plt.savefig(f"dynaopt_reward_weight_history_{idx}.png")
plt.savefig(f"{prefix}_reward_weight_history_{idx}.png")
return
def main(path = 'voutputs/con_scst_contextual_MI_rl_2023_10_03_10_10_04/bandit_weight_history.json',idx=0, prefix="none"):
with open(path, 'r') as f:
weight_history = json.load(f)
draw_bandit_distribution(weight_history, idx, prefix)
def draw_reward_history():
with open('dorb_rewards', 'r') as f:
# read the text file
rewards = f.readlines()
dorb_history = []
for r in rewards:
r = r.split("39m")[-1]
r = ast.literal_eval(r)
dorb_history.append(r)
with open('dynaopt_rewards', 'r') as f:
# read the text file
rewards = f.readlines()
dynaopt_history = []
for r in rewards:
r = r.split("39m")[-1]
r = ast.literal_eval(r)
dynaopt_history.append(r)
labels = [ "DORB", "DynaOpt"]
reward_names = [ "Reflection", "Fluency", "Coherence"]
# visualize weight history with pyplot
plt.figure()
plt.plot(dorb_history)
plt.xlabel("Number of Rounds")
plt.ylabel("Reward")
plt.title("DORB Reward History")
plt.legend(reward_names)
# plt.show()
plt.savefig(f"dorb_reward_history.png")
plt.figure()
plt.plot(dynaopt_history)
plt.xlabel("Number of Rounds")
plt.ylabel("Reward")
plt.title("DynaOpt Reward History")
plt.legend(reward_names)
# plt.show()
plt.savefig(f"dynaopt_reward_history.png")
if __name__ == "__main__":
draw_reward_history()
files = glob.glob("voutputs/*con*/pmf_history.json")
files = sorted(files)
for i,f in enumerate(files):
print(f)
prefix="con"
main(f, i, prefix)
# fire.Fire(main)