Ludwig 是一个用于构建 自定义 AI 模型(如 LLMs 和其他深度神经网络)的 低代码 框架。
主要特性:
Ludwig 由 Linux Foundation AI & Data 托管。
技术栈: Python 3.12 | PyTorch 2.6 | Pydantic 2 | Transformers 5 | Ray 2.54

从 PyPI 安装。请注意 Ludwig 需要 Python 3.12+。
pip install ludwig
或安装所有可选依赖项:
pip install ludwig[full]
请参阅 贡献指南 获取更详细的安装说明。
想快速了解 Ludwig 的一些功能吗?查看这个 Colab Notebook 🚀
想要微调 LLMs?查看这些笔记本:
让我们微调一个预训练的 LLM,使其能像聊天机器人一样遵循指令("指令微调")。
我们将使用 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-3.1-8B
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"
让我们构建一个神经网络,预测 烂番茄 上某位影评人的评论是正面还是负面。
我们的数据集将是一个如下所示的 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 |
| ... | ... | ... | ... | ... | ... | ... |
从 这里 下载数据集的样本。
wget https://ludwig.ai/latest/data/rotten_tomatoes.csv
接下来创建一个名为 model.yaml 的 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 应用到您的数据上。如果您有任何问题,请 通过 Discord 联系我们。
Ludwig 开箱即用地处理了机器学习的工程复杂性,使研究人员能够专注于最高抽象级别的模型构建。数据预处理、超参数优化、设备管理和 torch.nn.Module 模型的分布式训练完全免费提供。
创建最先进的基线模型并与新模型进行比较,只需更改一下配置。
将新模型应用于 Ludwig 支持的广泛任务和数据集。Ludwig 包含一个 完整的基准测试工具包,任何用户都可以使用,只需一个简单的配置即可跨多个数据集运行多个模型的实验。
模型架构、训练循环、超参数搜索和后端基础设施的任何方面都可以通过声明式配置中的附加字段进行修改,以定制管道满足您的要求。有关可配置内容的详细信息,请查看 Ludwig 配置 文档。
无需编写代码,即可将表格数据、文本、图像甚至音频混合搭配到复杂的模型配置中。
使用 Tensorboard、Comet ML、Weights & Biases、MLFlow 和 Aim Stack 等工具自动跟踪所有试验和指标。
无需更改代码,即可从本地机器训练扩展到云端。
Ludwig 还原生集成了预训练模型,例如 Huggingface Transformers 中可用的模型。用户可以从大量最先进的预训练 PyTorch 模型中选择使用,无需编写任何代码。例如,使用 Ludwig 训练基于 BERT 的情感分析模型非常简单:
shell
ludwig train --dataset sst5 --config_str "{input_features: [{name: sentence, type: text, encoder: bert}], output_features: [{name: label, type: category}]}"
Ludwig AutoML 允许用户仅提供数据集、目标列和时间预算即可获得训练好的模型。
python
auto_train_results = ludwig.automl.auto_train(dataset=my_dataset_df, target=target_column_name, time_limit_s=7200)
Ludwig 可以轻松部署深度学习模型,包括在 GPU 上。为训练好的 Ludwig 模型启动 REST API。
shell
ludwig serve --model_path=/path/to/model
Ludwig 支持将模型导出为高效的 Torchscript 包。
shell
ludwig export_torchscript -–model_path=/path/to/model
阅读我们关于 Ludwig、声明式 ML 和 Ludwig 的 SoTA 基准测试 的出版物。
了解更多关于 Ludwig 的工作原理、如何入门 并完成更多 示例。
如果您有兴趣 贡献、有问题、评论或想法要分享,或者只是想了解最新动态,请考虑 加入我们的社区 Discord 并在 X 上关注我们!
Ludwig 是一个积极维护的开源项目,依赖于像您一样的人的贡献。考虑加入活跃的 Ludwig 贡献者群体,让 Ludwig 成为一个对所有人都更易用、功能更丰富的框架!