transformer注意力权重系数绘图

参考绘制tsne图,首先将模型中的注意力权重导出,因为我的模型中L=2,所以导出两层

python 复制代码
# plot_weight
weight_model_layer0 = Model(inputs=model.inputs, outputs=model.get_layer('transformer_0').output)
weight_output_layer0 = weight_model_layer0.predict(X_test)
np.save('weight_output_layer0', weight_output_layer0[1])

weight_model_layer1 = Model(inputs=model.inputs, outputs=model.get_layer('transformer_1').output)
weight_output_layer1 = weight_model_layer1.predict(X_test)
np.save('weight_output_layer1', weight_output_layer1[1])

然后,搜到一些使用seaborn绘制热力图的代码,其中我查的比较多的问题是

1、如何修改colorbar字体的大小

2、如何修改xy轴labelsize

3、如何给子图添加标题

就是cbar=False,再重新绘制一个colorbar

参考python使用seaborn画热力图中设置colorbar图例刻度字体大小_seaborn 设置colorbar刻度-CSDN博客

python 复制代码
# 绘制热力图
hm1 = sns.heatmap(attention_per_head_0[0:40, 0:40], cbar=False, cbar_kws={'shrink': 0.8}, square=True, xticklabels='auto', yticklabels='auto')
# 修改xy轴labelsize
hm1.tick_params(labelsize=8)
# 设置标题
hm1.set_title('layer1_head_{}'.format(4), size=12)
# 显示colorbar
cb = hm1.figure.colorbar(hm1.collections[0])  
# 修改colorbar的labelsize
cb.ax.tick_params(labelsize=8)
相关推荐
曲径海盗13 分钟前
【Python 学习第 65 天】字典基础
python·学习笔记·编程入门
Wellijohn22 分钟前
Docker 瘦身、手写 MCP、异步并发:被问得最多的 12 个问题,一次答完
python·docker·mcp
VW_4043 小时前
csdn-2026-11-23-第一站踩坑
爬虫·python
yiangL_4 小时前
课后作业4:基于 Python 的古诗词词云可视化(P205)
开发语言·python
Dmz丶4 小时前
前端转 AI 100 天 Day 52:RAG 项目实战(一)——从零搭建企业知识库
python
高洁016 小时前
数字孪生的冷启动#数字孪生 #人工智能 #大模型 #世界模型
深度学习·机器学习·transformer·迁移学习·tornado
卷无止境8 小时前
用 SimPy 做离散事件仿真:时钟不走步,事件自己排队
后端·python
撸码猿8 小时前
数据采集场景的语序点选验证码:90.2% 整词准确率的本地 OCR 实践
python·ocr
用户0332126663679 小时前
使用 Python 在 Excel 中创建折线图和条形图
python·excel
木子欢儿10 小时前
Ubuntu 26.04 LTS 安装Python 3.12
linux·运维·服务器·python·ubuntu