python的tril,pad,block_diag在LLM上的使用

最近需要对position_ids和attention_mask进行重构,所以需要掌握numpy的一些操作,以下是一些示例,

多个下三角矩阵拼接:

import numpy as np
from scipy.linalg import block_diag

A = np.ones((2,2))
B = np.ones((3,3)) 

b = [A,B]
print(np.tril(block_diag(*b)))
[[1. 0. 0. 0. 0.]
 [1. 1. 0. 0. 0.]
 [0. 0. 1. 0. 0.]
 [0. 0. 1. 1. 0.]
 [0. 0. 1. 1. 1.]]

二维的position_ids的padding:

encoded_inputs ={}
encoded_inputs["position_ids"] =np.array([[1,2,3,4],[4,5,6,7]])
difference = 4
encoded_inputs["position_ids"] = np.pad(
                    encoded_inputs["position_ids"], pad_width=[(0, 0), (difference, 0)]
                )

print(encoded_inputs)
{'position_ids': array([[0, 0, 0, 0, 1, 2, 3, 4],
       [0, 0, 0, 0, 4, 5, 6, 7]])}

attention_mask的拼接:

encoded_inputs["attention_mask"] = np.zeros((1,3,3))
encoded_inputs["attention_mask"] = np.pad(
                    encoded_inputs["attention_mask"],
                    pad_width=[(0, 0), (difference, 0), (difference, 0)],
                    mode="constant",
                    constant_values=1,
                )
print(encoded_inputs)
{'position_ids': array([[0, 0, 0, 0, 1, 2, 3, 4],
       [0, 0, 0, 0, 4, 5, 6, 7]]), 'attention_mask': array([[[1., 1., 1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 0., 0., 0.],
        [1., 1., 1., 1., 0., 0., 0.],
        [1., 1., 1., 1., 0., 0., 0.]]])}

list里面多个list进行拼接:

inputs =[[1,2],[0,1]]
# inputs =[[[1,2],[0,1]],[[1,2,3,4],[4,5,6,7]]]

# out = sum(inputs,[[]])
out = np.concatenate(inputs, axis=-1)
print(out)
[1 2 0 1]

chatglm里面的attention_mask的创建:

seq_length = 4
context_length=2
attention_mask = np.ones((seq_length, seq_length))
attention_mask = np.tril(attention_mask)
attention_mask[:, :context_length] = 1
attention_mask = (attention_mask < 0.5).astype("int64")
print(attention_mask)
[[0 0 1 1]
 [0 0 1 1]
 [0 0 0 1]
 [0 0 0 0]]

我发现LLM的输入里面attention_mask,position_ids的构造会不一样,其他的都还好,所以这里分享出来,与大家共同进步。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
这段代码使用了Seaborn库绘制了一个热力图来展示数据的相关性。让我来解释一下您的代码: - `import seaborn as sns`:导入Seaborn库。 - `sns.set()`:设置Seaborn的默认样式。 - `%matplotlib inline`:在Jupyter Notebook中设置matplotlib图形的显示方式为内联模式,以便图形能够直接显示在Notebook中。 - `%config InlineBack.figure_format="retina"`:设置图形显示的分辨率为retina,以提高图像的清晰度。 接下来,使用`data[["Income Classification","Survey Sample (N)","Overweight","Stunting","Underweight",]].corr()`计算了数据集中指定列之间的相关系数,并将结果存储在名为`correlation`的DataFrame中。 然后,创建了一个名为`mask`的数组,用于掩盖热力图中的下三角部分。并使用`np.tril_indices_from(mask)`函数获取下三角部分的索引,并将这些索引对应的元素设置为False。 接着,创建了一个图形对象(`fig`)和一个坐标轴对象(`ax`),并使用`fig.set_size_inches(20,10)`设置图形的尺寸为20x10英寸。 然后,使用`sns.heatmap`函数绘制热力图。该函数的参数说明如下: - `correlation`:指定要绘制热力图的数据。 - `mask=mask`:指定要使用的掩码数组。 - `vmax=.8`:设置热力图的颜色映射的最大值为0.8。 - `square=True`:设置热力图的每个单元格为正方形。 - `annot=True`:在热力图中显示每个单元格的数值。 最后,使用`plt.show()`显示图形。 请确保在运行这段代码之前已经导入了必要的库,并且已经正确加载了数据。如果遇到任何错误,请提供详细的错误消息以便我能够更好地帮助您解决问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

农民小飞侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值