LLM、AIGC、RAG 开发交流裙:377891973
一、关于 Ludwig 📖
- github : https://github.com/ludwig-ai/ludwig
- 官方文档:https://ludwig.ai/latest/
- discord : https://discord.gg/CBgdrGnZjy
Ludwig 是一个低代码框架,用于构建自定义AI 模型,如LLM和其他深度神经网络。
主要特征:
- 🛠**轻松构建自定义模型:**声明式 YAML 配置文件就是您训练最先进的 LLM 所需的全部内容。支持多任务和多模态学习。全面的配置验证可检测无效的参数组合并防止运行时失败。
- ⚡**针对规模和效率进行了优化:**自动批量大小选择、分布式训练(DDP、DeepSpeed)、参数高效微调(PEFT)、4 位量化(QLoRA)、分页和 8 位优化器以及大于内存的数据集。
- 📐**专家级控制:**保留对模型的完全控制,直至激活函数。支持超参数优化、可解释性和丰富的指标可视化。
- 🧱**模块化和可扩展性:**只需在配置中更改几个参数,即可尝试不同的模型架构、任务、功能和模式。思考深度学习的构建模块。
- 🚢**专为生产而设计:**预构建的Docker容器、对在Kubernetes上使用Ray 的原生支持、将模型导出到Torchscript和Triton 、使用一个命令上传到HuggingFace 。
Ludwig 由Linux 基金会 AI & Data托管 。
二、 安装 💾
从 PyPi 安装。请注意,Ludwig 需要 Python 3.8+。
pip install ludwig
或者安装所有可选依赖项:
pip install ludwig[full]
请参阅贡献以获取更详细的安装说明。
三、入门 🚂
想要快速了解 Ludwig 0.8 的一些功能?查看这个 Colab Notebook 🚀 : https://colab.research.google.com/drive/1lB4ALmEyvcMycE3Mlnsd7I3bc0zxvk39
想要微调 Llama-2 或 Mistral?查看以下笔记本:
四、大型语言模型微调
https://colab.research.google.com/drive/1c3AO8l_H6V_x37RwQ8V7M6A-RmcBf2tG
让我们对预先训练的 LLaMA-2-7b 大型语言模型进行微调,使其像聊天机器人一样遵循指令(“指令调整”)。
先决条件
- HuggingFace API 令牌
- 获得 Llama2-7b-hf的访问批准
- 具有至少 12 GiB VRAM 的 GPU(在我们的测试中,我们使用了 Nvidia T4)
运行
我们将使用Stanford Alpaca数据集,该数据集将被格式化为如下所示的表格文件:
instruction | input | output |
---|---|---|
Give three tips for staying healthy. | 1.Eat a balanced diet and make sure to include… | |
Arrange the items given below in the order to … | cake, me, eating | I eating cake. |
Write an introductory paragraph about a famous… | Michelle Obama | Michelle Obama is an inspirational woman who r… |
… | … | … |
创建一个名为 model.yaml
的YAML 配置文件:
model_type: llm
base_model: meta-llama/Llama-2-7b-hf
quantization:
bits: 4
adapter:
type: lora
prompt:
template: |
Below is an instruction that describes a task, paired with an input that may provide further context.
Write a response that appropriately completes the request.
### Instruction:
{instruction}
### Input:
{input}
### Response:
input_features:
- name: prompt
type: text
output_features:
- name: output
type: text
trainer:
type: finetune
learning_rate: 0.0001
batch_size: 1
gradient_accumulation_steps: 16
epochs: 3
learning_rate_scheduler:
decay: cosine
warmup_fraction: 0.01
preprocessing:
sample_ratio: 0.1
backend:
type: local
现在让我们训练模型:
export HUGGING_FACE_HUB_TOKEN = "<api_token>"
ludwig train --config model.yaml --dataset "ludwig://alpaca"
监督机器学习
让我们建立一个神经网络,预测 Rotten Tomatoes 上某位影评人的评价是正面的还是负面的。
我们的数据集将是一个如下所示的 CSV 文件:
movie_title | content_rating | genres | runtime | top_critic | review_content | recommended |
---|---|---|---|---|---|---|
Deliver Us from Evil | R | Action & Adventure, Horror | 117.0 | TRUE | Director Scott Derrickson and his co-writer, Paul Harris Boardman, deliver a routine procedural with unremarkable frights. | 0 |
Barbara | PG-13 | Art House & International, Drama | 105.0 | FALSE | Somehow, in this stirring narrative, Barbara manages to keep hold of her principles, and her humanity and courage, and battles to save a dissident teenage girl whose life the Communists are trying to destroy. | 1 |
Horrible Bosses | R | Comedy | 98.0 | FALSE | These bosses cannot justify either murder or lasting comic memories, fatally compromising a farce that could have been great but ends up merely mediocre. | 0 |
… | … | … | … | … | … | … |
下载数据集样本:https://ludwig.ai/latest/data/rotten_tomatoes.csv
wget https://ludwig.ai/latest/data/rotten_tomatoes.csv
接下来创建一个 YAML 配置文件,model.yaml
其名称如下:
input_features:
- name: genres
type: set
preprocessing:
tokenizer: comma
- name: content_rating
type: category
- name: top_critic
type: binary
- name: runtime
type: number
- name: review_content
type: text
encoder:
type: embed
output_features:
- name: recommended
type: binary
就这样!现在让我们训练模型:
ludwig train --config model.yaml --dataset rotten_tomatoes.csv
五、为什么你应该使用 Ludwig ❓
- 最少的机器学习样板
Ludwig 开箱即用地解决了机器学习的工程复杂性,使研究科学家能够专注于构建最高抽象级别的模型。数据预处理、超参数优化、设备管理和torch.nn.Module
模型的分布式训练完全免费。 - 轻松建立基准
创建最先进的基线并将其与新模型进行比较是一个简单的配置更改。 - 轻松将新架构应用于多个问题和数据集
在 Ludwig 支持的大量任务和数据集中应用新模型。Ludwig 包含一个 可供任何用户使用的完整基准测试工具包,只需进行简单的配置即可在多个数据集上使用多个模型运行实验。 - 高度可配置的数据预处理、建模和指标
可以将模型架构、训练循环、超参数搜索和后端基础架构的任何方面作为声明性配置中的附加字段进行修改,以自定义管道以满足您的要求。有关可配置内容的详细信息,请查看 Ludwig 配置 文档。 - 开箱即用的多模式、多任务学习
无需编写代码,即可将表格数据、文本、图像甚至音频混合并匹配到复杂的模型配置中。 - 丰富的模型导出和追踪
使用 Tensorboard、Comet ML、Weights & Biases、MLFlow 和 Aim Stack 等工具自动跟踪所有试验和指标。 - 自动将训练扩展到多 GPU、多节点集群
无需更改代码,即可从本地机器上的训练转移到云端。 - 适用于最先进模型的低代码接口,包括预先训练的 Huggingface Transformers
Ludwig 还原生集成了预训练模型,例如Huggingface Transformers中提供的模型。用户可以从大量最先进的预训练 PyTorch 模型中进行选择,而无需编写任何代码。例如,使用 Ludwig 训练基于 BERT 的情绪分析模型非常简单:
ludwig train --dataset sst5 --config_str "{input_features: [{name: sentence, type: text, encoder: bert}], output_features: [{name: label, type: category}]}"
- AutoML 的低代码界面
Ludwig AutoML 允许用户仅提供数据集、目标列和时间预算即可获得训练有素的模型。
auto_train_results = ludwig.automl.auto_train(dataset=my_dataset_df, target=target_column_name, time_limit_s=7200)
- 轻松实现生产化
Ludwig 可轻松为深度学习模型(包括 GPU)提供服务。为经过训练的 Ludwig 模型启动 REST API。
ludwig serve --model_path=/path/to/model
ludwig 支持将模型导出到高效的 Torchscript 包。
ludwig export_torchscript -–model_path=/path/to/model
六、详细教程 📚
七、使用示例 🔬
- 命名实体识别标记
- 自然语言理解
- 机器翻译
- 通过 seq2seq 进行闲聊对话建模
- 情绪分析
- 利用孪生网络进行一次性学习
- 视觉问答
- 口语数字语音识别
- 说话人验证
- 二元分类(泰坦尼克号)
- 时间序列预测
- 时间序列预测(天气)
- 电影评级预测
- 多标签分类
- 多任务学习
- 简单回归:燃油效率预测
- 欺诈识别
八、更多信息 💡
阅读我们关于Ludwig、声明式 ML和Ludwig 的 SoTA 基准的出版物。
进一步了解Ludwig 的工作原理、如何开始使用以及了解更多示例。
如果您有兴趣做出贡献,有问题、意见或想法要分享,或者您只是想知道最新情况,请考虑加入我们的社区 Discord并在X上关注我们!
2024-05-24(五)