[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"similar-brightmart--albert_zh":3,"tool-brightmart--albert_zh":61},[4,18,26,36,44,53],{"id":5,"name":6,"github_repo":7,"description_zh":8,"stars":9,"difficulty_score":10,"last_commit_at":11,"category_tags":12,"status":17},4358,"openclaw","openclaw\u002Fopenclaw","OpenClaw 是一款专为个人打造的本地化 AI 助手，旨在让你在自己的设备上拥有完全可控的智能伙伴。它打破了传统 AI 助手局限于特定网页或应用的束缚，能够直接接入你日常使用的各类通讯渠道，包括微信、WhatsApp、Telegram、Discord、iMessage 等数十种平台。无论你在哪个聊天软件中发送消息，OpenClaw 都能即时响应，甚至支持在 macOS、iOS 和 Android 设备上进行语音交互，并提供实时的画布渲染功能供你操控。\n\n这款工具主要解决了用户对数据隐私、响应速度以及“始终在线”体验的需求。通过将 AI 部署在本地，用户无需依赖云端服务即可享受快速、私密的智能辅助，真正实现了“你的数据，你做主”。其独特的技术亮点在于强大的网关架构，将控制平面与核心助手分离，确保跨平台通信的流畅性与扩展性。\n\nOpenClaw 非常适合希望构建个性化工作流的技术爱好者、开发者，以及注重隐私保护且不愿被单一生态绑定的普通用户。只要具备基础的终端操作能力（支持 macOS、Linux 及 Windows WSL2），即可通过简单的命令行引导完成部署。如果你渴望拥有一个懂你",349277,3,"2026-04-06T06:32:30",[13,14,15,16],"Agent","开发框架","图像","数据工具","ready",{"id":19,"name":20,"github_repo":21,"description_zh":22,"stars":23,"difficulty_score":10,"last_commit_at":24,"category_tags":25,"status":17},3808,"stable-diffusion-webui","AUTOMATIC1111\u002Fstable-diffusion-webui","stable-diffusion-webui 是一个基于 Gradio 构建的网页版操作界面，旨在让用户能够轻松地在本地运行和使用强大的 Stable Diffusion 图像生成模型。它解决了原始模型依赖命令行、操作门槛高且功能分散的痛点，将复杂的 AI 绘图流程整合进一个直观易用的图形化平台。\n\n无论是希望快速上手的普通创作者、需要精细控制画面细节的设计师，还是想要深入探索模型潜力的开发者与研究人员，都能从中获益。其核心亮点在于极高的功能丰富度：不仅支持文生图、图生图、局部重绘（Inpainting）和外绘（Outpainting）等基础模式，还独创了注意力机制调整、提示词矩阵、负向提示词以及“高清修复”等高级功能。此外，它内置了 GFPGAN 和 CodeFormer 等人脸修复工具，支持多种神经网络放大算法，并允许用户通过插件系统无限扩展能力。即使是显存有限的设备，stable-diffusion-webui 也提供了相应的优化选项，让高质量的 AI 艺术创作变得触手可及。",162132,"2026-04-05T11:01:52",[14,15,13],{"id":27,"name":28,"github_repo":29,"description_zh":30,"stars":31,"difficulty_score":32,"last_commit_at":33,"category_tags":34,"status":17},1381,"everything-claude-code","affaan-m\u002Feverything-claude-code","everything-claude-code 是一套专为 AI 编程助手（如 Claude Code、Codex、Cursor 等）打造的高性能优化系统。它不仅仅是一组配置文件，而是一个经过长期实战打磨的完整框架，旨在解决 AI 代理在实际开发中面临的效率低下、记忆丢失、安全隐患及缺乏持续学习能力等核心痛点。\n\n通过引入技能模块化、直觉增强、记忆持久化机制以及内置的安全扫描功能，everything-claude-code 能显著提升 AI 在复杂任务中的表现，帮助开发者构建更稳定、更智能的生产级 AI 代理。其独特的“研究优先”开发理念和针对 Token 消耗的优化策略，使得模型响应更快、成本更低，同时有效防御潜在的攻击向量。\n\n这套工具特别适合软件开发者、AI 研究人员以及希望深度定制 AI 工作流的技术团队使用。无论您是在构建大型代码库，还是需要 AI 协助进行安全审计与自动化测试，everything-claude-code 都能提供强大的底层支持。作为一个曾荣获 Anthropic 黑客大奖的开源项目，它融合了多语言支持与丰富的实战钩子（hooks），让 AI 真正成长为懂上",153609,2,"2026-04-13T11:34:59",[14,13,35],"语言模型",{"id":37,"name":38,"github_repo":39,"description_zh":40,"stars":41,"difficulty_score":32,"last_commit_at":42,"category_tags":43,"status":17},2271,"ComfyUI","Comfy-Org\u002FComfyUI","ComfyUI 是一款功能强大且高度模块化的视觉 AI 引擎，专为设计和执行复杂的 Stable Diffusion 图像生成流程而打造。它摒弃了传统的代码编写模式，采用直观的节点式流程图界面，让用户通过连接不同的功能模块即可构建个性化的生成管线。\n\n这一设计巧妙解决了高级 AI 绘图工作流配置复杂、灵活性不足的痛点。用户无需具备编程背景，也能自由组合模型、调整参数并实时预览效果，轻松实现从基础文生图到多步骤高清修复等各类复杂任务。ComfyUI 拥有极佳的兼容性，不仅支持 Windows、macOS 和 Linux 全平台，还广泛适配 NVIDIA、AMD、Intel 及苹果 Silicon 等多种硬件架构，并率先支持 SDXL、Flux、SD3 等前沿模型。\n\n无论是希望深入探索算法潜力的研究人员和开发者，还是追求极致创作自由度的设计师与资深 AI 绘画爱好者，ComfyUI 都能提供强大的支持。其独特的模块化架构允许社区不断扩展新功能，使其成为当前最灵活、生态最丰富的开源扩散模型工具之一，帮助用户将创意高效转化为现实。",108322,"2026-04-10T11:39:34",[14,15,13],{"id":45,"name":46,"github_repo":47,"description_zh":48,"stars":49,"difficulty_score":32,"last_commit_at":50,"category_tags":51,"status":17},6121,"gemini-cli","google-gemini\u002Fgemini-cli","gemini-cli 是一款由谷歌推出的开源 AI 命令行工具，它将强大的 Gemini 大模型能力直接集成到用户的终端环境中。对于习惯在命令行工作的开发者而言，它提供了一条从输入提示词到获取模型响应的最短路径，无需切换窗口即可享受智能辅助。\n\n这款工具主要解决了开发过程中频繁上下文切换的痛点，让用户能在熟悉的终端界面内直接完成代码理解、生成、调试以及自动化运维任务。无论是查询大型代码库、根据草图生成应用，还是执行复杂的 Git 操作，gemini-cli 都能通过自然语言指令高效处理。\n\n它特别适合广大软件工程师、DevOps 人员及技术研究人员使用。其核心亮点包括支持高达 100 万 token 的超长上下文窗口，具备出色的逻辑推理能力；内置 Google 搜索、文件操作及 Shell 命令执行等实用工具；更独特的是，它支持 MCP（模型上下文协议），允许用户灵活扩展自定义集成，连接如图像生成等外部能力。此外，个人谷歌账号即可享受免费的额度支持，且项目基于 Apache 2.0 协议完全开源，是提升终端工作效率的理想助手。",100752,"2026-04-10T01:20:03",[52,13,15,14],"插件",{"id":54,"name":55,"github_repo":56,"description_zh":57,"stars":58,"difficulty_score":32,"last_commit_at":59,"category_tags":60,"status":17},4721,"markitdown","microsoft\u002Fmarkitdown","MarkItDown 是一款由微软 AutoGen 团队打造的轻量级 Python 工具，专为将各类文件高效转换为 Markdown 格式而设计。它支持 PDF、Word、Excel、PPT、图片（含 OCR）、音频（含语音转录）、HTML 乃至 YouTube 链接等多种格式的解析，能够精准提取文档中的标题、列表、表格和链接等关键结构信息。\n\n在人工智能应用日益普及的今天，大语言模型（LLM）虽擅长处理文本，却难以直接读取复杂的二进制办公文档。MarkItDown 恰好解决了这一痛点，它将非结构化或半结构化的文件转化为模型“原生理解”且 Token 效率极高的 Markdown 格式，成为连接本地文件与 AI 分析 pipeline 的理想桥梁。此外，它还提供了 MCP（模型上下文协议）服务器，可无缝集成到 Claude Desktop 等 LLM 应用中。\n\n这款工具特别适合开发者、数据科学家及 AI 研究人员使用，尤其是那些需要构建文档检索增强生成（RAG）系统、进行批量文本分析或希望让 AI 助手直接“阅读”本地文件的用户。虽然生成的内容也具备一定可读性，但其核心优势在于为机器",93400,"2026-04-06T19:52:38",[52,14],{"id":62,"github_repo":63,"name":64,"description_en":65,"description_zh":66,"ai_summary_zh":67,"readme_en":68,"readme_zh":69,"quickstart_zh":70,"use_case_zh":71,"hero_image_url":72,"owner_login":73,"owner_name":73,"owner_avatar_url":74,"owner_bio":75,"owner_company":76,"owner_location":77,"owner_email":78,"owner_twitter":79,"owner_website":76,"owner_url":80,"languages":81,"stars":90,"forks":91,"last_commit_at":92,"license":79,"difficulty_score":10,"env_os":93,"env_gpu":94,"env_ram":95,"env_deps":96,"category_tags":102,"github_topics":103,"view_count":32,"oss_zip_url":79,"oss_zip_packed_at":79,"status":17,"created_at":113,"updated_at":114,"faqs":115,"releases":151},7108,"brightmart\u002Falbert_zh","albert_zh","A LITE BERT FOR SELF-SUPERVISED LEARNING OF LANGUAGE REPRESENTATIONS, 海量中文预训练ALBERT模型","albert_zh 是一个专为中文自然语言处理打造的轻量级预训练模型系列。它基于谷歌提出的 ALBERT 架构，旨在解决传统 BERT 模型参数量巨大、部署成本高且推理速度慢的痛点。通过在海量中文语料上进行预训练，albert_zh 在大幅压缩模型体积的同时，依然保持了卓越的语义理解能力，甚至在多项权威基准测试中超越了原版 BERT。\n\n这款工具特别适合需要高效部署 NLP 应用的开发者、追求实验效率的研究人员，以及关注移动端或边缘计算场景的技术团队。其核心亮点在于独特的参数共享机制和因子化嵌入技术，使得最小版本（albert_tiny）的参数量仅为原始 BERT 的约十分之一，文件大小低至 16MB，推理速度提升近 10 倍，却仅损失极少的精度。此外，albert_zh 提供了从 Tiny 到 XLarge 多种规格的模型选择，并完美兼容 TensorFlow、PyTorch 及 Hugging Face Transformers 生态，支持一键运行多个中文任务数据集。无论是构建实时客服机器人、文本分类系统，还是在资源受限的设备上运行语义相似度匹配，albert_zh 都能以“小身材","albert_zh 是一个专为中文自然语言处理打造的轻量级预训练模型系列。它基于谷歌提出的 ALBERT 架构，旨在解决传统 BERT 模型参数量巨大、部署成本高且推理速度慢的痛点。通过在海量中文语料上进行预训练，albert_zh 在大幅压缩模型体积的同时，依然保持了卓越的语义理解能力，甚至在多项权威基准测试中超越了原版 BERT。\n\n这款工具特别适合需要高效部署 NLP 应用的开发者、追求实验效率的研究人员，以及关注移动端或边缘计算场景的技术团队。其核心亮点在于独特的参数共享机制和因子化嵌入技术，使得最小版本（albert_tiny）的参数量仅为原始 BERT 的约十分之一，文件大小低至 16MB，推理速度提升近 10 倍，却仅损失极少的精度。此外，albert_zh 提供了从 Tiny 到 XLarge 多种规格的模型选择，并完美兼容 TensorFlow、PyTorch 及 Hugging Face Transformers 生态，支持一键运行多个中文任务数据集。无论是构建实时客服机器人、文本分类系统，还是在资源受限的设备上运行语义相似度匹配，albert_zh 都能以“小身材”释放“大能量”，让高质量的中文语言理解变得更加触手可及。","# albert_zh\n\nAn Implementation of \u003Ca href=\"https:\u002F\u002Farxiv.org\u002Fpdf\u002F1909.11942.pdf\">A Lite Bert For Self-Supervised Learning Language Representations\u003C\u002Fa> with TensorFlow\n\nALBert is based on Bert, but with some improvements. It achieves state of the art performance on main benchmarks with 30% parameters less. \n\nFor albert_base_zh it only has ten percentage parameters compare of original bert model, and main accuracy is retained. \n\n\nDifferent version of ALBERT pre-trained model for Chinese, including TensorFlow, PyTorch and Keras, is available now.\n\n海量中文语料上预训练ALBERT模型：参数更少，效果更好。预训练小模型也能拿下13项NLP任务，ALBERT三大改造登顶GLUE基准\n\n\u003Ca href='https:\u002F\u002Fwww.cluebenchmarks.com\u002Fclueai.html'>clueai工具包: 三行代码，三分钟定制一个NLP的API（零样本学习）\u003C\u002Fa>\n\n\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fbrightmart_albert_zh_readme_ccf223647a74.jpg\"  width=\"90%\" height=\"70%\" \u002F>\n\n一键运行10个数据集、9个基线模型、不同任务上模型效果的详细对比，见\u003Ca href=\"http:\u002F\u002Fwww.CLUEbenchmarks.com\">CLUE benchmark\u003C\u002Fa>\n\n一键运行CLUE中文任务：6个中文分类或句子对任务（新）\n---------------------------------------------------------------------\n    使用方式：\n    1、克隆项目\n       git clone https:\u002F\u002Fgithub.com\u002Fbrightmart\u002Falbert_zh.git\n    2、运行一键运行脚本(GPU方式): 会自动下载模型和所有任务数据并开始运行。\n       bash run_classifier_clue.sh\n       执行该一键运行脚本将会自动下载所有任务数据，并为所有任务找到最优模型，然后测试得到提交结果\n    \n\n模型下载 Download Pre-trained Models of Chinese\n-----------------------------------------------\n1、\u003Ca href=\"https:\u002F\u002Fstorage.googleapis.com\u002Falbert_zh\u002Falbert_tiny.zip\">albert_tiny_zh\u003C\u002Fa>, \u003Ca href=\"https:\u002F\u002Fstorage.googleapis.com\u002Falbert_zh\u002Falbert_tiny_489k.zip\">albert_tiny_zh(训练更久，累积学习20亿个样本)\u003C\u002Fa>，文件大小16M、参数为4M\n\n    训练和推理预测速度提升约10倍，精度基本保留，模型大小为bert的1\u002F25；语义相似度数据集LCQMC测试集上达到85.4%，相比bert_base仅下降1.5个点。\n\n    lcqmc训练使用如下参数： --max_seq_length=128 --train_batch_size=64   --learning_rate=1e-4   --num_train_epochs=5 \n    \n    albert_tiny使用同样的大规模中文语料数据，层数仅为4层、hidden size等向量维度大幅减少; 尝试使用如下学习率来获得更好效果：{2e-5, 6e-5, 1e-4} \n    \n    【使用场景】任务相对比较简单一些或实时性要求高的任务，如语义相似度等句子对任务、分类任务；比较难的任务如阅读理解等，可以使用其他大模型。\n\n     例如，可以使用[Tensorflow Lite](https:\u002F\u002Fwww.tensorflow.org\u002Flite)在移动端进行部署，本文[随后](#use_tflite)针对这一点进行了介绍，包括如何把模型转换成Tensorflow Lite格式和对其进行性能测试等。\n     \n     一键运行albert_tiny_zh(linux,lcqmc任务)：\n     1) git clone https:\u002F\u002Fgithub.com\u002Fbrightmart\u002Falbert_zh\n     2) cd albert_zh\n     3) bash run_classifier_lcqmc.sh\n1.1、\u003Ca href=\"https:\u002F\u002Fstorage.googleapis.com\u002Falbert_zh\u002Falbert_tiny_zh_google.zip\">albert_tiny_google_zh(累积学习10亿个样本,google版本)\u003C\u002Fa>，模型大小16M、性能与albert_tiny_zh一致\n\n1.2、\u003Ca href=\"https:\u002F\u002Fstorage.googleapis.com\u002Falbert_zh\u002Falbert_small_zh_google.zip\">albert_small_google_zh(累积学习10亿个样本,google版本)\u003C\u002Fa>，\n     \n     速度比bert_base快4倍；LCQMC测试集上比Bert下降仅0.9个点；去掉adam后模型大小18.5M；使用方法，见 #下游任务 Fine-tuning on Downstream Task     \n     \n2、\u003Ca href=\"https:\u002F\u002Fstorage.googleapis.com\u002Falbert_zh\u002Falbert_large_zh.zip\">albert_large_zh\u003C\u002Fa>,参数量，层数24，文件大小为64M\n   \n    参数量和模型大小为bert_base的六分之一；在口语化描述相似性数据集LCQMC的测试集上相比bert_base上升0.2个点\n\n3、\u003Ca href=\"https:\u002F\u002Fstorage.googleapis.com\u002Falbert_zh\u002Falbert_base_zh_additional_36k_steps.zip\">albert_base_zh(额外训练了1.5亿个实例即 36k steps * batch_size 4096)\u003C\u002Fa>; \u003Ca href=\"https:\u002F\u002Fstorage.googleapis.com\u002Falbert_zh\u002Falbert_base_zh.zip\"> albert_base_zh(小模型体验版)\u003C\u002Fa>, 参数量12M, 层数12，大小为40M\n\n    参数量为bert_base的十分之一，模型大小也十分之一；在口语化描述相似性数据集LCQMC的测试集上相比bert_base下降约0.6~1个点；\n    相比未预训练，albert_base提升14个点\n\n4、\u003Ca href=\"https:\u002F\u002Fstorage.googleapis.com\u002Falbert_zh\u002Falbert_xlarge_zh_177k.zip\">albert_xlarge_zh_177k \u003C\u002Fa>; \n\u003Ca href=\"https:\u002F\u002Fstorage.googleapis.com\u002Falbert_zh\u002Falbert_xlarge_zh_183k.zip\">albert_xlarge_zh_183k(优先尝试)\u003C\u002Fa>参数量，层数24，文件大小为230M\n   \n    参数量和模型大小为bert_base的二分之一；需要一张大的显卡；完整测试对比将后续添加；batch_size不能太小，否则可能影响精度\n\n### 快速加载\n依托于[Huggingface-Transformers 2.2.2](https:\u002F\u002Fgithub.com\u002Fhuggingface\u002Ftransformers)，可轻松调用以上模型。\n```\ntokenizer = AutoTokenizer.from_pretrained(\"MODEL_NAME\")\nmodel = AutoModel.from_pretrained(\"MODEL_NAME\")\n```\n\n其中`MODEL_NAME`对应列表如下：\n\n| 模型名 | MODEL_NAME |\n| - | - |\n| albert_tiny_google_zh | voidful\u002Falbert_chinese_tiny |\n| albert_small_google_zh | voidful\u002Falbert_chinese_small  |\n| albert_base_zh (from google) | voidful\u002Falbert_chinese_base   |\n| albert_large_zh (from google) | voidful\u002Falbert_chinese_large   |\n| albert_xlarge_zh (from google) | voidful\u002Falbert_chinese_xlarge   |\n| albert_xxlarge_zh (from google) | voidful\u002Falbert_chinese_xxlarge   |\n\n更多通过transformers使用albert的\u003Ca href='https:\u002F\u002Fhuggingface.co\u002Fmodels?search=albert_chinese'>示例\u003C\u002Fa>\n\n预训练 Pre-training\n-----------------------------------------------\n\n#### 生成特定格式的文件(tfrecords) Generate tfrecords Files\n\nRun following command 运行以下命令即可。项目自动了一个示例的文本文件(data\u002Fnews_zh_1.txt)\n   \n       bash create_pretrain_data.sh\n   \n如果你有很多文本文件，可以通过传入参数的方式，生成多个特定格式的文件(tfrecords）\n\n###### Support English and Other Non-Chinese Language: \n    If you are doing pre-train for english or other language,which is not chinese, \n    you should set hyperparameter of non_chinese to True on create_pretraining_data.py; \n    otherwise, by default it is doing chinese pre-train using whole word mask of chinese.\n\n#### 执行预训练 pre-training on GPU\u002FTPU using the command\n    GPU(brightmart版, tiny模型):\n    export BERT_BASE_DIR=.\u002Falbert_tiny_zh\n    nohup python3 run_pretraining.py --input_file=.\u002Fdata\u002Ftf*.tfrecord  \\\n    --output_dir=.\u002Fmy_new_model_path --do_train=True --do_eval=True --bert_config_file=$BERT_BASE_DIR\u002Falbert_config_tiny.json \\\n    --train_batch_size=4096 --max_seq_length=512 --max_predictions_per_seq=51 \\\n    --num_train_steps=125000 --num_warmup_steps=12500 --learning_rate=0.00176    \\\n    --save_checkpoints_steps=2000  --init_checkpoint=$BERT_BASE_DIR\u002Falbert_model.ckpt &\n    \n    GPU(Google版本, small模型):\n    export BERT_BASE_DIR=.\u002Falbert_small_zh_google\n    nohup python3 run_pretraining_google.py --input_file=.\u002Fdata\u002Ftf*.tfrecord --eval_batch_size=64 \\\n    --output_dir=.\u002Fmy_new_model_path --do_train=True --do_eval=True --albert_config_file=$BERT_BASE_DIR\u002Falbert_config_small_google.json  --export_dir=.\u002Fmy_new_model_path_export \\\n    --train_batch_size=4096 --max_seq_length=512 --max_predictions_per_seq=20 \\\n    --num_train_steps=125000 --num_warmup_steps=12500 --learning_rate=0.00176   \\\n    --save_checkpoints_steps=2000 --init_checkpoint=$BERT_BASE_DIR\u002Falbert_model.ckpt\n    \n    TPU, add something like this:\n        --use_tpu=True  --tpu_name=grpc:\u002F\u002F10.240.1.66:8470 --tpu_zone=us-central1-a\n        \n    注：如果你重头开始训练，可以不指定init_checkpoint；\n    如果你从现有的模型基础上训练，指定一下BERT_BASE_DIR的路径，并确保bert_config_file和init_checkpoint两个参数的值能对应到相应的文件上；\n    领域上的预训练，根据数据的大小，可以不用训练特别久。\n\n环境 Environment\n-----------------------------------------------\nUse Python3 + Tensorflow 1.x \n\ne.g. Tensorflow 1.4 or 1.5\n\n\n下游任务 Fine-tuning on Downstream Task\n-----------------------------------------------\n##### 使用TensorFlow:\n\n以使用albert_base做LCQMC任务为例。LCQMC任务是在口语化描述的数据集上做文本的相似性预测。\n\nWe will use LCQMC dataset for fine-tuning, it is oral language corpus, it is used to train and predict semantic similarity of a pair of sentences.\n\n下载\u003Ca href=\"https:\u002F\u002Fdrive.google.com\u002Fopen?id=1HXYMqsXjmA5uIfu_SFqP7r_vZZG-m_H0\">LCQMC\u003C\u002Fa>数据集，包含训练、验证和测试集，训练集包含24万口语化描述的中文句子对，标签为1或0。1为句子语义相似，0为语义不相似。\n\n通过运行下列命令做LCQMC数据集上的fine-tuning:\n    \n    1. Clone this project:\n          \n          git clone https:\u002F\u002Fgithub.com\u002Fbrightmart\u002Falbert_zh.git\n          \n    2. Fine-tuning by running the following command.\n        brightmart版本的tiny模型\n        export BERT_BASE_DIR=.\u002Falbert_tiny_zh\n        export TEXT_DIR=.\u002Flcqmc\n        nohup python3 run_classifier.py   --task_name=lcqmc_pair   --do_train=true   --do_eval=true   --data_dir=$TEXT_DIR   --vocab_file=.\u002Falbert_config\u002Fvocab.txt  \\\n        --bert_config_file=.\u002Falbert_config\u002Falbert_config_tiny.json --max_seq_length=128 --train_batch_size=64   --learning_rate=1e-4  --num_train_epochs=5 \\\n        --output_dir=.\u002Falbert_lcqmc_checkpoints --init_checkpoint=$BERT_BASE_DIR\u002Falbert_model.ckpt &\n        \n        google版本的small模型\n        export BERT_BASE_DIR=.\u002Falbert_small_zh\n        export TEXT_DIR=.\u002Flcqmc\n        nohup python3 run_classifier_sp_google.py --task_name=lcqmc_pair   --do_train=true   --do_eval=true   --data_dir=$TEXT_DIR   --vocab_file=.\u002Falbert_config\u002Fvocab.txt  \\\n        --albert_config_file=.\u002F$BERT_BASE_DIR\u002Falbert_config_small_google.json --max_seq_length=128 --train_batch_size=64   --learning_rate=1e-4   --num_train_epochs=5 \\\n        --output_dir=.\u002Falbert_lcqmc_checkpoints --init_checkpoint=$BERT_BASE_DIR\u002Falbert_model.ckpt &\n\n    Notice\u002F注：\n        1) you need to download pre-trained chinese albert model, and also download LCQMC dataset \n        你需要下载预训练的模型，并放入到项目当前项目，假设目录名称为albert_tiny_zh; 需要下载LCQMC数据集，并放入到当前项目，\n        假设数据集目录名称为lcqmc\n\n        2) for Fine-tuning, you can try to add small percentage of dropout(e.g. 0.1) by changing parameters of \n          attention_probs_dropout_prob & hidden_dropout_prob on albert_config_xxx.json. By default, we set dropout as zero. \n        \n        3) you can try different learning rate {2e-5, 6e-5, 1e-4} for better performance \n\n\nUpdates\n-----------------------------------------------\n**\\*\\*\\*\\*\\* 2019-11-03: add google version of albert_small, albert_tiny; \n\nadd method to deploy ablert_tiny to mobile devices with only 0.1 second inference time for sequence length 128, 60M memory \\*\\*\\*\\*\\***\n\n**\\*\\*\\*\\*\\* 2019-10-30: add a simple guide about converting the model to Tensorflow Lite for edge deployment \\*\\*\\*\\*\\***\n\n**\\*\\*\\*\\*\\* 2019-10-15: albert_tiny_zh, 10 times fast than bert base for training and inference, accuracy remains \\*\\*\\*\\*\\***\n\n**\\*\\*\\*\\*\\* 2019-10-07: more models of albert \\*\\*\\*\\*\\***\n\nadd albert_xlarge_zh; albert_base_zh_additional_steps, training with more instances\n\n**\\*\\*\\*\\*\\* 2019-10-04: PyTorch and Keras versions of albert were supported \\*\\*\\*\\*\\***\n\na.Convert to PyTorch version and do your tasks through \u003Ca href=\"https:\u002F\u002Fgithub.com\u002FlonePatient\u002Falbert_pytorch\">albert_pytorch\u003C\u002Fa>\n\nb.Load pre-trained model with keras using one line of codes through \u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fbojone\u002Fbert4keras\">bert4keras\u003C\u002Fa>\n\nc.Use albert with TensorFlow 2.0: Use or load pre-trained model with tf2.0 through \u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fkpe\u002Fbert-for-tf2\">bert-for-tf2\u003C\u002Fa>\n\nReleasing albert_xlarge on 6th Oct\n\n**\\*\\*\\*\\*\\* 2019-10-02: albert_large_zh,albert_base_zh \\*\\*\\*\\*\\***\n\nRelesed albert_base_zh with only 10% parameters of bert_base, a small model(40M) & training can be very fast. \n\nRelased albert_large_zh with only 16% parameters of bert_base(64M)\n\n**\\*\\*\\*\\*\\* 2019-09-28: codes and test functions \\*\\*\\*\\*\\*** \n\nAdd codes and test functions for three main changes of albert from bert\n\nALBERT模型介绍 Introduction of ALBERT\n-----------------------------------------------\nALBERT模型是BERT的改进版，与最近其他State of the art的模型不同的是，这次是预训练小模型，效果更好、参数更少。\n\n它对BERT进行了三个改造 Three main changes of ALBert from Bert：\n\n1）词嵌入向量参数的因式分解 Factorized embedding parameterization\n   \n     O(V * H) to O(V * E + E * H)\n     \n     如以ALBert_xxlarge为例，V=30000, H=4096, E=128\n       \n     那么原先参数为V * H= 30000 * 4096 = 1.23亿个参数，现在则为V * E + E * H = 30000*128+128*4096 = 384万 + 52万 = 436万，\n       \n     词嵌入相关的参数变化前是变换后的28倍。\n\n\n2）跨层参数共享 Cross-Layer Parameter Sharing\n\n     参数共享能显著减少参数。共享可以分为全连接层、注意力层的参数共享；注意力层的参数对效果的减弱影响小一点。\n\n3）段落连续性任务 Inter-sentence coherence loss.\n     \n     使用段落连续性任务。正例，使用从一个文档中连续的两个文本段落；负例，使用从一个文档中连续的两个文本段落，但位置调换了。\n     \n     避免使用原有的NSP任务，原有的任务包含隐含了预测主题这类过于简单的任务。\n\n      We maintain that inter-sentence modeling is an important aspect of language understanding, but we propose a loss \n      based primarily on coherence. That is, for ALBERT, we use a sentence-order prediction (SOP) loss, which avoids topic \n      prediction and instead focuses on modeling inter-sentence coherence. The SOP loss uses as positive examples the \n      same technique as BERT (two consecutive segments from the same document), and as negative examples the same two \n      consecutive segments but with their order swapped. This forces the model to learn finer-grained distinctions about\n      discourse-level coherence properties. \n\n其他变化，还有 Other changes：\n\n    1）去掉了dropout  Remove dropout to enlarge capacity of model.\n        最大的模型，训练了1百万步后，还是没有过拟合训练数据。说明模型的容量还可以更大，就移除了dropout\n        （dropout可以认为是随机的去掉网络中的一部分，同时使网络变小一些）\n        We also note that, even after training for 1M steps, our largest models still do not overfit to their training data. \n        As a result, we decide to remove dropout to further increase our model capacity.\n        其他型号的模型，在我们的实现中我们还是会保留原始的dropout的比例，防止模型对训练数据的过拟合。\n        \n    2）为加快训练速度，使用LAMB做为优化器 Use LAMB as optimizer, to train with big batch size\n      使用了大的batch_size来训练(4096)。 LAMB优化器使得我们可以训练，特别大的批次batch_size，如高达6万。\n    \n    3）使用n-gram(uni-gram,bi-gram, tri-gram）来做遮蔽语言模型 Use n-gram as make language model\n       即以不同的概率使用n-gram,uni-gram的概率最大，bi-gram其次，tri-gram概率最小。\n       本项目中目前使用的是在中文上做whole word mask，稍后会更新一下与n-gram mask的效果对比。n-gram从spanBERT中来。\n\n\n训练语料\u002F训练配置 Training Data & Configuration\n-----------------------------------------------\n30g中文语料，超过100亿汉字，包括多个百科、新闻、互动社区。\n\n预训练序列长度sequence_length设置为512，批次batch_size为4096，训练产生了3.5亿个训练数据(instance)；每一个模型默认会训练125k步，albert_xxlarge将训练更久。\n\n作为比较，roberta_zh预训练产生了2.5亿个训练数据、序列长度为256。由于albert_zh预训练生成的训练数据更多、使用的序列长度更长，\n \n    我们预计albert_zh会有比roberta_zh更好的性能表现，并且能更好处理较长的文本。\n\n训练使用TPU v3 Pod，我们使用的是v3-256，它包含32个v3-8。每个v3-8机器，含有128G的显存。\n\n\n模型性能与对比(英文) Performance and Comparision\n-----------------------------------------------    \n\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fbrightmart_albert_zh_readme_987536b1e775.jpg\"  width=\"80%\" height=\"40%\" \u002F>\n  \n   \n\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fbrightmart_albert_zh_readme_fbd40aba3713.jpg\"  width=\"80%\" height=\"40%\" \u002F>\n\n\n\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fbrightmart_albert_zh_readme_a5b93192f754.jpg\"  width=\"80%\" height=\"40%\" \u002F>\n\n\n中文任务集上效果对比测试 Performance on Chinese datasets\n----------------------------------------------- \n\n###  问题匹配语任务：LCQMC(Sentence Pair Matching)\n\n| 模型 | 开发集(Dev) | 测试集(Test) |\n| :------- | :---------: | :---------: |\n| BERT | 89.4(88.4) | 86.9(86.4) | \n| ERNIE | 89.8 (89.6) | 87.2 (87.0) | \n| BERT-wwm |89.4 (89.2) | 87.0 (86.8) | \n| BERT-wwm-ext | - |-  |\n| RoBERTa-zh-base | 88.7 | 87.0  |\n| RoBERTa-zh-Large | ***89.9(89.6)*** | 87.2(86.7) |\n| RoBERTa-zh-Large(20w_steps) | 89.7| 87.0 |\n| ALBERT-zh-tiny | -- | 85.4 |\n| ALBERT-zh-small | -- | 86.0 |\n| ALBERT-zh-small(Pytorch) | -- | 86.8 |\n| ALBERT-zh-base-additional-36k-steps | 87.8 | 86.3 |\n| ALBERT-zh-base | 87.2 | 86.3 |\n| ALBERT-large | 88.7 | 87.1 |\n| ALBERT-xlarge | 87.3 | ***87.7*** |\n\n注：只跑了一次ALBERT-xlarge，效果还可能提升\n\n### 自然语言推断：XNLI of Chinese Version\n\n| 模型 | 开发集 | 测试集 |\n| :------- | :---------: | :---------: |\n| BERT | 77.8 (77.4) | 77.8 (77.5) | \n| ERNIE | 79.7 (79.4) | 78.6 (78.2) | \n| BERT-wwm | 79.0 (78.4) | 78.2 (78.0) | \n| BERT-wwm-ext | 79.4 (78.6) | 78.7 (78.3) |\n| XLNet | 79.2  | 78.7 |\n| RoBERTa-zh-base | 79.8 |78.8  |\n| RoBERTa-zh-Large | 80.2 (80.0) | 79.9 (79.5) |\n| ALBERT-base | 77.0 | 77.1 |\n| ALBERT-large | 78.0 | 77.5 |\n| ALBERT-xlarge | ? | ? |\n\n注：BERT-wwm-ext来自于\u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fymcui\u002FChinese-BERT-wwm\">这里\u003C\u002Fa>；XLNet来自于\u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fymcui\u002FChinese-PreTrained-XLNet\">这里\u003C\u002Fa>; RoBERTa-zh-base，指12层RoBERTa中文模型\n   \n\n###  阅读理解任务：CRMC2018\n\n\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fbrightmart_albert_zh_readme_e58449f45a98.jpg\"  width=\"90%\" height=\"70%\" \u002F>\n\n\n### 语言模型、文本段预测准确性、训练时间 Mask Language Model Accuarcy & Training Time\n\n| Model | MLM eval acc | SOP eval acc | Training(Hours) | Loss eval |\n| :------- | :---------: | :---------: | :---------: |:---------: |\n| albert_zh_base | 79.1% | 99.0% | 6h | 1.01|\n| albert_zh_large | 80.9% | 98.6% | 22.5h | 0.93|\n| albert_zh_xlarge | ? | ? | 53h(预估) | ? |\n| albert_zh_xxlarge | ? | ? | 106h(预估) | ? |\n\n注：? 将很快替换\n\n模型参数和配置 Configuration of Models\n-----------------------------------------------\n\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fbrightmart_albert_zh_readme_c98c13ff2de0.jpg\"  width=\"80%\" height=\"40%\" \u002F>\n\n代码实现和测试 Implementation and Code Testing\n-----------------------------------------------\n通过运行以下命令测试主要的改进点，包括但不限于词嵌入向量参数的因式分解、跨层参数共享、段落连续性任务等。\n\n    python test_changes.py\n\n##### \u003Ca name=\"use_tflite\">\u003C\u002Fa>使用TensorFlow Lite(TFLite)在移动端进行部署:\n这里我们主要介绍TFLite模型格式转换和性能测试。转换成TFLite模型后，对于如何在移\n动端使用该模型，可以参考TFLite提供的[Android\u002FiOS应用完整开发案例教程页面](https:\u002F\u002Fwww.tensorflow.org\u002Flite\u002Fexamples)。\n该页面目前已经包含了[文本分类](https:\u002F\u002Fgithub.com\u002Ftensorflow\u002Fexamples\u002Fblob\u002Fmaster\u002Flite\u002Fexamples\u002Ftext_classification\u002Fandroid)，\n[文本问答](https:\u002F\u002Fgithub.com\u002Ftensorflow\u002Fexamples\u002Fblob\u002Fmaster\u002Flite\u002Fexamples\u002Fbert_qa\u002Fandroid)两个Android案例。\n\n下面以\u003Ca href=\"https:\u002F\u002Fstorage.googleapis.com\u002Falbert_zh\u002Falbert_tiny.zip\">albert_tiny_zh\u003C\u002Fa>\n为例来介绍TFLite模型格式转换和性能测试：\n\n1. Freeze graph from the checkpoint\n\nEnsure to have >=1.14 1.x installed to use the freeze_graph tool as it is removed from 2.x distribution\n\n    pip install tensorflow==1.15\n\n    freeze_graph --input_checkpoint=.\u002Falbert_model.ckpt \\\n      --output_graph=\u002Ftmp\u002Falbert_tiny_zh.pb \\\n      --output_node_names=cls\u002Fpredictions\u002Ftruediv \\\n      --checkpoint_version=1 --input_meta_graph=.\u002Falbert_model.ckpt.meta --input_binary=true\n\n2. Convert to TFLite format\n\nWe are going to use the new experimental tf->tflite converter that's distributed with the Tensorflow nightly build.\n\n    pip install tf-nightly\n\n    tflite_convert --graph_def_file=\u002Ftmp\u002Falbert_tiny_zh.pb \\\n      --input_arrays='input_ids,input_mask,segment_ids,masked_lm_positions,masked_lm_ids,masked_lm_weights' \\\n      --output_arrays='cls\u002Fpredictions\u002Ftruediv' \\\n      --input_shapes=1,128:1,128:128:1,128:1,128:1,128 \\\n      --output_file=\u002Ftmp\u002Falbert_tiny_zh.tflite \\\n      --enable_v1_converter --experimental_new_converter\n\n3. Benchmark the performance of the TFLite model\n\nSee [here](https:\u002F\u002Fgithub.com\u002Ftensorflow\u002Ftensorflow\u002Ftree\u002Fmaster\u002Ftensorflow\u002Flite\u002Ftools\u002Fbenchmark) \nfor details about the performance benchmark tools in TFLite. For example: after\nbuilding the benchmark tool binary for an Android phone, do the following to\nget an idea of how the TFLite model performs on the phone\n\n    adb push \u002Ftmp\u002Falbert_tiny_zh.tflite \u002Fdata\u002Flocal\u002Ftmp\u002F\n    adb shell \u002Fdata\u002Flocal\u002Ftmp\u002Fbenchmark_model_performance_options --graph=\u002Fdata\u002Flocal\u002Ftmp\u002Falbert_tiny_zh.tflite --perf_options_list=cpu\n\nOn an Android phone w\u002F Qualcomm's SD845 SoC, via the above benchmark tool, as\nof 2019\u002F11\u002F01, the inference latency is ~120ms w\u002F this converted TFLite model\nusing 4 threads on CPU, and the memory usage is ~60MB for the model during\ninference. Note the performance will improve further with future TFLite\nimplementation optimizations.\n\n##### 使用PyTorch版本:\n\n    download pre-trained model, and convert to PyTorch using:\n     \n      python convert_albert_tf_checkpoint_to_pytorch.py     \n     \n   using \u003Ca href=\"https:\u002F\u002Fgithub.com\u002FlonePatient\u002Falbert_pytorch\">albert_pytorch\n   \n##### 使用Keras加载:\n\n\u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fbojone\u002Fbert4keras\">bert4keras\u003C\u002Fa> 适配albert，能成功加载albert_zh的权重，只需要在load_pretrained_model函数里加上albert=True\n\nload pre-trained model with bert4keras\n\n##### 使用tf2.0加载:\n\n\u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fkpe\u002Fbert-for-tf2\">bert-for-tf2\u003C\u002Fa>\n\n\n使用案例-基于用户输入预测文本相似性 Use Case-Text Similarity Based on User Input\n-------------------------------------------------\n\n功能说明：用户可以通过本例了解如何加载训训练集实现基于用户输入的短文本相似度判断。可以基于该代码将程序灵活地拓展为后台服务或增加文本分类等示例。\n\n涉及代码：similarity.py、args.py\n\n步骤：\n\n1、使用本模型进行文本相似性训练，保存模型文件至相应目录下\n\n2、根据实际情况，修改args.py中的参数，参数说明如下：\n\n```python\n#模型目录，存放ckpt文件\nmodel_dir = os.path.join(file_path, 'albert_lcqmc_checkpoints\u002F')\n\n#config文件，存放模型的json文件\nconfig_name = os.path.join(file_path, 'albert_config\u002Falbert_config_tiny.json')\n\n#ckpt文件名称\nckpt_name = os.path.join(model_dir, 'model.ckpt')\n\n#输出文件目录，训练时的模型输出目录\noutput_dir = os.path.join(file_path, 'albert_lcqmc_checkpoints\u002F')\n\n#vocab文件目录\nvocab_file = os.path.join(file_path, 'albert_config\u002Fvocab.txt')\n\n#数据目录，训练使用的数据集存放目录\ndata_dir = os.path.join(file_path, 'data\u002F')\n```\n\n本例中的文件结构为：\n\n    |__args.py\n    \n    |__similarity.py\n    \n    |__data\n    \n    |__albert_config\n    \n    |__albert_lcqmc_checkpoints\n    \n    |__lcqmc\n\n3、修改用户输入单词\n\n打开similarity.py，最底部如下代码：\n\n```python\nif __name__ == '__main__':\n    sim = BertSim()\n    sim.start_model()\n    sim.predict_sentences([(\"我喜欢妈妈做的汤\", \"妈妈做的汤我很喜欢喝\")])\n```\n\n其中sim.start_model()表示加载模型，sim.predict_sentences的输入为一个元组数组，元组中包含两个元素分别为需要判定相似的句子。\n\n4、运行python文件：similarity.py\n\n\n支持的序列长度与批次大小的关系,12G显存 Trade off between batch Size and sequence length\n-------------------------------------------------\n\nSystem       | Seq Length | Max Batch Size\n------------ | ---------- | --------------\n`albert-base`  | 64         | 64\n...          | 128        | 32\n...          | 256        | 16\n...          | 320        | 14\n...          | 384        | 12\n...          | 512        | 6\n`albert-large` | 64         | 12\n...          | 128        | 6\n...          | 256        | 2\n...          | 320        | 1\n...          | 384        | 0\n...          | 512        | 0\n`albert-xlarge` | -         | -\n\n学习曲线 Training Loss of xlarge of albert_zh\n-------------------------------------------------\n\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fbrightmart_albert_zh_readme_a924497ae298.jpg\"  width=\"80%\" height=\"40%\" \u002F>\n\n所有的参数 Parameters of albert_xlarge\n-------------------------------------------------\n\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fbrightmart_albert_zh_readme_0a025f8c6bf2.jpg\"  width=\"80%\" height=\"40%\" \u002F>\n\n\n#### 技术交流与问题讨论QQ群: 836811304 Join us on QQ group\n\nIf you have any question, you can raise an issue, or send me an email: brightmart@hotmail.com;\n\nCurrently how to use PyTorch version of albert is not clear yet, if you know how to do that, just email us or open an issue.\n\nYou can also send pull request to report you performance on your task or add methods on how to load models for PyTorch and so on.\n\nIf you have ideas for generate best performance pre-training Chinese model, please also let me know.\n\n##### Research supported with Cloud TPUs from Google's TensorFlow Research Cloud (TFRC)\n\nCite Us\n-----------------------------------------------\nBright Liang Xu, albert_zh, (2019), GitHub repository, https:\u002F\u002Fgithub.com\u002Fbrightmart\u002Falbert_zh\n\nReference\n-----------------------------------------------\n1、\u003Ca href=\"https:\u002F\u002Farxiv.org\u002Fpdf\u002F1909.11942.pdf\">ALBERT: A Lite BERT For Self-Supervised Learning Of Language Representations\u003C\u002Fa>\n\n2、\u003Ca href=\"https:\u002F\u002Farxiv.org\u002Fpdf\u002F1810.04805.pdf\">BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding\u003C\u002Fa>\n\n3、\u003Ca href=\"https:\u002F\u002Farxiv.org\u002Fabs\u002F1907.10529\">SpanBERT: Improving Pre-training by Representing and Predicting Spans\u003C\u002Fa>\n\n4、\u003Ca href=\"https:\u002F\u002Farxiv.org\u002Fpdf\u002F1907.11692.pdf\">RoBERTa: A Robustly Optimized BERT Pretraining Approach\u003C\u002Fa>\n\n5、\u003Ca href=\"https:\u002F\u002Farxiv.org\u002Fpdf\u002F1904.00962.pdf\">Large Batch Optimization for Deep Learning: Training BERT in 76 minutes(LAMB)\u003C\u002Fa>\n\n6、\u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fymcui\u002FLAMB_Optimizer_TF\">LAMB Optimizer,TensorFlow version\u003C\u002Fa>\n\n7、\u003Ca href=\"http:\u002F\u002Fbaijiahao.baidu.com\u002Fs?id=1645712785366950083&wfr=spider&for=pc\">预训练小模型也能拿下13项NLP任务，ALBERT三大改造登顶GLUE基准\u003C\u002Fa>\n\n8、 \u003Ca href=\"https:\u002F\u002Fgithub.com\u002FlonePatient\u002Falbert_pytorch\">albert_pytorch\u003C\u002Fa>\n\n9、\u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fbojone\u002Fbert4keras\">load albert with keras\u003C\u002Fa>\n\n10、\u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fkpe\u002Fbert-for-tf2\">load albert with tf2.0\u003C\u002Fa>\n\n11、\u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fgoogle-research\u002Fgoogle-research\u002Ftree\u002Fmaster\u002Falbert\">repo of albert from google\u003C\u002Fa>\n\n12、\u003Ca href=\"https:\u002F\u002Fgithub.com\u002FchineseGLUE\u002FchineseGLUE\">chineseGLUE-中文任务基准测评：公开可用多个任务、基线模型、广泛测评与效果对比\u003C\u002Fa>\n\n\n\n\n","# albert_zh\n\n使用 TensorFlow 实现 \u003Ca href=\"https:\u002F\u002Farxiv.org\u002Fpdf\u002F1909.11942.pdf\">用于自监督学习语言表示的轻量级 BERT\u003C\u002Fa>\n\nALBert 是在 BERT 的基础上进行了一些改进。它在主要基准测试中以减少 30% 参数量的情况下，达到了最先进的性能。\n\n对于 albert_base_zh 模型而言，其参数量仅为原始 BERT 模型的十分之一，同时保持了较高的准确率。\n\n\n目前已有适用于中文的 ALBERT 预训练模型的不同版本，包括 TensorFlow、PyTorch 和 Keras。\n\n基于海量中文语料预训练的 ALBERT 模型：参数更少，效果更好。即使是小模型也能在 13 项 NLP 任务中取得优异成绩，ALBERT 的三大改进使其登顶 GLUE 基准。\n\n\u003Ca href='https:\u002F\u002Fwww.cluebenchmarks.com\u002Fclueai.html'>clueai 工具包：三行代码，三分钟即可定制一个 NLP API（零样本学习）\u003C\u002Fa>\n\n\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fbrightmart_albert_zh_readme_ccf223647a74.jpg\"  width=\"90%\" height=\"70%\" \u002F>\n\n一键运行 10 个数据集、9 个基线模型，并详细对比不同任务上的模型效果，请访问 \u003Ca href=\"http:\u002F\u002Fwww.CLUEbenchmarks.com\">CLUE 基准测试\u003C\u002Fa>。\n\n一键运行 CLUE 中文任务：6 个中文分类或句子对任务（新）\n---------------------------------------------------------------------\n    使用方式：\n    1、克隆项目\n       git clone https:\u002F\u002Fgithub.com\u002Fbrightmart\u002Falbert_zh.git\n    2、运行一键运行脚本（GPU 方式）：会自动下载模型和所有任务数据并开始运行。\n       bash run_classifier_clue.sh\n       执行该一键运行脚本将会自动下载所有任务数据，并为所有任务找到最优模型，然后测试得到提交结果\n    \n\n模型下载 中文预训练模型下载\n-----------------------------------------------\n1、\u003Ca href=\"https:\u002F\u002Fstorage.googleapis.com\u002Falbert_zh\u002Falbert_tiny.zip\">albert_tiny_zh\u003C\u002Fa>, \u003Ca href=\"https:\u002F\u002Fstorage.googleapis.com\u002Falbert_zh\u002Falbert_tiny_489k.zip\">albert_tiny_zh（训练时间更长，累计学习了 20 亿个样本）\u003C\u002Fa>, 文件大小 16M，参数量 4M\n\n    训练和推理预测速度提升约 10 倍，精度基本保持不变，模型大小仅为 BERT 的 1\u002F25；在语义相似度数据集 LCQMC 测试集上达到 85.4%，相比 BERT_base 仅下降 1.5 个百分点。\n\n    lcqmc 训练使用如下参数： --max_seq_length=128 --train_batch_size=64   --learning_rate=1e-4   --num_train_epochs=5 \n    \n    albert_tiny 使用同样的大规模中文语料数据，层数仅为 4 层，hidden size 等向量维度大幅减少；尝试使用如下学习率来获得更好效果：{2e-5, 6e-5, 1e-4} \n    \n    【使用场景】任务相对比较简单一些或实时性要求高的任务，如语义相似度等句子对任务、分类任务；比较难的任务如阅读理解等，可以使用其他大模型。\n\n     例如，可以使用 [TensorFlow Lite](https:\u002F\u002Fwww.tensorflow.org\u002Flite) 在移动端进行部署，本文 [随后](#use_tflite) 对此进行了介绍，包括如何把模型转换成 TensorFlow Lite 格式以及对其进行性能测试等。\n     \n     一键运行 albert_tiny_zh（Linux，lcqmc 任务）：\n     1) git clone https:\u002F\u002Fgithub.com\u002Fbrightmart\u002Falbert_zh\n     2) cd albert_zh\n     3) bash run_classifier_lcqmc.sh\n1.1、\u003Ca href=\"https:\u002F\u002Fstorage.googleapis.com\u002Falbert_zh\u002Falbert_tiny_zh_google.zip\">albert_tiny_google_zh（累计学习了 10 亿个样本，Google 版本）\u003C\u002Fa>, 模型大小 16M，性能与 albert_tiny_zh 一致\n\n1.2、\u003Ca href=\"https:\u002F\u002Fstorage.googleapis.com\u002Falbert_zh\u002Falbert_small_zh_google.zip\">albert_small_google_zh（累计学习了 10 亿个样本，Google 版本）\u003C\u002Fa>，\n     \n     速度比 BERT_base 快 4 倍；LCQMC 测试集上比 BERT 下降仅 0.9 个百分点；去掉 Adam 后模型大小 18.5M；使用方法，见 #下游任务 微调\n     \n2、\u003Ca href=\"https:\u002F\u002Fstorage.googleapis.com\u002Falbert_zh\u002Falbert_large_zh.zip\">albert_large_zh\u003C\u002Fa>, 参数量多，层数 24，文件大小 64M\n   \n    参数量和模型大小仅为 BERT_base 的六分之一；在口语化描述相似性数据集 LCQMC 的测试集上相比 BERT_base 提升 0.2 个百分点\n\n3、\u003Ca href=\"https:\u002F\u002Fstorage.googleapis.com\u002Falbert_zh\u002Falbert_base_zh_additional_36k_steps.zip\">albert_base_zh（额外训练了 1.5 亿个实例，即 36k 步骤 * batch_size 4096）\u003C\u002Fa>; \u003Ca href=\"https:\u002F\u002Fstorage.googleapis.com\u002Falbert_zh\u002Falbert_base_zh.zip\">albert_base_zh（小模型体验版）\u003C\u002Fa>, 参数量 12M，层数 12，大小 40M\n\n    参数量仅为 BERT_base 的十分之一，模型大小也仅为十分之一；在口语化描述相似性数据集 LCQMC 的测试集上相比 BERT_base 下降约 0.6~1 个百分点；\n    相比未预训练，albert_base 提升 14 个百分点\n\n4、\u003Ca href=\"https:\u002F\u002Fstorage.googleapis.com\u002Falbert_zh\u002Falbert_xlarge_zh_177k.zip\">albert_xlarge_zh_177k \u003C\u002Fa>; \n\u003Ca href=\"https:\u002F\u002Fstorage.googleapis.com\u002Falbert_zh\u002Falbert_xlarge_zh_183k.zip\">albert_xlarge_zh_183k（优先尝试）\u003C\u002Fa>参数量多，层数 24，文件大小 230M\n   \n    参数量和模型大小仅为 BERT_base 的一半；需要一张大的显卡；完整测试对比将后续添加；batch_size 不可太小，否则可能影响精度\n\n### 快速加载\n依托于 [Huggingface-Transformers 2.2.2](https:\u002F\u002Fgithub.com\u002Fhuggingface\u002Ftransformers)，可轻松调用以上模型。\n```\ntokenizer = AutoTokenizer.from_pretrained(\"MODEL_NAME\")\nmodel = AutoModel.from_pretrained(\"MODEL_NAME\")\n```\n\n其中 `MODEL_NAME` 对应列表如下：\n\n| 模型名 | MODEL_NAME |\n| - | - |\n| albert_tiny_google_zh | voidful\u002Falbert_chinese_tiny |\n| albert_small_google_zh | voidful\u002Falbert_chinese_small  |\n| albert_base_zh（来自 Google） | voidful\u002Falbert_chinese_base   |\n| albert_large_zh（来自 Google） | voidful\u002Falbert_chinese_large   |\n| albert_xlarge_zh（来自 Google） | voidful\u002Falbert_chinese_xlarge   |\n| albert_xxlarge_zh（来自 Google） | voidful\u002Falbert_chinese_xxlarge   |\n\n更多通过 transformers 使用 albert 的 \u003Ca href='https:\u002F\u002Fhuggingface.co\u002Fmodels?search=albert_chinese'>示例\u003C\u002Fa>\n\n预训练 预训练\n-----------------------------------------------\n\n#### 生成特定格式的文件（tfrecords） 生成 tfrecords 文件\n\n运行以下命令即可。项目自动了一个示例的文本文件（data\u002Fnews_zh_1.txt）\n   \n       bash create_pretrain_data.sh\n   \n如果你有很多文本文件，可以通过传入参数的方式，生成多个特定格式的文件（tfrecords）\n\n###### 支持英语和其他非中文语言：\n    如果你正在为英语或其他非中文语言进行预训练，\n    你应该在 create_pretraining_data.py 中将 non_chinese 参数设置为 True；否则，默认情况下是使用中文全词掩码进行中文预训练。\n\n#### 使用命令执行预训练 GPU\u002FTPU 上的预训练\n    GPU（brightmart 版，tiny 模型）：\n    export BERT_BASE_DIR=.\u002Falbert_tiny_zh\n    nohup python3 run_pretraining.py --input_file=.\u002Fdata\u002Ftf*.tfrecord  \\\n    --output_dir=.\u002Fmy_new_model_path --do_train=True --do_eval=True --bert_config_file=$BERT_BASE_DIR\u002Falbert_config_tiny.json \\\n    --train_batch_size=4096 --max_seq_length=512 --max_predictions_per_seq=51 \\\n    --num_train_steps=125000 --num_warmup_steps=12500 --learning_rate=0.00176    \\\n    --save_checkpoints_steps=2000 --init_checkpoint=$BERT_BASE_DIR\u002Falbert_model.ckpt &\n    \n    GPU（Google 版，small 模型）：\n    export BERT_BASE_DIR=.\u002Falbert_small_zh_google\n    nohup python3 run_pretraining_google.py --input_file=.\u002Fdata\u002Ftf*.tfrecord --eval_batch_size=64 \\\n    --output_dir=.\u002Fmy_new_model_path --do_train=True --do_eval=True --albert_config_file=$BERT_BASE_DIR\u002Falbert_config_small_google.json  --export_dir=.\u002Fmy_new_model_path_export \\\n    --train_batch_size=4096 --max_seq_length=512 --max_predictions_per_seq=20 \\\n    --num_train_steps=125000 --num_warmup_steps=12500 --learning_rate=0.00176   \\\n    --save_checkpoints_steps=2000 --init_checkpoint=$BERT_BASE_DIR\u002Falbert_model.ckpt\n    \n    TPU，需添加类似以下内容：\n        --use_tpu=True  --tpu_name=grpc:\u002F\u002F10.240.1.66:8470 --tpu_zone=us-central1-a\n        \n    注：如果你从头开始训练，可以不指定 init_checkpoint；\n    如果你从现有的模型基础上训练，指定一下 BERT_BASE_DIR 的路径，并确保 bert_config_file 和 init_checkpoint 两个参数的值能对应到相应的文件上；\n    领域上的预训练，根据数据的大小，可以不用训练特别久。\n\n环境 环境\n-----------------------------------------------\n使用 Python3 + Tensorflow 1.x \n\n例如 Tensorflow 1.4 或 1.5\n\n\n下游任务 下游任务微调\n-----------------------------------------------\n##### 使用 TensorFlow：\n\n以使用 albert_base 做 LCQMC 任务为例。LCQMC 任务是在口语化描述的数据集上做文本的相似性预测。\n\n我们将使用 LCQMC 数据集进行微调，这是一个口语语言语料库，用于训练和预测一对句子之间的语义相似性。\n\n下载 \u003Ca href=\"https:\u002F\u002Fdrive.google.com\u002Fopen?id=1HXYMqsXjmA5uIfu_SFqP7r_vZZG-m_H0\">LCQMC\u003C\u002Fa> 数据集，包含训练、验证和测试集，训练集包含 24 万句口语化的中文句子对，标签为 1 或 0。1 表示句子语义相似，0 表示语义不相似。\n\n通过运行下列命令做LCQMC数据集上的fine-tuning:\n    \n    1. 克隆该项目：\n          \n          git clone https:\u002F\u002Fgithub.com\u002Fbrightmart\u002Falbert_zh.git\n          \n    2. 运行以下命令进行微调。\n        brightmart版本的tiny模型\n        export BERT_BASE_DIR=.\u002Falbert_tiny_zh\n        export TEXT_DIR=.\u002Flcqmc\n        nohup python3 run_classifier.py   --task_name=lcqmc_pair   --do_train=true   --do_eval=true   --data_dir=$TEXT_DIR   --vocab_file=.\u002Falbert_config\u002Fvocab.txt  \\\n        --bert_config_file=.\u002Falbert_config\u002Falbert_config_tiny.json --max_seq_length=128 --train_batch_size=64   --learning_rate=1e-4  --num_train_epochs=5 \\\n        --output_dir=.\u002Falbert_lcqmc_checkpoints --init_checkpoint=$BERT_BASE_DIR\u002Falbert_model.ckpt &\n        \n        google版本的small模型\n        export BERT_BASE_DIR=.\u002Falbert_small_zh\n        export TEXT_DIR=.\u002Flcqmc\n        nohup python3 run_classifier_sp_google.py --task_name=lcqmc_pair   --do_train=true   --do_eval=true   --data_dir=$TEXT_DIR   --vocab_file=.\u002Falbert_config\u002Fvocab.txt  \\\n        --albert_config_file=.\u002F$BERT_BASE_DIR\u002Falbert_config_small_google.json --max_seq_length=128 --train_batch_size=64   --learning_rate=1e-4   --num_train_epochs=5 \\\n        --output_dir=.\u002Falbert_lcqmc_checkpoints --init_checkpoint=$BERT_BASE_DIR\u002Falbert_model.ckpt &\n\n    注意事项\u002F注：\n        1) 你需要下载预训练的中文ALBERT模型，并将LCQMC数据集也下载到项目目录中，假设模型目录名为albert_tiny_zh；数据集目录名为lcqmc。\n\n        2) 在微调过程中，你可以尝试在albert_config_xxx.json文件中调整attention_probs_dropout_prob和hidden_dropout_prob参数，加入少量的dropout（例如0.1）。默认情况下，我们没有设置dropout。\n\n        3) 为了获得更好的性能，可以尝试不同的学习率{2e-5, 6e-5, 1e-4}。\n\n\n更新\n-----------------------------------------------\n**\\*\\*\\*\\*\\* 2019年11月3日：新增Google版本的albert_small和albert_tiny；\n\n同时提供了一种方法，可将albert_tiny部署到移动设备上，对于序列长度为128的输入，推理时间仅需0.1秒，内存占用仅为60M \\*\\*\\*\\*\\***\n\n**\\*\\*\\*\\*\\* 2019年10月30日：添加了关于如何将模型转换为TensorFlow Lite以便边缘部署的简单指南 \\*\\*\\*\\*\\***\n\n**\\*\\*\\*\\*\\* 2019年10月15日：albert_tiny_zh的训练和推理速度是bert base的10倍，且保持了较高的准确率 \\*\\*\\*\\*\\***\n\n**\\*\\*\\*\\*\\* 2019年10月7日：增加了更多ALBERT模型 \\*\\*\\*\\*\\***\n\n新增albert_xlarge_zh；albert_base_zh_additional_steps，使用更多实例进行训练。\n\n**\\*\\*\\*\\*\\* 2019年10月4日：支持了ALBERT的PyTorch和Keras版本 \\*\\*\\*\\*\\***\n\na. 转换为PyTorch版本，并通过\u003Ca href=\"https:\u002F\u002Fgithub.com\u002FlonePatient\u002Falbert_pytorch\">albert_pytorch\u003C\u002Fa>完成你的任务。\n\nb. 使用keras加载预训练模型，只需一行代码，通过\u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fbojone\u002Fbert4keras\">bert4keras\u003C\u002Fa>即可实现。\n\nc. 使用TensorFlow 2.0中的ALBERT：通过\u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fkpe\u002Fbert-for-tf2\">bert-for-tf2\u003C\u002Fa>使用或加载预训练模型。\n\n10月6日发布了albert_xlarge。\n\n**\\*\\*\\*\\*\\* 2019年10月2日：albert_large_zh和albert_base_zh \\*\\*\\*\\*\\***\n\n发布了albert_base_zh，其参数量仅为bert_base的10%，是一个小型模型（40M），训练速度非常快。\n\n同时发布了albert_large_zh，其参数量仅为bert_base的16%（64M）。\n\n**\\*\\*\\*\\*\\* 2019年9月28日：代码和测试函数 \\*\\*\\*\\*\\***\n\n为ALBERT相对于BERT的三项主要改动添加了代码和测试函数。\n\nALBERT模型介绍 Introduction of ALBERT\n-----------------------------------------------\nALBERT模型是BERT的改进版，与最近其他State of the art的模型不同的是，这次是预训练小模型，效果更好、参数更少。\n\n它对BERT进行了三个改造 Three main changes of ALBert from Bert：\n\n1）词嵌入向量参数的因式分解 Factorized embedding parameterization\n   \n     O(V * H) to O(V * E + E * H)\n     \n     如以ALBert_xxlarge为例，V=30000, H=4096, E=128\n       \n     那么原先参数为V * H= 30000 * 4096 = 1.23亿个参数，现在则为V * E + E * H = 30000*128+128*4096 = 384万 + 52万 = 436万，\n       \n     词嵌入相关的参数变化前是变换后的28倍。\n\n\n2）跨层参数共享 Cross-Layer Parameter Sharing\n\n     参数共享能显著减少参数。共享可以分为全连接层、注意力层的参数共享；注意力层的参数对效果的减弱影响小一点。\n\n3）段落连续性任务 Inter-sentence coherence loss.\n     \n     使用段落连续性任务。正例，使用从一个文档中连续的两个文本段落；负例，使用从一个文档中连续的两个文本段落，但位置调换了。\n     \n     避免使用原有的NSP任务，原有的任务包含隐含了预测主题这类过于简单的任务。\n\n      We maintain that inter-sentence modeling is an important aspect of language understanding, but we propose a loss \n      based primarily on coherence. That is, for ALBERT, we use a sentence-order prediction (SOP) loss, which avoids topic \n      prediction and instead focuses on modeling inter-sentence coherence. The SOP loss uses as positive examples the \n      same technique as BERT (two consecutive segments from the same document), and as negative examples the same two \n      consecutive segments but with their order swapped. This forces the model to learn finer-grained distinctions about\n      discourse-level coherence properties. \n\n其他变化，还有 Other changes：\n\n    1）去掉了dropout Remove dropout to enlarge capacity of model.\n        最大的模型，训练了1百万步后，还是没有过拟合训练数据。说明模型的容量还可以更大，就移除了dropout\n        （dropout可以认为是随机的去掉网络中的一部分，同时使网络变小一些）\n        We also note that, even after training for 1M steps, our largest models still do not overfit to their training data. \n        As a result, we decide to remove dropout to further increase our model capacity.\n        其他型号的模型，在我们的实现中我们还是会保留原始的dropout的比例，防止模型对训练数据的过拟合。\n        \n    2）为加快训练速度，使用LAMB做为优化器 Use LAMB as optimizer, to train with big batch size\n      使用了大的batch_size来训练(4096)。 LAMB优化器使得我们可以训练，特别大的批次batch_size，如高达6万。\n    \n    3）使用n-gram(uni-gram,bi-gram, tri-gram）来做遮蔽语言模型 Use n-gram as make language model\n       即以不同的概率使用n-gram，uni-gram的概率最大，bi-gram其次，tri-gram概率最小。\n       本项目中目前使用的是在中文上做whole word mask，稍后会更新一下与n-gram mask的效果对比。n-gram从spanBERT中来。\n\n\n训练语料\u002F训练配置 Training Data & Configuration\n-----------------------------------------------\n30g中文语料，超过100亿汉字，包括多个百科、新闻、互动社区。\n\n预训练序列长度sequence_length设置为512，批次batch_size为4096，训练产生了3.5亿个训练数据(instance)；每一个模型默认会训练125k步，albert_xxlarge将训练更久。\n\n作为比较，roberta_zh预训练产生了2.5亿个训练数据、序列长度为256。由于albert_zh预训练生成的训练数据更多、使用的序列长度更长，\n \n    我们预计albert_zh会有比roberta_zh更好的性能表现，并且能更好处理较长的文本。\n\n训练使用TPU v3 Pod，我们使用的是v3-256，它包含32个v3-8。每个v3-8机器，含有128G的显存。\n\n\n模型性能与对比(英文) Performance and Comparision\n-----------------------------------------------    \n\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fbrightmart_albert_zh_readme_987536b1e775.jpg\"  width=\"80%\" height=\"40%\" \u002F>\n  \n   \n\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fbrightmart_albert_zh_readme_fbd40aba3713.jpg\"  width=\"80%\" height=\"40%\" \u002F>\n\n\n\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fbrightmart_albert_zh_readme_a5b93192f754.jpg\"  width=\"80%\" height=\"40%\" \u002F>\n\n\n中文任务集上效果对比测试 Performance on Chinese datasets\n-----------------------------------------------\n\n### 问题匹配语任务：LCQMC（句子对匹配）\n\n| 模型 | 开发集(Dev) | 测试集(Test) |\n| :------- | :---------: | :---------: |\n| BERT | 89.4(88.4) | 86.9(86.4) | \n| ERNIE | 89.8 (89.6) | 87.2 (87.0) | \n| BERT-wwm |89.4 (89.2) | 87.0 (86.8) | \n| BERT-wwm-ext | - |-  |\n| RoBERTa-zh-base | 88.7 | 87.0  |\n| RoBERTa-zh-Large | ***89.9(89.6)*** | 87.2(86.7) |\n| RoBERTa-zh-Large(20w_steps) | 89.7| 87.0 |\n| ALBERT-zh-tiny | -- | 85.4 |\n| ALBERT-zh-small | -- | 86.0 |\n| ALBERT-zh-small(Pytorch) | -- | 86.8 |\n| ALBERT-zh-base-additional-36k-steps | 87.8 | 86.3 |\n| ALBERT-zh-base | 87.2 | 86.3 |\n| ALBERT-large | 88.7 | 87.1 |\n| ALBERT-xlarge | 87.3 | ***87.7*** |\n\n注：只跑了一次ALBERT-xlarge，效果还可能提升\n\n### 自然语言推断：XNLI 的中文版\n\n| 模型 | 开发集 | 测试集 |\n| :------- | :---------: | :---------: |\n| BERT | 77.8 (77.4) | 77.8 (77.5) | \n| ERNIE | 79.7 (79.4) | 78.6 (78.2) | \n| BERT-wwm | 79.0 (78.4) | 78.2 (78.0) | \n| BERT-wwm-ext | 79.4 (78.6) | 78.7 (78.3) |\n| XLNet | 79.2  | 78.7 |\n| RoBERTa-zh-base | 79.8 |78.8  |\n| RoBERTa-zh-Large | 80.2 (80.0) | 79.9 (79.5) |\n| ALBERT-base | 77.0 | 77.1 |\n| ALBERT-large | 78.0 | 77.5 |\n| ALBERT-xlarge | ? | ? |\n\n注：BERT-wwm-ext来自于\u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fymcui\u002FChinese-BERT-wwm\">这里\u003C\u002Fa>；XLNet来自于\u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fymcui\u002FChinese-PreTrained-XLNet\">这里\u003C\u002Fa>; RoBERTa-zh-base，指12层RoBERTa中文模型\n   \n\n###  阅读理解任务：CRMC2018\n\n\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fbrightmart_albert_zh_readme_e58449f45a98.jpg\"  width=\"90%\" height=\"70%\" \u002F>\n\n\n### 语言模型、文本段预测准确性、训练时间 Mask Language Model Accuarcy & Training Time\n\n| Model | MLM eval acc | SOP eval acc | Training(Hours) | Loss eval |\n| :------- | :---------: | :---------: | :---------: |:---------: |\n| albert_zh_base | 79.1% | 99.0% | 6h | 1.01|\n| albert_zh_large | 80.9% | 98.6% | 22.5h | 0.93|\n| albert_zh_xlarge | ? | ? | 53h(预估) | ? |\n| albert_zh_xxlarge | ? | ? | 106h(预估) | ? |\n\n注：? 将很快替换\n\n模型参数和配置 Configuration of Models\n-----------------------------------------------\n\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fbrightmart_albert_zh_readme_c98c13ff2de0.jpg\"  width=\"80%\" height=\"40%\" \u002F>\n\n代码实现和测试 Implementation and Code Testing\n-----------------------------------------------\n通过运行以下命令测试主要的改进点，包括但不限于词嵌入向量参数的因式分解、跨层参数共享、段落连续性任务等。\n\n    python test_changes.py\n\n##### \u003Ca name=\"use_tflite\">\u003C\u002Fa>使用TensorFlow Lite(TFLite)在移动端进行部署:\n这里我们主要介绍TFLite模型格式转换和性能测试。转换成TFLite模型后，对于如何在移\n动端使用该模型，可以参考TFLite提供的[Android\u002FiOS应用完整开发案例教程页面](https:\u002F\u002Fwww.tensorflow.org\u002Flite\u002Fexamples)。\n该页面目前已经包含了[文本分类](https:\u002F\u002Fgithub.com\u002Ftensorflow\u002Fexamples\u002Fblob\u002Fmaster\u002Flite\u002Fexamples\u002Ftext_classification\u002Fandroid)，\n[文本问答](https:\u002F\u002Fgithub.com\u002Ftensorflow\u002Fexamples\u002Fblob\u002Fmaster\u002Flite\u002Fexamples\u002Fbert_qa\u002Fandroid)两个Android案例。\n\n下面以\u003Ca href=\"https:\u002F\u002Fstorage.googleapis.com\u002Falbert_zh\u002Falbert_tiny.zip\">albert_tiny_zh\u003C\u002Fa>\n为例来介绍TFLite模型格式转换和性能测试：\n\n1. Freeze graph from the checkpoint\n\nEnsure to have >=1.14 1.x installed to use the freeze_graph tool as it is removed from 2.x distribution\n\n    pip install tensorflow==1.15\n\n    freeze_graph --input_checkpoint=.\u002Falbert_model.ckpt \\\n      --output_graph=\u002Ftmp\u002Falbert_tiny_zh.pb \\\n      --output_node_names=cls\u002Fpredictions\u002Ftruediv \\\n      --checkpoint_version=1 --input_meta_graph=.\u002Falbert_model.ckpt.meta --input_binary=true\n\n2. Convert to TFLite format\n\nWe are going to use the new experimental tf->tflite converter that's distributed with the Tensorflow nightly build.\n\n    pip install tf-nightly\n\n    tflite_convert --graph_def_file=\u002Ftmp\u002Falbert_tiny_zh.pb \\\n      --input_arrays='input_ids,input_mask,segment_ids,masked_lm_positions,masked_lm_ids,masked_lm_weights' \\\n      --output_arrays='cls\u002Fpredictions\u002Ftruediv' \\\n      --input_shapes=1,128:1,128:128:1,128:1,128:1,128 \\\n      --output_file=\u002Ftmp\u002Falbert_tiny_zh.tflite \\\n      --enable_v1_converter --experimental_new_converter\n\n3. Benchmark the performance of the TFLite model\n\nSee [here](https:\u002F\u002Fgithub.com\u002Ftensorflow\u002Ftensorflow\u002Ftree\u002Fmaster\u002Ftensorflow\u002Flite\u002Ftools\u002Fbenchmark) \nfor details about the performance benchmark tools in TFLite. For example: after\nbuilding the benchmark tool binary for an Android phone, do the following to\nget an idea of how the TFLite model performs on the phone\n\n    adb push \u002Ftmp\u002Falbert_tiny_zh.tflite \u002Fdata\u002Flocal\u002Ftmp\u002F\n    adb shell \u002Fdata\u002Flocal\u002Ftmp\u002Fbenchmark_model_performance_options --graph=\u002Fdata\u002Flocal\u002Ftmp\u002Falbert_tiny_zh.tflite --perf_options_list=cpu\n\nOn an Android phone w\u002F Qualcomm's SD845 SoC, via the above benchmark tool, as\nof 2019\u002F11\u002F01, the inference latency is ~120ms w\u002F this converted TFLite model\nusing 4 threads on CPU, and the memory usage is ~60MB for the model during\ninference. Note the performance will improve further with future TFLite\nimplementation optimizations.\n\n##### 使用PyTorch版本:\n\n    download pre-trained model, and convert to PyTorch using:\n     \n      python convert_albert_tf_checkpoint_to_pytorch.py     \n     \n   using \u003Ca href=\"https:\u002F\u002Fgithub.com\u002FlonePatient\u002Falbert_pytorch\">albert_pytorch\n   \n##### 使用Keras加载:\n\n\u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fbojone\u002Fbert4keras\">bert4keras\u003C\u002Fa> 适配albert，能成功加载albert_zh的权重，只需要在load_pretrained_model函数里加上albert=True\n\nload pre-trained model with bert4keras\n\n##### 使用tf2.0加载:\n\n\u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fkpe\u002Fbert-for-tf2\">bert-for-tf2\u003C\u002Fa>\n\n\n使用案例-基于用户输入预测文本相似性 Use Case-Text Similarity Based on User Input\n-------------------------------------------------\n\n功能说明：用户可以通过本例了解如何加载训训练集实现基于用户输入的短文本相似度判断。可以基于该代码将程序灵活地拓展为后台服务或增加文本分类等示例。\n\n涉及代码：similarity.py、args.py\n\n步骤：\n\n1、使用本模型进行文本相似性训练，保存模型文件至相应目录下\n\n2、根据实际情况，修改args.py中的参数，参数说明如下：\n\n```python\n#模型目录，存放ckpt文件\nmodel_dir = os.path.join(file_path, 'albert_lcqmc_checkpoints\u002F')\n\n#config文件，存放模型的json文件\nconfig_name = os.path.join(file_path, 'albert_config\u002Falbert_config_tiny.json')\n\n#ckpt文件名称\nckpt_name = os.path.join(model_dir, 'model.ckpt')\n\n#输出文件目录，训练时的模型输出目录\noutput_dir = os.path.join(file_path, 'albert_lcqmc_checkpoints\u002F')\n\n#vocab文件目录\nvocab_file = os.path.join(file_path, 'albert_config\u002Fvocab.txt')\n\n#数据目录，训练使用的数据集存放目录\ndata_dir = os.path.join(file_path, 'data\u002F')\n```\n\n本例中的文件结构为：\n\n    |__args.py\n    \n    |__similarity.py\n    \n    |__data\n    \n    |__albert_config\n    \n    |__albert_lcqmc_checkpoints\n    \n    |__lcqmc\n\n3、修改用户输入单词\n\n打开similarity.py，最底部如下代码：\n\n```python\nif __name__ == '__main__':\n    sim = BertSim()\n    sim.start_model()\n    sim.predict_sentences([(\"我喜欢妈妈做的汤\", \"妈妈做的汤我很喜欢喝\")])\n```\n\n其中sim.start_model()表示加载模型，sim.predict_sentences的输入为一个元组数组，元组中包含两个元素分别为需要判定相似的句子。\n\n4、运行python文件：similarity.py\n\n\n支持的序列长度与批次大小的关系,12G显存 Trade off between batch Size and sequence length\n-------------------------------------------------\n\nSystem       | Seq Length | Max Batch Size\n------------ | ---------- | --------------\n`albert-base`  | 64         | 64\n...          | 128        | 32\n...          | 256        | 16\n...          | 320        | 14\n...          | 384        | 12\n...          | 512        | 6\n`albert-large` | 64         | 12\n...          | 128        | 6\n...          | 256        | 2\n...          | 320        | 1\n...          | 384        | 0\n...          | 512        | 0\n`albert-xlarge` | -         | -\n\nAlbert_zh xlarge 的训练损失学习曲线\n-------------------------------------------------\n\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fbrightmart_albert_zh_readme_a924497ae298.jpg\"  width=\"80%\" height=\"40%\" \u002F>\n\nAlbert_xlarge 的所有参数\n-------------------------------------------------\n\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fbrightmart_albert_zh_readme_0a025f8c6bf2.jpg\"  width=\"80%\" height=\"40%\" \u002F>\n\n\n#### 技术交流与问题讨论QQ群: 836811304 加入我们的QQ群\n\n如果您有任何疑问，可以提交一个问题，或者发送邮件至：brightmart@hotmail.com；\n\n目前使用 PyTorch 版本的 Albert 尚不明确，如果您知道如何操作，请随时发邮件给我们或提交一个问题。\n\n您也可以提交 Pull Request，报告您在特定任务上的性能表现，或者添加关于如何加载 PyTorch 模型的方法等内容。\n\n如果您有关于训练出最佳性能中文预训练模型的想法，也欢迎告知我们。\n\n##### 研究得到了来自 Google TensorFlow Research Cloud (TFRC) 的 Cloud TPU 支持\n\n引用我们\n-----------------------------------------------\nBright Liang Xu, albert_zh, (2019), GitHub 仓库, https:\u002F\u002Fgithub.com\u002Fbrightmart\u002Falbert_zh\n\n参考文献\n-----------------------------------------------\n1、\u003Ca href=\"https:\u002F\u002Farxiv.org\u002Fpdf\u002F1909.11942.pdf\">ALBERT: 一种用于语言表示自监督学习的轻量级 BERT\u003C\u002Fa>\n\n2、\u003Ca href=\"https:\u002F\u002Farxiv.org\u002Fpdf\u002F1810.04805.pdf\">BERT: 面向语言理解的深度双向 Transformer 预训练\u003C\u002Fa>\n\n3、\u003Ca href=\"https:\u002F\u002Farxiv.org\u002Fabs\u002F1907.10529\">SpanBERT: 通过表示和预测跨度提升预训练效果\u003C\u002Fa>\n\n4、\u003Ca href=\"https:\u002F\u002Farxiv.org\u002Fpdf\u002F1907.11692.pdf\">RoBERTa: 一种鲁棒优化的 BERT 预训练方法\u003C\u002Fa>\n\n5、\u003Ca href=\"https:\u002F\u002Farxiv.org\u002Fpdf\u002F1904.00962.pdf\">面向深度学习的大批量优化：76 分钟内完成 BERT 训练（LAMB）\u003C\u002Fa>\n\n6、\u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fymcui\u002FLAMB_Optimizer_TF\">LAMB 优化器，TensorFlow 版本\u003C\u002Fa>\n\n7、\u003Ca href=\"http:\u002F\u002Fbaijiahao.baidu.com\u002Fs?id=1645712785366950083&wfr=spider&for=pc\">预训练小模型也能拿下 13 项 NLP 任务，ALBERT 三大改造登顶 GLUE 基准\u003C\u002Fa>\n\n8、 \u003Ca href=\"https:\u002F\u002Fgithub.com\u002FlonePatient\u002Falbert_pytorch\">albert_pytorch\u003C\u002Fa>\n\n9、\u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fbojone\u002Fbert4keras\">使用 Keras 加载 Albert\u003C\u002Fa>\n\n10、\u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fkpe\u002Fbert-for-tf2\">使用 TF 2.0 加载 Albert\u003C\u002Fa>\n\n11、\u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fgoogle-research\u002Fgoogle-research\u002Ftree\u002Fmaster\u002Falbert\">Google 官方的 Albert 仓库\u003C\u002Fa>\n\n12、\u003Ca href=\"https:\u002F\u002Fgithub.com\u002FchineseGLUE\u002FchineseGLUE\">chineseGLUE-中文任务基准测评：公开可用多个任务、基线模型、广泛测评与效果对比\u003C\u002Fa>","# albert_zh 快速上手指南\n\n`albert_zh` 是基于 BERT 改进的轻量级中文预训练模型项目。它在大幅减少参数量（仅为 BERT 的 10%~50%）的同时，保持了优异的 NLP 任务性能，特别适合资源受限或对推理速度有要求的场景。\n\n## 1. 环境准备\n\n本项目主要基于 **Python 3** 和 **TensorFlow 1.x** 开发。\n\n*   **操作系统**: Linux (推荐), macOS, Windows\n*   **Python 版本**: Python 3.6+\n*   **核心依赖**:\n    *   TensorFlow 1.4 ~ 1.15 (官方示例基于 TF 1.x)\n    *   Git\n*   **硬件要求**:\n    *   **训练\u002F微调**: 建议使用 NVIDIA GPU (支持 CUDA)。\n    *   **推理**: CPU 即可运行 Tiny\u002FSmall 版本模型；大模型建议配备 GPU。\n\n> **注意**: 虽然项目原生支持 TensorFlow 1.x，但你也可以通过 `Huggingface Transformers` 或 `bert4keras` 等库在 TensorFlow 2.x 或 PyTorch 环境中加载这些预训练权重（详见“基本使用”部分）。\n\n## 2. 安装步骤\n\n### 第一步：克隆项目\n使用 Git 将代码仓库下载到本地：\n\n```bash\ngit clone https:\u002F\u002Fgithub.com\u002Fbrightmart\u002Falbert_zh.git\ncd albert_zh\n```\n\n### 第二步：下载预训练模型\n根据任务需求选择合适的模型版本。以下是常用模型的下载链接（直接下载解压到项目根目录）：\n\n| 模型版本 | 特点 | 文件大小 | 下载链接 |\n| :--- | :--- | :--- | :--- |\n| **albert_tiny_zh** | 极速，参数量极少 (4M)，适合移动端或简单任务 | 16M | [下载 albert_tiny_zh](https:\u002F\u002Fstorage.googleapis.com\u002Falbert_zh\u002Falbert_tiny.zip) |\n| **albert_base_zh** | 平衡版，参数量为 BERT 的 1\u002F10，通用性强 | 40M | [下载 albert_base_zh](https:\u002F\u002Fstorage.googleapis.com\u002Falbert_zh\u002Falbert_base_zh.zip) |\n| **albert_large_zh** | 高性能，参数量为 BERT 的 1\u002F6 | 64M | [下载 albert_large_zh](https:\u002F\u002Fstorage.googleapis.com\u002Falbert_zh\u002Falbert_large_zh.zip) |\n\n*下载后请确保目录结构正确，例如 `albert_tiny_zh\u002F` 文件夹下应包含 `albert_model.ckpt` 和 `albert_config_tiny.json` 等文件。*\n\n### 第三步：安装 Python 依赖\n确保已安装 TensorFlow 1.x 环境：\n\n```bash\npip install tensorflow-gpu==1.15.0  # 如有 GPU\n# 或者\npip install tensorflow==1.15.0      # 仅 CPU\n```\n\n## 3. 基本使用\n\n### 方式一：一键运行示例任务 (原生 TensorFlow)\n项目提供了脚本，可自动下载数据并运行经典的 **LCQMC** (句子语义相似度) 任务。以下以 `albert_tiny` 模型为例：\n\n```bash\n# 1. 设置模型路径环境变量\nexport BERT_BASE_DIR=.\u002Falbert_tiny_zh\n\n# 2. 运行分类任务脚本 (会自动下载 LCQMC 数据集并开始训练\u002F评估)\nbash run_classifier_lcqmc.sh\n```\n*注：该脚本会在后台运行 (`nohup`)，日志输出在当前目录。如需自定义参数（如学习率、batch_size），可编辑对应的 `.sh` 文件或参考 README 中的详细命令。*\n\n### 方式二：通过 Huggingface Transformers 快速加载 (推荐)\n如果你习惯使用 `transformers` 库，可以直接一行代码加载中文 ALBERT 模型，无需手动下载权重文件。\n\n**前置安装：**\n```bash\npip install transformers torch tensorflow\n```\n\n**Python 代码示例：**\n```python\nfrom transformers import AutoTokenizer, AutoModel\n\n# 选择模型名称 (对应不同的 albert_zh 版本)\n# 可选: voidful\u002Falbert_chinese_tiny, voidful\u002Falbert_chinese_base, voidful\u002Falbert_chinese_large 等\nmodel_name = \"voidful\u002Falbert_chinese_tiny\"\n\n# 加载分词器和模型\ntokenizer = AutoTokenizer.from_pretrained(model_name)\nmodel = AutoModel.from_pretrained(model_name)\n\n# 简单测试\ntext = \"ALBERT 模型非常轻量且高效\"\ninputs = tokenizer(text, return_tensors=\"tf\") # 或 return_tensors=\"pt\" 用于 PyTorch\n\noutputs = model(**inputs)\nprint(\"最后隐藏层状态形状:\", outputs.last_hidden_state.shape)\n```\n\n**支持的 Huggingface 模型标识符：**\n\n| 本地模型名 | Huggingface Model Name |\n| :--- | :--- |\n| albert_tiny_google_zh | `voidful\u002Falbert_chinese_tiny` |\n| albert_small_google_zh | `voidful\u002Falbert_chinese_small` |\n| albert_base_zh | `voidful\u002Falbert_chinese_base` |\n| albert_large_zh | `voidful\u002Falbert_chinese_large` |\n| albert_xlarge_zh | `voidful\u002Falbert_chinese_xlarge` |\n\n### 方式三：自定义下游任务微调\n若要针对自己的数据集进行微调，需准备 TFRecord 格式数据（或使用项目提供的转换脚本 `create_pretrain_data.sh` 生成），然后运行分类脚本：\n\n```bash\nexport BERT_BASE_DIR=.\u002Falbert_base_zh\nexport TEXT_DIR=.\u002Fyour_custom_data_dir\n\npython3 run_classifier.py \\\n  --task_name=your_task \\\n  --do_train=true \\\n  --do_eval=true \\\n  --data_dir=$TEXT_DIR \\\n  --vocab_file=.\u002Falbert_config\u002Fvocab.txt \\\n  --bert_config_file=$BERT_BASE_DIR\u002Falbert_config_base.json \\\n  --max_seq_length=128 \\\n  --train_batch_size=32 \\\n  --learning_rate=2e-5 \\\n  --num_train_epochs=3 \\\n  --output_dir=.\u002Foutput_checkpoints \\\n  --init_checkpoint=$BERT_BASE_DIR\u002Falbert_model.ckpt\n```\n\n> **提示**: 对于中文任务，建议尝试的学习率范围为 `{2e-5, 6e-5, 1e-4}`。","某电商初创团队需要在移动端 App 中实时上线“用户评论语义相似度匹配”功能，以自动合并重复反馈并优化客服响应。\n\n### 没有 albert_zh 时\n- **部署困难**：传统的 BERT 基座模型体积庞大（约 400MB+），难以嵌入对安装包大小敏感的移动应用，导致无法在端侧运行。\n- **响应延迟**：服务器端推理耗时高，用户提交评论后需等待数秒才能看到匹配结果，严重破坏实时交互体验。\n- **成本高昂**：为了维持低延迟，不得不采购大量高性能 GPU 服务器进行并发处理，初创期算力成本压力巨大。\n- **精度妥协**：若强行替换为简单的关键词匹配算法，虽速度提升但无法理解“质量不错”与“做工很好”等语义相似性，准确率大幅下滑。\n\n### 使用 albert_zh 后\n- **轻量部署**：选用仅 16MB 的 `albert_tiny_zh` 模型，参数量仅为 BERT 的 1\u002F25，轻松通过 TensorFlow Lite 转换并集成至手机端离线运行。\n- **极速响应**：得益于参数精简与架构优化，推理速度提升约 10 倍，实现了毫秒级的语义匹配反馈，用户无感知等待。\n- **降本增效**：极小的模型体积降低了对服务器算力的依赖，同等并发下资源消耗减少 90%，显著节省运营成本。\n- **精度保留**：在 LCQMC 语义相似度任务上，albert_zh 相比 BERT 仅损失 1.5% 的准确率（仍达 85.4%），远优于传统规则算法，完美平衡了性能与效果。\n\nalbert_zh 通过极致的轻量化设计，让高精度的中文语义理解能力得以低成本、低延迟地落地于资源受限的边缘设备与实时业务场景中。","https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fbrightmart_albert_zh_c4af4813.png","brightmart","https:\u002F\u002Foss.gittoolsai.com\u002Favatars\u002Fbrightmart_e93d5e1a.jpg","Liang Xu, NLP with deep learning. \r\nMainly focus on text classification, intention recognition, QA & task oriented dialogue, pre-trained models.","https:\u002F\u002Fwww.CLUEbenchmarks.com","hangzhou,china","brightmart@hotmail.com",null,"https:\u002F\u002Fgithub.com\u002Fbrightmart",[82,86],{"name":83,"color":84,"percentage":85},"Python","#3572A5",98.8,{"name":87,"color":88,"percentage":89},"Shell","#89e051",1.2,3984,746,"2026-04-13T02:19:30","Linux","训练和一键运行脚本需要 GPU (支持 TPU)，具体型号未说明。albert_xlarge_zh 版本明确提到“需要一张大的显卡”。移动端部署可使用 Tensorflow Lite。","未说明 (移动端部署提及需 60M 内存)",{"notes":97,"python":98,"dependencies":99},"1. 核心环境要求为 Python 3 + TensorFlow 1.x (如 1.4\u002F1.5)，与当前主流 TF 2.x 不直接兼容，需注意版本控制。\n2. 提供 PyTorch 和 Keras 版本的第三方支持链接，但本项目原生代码基于 TensorFlow。\n3. albert_xlarge_zh 等大模型对显存要求较高，且 batch_size 不能太小以免影响精度。\n4. 预训练非中文数据时，需在脚本中将 non_chinese 参数设为 True。\n5. 一键运行脚本 (bash run_classifier_clue.sh) 仅在 Linux 环境下测试提及。","Python 3",[100,101],"TensorFlow 1.x (推荐 1.4 或 1.5)","Huggingface-Transformers (>=2.2.2, 用于快速加载)",[35,14],[104,105,106,107,108,109,110,111,112],"albert","bert","roberta","xlnet","tensorflow","pytorch","pre-trained-model","chinese-corpus","pre-trained","2026-03-27T02:49:30.150509","2026-04-13T22:50:59.531239",[116,121,126,131,136,141,146],{"id":117,"question_zh":118,"answer_zh":119,"source_url":120},31947,"ALBERT 模型推理速度提升不明显，只是训练速度变快了吗？","ALBERT 的 tiny 版本在 GPU 上比 BERT 快很多，具体效果对比可参考 README。GPU 的效率提升通常依赖于批量处理（batching），单条数据的加速可能不如批量处理显著。","https:\u002F\u002Fgithub.com\u002Fbrightmart\u002Falbert_zh\u002Fissues\u002F2",{"id":122,"question_zh":123,"answer_zh":124,"source_url":125},31948,"是否会发布英文版的 ALBERT 预训练模型？","目前暂无发布英文版预训练模型的计划。如果需要相关数据集，可以尝试使用开源工具（如 bookcorpus 爬虫库）自行获取数据。","https:\u002F\u002Fgithub.com\u002Fbrightmart\u002Falbert_zh\u002Fissues\u002F11",{"id":127,"question_zh":128,"answer_zh":129,"source_url":130},31949,"在自己数据上训练 ALBERT 时收敛速度慢或 Loss 不下降怎么办？","这可能是因为数据量较小或学习率设置不当。如果是使用 12 层模型，需要调整学习率。另外，有用户发现离线计算 embedding 和 projection 矩阵相乘后值域过小，导致优化在 0 附近徘徊，手动将参数乘以 2 后收敛效果有明显改善。","https:\u002F\u002Fgithub.com\u002Fbrightmart\u002Falbert_zh\u002Fissues\u002F8",{"id":132,"question_zh":133,"answer_zh":134,"source_url":135},31950,"ALBERT 项目是否支持 MXNet 框架？","官方不直接提供 MXNet 支持，因为 ALBERT 模型结构与 BERT 不同。建议直接使用 TensorFlow 或 PyTorch，这两个框架使用人数更多且操作简单（下载预训练模型、克隆项目、训练即可）。如果必须使用 MXNet，可以尝试寻找 TensorFlow checkpoint 转 MXNet 的转换脚本。","https:\u002F\u002Fgithub.com\u002Fbrightmart\u002Falbert_zh\u002Fissues\u002F10",{"id":137,"question_zh":138,"answer_zh":139,"source_url":140},31951,"运行 fine-tuning 时报错 'Failed to find any matching files for bert_model.ckpt' 如何解决？","这是因为初始化检查点文件名错误。ALBERT 模型的检查点文件名为 `albert_model.ckpt` 而不是 `bert_model.ckpt`。请将命令中的 `--init_checkpoint=$BERT_BASE_DIR\u002Fbert_model.ckpt` 修改为 `--init_checkpoint=$BERT_BASE_DIR\u002Falbert_model.ckpt`。","https:\u002F\u002Fgithub.com\u002Fbrightmart\u002Falbert_zh\u002Fissues\u002F22",{"id":142,"question_zh":143,"answer_zh":144,"source_url":145},31952,"如何从 TPU 预训练好的模型中去掉 Adam 相关的变量以便在 GPU 上使用？","可以通过以下代码过滤掉包含 'adam' 的变量并保存新模型：\n1. 获取可训练变量列表：`trainable_variable_list = tf.trainable_variables()`\n2. 过滤变量：`trainable_variable_list = [x for x in trainable_variable_list if 'adam' not in x.name]`\n3. 创建 saver 并保存：`saver_new = tf.train.Saver(trainable_variable_list)` 然后调用 `saver_new.save(sess, '.\u002Fnew_model_path')`。","https:\u002F\u002Fgithub.com\u002Fbrightmart\u002Falbert_zh\u002Fissues\u002F20",{"id":147,"question_zh":148,"answer_zh":149,"source_url":150},31953,"albert_zh 项目是否支持运行 SQuAD 任务？","支持运行 SQuAD 任务。但在某些情况下，如果使用 `30k-clean.model` 作为 spm_model_file 失败，可能需要回退使用传统的 `vocab.txt` 文件方式。","https:\u002F\u002Fgithub.com\u002Fbrightmart\u002Falbert_zh\u002Fissues\u002F120",[]]