spacy微调BERT-NER模型

数据准备

加载数据集

from tqdm.notebook import tqdm
import os

dataset = []
with open(train_file, 'r') as file:
    for line in tqdm(file.readlines()):
        data = json.loads(line.strip())
        dataset.append(data)

你可以按照 CLUENER 的格式准备训练数据,
例如:

{'text': '胡建新经营着位于深圳市福田区华富街道田面社区深南中路4028号田面城市大厦19B-19C的公司。',
 'label': {'person': {'胡建新': [[0, 2]]},
  'address': {'深圳市福田区华富街道田面社区深南中路4028号田面城市大厦19B-19C': [[8, 43]]}}}

拆分训练集验证集

import random
import numpy as np

def split_train_test_valid(dataset, train_size=0.8, test_size=0.1):
    dataset = np.array(dataset)
    total_size = len(dataset)
    
    # define the ratios
    train_len = int(total_size * train_size)
    test_len = int(total_size * test_size)

    # split the dataframe
    idx = list(range(total_size))
    random.shuffle(idx)  # 将index列表打乱
    data_train = dataset[idx[:train_len]]
    data_test = dataset[idx[train_len:train_len+test_len]]
    data_valid = dataset[idx[train_len+test_len:]]  # 剩下的就是valid
 
    return data_train, data_test, data_valid

data_train, data_test, data_valid = split_train_test_valid(dataset)

转化成 spacy docbin 格式

from spacy.tokens import DocBin
from tqdm import tqdm
from spacy.util import filter_spans

def to_docbin(dataset):
    data = dataset
    data_spacy = []
    for d in tqdm(data):
        text = d['text']
        tags = []
        labels = d['label']
        for label in labels:
            entities = labels[label]
            for entity in entities:
                for loc in entities[entity]:
                    tags.append((loc[0], loc[1]+1, label))
        data_spacy.append({"text":text, "entities": tags})
    
    nlp = spacy.blank('zh')   # 选择中文空白模型
    doc_bin = DocBin()
    for training_example in tqdm(data_spacy):
        text = training_example['text']
        labels = training_example['entities']
        doc = nlp.make_doc(text)
        ents = []
        for start, end, label in labels:
            span = doc.char_span(start, end, label=label, alignment_mode="contract")
            if span is None:
                print("Skipping entity")
            else:
                ents.append(span)
        filtered_ents = filter_spans(ents)
        doc.ents = filtered_ents
        doc_bin.add(doc)

    return doc_bin
    

doc_bin_train = to_docbin(data_train)
doc_bin_train.to_disk("train.spacy")
doc_bin_valid = to_docbin(data_valid)
doc_bin_valid.to_disk("valid.spacy")

训练集和验证集保存到了 train.spacyvalid.spacy

获取spacy训练配置

进入网页:https://spacy.io/usage/training#quickstart
选择Chinese/ner/GPU,自动生成配置文件 base_config.cfg
在这里插入图片描述

自动补全配置

python -m spacy init fill-config base_config.cfg config.cfg

训练模型

python -m spacy train config.cfg --output . --paths.train ./train.spacy --paths.dev ./valid.spacy --gpu-id 0

日志如下:

 python -m spacy train config.cfg --output . --paths.train ./train.spacy --paths.dev ./dev.spacy --gpu-id 0
ℹ Saving to output directory: .
ℹ Using GPU: 0

=========================== Initializing pipeline ===========================
Some weights of the model checkpoint at ../models/bert-base-chinese were not used when initializing BertModel: ['cls.seq_relationship.bias', 'cls.predictions.decoder.weight', 'cls.predictions.transform.LayerNorm.weight', 'cls.predictions.bias', 'cls.predictions.transform.dense.bias', 'cls.seq_relationship.weight', 'cls.predictions.transform.dense.weight', 'cls.predictions.transform.LayerNorm.bias']
- This IS expected if you are initializing BertModel from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).
- This IS NOT expected if you are initializing BertModel from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).
✔ Initialized pipeline

