• 大小: 6.17MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-11-07
  • 语言: 其他
  • 标签: bert  nlp  pytorch  

资源简介

完整的bert模型源代码,对代码做了很多注释和精简,以中文文本分类为例的一个deom,可以拿来就用,把代码稍微改改就可用在你的任务中。

资源截图

代码片段和文件信息

# coding: UTF-8
import time
import torch
import numpy as np
from train_eval import train init_network
from importlib import import_module
import argparse
from utils import build_dataset build_iterator get_time_dif

parser = argparse.ArgumentParser(description=‘Chinese Text Classification‘)
parser.add_argument(‘--model‘ type=str required=True help=‘choose a model: Bert ERNIE‘)
args = parser.parse_args()


if __name__ == ‘__main__‘:
    dataset = ‘THUCNews‘  # 数据集

    model_name = args.model  # bert
    x = import_module(‘models.‘ + model_name)
    config = x.Config(dataset)
    np.random.seed(1)
    torch.manual_seed(1)
    torch.cuda.manual_seed_all(1)
    torch.backends.cudnn.deterministic = True  # 保证每次结果一样

    start_time = time.time()
    print(“Loading data...“)
    train_data dev_data test_data = build_dataset(config)
    train_iter = build_iterator(train_data config)
    dev_iter = build_iterator(dev_data config)
    test_iter = build_iterator(test_data config)
    time_dif = get_time_dif(start_time)
    print(“Time usage:“ time_dif)

    # train
    model = x.Model(config).to(config.device)
    train(config model train_iter dev_iter test_iter)

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2019-10-26 09:17  Bert-Chinese-Text-Classification-Pytorch-master\
     目录           0  2019-10-26 09:17  Bert-Chinese-Text-Classification-Pytorch-master\ERNIE_pretrain\
     文件         151  2019-10-26 09:17  Bert-Chinese-Text-Classification-Pytorch-master\ERNIE_pretrain\README.md
     文件        1066  2019-10-26 09:17  Bert-Chinese-Text-Classification-Pytorch-master\LICENSE
     文件        3130  2019-10-26 09:17  Bert-Chinese-Text-Classification-Pytorch-master\README.md
     目录           0  2019-10-26 09:17  Bert-Chinese-Text-Classification-Pytorch-master\THUCNews\
     目录           0  2019-10-26 09:17  Bert-Chinese-Text-Classification-Pytorch-master\THUCNews\data\
     文件          82  2019-10-26 09:17  Bert-Chinese-Text-Classification-Pytorch-master\THUCNews\data\class.txt
     文件      551313  2019-10-26 09:17  Bert-Chinese-Text-Classification-Pytorch-master\THUCNews\data\dev.txt
     文件      551596  2019-10-26 09:17  Bert-Chinese-Text-Classification-Pytorch-master\THUCNews\data\test.txt
     文件     9946122  2019-10-26 09:17  Bert-Chinese-Text-Classification-Pytorch-master\THUCNews\data\train.txt
     目录           0  2019-10-26 09:17  Bert-Chinese-Text-Classification-Pytorch-master\bert_pretrain\
     文件         191  2019-10-26 09:17  Bert-Chinese-Text-Classification-Pytorch-master\bert_pretrain\README.md
     目录           0  2019-10-26 09:17  Bert-Chinese-Text-Classification-Pytorch-master\models\
     文件        2346  2019-10-26 09:17  Bert-Chinese-Text-Classification-Pytorch-master\models\ERNIE.py
     文件        2315  2019-10-26 09:17  Bert-Chinese-Text-Classification-Pytorch-master\models\bert.py
     文件        3036  2019-10-26 09:17  Bert-Chinese-Text-Classification-Pytorch-master\models\bert_CNN.py
     文件        3776  2019-10-26 09:17  Bert-Chinese-Text-Classification-Pytorch-master\models\bert_DPCNN.py
     文件        3014  2019-10-26 09:17  Bert-Chinese-Text-Classification-Pytorch-master\models\bert_RCNN.py
     文件        2928  2019-10-26 09:17  Bert-Chinese-Text-Classification-Pytorch-master\models\bert_RNN.py
     目录           0  2019-10-26 09:17  Bert-Chinese-Text-Classification-Pytorch-master\pytorch_pretrained\
     文件        1313  2019-10-26 09:17  Bert-Chinese-Text-Classification-Pytorch-master\pytorch_pretrained\__init__.py
     文件        4393  2019-10-26 09:17  Bert-Chinese-Text-Classification-Pytorch-master\pytorch_pretrained\__main__.py
     目录           0  2019-10-26 09:17  Bert-Chinese-Text-Classification-Pytorch-master\pytorch_pretrained\__pycache__\
     文件        1606  2019-10-26 09:17  Bert-Chinese-Text-Classification-Pytorch-master\pytorch_pretrained\__pycache__\__init__.cpython-37.pyc
     文件        2365  2019-10-26 09:17  Bert-Chinese-Text-Classification-Pytorch-master\pytorch_pretrained\__pycache__\__main__.cpython-37.pyc
     文件        1763  2019-10-26 09:17  Bert-Chinese-Text-Classification-Pytorch-master\pytorch_pretrained\__pycache__\convert_gpt2_checkpoint_to_pytorch.cpython-37.pyc
     文件        1807  2019-10-26 09:17  Bert-Chinese-Text-Classification-Pytorch-master\pytorch_pretrained\__pycache__\convert_openai_checkpoint_to_pytorch.cpython-37.pyc
     文件        1564  2019-10-26 09:17  Bert-Chinese-Text-Classification-Pytorch-master\pytorch_pretrained\__pycache__\convert_tf_checkpoint_to_pytorch.cpython-37.pyc
     文件        3045  2019-10-26 09:17  Bert-Chinese-Text-Classification-Pytorch-master\pytorch_pretrained\__pycache__\convert_transfo_xl_checkpoint_to_pytorch.cpython-37.pyc
     文件        7230  2019-10-26 09:17  Bert-Chinese-Text-Classification-Pytorch-master\pytorch_pretrained\__pycache__\file_utils.cpython-37.pyc
............此处省略31个文件信息

评论

共有 条评论