-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw_picture.py
164 lines (111 loc) · 4.71 KB
/
draw_picture.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import os
import pickle
import json
import pandas as pd
import os
import networkx as nx
import json
import scipy
from tqdm import tqdm
from matplotlib.patches import Patch
def softmax(x):
exp_x = np.exp(x)
return exp_x/np.sum(exp_x)
def Hyperparameter_experiment():
sns.set_style('darkgrid')
t10 = [0.842, 0.907, 0.921 ]
t7 = [0.826, 0.879, 0.975 ]
t4 = [0.609, 0.727, 0.773 ]
t1 = [0.312, 0.474, 0.557 ]
plt.figure(figsize=(9, 9))
labels = ['Hit@1', 'Hit@3', 'Hit@5']
y = [0.0, 0.2, 0.4, 0.6, 0.8, 1.0]
y_label = ['0.0', '0.2', '0.4', '0.6', '0.8', '1.0']
x = np.arange(len(labels))
width = 0.2
plt.bar(x - 1.5 * width, t1, width, label='t = 1', color='
plt.bar(x - 0.5 * width, t4, width, label='t = 4', color='
plt.bar(x + 0.5 * width, t7, width, label='t = 7', color='
plt.bar(x + 1.5 * width, t10, width, label='t = 10', color='
plt.xticks(x, labels=labels, fontsize=30)
plt.yticks(y, labels=y_label, fontsize=30)
plt.legend(ncol=4, loc='upper center', bbox_to_anchor=(0.5, 1.15), fontsize=23, columnspacing=0.5)
plt.savefig('pic/turns.pdf', dpi=400)
plt.show()
n5 = [0.818, 0.863, 0.923 ]
n10 = [0.837, 0.899, 0.920]
n15 = [0.807, 0.864, 0.888 ]
n20 = [0.768, 0.827, 0.860]
plt.figure(figsize=(9, 9))
labels = ['Hit@1', 'Hit@3', 'Hit@5']
x = np.arange(len(labels))
width = 0.2
plt.bar(x - 1.5 * width, n5, width, label='k = 5', color='
plt.bar(x - 0.5 * width, n10, width, label='k = 10', color='
plt.bar(x + 0.5 * width, n15, width, label='k = 15', color='
plt.bar(x + 1.5 * width, n20, width, label='k = 20', color='
plt.xticks(x, labels=labels, fontsize=30)
plt.yticks(y, labels=y_label, fontsize=30)
plt.legend(ncol=4, loc='upper center', bbox_to_anchor=(0.5, 1.15), fontsize=23, columnspacing=0.5)
plt.savefig('pic/topics.pdf', dpi=400)
plt.show()
def show_kl(dataset):
co_topic_path = './dataset/{}/processed_data/co_topic.pkl'.format(dataset)
co_topic = pickle.load(open(co_topic_path, 'rb'))
co_topic_graph = co_topic['co_topic_graph']
persona_co_topic = co_topic['persona_co_topic']
max_kl = []
max_pl_p = []
for i, num in enumerate(tqdm(co_topic_graph.sum(-1)[:100])):
if num == 0:
max_kl.append(-1)
max_pl_p.append(-1)
else:
persona_kl = []
for p in range(persona_co_topic.shape[0]):
if co_topic_graph[i].sum() == 0 or persona_co_topic[p, i].sum() == 0:
persona_kl.append(0)
continue
kl = scipy.stats.entropy(co_topic_graph[i]+1E-5, persona_co_topic[p, i]+1E-5)
persona_kl.append(kl)
max_persona_kl = max(persona_kl)
max_presona = persona_kl.index(max_persona_kl)
max_kl.append(max_persona_kl)
max_pl_p.append(max_presona)
for i, p in enumerate(max_pl_p):
if p == -1:
continue
all_global = co_topic_graph[i]
persona_global = persona_co_topic[p, i]
all_global_cleared, persona_global_cleared = [], []
for all, per in zip(all_global, persona_global):
if all != 0:
all_global_cleared.append(all if all >= 0 else all+1000)
persona_global_cleared.append(per if per >= 0 else per+1000)
show_num = 20
if len(all_global_cleared) < show_num:
continue
all_global_cleared = all_global_cleared[:show_num]
persona_global_cleared = persona_global_cleared[:show_num]
all_global_cleared = [idx for idx, num in enumerate(all_global_cleared) for jdx in range(num)]
persona_global_cleared = [idx for idx, num in enumerate(persona_global_cleared) for jdx in range(num)]
plt.rcParams['axes.facecolor'] = '
ax = sns.displot(all_global_cleared, stat="density", common_norm=False, label='all_global')
ax = sns.displot(persona_global_cleared, stat="density", common_norm=False, label='persona_global')
text_size = 18
legend_elements = [Patch(facecolor='
Patch(facecolor='
]
ax.set(yticklabels=[])
ax.tick_params(left=False)
ax.set(ylabel=None)
plt.show()
print('kl:{:.4f}'.format(max_kl[i]))
a = 1
if __name__ == '__main__':
if not os.path.exists('pic/'):
os.mkdir('pic/')
show_kl('TG-ReDial')