============================= Training pipeline =============================
ℹ Pipeline: ['transformer', 'ner']
ℹ Initial learn rate: 0.0
E    #       LOSS TRANS...  LOSS NER  ENTS_F  ENTS_P  ENTS_R  SCORE 
---  ------  -------------  --------  ------  ------  ------  ------
  0       0        2414.47    804.03    0.41    0.25    1.17    0.00
  0     200      553440.62  100815.50   25.73   27.65   24.06    0.26
  1     400      379529.80  55305.57   36.83   43.31   32.03    0.37
  2     600      164609.24  36629.69   62.07   60.54   63.67    0.62
  3     800      163662.29  38876.53   32.75   42.38   26.69    0.33
  4    1000       81601.30  28677.56   62.02   63.22   60.87    0.62
  5    1200       75558.20  26489.57   61.61   63.17   60.12    0.62
  6    1400       87824.25  25230.27   69.77   69.59   69.95    0.70
  6    1600       54173.95  21436.94   70.03   69.52   70.54    0.70
  7    1800       30978.67  15641.39   71.80   72.03   71.58    0.72
  8    2000       27723.05  13770.74   69.07   69.53   68.62    0.69
  9    2200       25622.08  12936.05   72.89   71.89   73.93    0.73
 10    2400       24126.19  13338.83   71.58   71.96   71.19    0.72
 11    2600       21804.75  11238.43   74.20   74.82   73.60    0.74
 12    2800       20628.26  10916.07   71.44   71.39   71.48    0.71
 13    3000       20134.37  11081.41   72.51   72.17   72.85    0.73
 14    3200       16227.69   8933.84   74.17   73.84   74.51    0.74
 14    3400       19235.74   9438.10   72.00   73.18   70.87    0.72
 15    3600       29307.03  12692.90   74.84   76.13   73.60    0.75
 16    3800       18102.06   8969.09   73.38   71.82   75.00    0.73
 17    4000       14903.23   8416.16   73.11   71.91   74.35    0.73
 18    4200       19608.45   9377.10   72.91   72.67   73.14    0.73
 19    4400       17153.18   8931.95   74.35   74.20   74.51    0.74
 20    4600       17934.71   9112.66   66.37   67.00   65.76    0.66
 20    4800       13376.17   7252.01   74.06   74.29   73.83    0.74
 21    5000       13659.26   6804.46   72.38   71.47   73.31    0.72
 22    5200       18188.32   8358.28   73.57   72.22   74.97    0.74
✔ Saved pipeline to output directory
model-last

验证集 F1 score 达到了 0.75,相比比非transform的模型的 0.65 如下,结果是有明显提升的:

ℹ Saving to output directory: .
ℹ Using GPU: 0

=========================== Initializing pipeline ===========================
✔ Initialized pipeline

