1-问题描述
特征向量存储格式为numpy数组,将该numpy数组与Bart模型的输入拼接在一起。
2-问题解决
首先需要将numpy数组转换为pytorch张量,然后通过pytorch的torch.cat()函数将特征向量与Bart模型的输入进行拼接。
3-具体步骤
-
加载库函数
import torch import numpy as np from transformers import BartTokenizer, BartModel
加载中文Bart模型需要导入的库函数与英文不同,可到Hugging Face官网搜索bart chinese查看使用方法
-
获取特征向量
sememe_features_train = np.load('./data/train_SATvec.npz') # 加载通过义原模型进行训练的特征向量 sememe_features_dev = np.load('./data/dev_SATvec.npz') # 加载通过义原模型进行验证的特征向量
训练义原特征向量的方法 paper
-
重写forward函数
def forward(self, input_ids, attention_mask=None, decoder_input_ids=None, labels=None, step=None, mode=None): outputs = self.bart( input_ids, attention_mask=attention_mask, decoder_input_ids=decoder_input_ids, ) lm_logits = outputs[0] loss_compute_dim = self.loss_compute_dim if self.args.use_feature: device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') fusion_embedding = lm_logits num_vectors = len(input_ids) if mode == 'train': sememe_batch = [] if self.args.use_sememe: for i in range(num_vectors): key = str(i * step + i) sememe = sememe_train[key][:511] sememe = torch.from_numpy(sememe).to(torch.float32) sememe_batch.append(sememe) sememe_batch = torch.stack(sememe_batch, dim=0) sememe_batch = sememe_batch.unsqueeze(2).to(device) fusion_embedding = torch.cat([fusion_embedding, sememe_batch], dim=2) elif mode == 'dev': sememe_batch = [] if self.args.use_sememe: for i in range(num_vectors): key = str(i * step + i) sememe = sememe_dev[key][:511] sememe = torch.from_numpy(sememe).to(torch.float32) sememe_batch.append(sememe) sememe_batch = torch.stack(sememe_batch, dim=0) sememe_batch = sememe_batch.unsqueeze(2).to(device) fusion_embedding = torch.cat([fusion_embedding, sememe_batch], dim=2) return [fusion_embedding, labels, loss_compute_dim] else: return [lm_logits, labels, loss_compute_dim]