跳到内容

DataSampler

从数据集采样的 Step。

GeneratorStep,用于从数据集采样并批量生成。当您的 Pipeline 可以从提示中使用示例中受益时,此 Step 非常有用,例如作为少样本学习,可以在每行上更改。例如,您可以传递包含 N 个示例的字典列表,并从中生成 M 个样本(假设您有另一个 Step 加载数据,则此 M 应与该 Step 中加载的数据大小相同)。size S 参数是每行生成的样本数,因此每个示例将包含 S 个示例以用作示例。

属性

  • data: 要从中采样的字典列表。

  • size: 每个示例的样本数。例如,在少样本学习场景中,每个示例将生成的少样本示例数。默认为 2。

  • samples: Step 将总共生成的示例数。如果与另一个加载器 Step 一起使用,则应与加载器 Step 中的样本数相同。默认为 100。

输入和输出列

graph TD
    subgraph Dataset
        subgraph New columns
            OCOL0[dynamic]
        end
    end

    subgraph DataSampler
        StepOutput[Output Columns: dynamic]
    end

    StepOutput --> OCOL0

输出

  • dynamic(基于在列表的第一个字典中找到的键):数据集的列。

示例

从字典列表采样数据

from distilabel.steps import DataSampler

sampler = DataSampler(
    data=[{"sample": f"sample {i}"} for i in range(30)],
    samples=10,
    size=2,
    batch_size=4
)
sampler.load()

result = next(sampler.process())
# >>> result
# ([{'sample': ['sample 7', 'sample 0']}, {'sample': ['sample 2', 'sample 21']}, {'sample': ['sample 17', 'sample 12']}, {'sample': ['sample 2', 'sample 14']}], False)

在单个流中组合加载器和采样器的 Pipeline

from datasets import load_dataset

from distilabel.steps import LoadDataFromDicts, DataSampler
from distilabel.steps.tasks.apigen.utils import PrepareExamples
from distilabel.pipeline import Pipeline

ds = (
    load_dataset("Salesforce/xlam-function-calling-60k", split="train")
    .shuffle(seed=42)
    .select(range(500))
    .to_list()
)
data = [
    {
        "func_name": "final_velocity",
        "func_desc": "Calculates the final velocity of an object given its initial velocity, acceleration, and time.",
    },
    {
        "func_name": "permutation_count",
        "func_desc": "Calculates the number of permutations of k elements from a set of n elements.",
    },
    {
        "func_name": "getdivision",
        "func_desc": "Divides two numbers by making an API call to a division service.",
    },
]
with Pipeline(name="APIGenPipeline") as pipeline:
    loader_seeds = LoadDataFromDicts(data=data)
    sampler = DataSampler(
        data=ds,
        size=2,
        samples=len(data),
        batch_size=8,
    )
    prep_examples = PrepareExamples()

    sampler >> prep_examples
    (
        [loader_seeds, prep_examples]
        >> combine_steps
    )
# Now we have a single stream of data with the loader and the sampler data