<jupyter_start><jupyter_text>0 Load Attention Weights<jupyter_code>future_index = 'IC'
params = base_io.load_best_params(future_index, 'EncoderGRU_AttnDecoderGRU')
train_data, val_data, test_data = base_io.load_data(future_index)
attention_weights_path = "./attention_weights/IC/EncoderGRU_AttnDecoderGRU/attention_weights.pkl"
attention_weights_map = joblib.load(attention_weights_path)
attention_weights_train = attention_weights_map['训练集']
attention_weights_val = attention_weights_map['验证集']
attention_weights_test = attention_weights_map['测试集']<jupyter_output><empty_output><jupyter_text>1 Convert to DF<jupyter_code>def convert2df(weights_map):
    weights_df = pd.DataFrame()
    for weight in weights_map:
        weight_df = pd.DataFrame(weight)
        weights_df = pd.concat([weights_df, weight_df])
        weights_df.reset_index(inplace=True, drop=True)
    return weights_df
train_attn_weights_df = convert2df(attention_weights_train)
val_attn_weights_df = convert2df(attention_weights_val)
test_attn_weights_df = convert2df(attention_weights_test)
train_attn_weights_df.index = train_data.index[:train_attn_weights_df.shape[0]]
val_attn_weights_df.index = val_data.index[:val_attn_weights_df.shape[0]]
test_attn_weights_df.index = test_data.index[:test_attn_weights_df.shape[0]]<jupyter_output><empty_output><jupyter_text>2 Plot Attention weights per time Bar<jupyter_code>train_attn_weights_mean = train_attn_weights_df.mean()
val_attn_weights_mean = val_attn_weights_df.mean()
test_attn_weights_mean = test_attn_weights_df.mean()
bar_plot_df = pd.DataFrame([train_attn_weights_mean, val_attn_weights_mean, test_attn_weights_mean], index=['训练集', '验证集', '测试集'])
bar_plot_df.columns=[f"t-{params['seq_length'] - i}"  for i in range(params['seq_length'])]
bar_plot_df.index.name='数据集'
bar_plot_df.columns.name='时间点'

plot_df = bar_plot_df.stack().reset_index()
plot_df = plot_df.rename(columns={0:'注意力权重'})
plt.figure(figsize=[14, 6], dpi=100)
sns.set_theme(style="white", context="talk", palette='Set2', rc=plotter.rc_params)
plt.title(f"各数据集 注意力权重均值")
sns.barplot(data=plot_df, x='时间点', y='注意力权重', hue='数据集')
plt.legend(loc='upper right')
plt.subplots_adjust(bottom=0.25, left=0.25)<jupyter_output><empty_output><jupyter_text>3 Plot Attention weights change through time<jupyter_code>def plot_series_apply_func(series):
    plt.plot(series.values, label = f"t-{(params['seq_length']-int(series.name))}")
show_attn_weights_df = test_attn_weights_df.copy(deep=True)[-240:]
plt.figure(figsize=[16,5], dpi=100)
plt.title("Bahdanau Attention 注意力权重", size=15)
show_attn_weights_df.apply(plot_series_apply_func, axis=0)

x_ticks = range(0, len(show_attn_weights_df.index), 59)
plt.xticks(ticks=x_ticks, labels=[show_attn_weights_df.index[i] for i in x_ticks])

plt.xlabel('时间点', size=15)
plt.ylabel('注意力权重', size=15)
plt.legend()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1292564280750521, 'fraction_numerical': 0.015288394718554551, 'mean_word_length': 9.702602230483272, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '600406', 'license': 'Apache-2.0', 'n_tokens_mistral': 1174, 'n_tokens_neox': 1147, 'n_words': 136, 'path': 'explainable_algorithm/Attention_weights_explain.ipynb', 'repo_name': 'AlongZG/XFinAI', 'size': '2878'}