import torch
import torch.nn as nn
import matplotlib.pyplot as plt
def show_heatmaps(matrices, xlabel, ylabel, titles=None, figsize=(2.5, 2.5), cmap='Reds'):
"""显示矩阵热图"""
num_rows, num_cols = matrices.shape[0], matrices.shape[1]
fig, axes = plt.subplots(num_rows, num_cols, figsize=figsize, sharex=True, sharey=True, squeeze=False)
for i, (row_axes, row_matrices) in enumerate(zip(axes, matrices)):
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 titles:
ax.set_title(titles[j])
fig.colorbar(pcm, ax=axes, shrink=0.6)
q = torch.randn(22, 77)
k = q
v = q
att=q @(k.T)
show_heatmaps(att.reshape(1,1,22,22),'x','y')
这个注意力权重矩阵应该没错吧,,,。文章来源地址https://www.toymoban.com/news/detail-503913.html
文章来源:https://www.toymoban.com/news/detail-503913.html
到了这里,关于注意力权重矩阵可视化的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!