============================= Training pipeline =============================
ℹ Pipeline: ['tok2vec', 'ner']
ℹ Initial learn rate: 0.001
E    #       LOSS TOK2VEC  LOSS NER  ENTS_F  ENTS_P  ENTS_R  SCORE 
---  ------  ------------  --------  ------  ------  ------  ------
  0       0          0.00     49.29    0.09    0.15    0.07    0.00
  0     200        496.94   3348.46    5.82    4.36    8.76    0.06
  0     400       1408.31   4107.52    9.38   20.41    6.09    0.09
  0     600       2121.99   5357.34   17.45   23.00   14.06    0.17
  0     800       1096.04   5009.92   19.90   27.89   15.46    0.20
  0    1000        931.30   5447.63   27.72   33.77   23.50    0.28
  0    1200       1375.05   6551.97   32.09   38.83   27.34    0.32
  0    1400       1388.81   7116.59   37.61   43.81   32.94    0.38
  0    1600       2521.46   9638.09   42.25   52.07   35.55    0.42
  1    1800       2172.77  10659.31   40.53   48.04   35.06    0.41
  1    2000       3563.99  12454.60   43.00   49.98   37.73    0.43
  1    2200       4926.80  15747.33   46.38   50.38   42.97    0.46
  2    2400       4712.95  18150.01   48.91   53.97   44.73    0.49
  2    2600       4945.91  18023.03   50.25   53.30   47.53    0.50
  3    2800       6100.79  18400.07   51.21   54.85   48.01    0.51
  3    3000       5124.39  17074.50   51.38   54.62   48.50    0.51
  4    3200       5595.23  17486.11   52.83   57.31   48.99    0.53
  4    3400       5857.02  16183.54   52.39   55.95   49.25    0.52
  5    3600       7097.00  16779.79   55.20   58.97   51.89    0.55
  5    3800       7305.36  16330.97   53.70   56.30   51.33    0.54
  6    4000       6912.16  15848.24   55.86   57.40   54.39    0.56
  6    4200       7083.29  15591.03   54.72   57.02   52.60    0.55
  7    4400       7072.32  14623.82   55.80   61.07   51.37    0.56
  7    4600       9153.78  15341.62   57.24   58.95   55.63    0.57
  8    4800       7584.10  14801.21   54.85   56.26   53.52    0.55
  8    5000       7514.11  14013.45   58.38   61.83   55.31    0.58
  9    5200       9505.86  14416.66   57.41   60.38   54.72    0.57
  9    5400       8458.73  13544.08   58.90   62.29   55.86    0.59
 10    5600       9179.71  12723.23   58.53   60.97   56.28    0.59
 10    5800       9730.11  13078.69   58.85   62.58   55.53    0.59
 11    6000       8485.15  13275.12   59.14   62.02   56.51    0.59
 11    6200      10376.37  12896.16   58.77   60.26   57.36    0.59
 12    6400       8562.07  12582.15   58.59   62.72   54.98    0.59
 12    6600       8131.18  11650.52   59.21   62.55   56.22    0.59
 13    6800      10618.73  11832.74   58.46   60.77   56.32    0.58
 13    7000      10180.18  12106.64   59.16   61.23   57.23    0.59
 14    7200      10455.71  11767.56   62.46   65.60   59.60    0.62
 14    7400      10277.93  11417.25   61.00   61.90   60.12    0.61
 15    7600      10416.83  11844.74   61.50   63.19   59.90    0.61
 15    7800       9843.24  10815.69   60.73   63.61   58.11    0.61
 16    8000      10849.20  11080.88   62.16   65.61   59.05    0.62
 16    8200      12479.84  10464.58   60.54   63.07   58.20    0.61
 16    8400      11960.47  10947.46   63.05   64.79   61.39    0.63
 17    8600      12225.40  10741.32   63.00   64.06   61.98    0.63
 17    8800      11885.81  10653.15   63.88   66.43   61.52    0.64
 18    9000       9813.91   9519.76   62.38   65.15   59.83    0.62
 18    9200      11317.17  10009.74   62.36   65.20   59.77    0.62
 19    9400      11061.72  10646.52   62.66   63.56   61.78    0.63
 19    9600      11708.71   9658.76   62.61   66.30   59.31    0.63
 20    9800      11545.23  10812.54   64.21   65.83   62.66    0.64
 20   10000      12078.46   9654.99   63.09   64.35   61.88    0.63
 21   10200      11745.36   9246.17   61.87   64.31   59.60    0.62
 21   10400      11913.01   9916.31   62.74   64.24   61.30    0.63
 22   10600      11860.46   9340.68   64.30   66.44   62.30    0.64
 22   10800      13450.33   9669.23   63.20   64.48   61.98    0.63
 23   11000      13385.45   9062.81   63.31   65.10   61.62    0.63
 23   11200      13600.88   9135.41   63.88   65.94   61.95    0.64
 24   11400      14294.13   8782.87   63.87   65.69   62.14    0.64
 24   11600      18930.36   9024.00   63.06   64.11   62.04    0.63
 25   11800      14705.22   8806.56   63.40   66.38   60.68    0.63
 25   12000      17361.70   8958.72   64.71   66.28   63.22    0.65
 26   12200      14182.36   8224.55   64.20   66.21   62.30    0.64
 26   12400      15606.35   8725.44   64.23   66.68   61.95    0.64
 27   12600      11960.69   7855.59   64.27   64.61   63.93    0.64
 27   12800      12869.61   8011.05   63.80   66.58   61.23    0.64
 28   13000      13938.21   8064.88   64.14   65.55   62.79    0.64
 28   13200      12936.39   8126.91   65.23   66.64   63.87    0.65
 29   13400      11387.84   7295.93   64.38   64.87   63.90    0.64
 29   13600      15525.57   8512.57   64.52   66.23   62.89    0.65
 30   13800      13474.02   8028.01   65.55   67.37   63.83    0.66
 30   14000      16685.29   7827.30   64.15   64.61   63.70    0.64
 31   14200      15312.08   7759.34   65.53   66.29   64.78    0.66
 31   14400      16065.35   7711.75   64.03   65.93   62.24    0.64
 32   14600      16316.15   7407.74   65.02   66.08   64.00    0.65
 32   14800      16318.76   7667.86   64.97   66.60   63.41    0.65
 33   15000      14086.54   7523.11   64.96   68.17   62.04    0.65
 33   15200      16476.11   7485.34   64.86   67.14   62.73    0.65
 34   15400      16635.40   7954.74   64.90   66.50   63.38    0.65
