Skip to content

Transforms

Functional API

functional

Functions:

preference_swap_transform

preference_swap_transform(
    dataset: Dataset,
    swap_probability: float,
    in_place: bool = False,
) -> Dataset

Swaps the 'chosen' and 'rejected' responses for each sample in the dataset.

Parameters:

  • dataset (Union[ContinualAlignmentDataset, AlignmentDataset]) –

    The dataset to transform.

  • in_place (bool, default: False ) –

    Whether to apply the transform in-place or return a new dataset.

  • swap_probability (float) –

    The independent probability of swapping responses for each sample in the dataset.

Returns:

  • Dataset

    Union[ContinualAlignmentDataset, AlignmentDataset]: The transformed dataset.

Raises:

  • ValueError

    If the swap probability is not in the range [0, 1].

Source code in aif_gen/transforms/functional.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def preference_swap_transform(
    dataset: Dataset, swap_probability: float, in_place: bool = False
) -> Dataset:
    r"""Swaps the 'chosen' and 'rejected' responses for each sample in the dataset.

    Args:
        dataset (Union[ContinualAlignmentDataset, AlignmentDataset]): The dataset to transform.
        in_place: Whether to apply the transform in-place or return a new dataset.
        swap_probability (float): The independent probability of swapping responses for each sample in the dataset.

    Returns:
        Union[ContinualAlignmentDataset, AlignmentDataset]: The transformed dataset.

    Raises:
        ValueError: If the swap probability is not in the range [0, 1].
    """
    transform = PreferenceSwapTransform(swap_probability)
    return transform(dataset, in_place=in_place)

split_transform

split_transform(
    dataset: Dataset,
    test_ratio: float,
    in_place: bool = False,
) -> Dataset

Splits a Dataset training data into train and test datasets.

Parameters:

  • dataset (Union[ContinualAlignmentDataset, AlignmentDataset]) –

    The dataset to transform.

  • in_place (bool, default: False ) –

    Whether to apply the transform in-place or return a new dataset.

  • test_ratio (float) –

    The test ratio to split the dataset with.

Returns:

  • Dataset

    Union[ContinualAlignmentDataset, AlignmentDataset]: The transformed dataset.

Raises:

  • ValueError

    If a dataset in the Continual Dataset has test data.

Source code in aif_gen/transforms/functional.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
def split_transform(
    dataset: Dataset, test_ratio: float, in_place: bool = False
) -> Dataset:
    r"""Splits a Dataset training data into train and test datasets.

    Args:
        dataset (Union[ContinualAlignmentDataset, AlignmentDataset]): The dataset to transform.
        in_place: Whether to apply the transform in-place or return a new dataset.
        test_ratio (float): The test ratio to split the dataset with.

    Returns:
        Union[ContinualAlignmentDataset, AlignmentDataset]: The transformed dataset.

    Raises:
        ValueError: If a dataset in the Continual Dataset has test data.
    """
    transform = SplitTransform(test_ratio)
    return transform(dataset, in_place=in_place)

Objected-Oriented API

base

Classes:

DatasetTransform

Bases: ABC

Base class for transforming Alignment Datasets.

Methods:

  • apply

    Apply the transform onto a dataset.

apply abstractmethod
apply(dataset: Dataset, in_place: bool = False) -> Dataset

Apply the transform onto a dataset.

Parameters:

  • dataset (Union[ContinualAlignmentDataset, AlignmentDataset]) –

    The dataset to transform.

  • in_place (bool, default: False ) –

    Whether to apply the transform in-place or return a new dataset.

Returns:

  • Dataset

    Union[ContinualAlignmentDataset, AlignmentDataset]: The transformed dataset.

Source code in aif_gen/transforms/base.py
10
11
12
13
14
15
16
17
18
19
20
@abstractmethod
def apply(self, dataset: Dataset, in_place: bool = False) -> Dataset:
    r"""Apply the transform onto a dataset.

    Args:
        dataset (Union[ContinualAlignmentDataset, AlignmentDataset]): The dataset to transform.
        in_place: Whether to apply the transform in-place or return a new dataset.

    Returns:
        Union[ContinualAlignmentDataset, AlignmentDataset]: The transformed dataset.
    """

preference_swap_transform

preference_swap_transform(
    dataset: Dataset,
    swap_probability: float,
    in_place: bool = False,
) -> Dataset

Swaps the 'chosen' and 'rejected' responses for each sample in the dataset.

Parameters:

  • dataset (Union[ContinualAlignmentDataset, AlignmentDataset]) –

    The dataset to transform.

  • in_place (bool, default: False ) –

    Whether to apply the transform in-place or return a new dataset.

  • swap_probability (float) –

    The independent probability of swapping responses for each sample in the dataset.

Returns:

  • Dataset

    Union[ContinualAlignmentDataset, AlignmentDataset]: The transformed dataset.

Raises:

  • ValueError

    If the swap probability is not in the range [0, 1].

Source code in aif_gen/transforms/functional.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def preference_swap_transform(
    dataset: Dataset, swap_probability: float, in_place: bool = False
) -> Dataset:
    r"""Swaps the 'chosen' and 'rejected' responses for each sample in the dataset.

    Args:
        dataset (Union[ContinualAlignmentDataset, AlignmentDataset]): The dataset to transform.
        in_place: Whether to apply the transform in-place or return a new dataset.
        swap_probability (float): The independent probability of swapping responses for each sample in the dataset.

    Returns:
        Union[ContinualAlignmentDataset, AlignmentDataset]: The transformed dataset.

    Raises:
        ValueError: If the swap probability is not in the range [0, 1].
    """
    transform = PreferenceSwapTransform(swap_probability)
    return transform(dataset, in_place=in_place)

split_transform

split_transform(
    dataset: Dataset,
    test_ratio: float,
    in_place: bool = False,
) -> Dataset

Splits a Dataset training data into train and test datasets.

Parameters:

  • dataset (Union[ContinualAlignmentDataset, AlignmentDataset]) –

    The dataset to transform.

  • in_place (bool, default: False ) –

    Whether to apply the transform in-place or return a new dataset.

  • test_ratio (float) –

    The test ratio to split the dataset with.

Returns:

  • Dataset

    Union[ContinualAlignmentDataset, AlignmentDataset]: The transformed dataset.

Raises:

  • ValueError

    If a dataset in the Continual Dataset has test data.

Source code in aif_gen/transforms/functional.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
def split_transform(
    dataset: Dataset, test_ratio: float, in_place: bool = False
) -> Dataset:
    r"""Splits a Dataset training data into train and test datasets.

    Args:
        dataset (Union[ContinualAlignmentDataset, AlignmentDataset]): The dataset to transform.
        in_place: Whether to apply the transform in-place or return a new dataset.
        test_ratio (float): The test ratio to split the dataset with.

    Returns:
        Union[ContinualAlignmentDataset, AlignmentDataset]: The transformed dataset.

    Raises:
        ValueError: If a dataset in the Continual Dataset has test data.
    """
    transform = SplitTransform(test_ratio)
    return transform(dataset, in_place=in_place)