<p align="center">
<img src="test/images/macadam_logo.png" width="480"\>
</p>
# [Macadam](https://github.com/yongzhuo/Macadam)
[![PyPI](https://img.shields.io/pypi/v/Macadam)](https://pypi.org/project/Macadam/)
[![Build Status](https://travis-ci.com/yongzhuo/Macadam.svg?branch=master)](https://travis-ci.com/yongzhuo/Macadam)
[![PyPI_downloads](https://img.shields.io/pypi/dm/Macadam)](https://pypi.org/project/Macadam/)
[![Stars](https://img.shields.io/github/stars/yongzhuo/Macadam?style=social)](https://github.com/yongzhuo/Macadam/stargazers)
[![Forks](https://img.shields.io/github/forks/yongzhuo/Macadam.svg?style=social)](https://github.com/yongzhuo/Macadam/network/members)
[![Join the chat at https://gitter.im/yongzhuo/Macadam](https://badges.gitter.im/yongzhuo/Macadam.svg)](https://gitter.im/yongzhuo/Macadam?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
>>> Macadam是一个以Tensorflow(Keras)和bert4keras为基础,专注于文本分类、序列标注和关系抽取的自然语言处理工具包。支持RANDOM、WORD2VEC、FASTTEXT、BERT、ALBERT、ROBERTA、NEZHA、XLNET、ELECTRA、GPT-2等EMBEDDING嵌入;
支持FineTune、FastText、TextCNN、CharCNN、BiRNN、RCNN、DCNN、CRNN、DeepMoji、SelfAttention、HAN、Capsule等文本分类算法;
支持CRF、Bi-LSTM-CRF、CNN-LSTM、DGCNN、Bi-LSTM-LAN、Lattice-LSTM-Batch、MRC等序列标注算法。
## 目录
* [安装](#安装)
* [数据](#数据)
* [使用方式](#使用方式)
* [TODO](#TODO)
* [paper](#paper)
* [参考](#参考)
# 安装
```bash
pip install Macadam
# 清华镜像源
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple Macadam
```
# 数据
## 数据来源
* [ner_clue_2020](https://github.com/CLUEbenchmark/CLUENER2020), CLUENER2020中文细粒度命名实体识别
* [ner_people_1998](http://www.icl.pku.edu.cn/icl_res/), 《人民日报》标注语料库中的语料, 1998.01
* [baidu_qa_2019](https://github.com/liuhuanyong/MiningZhiDaoQACorpus), 百度知道问答语料
* [thucnews](http://thuctc.thunlp.org/), 新浪新闻RSS订阅频道2005-2011年间的历史数据筛
## 数据格式
```
1. 文本分类 (txt格式, 每行为一个json):
{"x": {"text": "人站在地球上为什么没有头朝下的感觉", "texts2": []}, "y": "教育"}
{"x": {"text": "我的小baby", "texts2": []}, "y": ["娱乐"]}
{"x": {"text": "请问这起交通事故是谁的责任居多小车和摩托车发生事故在无红绿灯", "texts2": []}, "y": "娱乐"}
2. 序列标注 (txt格式, 每行为一个json):
{"x": {"text": "海钓比赛地点在厦门与金门之间的海域。", "texts2": []}, "y": ["O", "O", "O", "O", "O", "O", "O", "B-LOC", "I-LOC", "O", "B-LOC", "I-LOC", "O", "O", "O", "O", "O", "O"]}
{"x": {"text": "参加步行的有男有女,有年轻人,也有中年人。", "texts2": []}, "y": ["O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O"]}
{"x": {"text": "山是稳重的,这是我最初的观念。", "texts2": []}, "y": ["O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O"]}
{"x": {"text": "立案不结、以罚代刑等问题有较大改观。", "texts2": []}, "y": ["O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O"]}
```
# 使用方式
更多样例sample详情见test目录
## 文本分类, text-classification
```bash
# !/usr/bin/python
# -*- coding: utf-8 -*-
# @time : 2020/5/8 21:33
# @author : Mo
# @function: test trainer of bert
# 适配linux
import sys
import os
path_root = os.path.abspath(os.path.join(os.path.dirname(__file__), "../.."))
sys.path.append(path_root)
# cpu-gpu与tf.keras
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
os.environ["TF_KERAS"] = "1"
# macadam
from macadam.conf.path_config import path_root, path_tc_baidu_qa_2019, path_tc_thucnews
from macadam.tc import trainer
if __name__=="__main__":
# bert-embedding地址, 必传
path_embed = "D:/soft_install/dataset/bert-model/chinese_L-12_H-768_A-12"
path_checkpoint = path_embed + "/bert_model.ckpt"
path_config = path_embed + "/bert_config.json"
path_vocab = path_embed + "/vocab.txt"
# 训练/验证数据地址, 必传
# path_train = os.path.join(path_tc_thucnews, "train.json")
# path_dev = os.path.join(path_tc_thucnews, "dev.json")
path_train = os.path.join(path_tc_baidu_qa_2019, "train.json")
path_dev = os.path.join(path_tc_baidu_qa_2019, "dev.json")
# 网络结构, 嵌入模型, 大小写都可以, 必传
# 网络模型架构(Graph), "FineTune", "FastText", "TextCNN", "CharCNN",
# "BiRNN", "RCNN", "DCNN", "CRNN", "DeepMoji", "SelfAttention", "HAN", "Capsule"
network_type = "TextCNN"
# 嵌入(embedding)类型, "ROBERTA", "ELECTRA", "RANDOM", "ALBERT", "XLNET", "NEZHA", "GPT2", "WORD", "BERT"
embed_type = "BERT"
# token级别, 一般为"char", 只有random和word的embedding时存在"word"
token_type = "CHAR"
# 任务, "TC"(文本分类), "SL"(序列标注), "RE"(关系抽取)
task = "TC"
# 模型保存目录, 必传
path_model_dir = os.path.join(path_root, "data", "model", network_type)
# 开始训练, 可能前几轮loss较大acc较低, 后边会好起来
trainer(path_model_dir, path_embed, path_train, path_dev, path_checkpoint, path_config, path_vocab,
network_type=network_type, embed_type=embed_type, token_type=token_type, task=task)
mm = 0
```
## 序列标注, sequence-labeling
```bash
# !/usr/bin/python
# -*- coding: utf-8 -*-
# @time : 2020/5/8 21:33
# @author : Mo
# @function: test trainer of bert
# 适配linux
import sys
import os
path_root = os.path.abspath(os.path.join(os.path.dirname(__file__), "../.."))
sys.path.append(path_root)
## cpu-gpu与tf.keras
# os.environ["CUDA_VISIBLE_DEVICES"] = "0"
os.environ["TF_KERAS"] = "1"
# 地址, tf.keras
from macadam.conf.path_config import path_embed_bert, path_embed_word2vec_word, path_embed_word2vec_char
from macadam.conf.path_config import path_root, path_ner_people_1998, path_ner_clue_2020
from macadam.sl import trainer
if __name__=="__main__":
# bert-embedding地址, 必传
path_embed = path_embed_bert # path_embed_bert, path_embed_word2vec_word, path_embed_word2vec_char
path_checkpoint = os.path.join(path_embed + "bert_model.ckpt")
path_config = os.path.join(path_embed + "bert_config.json")
path_vocab = os.path.join(path_embed + "vocab.txt")
# # 训练/验证数据地址
# path_train = os.path.join(path_ner_people_1998, "train.json")
# path_dev = os.path.join(path_ner_people_1998, "dev.json")
path_train = os.path.join(path_ner_clue_2020, "ner_clue_2020.train")
path_dev = os.path.join(path_ner_clue_2020, "ner_clue_2020.dev")
# 网络结构
# "CRF", "Bi-LSTM-CRF", "Bi-LSTM-LAN", "CNN-LSTM", "DGCNN", "LATTICE-LSTM-BATCH"
network_type = "CRF"
# 嵌入(embedding)类型, "ROOBERTA", "ELECTRA", "RANDOM", "ALBERT", "XLNET", "NEZHA", "GPT2", "WORD", "BERT"
# MIX, WC_LSTM时候填两个["RANDOM", "WORD"], ["WORD", "RANDOM"], ["RANDOM", "RANDOM"], ["WORD", "WORD"]
embed_type = "RANDOM"
token_type = "CHAR"
task = "SL"
lr = 1e-5 if embed_type in ["ROBERTA", "ELECTRA", "ALBERT", "XLNET", "NEZHA", "GPT2", "BERT"] else 1e-3
# 模型保存目录, 如果不存在则创建
path_model_dir = os.path.join(path_root, "data", "model", network_type)
if not os.path.exists(path_model_dir):
os.mkdir(path_model_dir)
# 开始训练
trainer(path_model_dir, path_embed, path_train, path_dev,
path_checkpoint, path_config, path_vocab,
network_type=network_type, embed_type=embed_type,
task=task, token_type=token_type,
is_length_max=False, use_onehot=False, use_file=False, use_crf=True,
layer_idx=[-2], learning_rate=lr,
batch_size=30, epochs=12, early_stop=6, rate=1)
mm = 0
```
# TODO
* 文本分�
没有合适的资源?快使用搜索试试~ 我知道了~
Macadam是一个以Tensorflow和bert4keras为基础,专注于文本分类、序列标注和关系抽取的自然语言处理工具包
共108个文件
py:83个
txt:7个
json:7个
需积分: 5 0 下载量 154 浏览量
2024-05-11
20:49:56
上传
评论
收藏 1001KB ZIP 举报
温馨提示
GPT, 通常指的是“Generative Pre-trained Transformer”(生成式预训练转换器),是一个在自然语言处理(NLP)领域非常流行的深度学习模型架构。GPT模型由OpenAI公司开发,并在多个NLP任务上取得了显著的性能提升。 GPT模型的核心是一个多层Transformer解码器结构,它通过在海量的文本数据上进行预训练来学习语言的规律。这种预训练方式使得GPT模型能够捕捉到丰富的上下文信息,并生成流畅、自然的文本。 GPT模型的训练过程可以分为两个阶段: 预训练阶段:在这个阶段,模型会接触到大量的文本数据,并通过无监督学习的方式学习语言的结构和规律。具体来说,模型会尝试预测文本序列中的下一个词或短语,从而学习到语言的语法、语义和上下文信息。 微调阶段(也称为下游任务训练):在预训练完成后,模型会被应用到具体的NLP任务中,如文本分类、机器翻译、问答系统等。在这个阶段,模型会使用有标签的数据进行微调,以适应特定任务的需求。通过微调,模型能够学习到与任务相关的特定知识,并进一步提高在该任务上的性能。 GPT模型的优势在于其强大的生成能力和对上下文信息的捕捉能力。这使得GPT模型在自然语言生成、文本摘要、对话系统等领域具有广泛的应用前景。同时,GPT模型也面临一些挑战,如计算资源消耗大、训练时间长等问题。为了解决这些问题,研究人员不断提出新的优化方法和扩展模型架构,如GPT-2、GPT-3等,以进一步提高模型的性能和效率。
资源推荐
资源详情
资源评论
收起资源包目录
Macadam是一个以Tensorflow和bert4keras为基础,专注于文本分类、序列标注和关系抽取的自然语言处理工具包 (108个子文件)
events.out.tfevents.1591285256.DESKTOP-L9RRESF 5.32MB
ner_clue_2020.dev 13KB
.gitignore 2KB
macadam.info 553KB
train.json 30KB
test.json 25KB
dev.json 15KB
train.json 12KB
dev.json 11KB
train.json 11KB
dev.json 10KB
LICENSE 1KB
README.md 12KB
README.md 113B
macadam_logo.png 115KB
events.out.tfevents.1591285307.DESKTOP-L9RRESF.profile-empty 40B
embedding.py 59KB
preprocess.py 35KB
s00_trainer.py 18KB
layers.py 14KB
s00_predict.py 14KB
utils.py 13KB
tet_preprocess_xy.py 12KB
t00_trainer.py 11KB
constant_params.py 11KB
tet_mix.py 10KB
t00_predict.py 8KB
tet_all_baidu_2019.py 7KB
graph.py 6KB
tet_embed+.py 5KB
tet_bert4keras.py 5KB
s02_bilstm_crf.py 5KB
tet_graph.py 4KB
embedding_visible.py 4KB
s04_dgcnn.py 4KB
s06_lattice_lstm_batch.py 3KB
s03_cnn_lstm.py 3KB
tet_tokenizer.py 3KB
t06_rcnn.py 3KB
s05_bilstm_lan.py 3KB
tet_word2vec.py 3KB
t09_deepmoji.py 3KB
tet_random.py 3KB
t07_dcnn.py 2KB
create_tc.py 2KB
t08_crnn.py 2KB
ner_clue_to_macadam.py 2KB
tet_trainer_word.py 2KB
tet_trainer_bert.py 2KB
tet_trainer_word.py 2KB
create_ner.py 2KB
tet_predict_sl.py 2KB
tet_trainer_bert.py 2KB
t11_capsule.py 2KB
tet_xlnet.py 2KB
setup.py 2KB
__init__.py 2KB
tet_albert.py 2KB
t04_charcnn.py 2KB
logger_config.py 2KB
path_config.py 2KB
s01_crf.py 2KB
t03_textcnn.py 2KB
tet_predict_tc.py 1KB
tet_electra.py 1KB
tet_roberta.py 1KB
tet_nezha.py 1KB
tet_bert.py 1KB
t05_birnn.py 1KB
s07_mrc.py 1KB
t02_fasttext.py 1KB
t01_finetune.py 1KB
t10_attention.py 1KB
t00_map.py 1KB
tet_bert4keras.py 878B
s00_map.py 790B
__init__.py 350B
__init__.py 336B
__init__.py 121B
__init__.py 102B
__init__.py 101B
__init__.py 100B
__init__.py 100B
__init__.py 100B
__init__.py 100B
__init__.py 100B
__init__.py 100B
__init__.py 100B
__init__.py 100B
__init__.py 100B
__init__.py 100B
__init__.py 100B
__init__.py 99B
__init__.py 99B
__init__.py 99B
__init__.py 99B
__init__.py 99B
__init__.py 99B
__init__.py 98B
local.trace 840KB
共 108 条
- 1
- 2
资源评论
生瓜蛋子
- 粉丝: 3824
- 资源: 5626
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功