✔ Saved pipeline to output directory
model-last

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/622150.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

彻底搞定找不到msvcp100.dll,无法继续执行代码的问题

当您在使用电脑过程中遇到程序运行异常,提示“缺失msvcp100.dll文件”时,不必过于焦虑,此问题可通过一系列简单步骤得到有效解决。MSVCP100.dll是Microsoft Visual C库的一部分,主要用于支持某些应用程序运行所需的特定功能。如果…

ohmyzsh的安装过程中失败拒绝连接问题的解决

1.打开官网Oh My Zsh - a delightful & open source framework for Zsh 在官网能看到下面的界面 有这两种自动安装的方式 个人本次选择的是: wget https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh -O - 1.打开终端输入安装的指令 sh -c "$(wget…

(done) 什么是马尔可夫链?Markov Chain

参考视频:https://www.bilibili.com/video/BV1ko4y1P7Zv/?spm_id_from333.337.search-card.all.click&vd_source7a1a0bc74158c6993c7355c5490fc600 如下图所示,马尔可夫链条实际上就是 “状态机”,只不过状态机里不同状态之间的边上是 “…

向银行家应用程序添加日期

● 首先我们将下面图片上的时间更换成现在的时间 const now new Date(); const day now.getDate(); const month now.getMonth() 1; const year now.getFullYear(); const hour now.getHours(); const min now.getMinutes();labelDate.textContent ${day}/${month}/$…

数据降维-主成分分析PCA

1.背景: 在以前计算能力还很弱的年代,我们要分析经济数据是一件很困难的事情,所以我们需要对指标特征进行降维; 2.数据降维的意义: 一般我们降维的特征数据彼此之间是存在一定的相关性的, 二维降至一维…

车载电子电器架构 —— Vector对于车载以太网的解决方案(协议栈)

车载电子电器架构 —— Vector对于车载以太网的解决方案(协议栈) 我是穿拖鞋的汉子,魔都中坚持长期主义的汽车电子工程师。 老规矩,分享一段喜欢的文字,避免自己成为高知识低文化的工程师: 屏蔽力是信息过载时代一个人的特殊竞争力,任何消耗你的人和事,多看一眼都是你…

252 基于MATLAB的自适应差分阈值法检测心电信号的QRS波

基于MATLAB的自适应差分阈值法检测心电信号的QRS波,QRS波群反映左、右心室除极电位和时间的变化,第一个向下的波为Q波,向上的波为R波,接着向下的波是S波。通过GUI进行数据处理,展示心率和QRS。程序已调通,可…

COMSOL粗略估算计算时间

COMSOL粗略估算模型计算时间 针对反复修改调试的有限元模型,需要大致估算有限元模型的计算时间。经查阅,模型求解的自由度数和求解时间密切相关。 测试条件 测试模型为声-固耦合模型,电脑内存32G,CPU-i7-10700K,核显…

【GD32】03 - EXTI外部中断

EXTI EXTI,全称External Interrupt/Event Controller,即外部中断/事件控制器,是微控制器中的一个重要组成部分。它主要用于管理来自外部设备的中断和事件请求。以下是关于EXTI的详细介绍: 功能概述: EXTI管理了控制器的…

