{"id":15290451,"url":"https://github.com/yongzhuo/macadam","last_synced_at":"2025-04-05T16:03:44.670Z","repository":{"id":38230827,"uuid":"269414777","full_name":"yongzhuo/Macadam","owner":"yongzhuo","description":"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等序列标注算法。","archived":false,"fork":false,"pushed_at":"2023-03-24T22:23:58.000Z","size":998,"stargazers_count":324,"open_issues_count":1,"forks_count":39,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-05T16:03:12.026Z","etag":null,"topics":["bert","embedding","keras","ner","python3","relation-extraction","sequence-labeling","tensorflow","text-classification"],"latest_commit_sha":null,"homepage":"https://blog.csdn.net/rensihui","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yongzhuo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2020-06-04T16:51:33.000Z","updated_at":"2025-03-16T19:49:07.000Z","dependencies_parsed_at":"2023-01-21T20:02:50.477Z","dependency_job_id":"b8892b9f-f1d2-47b2-8c1b-a70eef853075","html_url":"https://github.com/yongzhuo/Macadam","commit_stats":{"total_commits":7,"total_committers":3,"mean_commits":"2.3333333333333335","dds":0.4285714285714286,"last_synced_commit":"794a29c760ce25264388c3a85a6b118733afb023"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yongzhuo%2FMacadam","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yongzhuo%2FMacadam/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yongzhuo%2FMacadam/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yongzhuo%2FMacadam/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yongzhuo","download_url":"https://codeload.github.com/yongzhuo/Macadam/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247361614,"owners_count":20926642,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bert","embedding","keras","ner","python3","relation-extraction","sequence-labeling","tensorflow","text-classification"],"created_at":"2024-09-30T16:08:14.576Z","updated_at":"2025-04-05T16:03:44.638Z","avatar_url":"https://github.com/yongzhuo.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n    \u003cimg src=\"test/images/macadam_logo.png\" width=\"480\"\\\u003e\n\u003c/p\u003e\n\n# [Macadam](https://github.com/yongzhuo/Macadam)\n\n[![PyPI](https://img.shields.io/pypi/v/Macadam)](https://pypi.org/project/Macadam/)\n[![Build Status](https://travis-ci.com/yongzhuo/Macadam.svg?branch=master)](https://travis-ci.com/yongzhuo/Macadam)\n[![PyPI_downloads](https://img.shields.io/pypi/dm/Macadam)](https://pypi.org/project/Macadam/)\n[![Stars](https://img.shields.io/github/stars/yongzhuo/Macadam?style=social)](https://github.com/yongzhuo/Macadam/stargazers)\n[![Forks](https://img.shields.io/github/forks/yongzhuo/Macadam.svg?style=social)](https://github.com/yongzhuo/Macadam/network/members)\n[![Join the chat at https://gitter.im/yongzhuo/Macadam](https://badges.gitter.im/yongzhuo/Macadam.svg)](https://gitter.im/yongzhuo/Macadam?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge)\n\u003e\u003e\u003e Macadam是一个以Tensorflow(Keras)和bert4keras为基础，专注于文本分类、序列标注和关系抽取的自然语言处理工具包。支持RANDOM、WORD2VEC、FASTTEXT、BERT、ALBERT、ROBERTA、NEZHA、XLNET、ELECTRA、GPT-2等EMBEDDING嵌入;\n    支持FineTune、FastText、TextCNN、CharCNN、BiRNN、RCNN、DCNN、CRNN、DeepMoji、SelfAttention、HAN、Capsule等文本分类算法; \n    支持CRF、Bi-LSTM-CRF、CNN-LSTM、DGCNN、Bi-LSTM-LAN、Lattice-LSTM-Batch、MRC等序列标注算法。\n\n\n## 目录\n* [安装](#安装)\n* [数据](#数据)\n* [使用方式](#使用方式)\n* [TODO](#TODO)\n* [paper](#paper)\n* [参考](#参考)\n\n\n# 安装 \n```bash\npip install Macadam\n\n# 清华镜像源\npip install -i https://pypi.tuna.tsinghua.edu.cn/simple Macadam\n```\n\n\n# 数据\n## 数据来源\n  * [ner_clue_2020](https://github.com/CLUEbenchmark/CLUENER2020), CLUENER2020中文细粒度命名实体识别\n  * [ner_people_1998](http://www.icl.pku.edu.cn/icl_res/), 《人民日报》标注语料库中的语料, 1998.01\n  * [baidu_qa_2019](https://github.com/liuhuanyong/MiningZhiDaoQACorpus), 百度知道问答语料\n  * [thucnews](http://thuctc.thunlp.org/), 新浪新闻RSS订阅频道2005-2011年间的历史数据筛\n## 数据格式\n```\n1. 文本分类  (txt格式, 每行为一个json):\n\n{\"x\": {\"text\": \"人站在地球上为什么没有头朝下的感觉\", \"texts2\": []}, \"y\": \"教育\"}\n{\"x\": {\"text\": \"我的小baby\", \"texts2\": []}, \"y\": [\"娱乐\"]}\n{\"x\": {\"text\": \"请问这起交通事故是谁的责任居多小车和摩托车发生事故在无红绿灯\", \"texts2\": []}, \"y\": \"娱乐\"}\n\n2. 序列标注 (txt格式, 每行为一个json):\n\n{\"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\"]}\n{\"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\"]}\n{\"x\": {\"text\": \"山是稳重的，这是我最初的观念。\", \"texts2\": []}, \"y\": [\"O\", \"O\", \"O\", \"O\", \"O\", \"O\", \"O\", \"O\", \"O\", \"O\", \"O\", \"O\", \"O\", \"O\", \"O\"]}\n{\"x\": {\"text\": \"立案不结、以罚代刑等问题有较大改观。\", \"texts2\": []}, \"y\": [\"O\", \"O\", \"O\", \"O\", \"O\", \"O\", \"O\", \"O\", \"O\", \"O\", \"O\", \"O\", \"O\", \"O\", \"O\", \"O\", \"O\", \"O\"]}\n\n```\n\n# 使用方式\n  更多样例sample详情见test目录\n## 文本分类, text-classification\n```bash\n# !/usr/bin/python\n# -*- coding: utf-8 -*-\n# @time    : 2020/5/8 21:33\n# @author  : Mo\n# @function: test trainer of bert\n\n\n# 适配linux\nimport sys\nimport os\npath_root = os.path.abspath(os.path.join(os.path.dirname(__file__), \"../..\"))\nsys.path.append(path_root)\n# cpu-gpu与tf.keras\nos.environ[\"CUDA_VISIBLE_DEVICES\"] = \"0\"\nos.environ[\"TF_KERAS\"] = \"1\"\n# macadam\nfrom macadam.conf.path_config import path_root, path_tc_baidu_qa_2019, path_tc_thucnews\nfrom macadam.tc import trainer\n\n\nif __name__==\"__main__\":\n    # bert-embedding地址, 必传\n    path_embed = \"D:/soft_install/dataset/bert-model/chinese_L-12_H-768_A-12\"\n    path_checkpoint = path_embed + \"/bert_model.ckpt\"\n    path_config = path_embed + \"/bert_config.json\"\n    path_vocab = path_embed + \"/vocab.txt\"\n\n    # 训练/验证数据地址, 必传\n    # path_train = os.path.join(path_tc_thucnews, \"train.json\")\n    # path_dev = os.path.join(path_tc_thucnews, \"dev.json\")\n    path_train = os.path.join(path_tc_baidu_qa_2019, \"train.json\")\n    path_dev = os.path.join(path_tc_baidu_qa_2019, \"dev.json\")\n\n    # 网络结构, 嵌入模型, 大小写都可以, 必传\n    # 网络模型架构(Graph), \"FineTune\", \"FastText\", \"TextCNN\", \"CharCNN\",\n    # \"BiRNN\", \"RCNN\", \"DCNN\", \"CRNN\", \"DeepMoji\", \"SelfAttention\", \"HAN\", \"Capsule\"\n    network_type = \"TextCNN\"\n    # 嵌入(embedding)类型, \"ROBERTA\", \"ELECTRA\", \"RANDOM\", \"ALBERT\", \"XLNET\", \"NEZHA\", \"GPT2\", \"WORD\", \"BERT\"\n    embed_type = \"BERT\"\n    # token级别, 一般为\"char\", 只有random和word的embedding时存在\"word\"\n    token_type = \"CHAR\"\n    # 任务, \"TC\"(文本分类), \"SL\"(序列标注), \"RE\"(关系抽取)\n    task = \"TC\"\n    \n    # 模型保存目录, 必传\n    path_model_dir = os.path.join(path_root, \"data\", \"model\", network_type)\n    # 开始训练, 可能前几轮loss较大acc较低, 后边会好起来\n    trainer(path_model_dir, path_embed, path_train, path_dev, path_checkpoint, path_config, path_vocab,\n            network_type=network_type, embed_type=embed_type, token_type=token_type, task=task)\n    mm = 0\n```\n## 序列标注, sequence-labeling\n```bash\n# !/usr/bin/python\n# -*- coding: utf-8 -*-\n# @time    : 2020/5/8 21:33\n# @author  : Mo\n# @function: test trainer of bert\n\n\n# 适配linux\nimport sys\nimport os\npath_root = os.path.abspath(os.path.join(os.path.dirname(__file__), \"../..\"))\nsys.path.append(path_root)\n## cpu-gpu与tf.keras\n# os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"0\"\nos.environ[\"TF_KERAS\"] = \"1\"\n\n# 地址, tf.keras\nfrom macadam.conf.path_config import path_embed_bert, path_embed_word2vec_word, path_embed_word2vec_char\nfrom macadam.conf.path_config import path_root, path_ner_people_1998, path_ner_clue_2020\nfrom macadam.sl import trainer\n\n\nif __name__==\"__main__\":\n    # bert-embedding地址, 必传\n    path_embed = path_embed_bert # path_embed_bert, path_embed_word2vec_word, path_embed_word2vec_char\n    path_checkpoint = os.path.join(path_embed + \"bert_model.ckpt\")\n    path_config = os.path.join(path_embed + \"bert_config.json\")\n    path_vocab = os.path.join(path_embed + \"vocab.txt\")\n\n    # # 训练/验证数据地址\n    # path_train = os.path.join(path_ner_people_1998, \"train.json\")\n    # path_dev = os.path.join(path_ner_people_1998, \"dev.json\")\n    path_train = os.path.join(path_ner_clue_2020, \"ner_clue_2020.train\")\n    path_dev = os.path.join(path_ner_clue_2020, \"ner_clue_2020.dev\")\n    # 网络结构\n    # \"CRF\", \"Bi-LSTM-CRF\", \"Bi-LSTM-LAN\", \"CNN-LSTM\", \"DGCNN\", \"LATTICE-LSTM-BATCH\"\n    network_type = \"CRF\"\n    # 嵌入(embedding)类型, \"ROOBERTA\", \"ELECTRA\", \"RANDOM\", \"ALBERT\", \"XLNET\", \"NEZHA\", \"GPT2\", \"WORD\", \"BERT\"\n    # MIX,  WC_LSTM时候填两个[\"RANDOM\", \"WORD\"], [\"WORD\", \"RANDOM\"], [\"RANDOM\", \"RANDOM\"], [\"WORD\", \"WORD\"]\n    embed_type = \"RANDOM\" \n    token_type = \"CHAR\"\n    task = \"SL\"\n    lr = 1e-5 if embed_type in [\"ROBERTA\", \"ELECTRA\", \"ALBERT\", \"XLNET\", \"NEZHA\", \"GPT2\", \"BERT\"] else 1e-3\n    # 模型保存目录, 如果不存在则创建\n    path_model_dir = os.path.join(path_root, \"data\", \"model\", network_type)\n    if not os.path.exists(path_model_dir):\n        os.mkdir(path_model_dir)\n    # 开始训练\n    trainer(path_model_dir, path_embed, path_train, path_dev,\n            path_checkpoint, path_config, path_vocab,\n            network_type=network_type, embed_type=embed_type,\n            task=task, token_type=token_type,\n            is_length_max=False, use_onehot=False, use_file=False, use_crf=True,\n            layer_idx=[-2], learning_rate=lr,\n            batch_size=30, epochs=12, early_stop=6, rate=1)\n    mm = 0\n```\n  \n# TODO\n * 文本分类TC(TextGCN)\n * 序列标注SL(MRC)\n * 关系抽取RE\n * 嵌入embed(xlnet)\n\n\n# paper\n## 文本分类(TC, text-classification)\n* FastText:   [Bag of Tricks for Efﬁcient Text Classiﬁcation](https://arxiv.org/abs/1607.01759)\n* TextCNN：   [Convolutional Neural Networks for Sentence Classiﬁcation](https://arxiv.org/abs/1408.5882)\n* charCNN-kim：   [Character-Aware Neural Language Models](https://arxiv.org/abs/1508.06615)\n* charCNN-zhang:  [Character-level Convolutional Networks for Text Classiﬁcation](https://arxiv.org/pdf/1509.01626.pdf)\n* TextRNN：   [Recurrent Neural Network for Text Classification with Multi-Task Learning](https://www.ijcai.org/Proceedings/16/Papers/408.pdf)\n* RCNN：      [Recurrent Convolutional Neural Networks for Text Classification](http://www.nlpr.ia.ac.cn/cip/~liukang/liukangPageFile/Recurrent%20Convolutional%20Neural%20Networks%20for%20Text%20Classification.pdf)\n* DCNN:       [A Convolutional Neural Network for Modelling Sentences](https://arxiv.org/abs/1404.2188)\n* DPCNN:      [Deep Pyramid Convolutional Neural Networks for Text Categorization](https://www.aclweb.org/anthology/P17-1052)\n* VDCNN:      [Very Deep Convolutional Networks](https://www.aclweb.org/anthology/E17-1104)\n* CRNN:        [A C-LSTM Neural Network for Text Classification](https://arxiv.org/abs/1511.08630)\n* DeepMoji:    [Using millions of emojio ccurrences to learn any-domain represent ations for detecting sentiment, emotion and sarcasm](https://arxiv.org/abs/1708.00524)\n* SelfAttention: [Attention Is All You Need](https://arxiv.org/abs/1706.03762)\n* HAN: [Hierarchical Attention Networks for Document Classification](https://www.cs.cmu.edu/~diyiy/docs/naacl16.pdf)\n* CapsuleNet: [Dynamic Routing Between Capsules](https://arxiv.org/pdf/1710.09829.pdf)\n* Transformer(encode or decode): [Attention Is All You Need](https://arxiv.org/abs/1706.03762)\n* Bert:                  [BERT: Pre-trainingofDeepBidirectionalTransformersfor LanguageUnderstanding]()\n* Xlnet:                 [XLNet: Generalized Autoregressive Pretraining for Language Understanding](https://arxiv.org/abs/1906.08237)\n* Albert:                [ALBERT: A LITE BERT FOR SELF-SUPERVISED LEARNING OF LANGUAGE REPRESENTATIONS](https://arxiv.org/pdf/1909.11942.pdf)\n* RoBERTa:               [RoBERTa: A Robustly Optimized BERT Pretraining Approach](https://arxiv.org/abs/1907.11692)\n* ELECTRA:               [ELECTRA: Pre-training Text Encoders as Discriminators Rather Than Generators](https://openreview.net/pdf?id=r1xMH1BtvB)\n* TextGCN:               [Graph Convolutional Networks for Text Classification](https://arxiv.org/abs/1809.05679)\n\n\n## 序列标注(SL, sequence-labeling)\n* CRF:            [Conditional Random Fields: Probabilistic Models for Segmenting and Labeling Sequence Data](https://repository.upenn.edu/cgi/viewcontent.cgi?article=1162\u0026context=cis_papers)\n* Bi-LSTM-CRF:    [Bidirectional LSTM-CRF Models for Sequence Tagging](https://arxiv.org/pdf/1508.01991.pdf)\n* CNN-LSTM:       [End-to-endSequenceLabelingviaBi-directionalLSTM-CNNs-CRF](https://arxiv.org/abs/1603.01354)\n* DGCNN:          [Multi-Scale Context Aggregation by Dilated Convolutions](https://arxiv.org/abs/1511.07122)\n* Bi-LSTM-LAN:    [Hierarchically-Reﬁned Label Attention Network for Sequence Labeling](https://arxiv.org/abs/1908.08676v2)\n* LATTICE-LSTM-BATCH:    [An Encoding Strategy Based Word-Character LSTM for Chinese NER](https://www.aclweb.org/anthology/N19-1247/)\n* MRC:            [A Unified MRC Framework for Named Entity Recognition](https://arxiv.org/abs/1910.11476v2)\n\n\n# 参考\n* keras与tensorflow版本对应: [https://docs.floydhub.com/guides/environments/](https://docs.floydhub.com/guides/environments/)\n* bert4keras:   [https://github.com/bojone/bert4keras](https://github.com/bojone/bert4keras)\n* Kashgari: [https://github.com/BrikerMan/Kashgari](https://github.com/BrikerMan/Kashgari)\n* fastNLP: [https://github.com/fastnlp/fastNLP](https://github.com/fastnlp/fastNLP)\n* HanLP: [https://github.com/hankcs/HanLP](https://github.com/hankcs/HanLP)\n\n\n# Reference\nFor citing this work, you can refer to the present GitHub project. For example, with BibTeX:\n```\n@misc{Macadam,\n    howpublished = {url{https://github.com/yongzhuo/Macadam}},\n    title = {Macadam},\n    author = {Yongzhuo Mo},\n    publisher = {GitHub},\n    year = {2020}\n}\n```\n*希望对你有所帮助!\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyongzhuo%2Fmacadam","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyongzhuo%2Fmacadam","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyongzhuo%2Fmacadam/lists"}