kaggle笔记:对推理函数inference(df, model, tokenizer, device)的分析

代码:

def inference(df, model, tokenizer, device):
    batch_size = 64
    max_length = 512
    sentences = list(df['query_text'].values) # 问题列表

    all_embeddings = []
    # 对句子列表按长度降序排序,以优化内存使用和处理速度。
    length_sorted_idx = np.argsort([-len(sen) for sen in sentences])
    sentences_sorted = [sentences[idx] for idx in length_sorted_idx]
    for start_index in trange(0, len(sentences), batch_size, desc="Batches", disable=False):
        sentences_batch = sentences_sorted[start_index: start_index + batch_size]
        features = tokenizer(sentences_batch, max_length=max_length, padding=True, truncation=True,
                             return_tensors="pt")
        features = batch_to_device(features, device)
        with torch.no_grad(): # 在不计算梯度的情况下运行模型,以节省内存和计算资源。
        # 使用双星号 ** 来传递参数是一种常见的做法,它允许你将字典中的键值对作为关键字参数传递给函数
            outputs = model.model
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值