week8

目标:

在另外两个文本匹配数据集试验不同方法效果。

解读:

在已有 AFQMC 文本匹配实验基础上,保持模型、超参数和训练流程基本不变,将相同的 3 种方法迁移到 LCQMC 和 BQ Corpus 两个数据集上,比较不同文本匹配方法在不同数据分布下的效果。

本次实验的方法:

内容:

python 复制代码
pip install torch transformers scikit-learn pandas tqdm

1. BiEncoder
import torch
import torch.nn as nn
import torch.nn.functional as F

from transformers import AutoModel


class BiEncoder(nn.Module):

    def __init__(
        self,
        model_name="bert-base-chinese"
    ):
        super().__init__()

        self.bert = AutoModel.from_pretrained(
            model_name
        )

    def mean_pooling(
        self,
        hidden,
        attention_mask
    ):
        mask = attention_mask.unsqueeze(-1)

        hidden = hidden * mask

        return hidden.sum(dim=1) / mask.sum(
            dim=1
        ).clamp(min=1e-9)

    def encode(
        self,
        input_ids,
        attention_mask
    ):

        output = self.bert(
            input_ids=input_ids,
            attention_mask=attention_mask
        )

        embedding = self.mean_pooling(
            output.last_hidden_state,
            attention_mask
        )

        # L2 Normalization
        embedding = F.normalize(
            embedding,
            p=2,
            dim=1
        )

        return embedding

    def forward(
        self,
        input_ids_a,
        attention_mask_a,
        input_ids_b,
        attention_mask_b
    ):

        va = self.encode(
            input_ids_a,
            attention_mask_a
        )

        vb = self.encode(
            input_ids_b,
            attention_mask_b
        )

        return va, vb



2. CrossEncoder
import torch.nn as nn

from transformers import AutoModel


class CrossEncoder(nn.Module):

    def __init__(
        self,
        model_name="bert-base-chinese"
    ):
        super().__init__()

        self.bert = AutoModel.from_pretrained(
            model_name
        )

        hidden_size = (
            self.bert.config.hidden_size
        )

        self.dropout = nn.Dropout(0.1)

        self.classifier = nn.Linear(
            hidden_size,
            2
        )

    def forward(
        self,
        input_ids,
        attention_mask,
        token_type_ids=None
    ):

        output = self.bert(
            input_ids=input_ids,
            attention_mask=attention_mask,
            token_type_ids=token_type_ids
        )

        cls = output.last_hidden_state[:, 0]

        cls = self.dropout(cls)

        logits = self.classifier(cls)

        return logits
相关推荐
Miya_Ye10 小时前
AI工具 Phoenix
ai·工具
吴名氏.11 小时前
Java转AI Agent教程(第十三周)混合检索与重排
java·数据库·人工智能·ai·ai agent
七夜zippoe14 小时前
Agent 无限循环烧 Token:死循环检测与中断机制设计
ai·agent·token·死循环·中断机制
AI算力16 小时前
私有云时代来临:AI NAS如何重塑你的数字生活?
人工智能·科技·ai·生活·nas·ai算力·ainas
dozenyaoyida20 小时前
AI与大模型新闻日报 | 2026-11-12
人工智能·ai·大模型·新闻
一心同学1 天前
Hermes架构拆解之Profiles
ai·架构·agent·profiles·hermes
XLYcmy1 天前
ASR识别任务两篇论文阅读分析与系统方案技术报告 上
论文阅读·ai·gpu·asr·论文分享·speechbrain·wespeaker
七夜zippoe2 天前
Agent 上线第一天就炸了:6 个生产事故的根因分析与修复记录
大数据·ai·agent·根因分析·修复记录
秋饼2 天前
GPT-Live 全双工语音 API Java 服务端落地实战:事件协议、会话恢复与生产踩坑
java·ai·技术分享·后端开发
bst@微胖子2 天前
SpringAI快速入门(7) RAG 实战(三):检索增强配置与重排序实战
ai