Skip to content

Ludwig

Ludwig 标志 Ludwig 标志

为规模和效率构建的声明式深度学习框架。

pypi package downloads license Discord


重要

❗ Ludwig 社区已迁移至 Discord -- 请加入我们!


📖 什么是Ludwig?

Ludwig 是一个用于构建自定义 AI 模型(如LLMs和其他深度神经网络)的低代码框架。

主要特点:

  • 🛠 轻松构建自定义模型: 只需一个声明性的YAML配置文件,即可在您的数据上训练最先进的LLM。支持多任务和多模态学习。全面的配置验证可检测无效的参数组合并防止运行时失败。
  • 为规模化和效率优化: 自动批量大小选择,分布式训练(DDP, DeepSpeed),参数高效微调(PEFT),4位量化(QLoRA),以及大于内存的数据集。
  • 📐 专家级控制: 保留对模型的全方位控制,直至激活函数。支持超参数优化、可解释性和丰富的指标可视化。
  • 🧱 模块化和可扩展:只需在配置中进行少量参数更改,即可尝试不同的模型架构、任务、特征和模态。将其视为深度学习的构建模块。
  • 🚢 为生产环境设计: 预构建的 Docker 容器,原生支持在 Kubernetes 上使用 Ray 运行,将模型导出到 TorchscriptTriton,一键上传到 HuggingFace

Ludwig 由 Linux Foundation AI & Data 托管。

img

[!重要] 我们的社区已迁移至 Discord -- 请加入我们!

💾 安装

从PyPi安装。请注意,Ludwig需要Python 3.8+。

pip install ludwig

或者安装所有可选依赖项:

pip install ludwig[full]

🏃 快速开始

要获取完整的教程,请查看官方的入门指南, 或者查看端到端的示例

大型语言模型微调

在Colab中打开

让我们微调一个预训练的LLaMA-2-7b大型语言模型,使其能够像聊天机器人一样遵循指令(“指令微调”)。

先决条件

运行

我们将使用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: |
    ### 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:
    warmup_fraction: 0.01

preprocessing:
  sample_ratio: 0.1

现在让我们训练模型:

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应用于您的数据。联系我们如果您有任何问题。

❓ 为什么你应该使用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

📚 教程

🔬 示例用例

💡 更多信息

阅读我们关于Ludwig声明式机器学习以及Ludwig的SoTA基准测试的出版物。

了解更多关于Ludwig的工作原理如何开始使用,并通过更多示例进行学习。

如果您对贡献感兴趣,有问题、评论或想法要分享,或者如果您只是想了解最新动态,请考虑加入我们的社区 Discord 并在 X 上关注我们!

🤝 加入社区,与我们一起构建 Ludwig

Ludwig 是一个积极管理的开源项目,依赖于像您这样的人的贡献。考虑加入活跃的 Ludwig 贡献者群体,使 Ludwig 成为一个更易于使用且功能丰富的框架,供所有人使用!


👋 参与其中