Skip to content

offline

learn_offline(environment, learner, inputs, outputs, *, input_transform=None)

Learn from an offline environment.

Learn from an offline environment by learning from the input-output pairs.

Parameters:

Name Type Description Default
environment OfflineEnvironment

The offline environment.

required
learner SupervisedLearner

The supervised learner.

required
inputs list[str]

The input feature names.

required
outputs list[str]

The output feature names.

required
input_transform Transform | None

The transform to apply to the input features. Will be part of the final model.

None

Returns:

Type Description
Model

The model learned from the environment.

Source code in src/flowcean/strategies/offline.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
def learn_offline(
    environment: OfflineEnvironment,
    learner: SupervisedLearner,
    inputs: list[str],
    outputs: list[str],
    *,
    input_transform: Transform | None = None,
) -> Model:
    """Learn from an offline environment.

    Learn from an offline environment by learning from the input-output pairs.

    Args:
        environment: The offline environment.
        learner: The supervised learner.
        inputs: The input feature names.
        outputs: The output feature names.
        input_transform: The transform to apply to the input features.
            Will be part of the final model.

    Returns:
        The model learned from the environment.
    """
    logger.info("Learning with offline strategy")
    data = environment.observe()
    logger.info("Selecting input and output features")
    input_features = data.select(inputs)
    output_features = data.select(outputs)

    if isinstance(input_transform, FitOnce):
        cast(FitOnce, input_transform).fit(input_features)

    if isinstance(input_transform, FitIncremetally):
        cast(FitIncremetally, input_transform).fit_incremental(input_features)

    logger.info("Learning model")
    model = learner.learn(input_features, output_features)
    if input_transform is None:
        return model
    return ModelWithTransform(
        model,
        input_transform,
    )

evaluate_offline(model, environment, inputs, outputs, metrics)

Evaluate a model on an offline environment.

Evaluate a model on an offline environment by predicting the outputs from the inputs and comparing them to the true outputs.

Parameters:

Name Type Description Default
model Model

The model to evaluate.

required
environment OfflineEnvironment

The offline environment.

required
inputs list[str]

The input feature names.

required
outputs list[str]

The output feature names.

required
metrics list[OfflineMetric]

The metrics to evaluate the model with.

required

Returns:

Type Description
Report

The evaluation report.

Source code in src/flowcean/strategies/offline.py
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
def evaluate_offline(
    model: Model,
    environment: OfflineEnvironment,
    inputs: list[str],
    outputs: list[str],
    metrics: list[OfflineMetric],
) -> Report:
    """Evaluate a model on an offline environment.

    Evaluate a model on an offline environment by predicting the outputs from
    the inputs and comparing them to the true outputs.

    Args:
        model: The model to evaluate.
        environment: The offline environment.
        inputs: The input feature names.
        outputs: The output feature names.
        metrics: The metrics to evaluate the model with.

    Returns:
        The evaluation report.
    """
    data = environment.observe()
    input_features = data.select(inputs)
    output_features = data.select(outputs)
    predictions = model.predict(input_features)
    return Report(
        {
            metric.name: metric(output_features, predictions)
            for metric in metrics
        },
    )