show_heatmaps-未完待续

文章目录

1. 代码

# -*- coding: utf-8 -*-
# @Project: zc
# @Author: ZhangChu
# @File name: show_heatmaps
# @Create time: 2021/11/6 7:30

import torch
from torch import nn
from d2l import torch as d2l
import matplotlib.pyplot as plt



def show_heatmaps(matries, xlabel, ylabel, tiles=None, figsize=(2.5, 2.5), cmap='Reds'):
	"""

	:param matries: matries = [要显示的行数,要显示的列数,查询的数目,键的数目]
	:param xlabel:  X 轴表示的标签含义
	:param ylabel:  Y 轴表示的标签含义
	:param tiles:
	:param figsize:
	:param cmap:
	:return:
	"""
	d2l.use_svg_display()
	num_rows, num_cols = matries.shape[0], matries.shape[1]
	fig, axes = d2l.plt.subplots(num_rows, num_cols, figsize=figsize,
								 sharex=True, sharey=True, squeeze=False)
	for i, (row_axes, row_matrices) in enumerate(zip(axes, matries)):
		for j, (ax, matrix) in enumerate(zip(row_axes, row_matrices)):
			pcm = ax.imshow(matrix.detach().numpy(), cmap=cmap)
			if i == num_rows - 1:
				ax.set_xlabel(xlabel)
			if j == 0:
				ax.set_ylabel(ylabel)
			if tiles:
				ax.set_titles(tiles[j])

	fig.colorbar(pcm, ax=axes, shrink=0.6)


attention_weight = torch.eye(10).reshape((1, 1, 10, 10))
show_heatmaps(attention_weight, xlabel='key', ylabel='Queries')

plt.show()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值