OpenAI 人工智能搜索产品即将推出,文本和图像都支持?!

OpenAI 人工智能搜索产品即将推出 OpenAI 计划于下周一(5 月 13 日)正式公布其人工智能搜索产品,不过报道中强调具体公告日期可能发生变化。OpenAI 拒绝对路透社的报道置评。外媒 The Information 在今年 2 月的报道中指出,OpenAI 一直在秘密开发其自家网络搜索服务,并将获…

C++初阶学习第七弹——探索STL奥秘(二)——string的模拟实现

标准库中的string:C初阶学习第六弹——string(1)——标准库中的string类-CSDN博客 前言: 在前面我们已经学习了如何使用标准库中的string类,但作为一个合格的程序员,我们不仅要会用,还要知道如…

Midjourney Imagine API 申请及使用

Midjourney Imagine API 申请及使用 申请流程 要使用 Midjourney Imagine API,首先可以到 Midjourney Imagine API 页面点击「Acquire」按钮,获取请求所需要的凭证: 如果你尚未登录或注册,会自动跳转到登录页面邀请您来注册和登…

[嵌入式系统-77]:RT-Thread-快速上手:嵌入式系统调测工具大全

目录 1. JTAG 下载调试器: 2. J-Link 仿真器: 3. ICE(In-Circuit Emulator): 4. ROM监视器(ROM Monitor): 5. 终端仿真工具: 6. 总线抓取工具: 7. 静态…

【教学类-55-02】20240512图层顺序挑战(四格长条纸加黑色边框、4*4、7张 、43200张去掉非7色有23040张,去掉重复样式有几种?)

作品展示 背景需求: 之前的代码吗存在几个问题,最大的问题是不能生成“”长条黑边框”” 【教学类-55-01】20240511图层顺序挑战(四格长条纸)(4*4)和“手工纸自制参考图”-CSDN博客文章浏览阅读485次&…

2024第八季完美童模 【星光】品牌赛区 【直通】赛 完美收官

2024年5月1日,春风徐徐的【星光品牌赛区】热闹非凡,备受瞩目的第八季完美童模【星光品牌赛区】赛区【直通赛】在这一天正式拉开了帷幕。比赛现场,童模们身着华服,在舞台上演绎了“亚特兰蒂斯”的时尚主题赛。 参赛选手们身着带有海…

【优先级队列】Leetcode 最后一块石头的重量

题目讲解 1046. 最后一块石头的重量 算法讲解 根据题目的意思,为了寻找到本次数组中的最大的两个值,我们需要使用一个数据结构:堆,使用大堆,每一次出两个数据,这两个数据就是当前数组中的两个最大值&…

拉链表实现过程+案例

第一种 1.从ODS层获取增量数据(上一天新增和更新的数据) 2.拿着DWD原始拉链表数据 left join 增量数据 ,修改原始拉链中历史数据的结束时间 3.拿着left join 的结果集 union all 增量数据 4.把最新的拉链数据优先保存到DWD对应的临时表中 5.使用insertselect 方式把临时表中…

力扣75. 颜色分类

Problem: 75. 颜色分类 文章目录 题目描述思路及解法复杂度Code 题目描述 思路及解法 由于题目只提供0,1,2分别代表颜色红、白、蓝,并按此排序,那么我们可以遍历两次数组,第一次将0,全部放到数组前面一部分…

Python如何绘制直流电机开闭环特性曲线?matplotlib

import matplotlib.pyplot as plt from pylab import mplmpl.rcParams[font.sans-serif] [FangSong] # 指定默认字体 mpl.rcParams[axes.unicode_minus] False # 解决保存图像是负号-显示为方块的问题# 数据集1 n1 [1206, 1174, 1141, 1116, 1037, 986] Id1 [0.505, 0.55…

GEVernova推出GEV新能源平台,引领新能源未来

近日,全球领先的能源设备制造和服务公司 GE Vernova 宣布推出 GEV 新能源平台,这是一个将金融、科技和产业深度融合的全新投资平台。GEV 新能源平台旨在为用户提供一站式可持续新能源投资解决方案,助力全球新能源转型和可持续发展。 新能源已…