Skip to content

NeuralNetworkClassifier

Bases: Generic[IFT, IPT]

A NeuralNetworkClassifier is a neural network that is used for classification tasks.

Parameters:

Name Type Description Default
input_conversion InputConversion[IFT, IPT]

to convert the input data for the neural network

required
layers list[Layer]

a list of layers for the neural network to learn

required

Raises:

Type Description
InvalidModelStructureError

if the defined model structure is invalid

Source code in src/safeds/ml/nn/_model.py
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
class NeuralNetworkClassifier(Generic[IFT, IPT]):
    """
    A NeuralNetworkClassifier is a neural network that is used for classification tasks.

    Parameters
    ----------
    input_conversion:
        to convert the input data for the neural network
    layers:
        a list of layers for the neural network to learn

    Raises
    ------
    InvalidModelStructureError
        if the defined model structure is invalid
    """

    def __init__(
        self,
        input_conversion: InputConversion[IFT, IPT],
        layers: list[Layer],
    ):
        if len(layers) == 0:
            raise InvalidModelStructureError("You need to provide at least one layer to a neural network.")
        if isinstance(input_conversion, InputConversionImageToImage):
            raise InvalidModelStructureError("A NeuralNetworkClassifier cannot be used with images as output.")
        if isinstance(input_conversion, _InputConversionImage) and isinstance(
            input_conversion._input_size,
            VariableImageSize,
        ):
            raise InvalidModelStructureError(
                "A NeuralNetworkClassifier cannot be used with a InputConversionImage that uses a VariableImageSize.",
            )
        elif isinstance(input_conversion, _InputConversionImage):
            data_dimensions = 2
            for layer in layers:
                if data_dimensions == 2 and (isinstance(layer, Convolutional2DLayer | _Pooling2DLayer)):
                    continue
                elif data_dimensions == 2 and isinstance(layer, FlattenLayer):
                    data_dimensions = 1
                elif data_dimensions == 1 and isinstance(layer, ForwardLayer):
                    continue
                else:
                    raise InvalidModelStructureError(
                        (
                            "The 2-dimensional data has to be flattened before using a 1-dimensional layer."
                            if data_dimensions == 2
                            else "You cannot use a 2-dimensional layer with 1-dimensional data."
                        ),
                    )
            if data_dimensions == 2 and (
                isinstance(input_conversion, InputConversionImageToColumn | InputConversionImageToTable)
            ):
                raise InvalidModelStructureError(
                    "The output data would be 2-dimensional but the provided output conversion uses 1-dimensional data.",
                )
        else:
            for layer in layers:
                if isinstance(layer, Convolutional2DLayer | FlattenLayer | _Pooling2DLayer):
                    raise InvalidModelStructureError("You cannot use a 2-dimensional layer with 1-dimensional data.")

        self._input_conversion: InputConversion[IFT, IPT] = input_conversion
        self._model: nn.Module | None = None
        self._layers: list[Layer] = layers
        self._input_size: int | ModelImageSize | None = None
        self._batch_size = 1
        self._is_fitted = False
        self._num_of_classes = (
            layers[-1].output_size if isinstance(layers[-1].output_size, int) else -1
        )  # Is always int but linter doesn't know
        self._total_number_of_batches_done = 0
        self._total_number_of_epochs_done = 0

    @staticmethod
    def from_pretrained_model(huggingface_repo: str) -> NeuralNetworkClassifier:  # pragma: no cover
        """
        Load a pretrained model from a [Huggingface repository](https://huggingface.co/models/).

        Parameters
        ----------
        huggingface_repo:
            the name of the huggingface repository

        Returns
        -------
        pretrained_model:
            the pretrained model as a NeuralNetworkClassifier
        """
        from transformers import AutoConfig, AutoImageProcessor, AutoModelForImageClassification, PretrainedConfig
        from transformers.models.auto.modeling_auto import MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES

        _init_default_device()

        config: PretrainedConfig = AutoConfig.from_pretrained(huggingface_repo)

        if config.model_type not in MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES:
            raise ValueError("This model is not supported")

        model: Module = AutoModelForImageClassification.from_pretrained(huggingface_repo)

        image_processor: BaseImageProcessor = AutoImageProcessor.from_pretrained(huggingface_repo)
        if hasattr(image_processor, "size") and hasattr(config, "num_channels"):
            if "shortest_edge" in image_processor.size:
                input_size = ConstantImageSize(
                    image_processor.size.get("shortest_edge"),
                    image_processor.size.get("shortest_edge"),
                    config.num_channels,
                )
            else:
                input_size = ConstantImageSize(
                    image_processor.size.get("width"),
                    image_processor.size.get("height"),
                    config.num_channels,
                )
        else:  # Should never happen due to model check
            raise ValueError("This model is not supported")  # pragma: no cover

        label_dict: dict[str, str] = config.id2label
        column_name = "label"
        labels_table = Table({column_name: [label for _, label in label_dict.items()]})
        one_hot_encoder = OneHotEncoder(column_names=[column_name]).fit(labels_table)

        in_conversion = InputConversionImageToColumn(input_size)

        in_conversion._column_name = column_name
        in_conversion._one_hot_encoder = one_hot_encoder
        in_conversion._input_size = input_size
        in_conversion._output_type = _ColumnAsTensor
        num_of_classes = labels_table.row_count

        network = NeuralNetworkClassifier.__new__(NeuralNetworkClassifier)
        network._input_conversion = in_conversion
        network._model = model
        network._input_size = input_size
        network._batch_size = 1
        network._is_fitted = True
        network._num_of_classes = num_of_classes
        network._total_number_of_epochs_done = 0
        network._total_number_of_batches_done = 0

        return network

    def fit(
        self,
        train_data: IFT,
        epoch_count: int = 25,
        batch_size: int = 1,
        learning_rate: float = 0.001,
        callback_on_batch_completion: Callable[[int, float], None] | None = None,
        callback_on_epoch_completion: Callable[[int, float], None] | None = None,
    ) -> Self:
        """
        Train the neural network with given training data.

        The original model is not modified.

        Parameters
        ----------
        train_data:
            The data the network should be trained on.
        epoch_count:
            The number of times the training cycle should be done.
        batch_size:
            The size of data batches that should be loaded at one time.
        learning_rate:
            The learning rate of the neural network.
        callback_on_batch_completion:
            Function used to view metrics while training. Gets called after a batch is completed with the index of the
            last batch and the overall loss average.
        callback_on_epoch_completion:
            Function used to view metrics while training. Gets called after an epoch is completed with the index of the
            last epoch and the overall loss average.

        Returns
        -------
        trained_model:
            The trained Model

        Raises
        ------
        ValueError
            If epoch_count < 1
            If batch_size < 1
        """
        import torch
        from torch import nn

        from ._internal_model import _InternalModel  # Slow import on global level

        _init_default_device()

        if self._contains_choices():
            raise FittingWithChoiceError

        if isinstance(train_data, TimeSeriesDataset) and train_data.continuous:
            raise NotImplementedError(
                "Continuous Predictions are currently not supported for Time Series Classification.",
            )

        if not self._input_conversion._is_fit_data_valid(train_data):
            raise FeatureDataMismatchError

        _check_bounds("epoch_count", epoch_count, lower_bound=_ClosedBound(1))
        _check_bounds("batch_size", batch_size, lower_bound=_ClosedBound(1))

        copied_model = copy.deepcopy(self)
        # TODO: How is this supposed to work with pre-trained models? Should the old weights be kept or discarded?
        copied_model._model = _InternalModel(self._input_conversion, self._layers, is_for_classification=True)
        copied_model._batch_size = batch_size
        copied_model._input_size = copied_model._model.input_size

        # TODO: Re-enable or remove depending on how the above TODO is resolved
        # if copied_model._input_conversion._data_size != copied_model._input_size:
        #     raise InputSizeError(copied_model._input_conversion._data_size, copied_model._input_size)

        dataloader = copied_model._input_conversion._data_conversion_fit(
            train_data,
            copied_model._batch_size,
            copied_model._num_of_classes,
        )

        if copied_model._num_of_classes > 1:
            loss_fn = nn.CrossEntropyLoss()
        else:
            loss_fn = nn.BCELoss()

        optimizer = torch.optim.SGD(copied_model._model.parameters(), lr=learning_rate)
        for _ in range(epoch_count):
            loss_sum = 0.0
            amount_of_loss_values_calculated = 0
            for x, y in iter(dataloader):
                optimizer.zero_grad()
                pred = copied_model._model(x)
                loss = loss_fn(pred, y)
                loss_sum += loss.item()
                amount_of_loss_values_calculated += 1
                loss.backward()
                optimizer.step()

                copied_model._total_number_of_batches_done += 1
                if callback_on_batch_completion is not None:
                    callback_on_batch_completion(
                        copied_model._total_number_of_batches_done,
                        loss_sum / amount_of_loss_values_calculated,
                    )
            copied_model._total_number_of_epochs_done += 1
            if callback_on_epoch_completion is not None:
                callback_on_epoch_completion(
                    copied_model._total_number_of_epochs_done,
                    loss_sum / amount_of_loss_values_calculated,
                )
        copied_model._is_fitted = True
        copied_model._model.eval()
        return copied_model

    # TODO: Does not work if tensorflow with CUDA is used
    # def fit_by_exhaustive_search(
    #     self,
    #     train_data: IFT,
    #     optimization_metric: Literal["accuracy", "precision", "recall", "f1_score"],
    #     positive_class: Any = None,
    #     epoch_count: int = 25,
    #     batch_size: int = 1,
    #     learning_rate: float = 0.001,
    # ) -> Self:
    #     """
    #     Use the hyperparameter choices to create multiple models and fit them.
    #
    #     **Note:** This model is not modified.
    #
    #     Parameters
    #     ----------
    #     train_data:
    #         The data the network should be trained on.
    #     optimization_metric:
    #         The metric that should be used for determining the performance of a model.
    #     positive_class:
    #         The class to be considered positive. Only needs to be provided when choosing precision, recall or f1_score as the optimization metric.
    #     epoch_count:
    #         The number of times the training cycle should be done.
    #     batch_size:
    #         The size of data batches that should be loaded at one time.
    #     learning_rate:
    #         The learning rate of the neural network.
    #
    #     Returns
    #     -------
    #     best_model:
    #         The model that performed the best out of all possible models given the Choices of hyperparameters.
    #
    #     Raises
    #     ------
    #     FittingWithoutChoiceError
    #         When calling this method on a model without hyperparameter choices.
    #     LearningError
    #         If the training data contains invalid values or if the training failed.
    #     """
    #     _init_default_device()
    #
    #     if not self._contains_choices():
    #         raise FittingWithoutChoiceError
    #
    #     if isinstance(train_data, TimeSeriesDataset) and train_data.continuous:
    #         raise NotImplementedError(
    #             "Continuous Predictions are currently not supported for Time Series Classification.",
    #         )
    #
    #     _check_bounds("epoch_count", epoch_count, lower_bound=_ClosedBound(1))
    #     _check_bounds("batch_size", batch_size, lower_bound=_ClosedBound(1))
    #
    #     list_of_models = self._get_models_for_all_choices()
    #     list_of_fitted_models: list[Self] = []
    #
    #     if isinstance(train_data, TabularDataset):
    #         (train_set, test_set) = self._data_split_table(train_data)
    #     elif isinstance(train_data, TimeSeriesDataset):
    #         (train_set, test_set) = self._data_split_time_series(train_data)  # type: ignore[assignment]
    #     else:  # train_data is ImageDataset
    #         (train_set, test_set) = self._data_split_image(train_data)  # type: ignore[assignment]
    #
    #     with ProcessPoolExecutor(max_workers=len(list_of_models), mp_context=mp.get_context("spawn")) as executor:
    #         futures = []
    #         for model in list_of_models:
    #             futures.append(
    #                 executor.submit(
    #                     model.fit,
    #                     train_set,  # type: ignore[arg-type]
    #                     epoch_count,
    #                     batch_size,
    #                     learning_rate,
    #                 ),
    #             )  # type: ignore[arg-type]
    #         [done, _] = wait(futures, return_when=ALL_COMPLETED)
    #         for future in done:
    #             list_of_fitted_models.append(future.result())
    #     executor.shutdown()
    #
    #     if isinstance(train_data, TabularDataset):
    #         return self._get_best_fnn_model(list_of_fitted_models, test_set, optimization_metric, positive_class)
    #     elif isinstance(train_data, TimeSeriesDataset):
    #         return self._get_best_rnn_model(
    #             list_of_fitted_models,
    #             train_set,  # type: ignore[arg-type]
    #             test_set,  # type: ignore[arg-type]
    #             optimization_metric,
    #             positive_class,
    #         )
    #     elif isinstance(self._input_conversion, InputConversionImageToColumn):
    #         return self._get_best_cnn_model_column(
    #             list_of_fitted_models,
    #             train_set,  # type: ignore[arg-type]
    #             optimization_metric,
    #             positive_class,
    #         )
    #     else:  # ImageToTable
    #         return self._get_best_cnn_model_table(
    #             list_of_fitted_models,
    #             train_set,  # type: ignore[arg-type]
    #             optimization_metric,
    #             positive_class,
    #         )

    def _data_split_table(self, data: TabularDataset) -> tuple[TabularDataset, TabularDataset]:
        [train_split, test_split] = data.to_table().split_rows(0.75)
        train_data = train_split.to_tabular_dataset(
            target_name=data.target.name,
            extra_names=data.extras.column_names,
        )
        test_data = test_split.to_tabular_dataset(
            target_name=train_data.target.name,
            extra_names=train_data.extras.column_names,
        )
        return (train_data, test_data)

    def _data_split_time_series(self, data: TimeSeriesDataset) -> tuple[TimeSeriesDataset, Table]:
        (train_split, test_split) = data.to_table().split_rows(0.75)
        train_data = train_split.to_time_series_dataset(
            target_name=data.target.name,
            window_size=data.window_size,
            extra_names=data.extras.column_names,
            continuous=data.continuous,
            forecast_horizon=data.forecast_horizon,
        )
        return train_data, test_split

    def _data_split_image(self, train_data: ImageDataset) -> tuple[ImageDataset, ImageDataset]:
        return train_data.split(0.75)

    def _get_best_fnn_model(
        self,
        list_of_fitted_models: list[Self],
        test_data: TabularDataset,
        optimization_metric: Literal["accuracy", "precision", "recall", "f1_score"],
        positive_class: Any = None,
    ) -> Self:
        test_features = test_data.features
        test_target = test_data.target
        best_model = None
        best_metric_value = None
        for fitted_model in list_of_fitted_models:
            if best_model is None:
                best_model = fitted_model
                match optimization_metric:
                    case "accuracy":
                        best_metric_value = ClassificationMetrics.accuracy(
                            predicted=fitted_model.predict(test_features),  # type: ignore[arg-type]
                            expected=test_target,
                        )  # type: ignore[arg-type]
                    case "precision":
                        best_metric_value = ClassificationMetrics.precision(
                            predicted=fitted_model.predict(test_features),  # type: ignore[arg-type]
                            expected=test_target,  # type: ignore[arg-type]
                            positive_class=positive_class,
                        )  # type: ignore[arg-type]
                    case "recall":
                        best_metric_value = ClassificationMetrics.recall(
                            predicted=fitted_model.predict(test_features),  # type: ignore[arg-type]
                            expected=test_target,  # type: ignore[arg-type]
                            positive_class=positive_class,
                        )  # type: ignore[arg-type]
                    case "f1_score":
                        best_metric_value = ClassificationMetrics.f1_score(
                            predicted=fitted_model.predict(test_features),  # type: ignore[arg-type]
                            expected=test_target,  # type: ignore[arg-type]
                            positive_class=positive_class,
                        )  # type: ignore[arg-type]
            else:
                match optimization_metric:
                    case "accuracy":
                        error_of_fitted_model = ClassificationMetrics.accuracy(
                            predicted=fitted_model.predict(test_features),  # type: ignore[arg-type]
                            expected=test_target,
                        )
                        if error_of_fitted_model > best_metric_value:
                            best_model = fitted_model  # pragma: no cover
                            best_metric_value = error_of_fitted_model  # pragma: no cover
                    case "precision":
                        error_of_fitted_model = ClassificationMetrics.precision(
                            predicted=fitted_model.predict(test_features),
                            expected=test_target,
                            # type: ignore[arg-type]
                            positive_class=positive_class,
                        )  # type: ignore[arg-type]
                        if error_of_fitted_model > best_metric_value:
                            best_model = fitted_model  # pragma: no cover
                            best_metric_value = error_of_fitted_model  # pragma: no cover
                    case "recall":
                        error_of_fitted_model = ClassificationMetrics.recall(
                            predicted=fitted_model.predict(test_features),  # type: ignore[arg-type]
                            expected=test_target,
                            positive_class=positive_class,
                        )  # type: ignore[arg-type]
                        if error_of_fitted_model > best_metric_value:
                            best_model = fitted_model  # pragma: no cover
                            best_metric_value = error_of_fitted_model  # pragma: no cover
                    case "f1_score":
                        error_of_fitted_model = ClassificationMetrics.f1_score(
                            predicted=fitted_model.predict(test_features),
                            expected=test_target,
                            # type: ignore[arg-type]
                            positive_class=positive_class,
                        )  # type: ignore[arg-type]
                        if error_of_fitted_model > best_metric_value:
                            best_model = fitted_model  # pragma: no cover
                            best_metric_value = error_of_fitted_model  # pragma: no cover
        assert best_model is not None  # just for linter
        best_model._is_fitted = True
        return best_model

    def _get_best_rnn_model(
        self,
        list_of_fitted_models: list[Self],
        train_data: TimeSeriesDataset,
        test_data: Table,
        optimization_metric: Literal["accuracy", "precision", "recall", "f1_score"],
        positive_class: Any = None,
    ) -> Self:
        test_target = test_data.get_column(train_data.target.name)

        size = test_target.row_count
        expected_values = []
        for i in range(size - (train_data.forecast_horizon + train_data.window_size)):
            label = test_target[i + train_data.window_size + train_data.forecast_horizon]
            expected_values.append(label)
        expected_values_as_col = Column("expected", expected_values)

        best_model = None
        best_metric_value = None
        for fitted_model in list_of_fitted_models:
            if best_model is None:
                best_model = fitted_model
                match optimization_metric:
                    case "accuracy":
                        best_metric_value = ClassificationMetrics.accuracy(
                            predicted=fitted_model.predict(test_data),  # type: ignore[arg-type]
                            expected=expected_values_as_col,
                        )  # type: ignore[arg-type]
                    case "precision":
                        best_metric_value = ClassificationMetrics.precision(
                            predicted=fitted_model.predict(test_data),  # type: ignore[arg-type]
                            expected=expected_values_as_col,
                            positive_class=positive_class,
                        )  # type: ignore[arg-type]
                    case "recall":
                        best_metric_value = ClassificationMetrics.recall(
                            predicted=fitted_model.predict(test_data),  # type: ignore[arg-type]
                            expected=expected_values_as_col,
                            positive_class=positive_class,
                        )  # type: ignore[arg-type]
                    case "f1_score":
                        best_metric_value = ClassificationMetrics.f1_score(
                            predicted=fitted_model.predict(test_data),  # type: ignore[arg-type]
                            expected=expected_values_as_col,
                            positive_class=positive_class,
                        )  # type: ignore[arg-type]
            else:
                match optimization_metric:
                    case "accuracy":
                        error_of_fitted_model = ClassificationMetrics.accuracy(
                            predicted=fitted_model.predict(test_data),  # type: ignore[arg-type]
                            expected=expected_values_as_col,
                        )  # type: ignore[arg-type]
                        if error_of_fitted_model > best_metric_value:
                            best_model = fitted_model  # pragma: no cover
                            best_metric_value = error_of_fitted_model  # pragma: no cover
                    case "precision":
                        error_of_fitted_model = ClassificationMetrics.precision(
                            predicted=fitted_model.predict(test_data),
                            expected=expected_values_as_col,
                            # type: ignore[arg-type]
                            positive_class=positive_class,
                        )  # type: ignore[arg-type]
                        if error_of_fitted_model > best_metric_value:
                            best_model = fitted_model  # pragma: no cover
                            best_metric_value = error_of_fitted_model  # pragma: no cover
                    case "recall":
                        error_of_fitted_model = ClassificationMetrics.recall(
                            predicted=fitted_model.predict(test_data),  # type: ignore[arg-type]
                            expected=expected_values_as_col,
                            positive_class=positive_class,
                        )  # type: ignore[arg-type]
                        if error_of_fitted_model > best_metric_value:
                            best_model = fitted_model  # pragma: no cover
                            best_metric_value = error_of_fitted_model  # pragma: no cover
                    case "f1_score":
                        error_of_fitted_model = ClassificationMetrics.f1_score(
                            predicted=fitted_model.predict(test_data),
                            expected=expected_values_as_col,
                            # type: ignore[arg-type]
                            positive_class=positive_class,
                        )  # type: ignore[arg-type]
                        if error_of_fitted_model > best_metric_value:
                            best_model = fitted_model  # pragma: no cover
                            best_metric_value = error_of_fitted_model  # pragma: no cover
        assert best_model is not None  # just for linter
        best_model._is_fitted = True
        return best_model

    def _get_best_cnn_model_column(
        self,
        list_of_fitted_models: list[Self],
        test_data: ImageDataset,
        optimization_metric: Literal["accuracy", "precision", "recall", "f1_score"],
        positive_class: Any = None,
    ) -> Self:
        input_data = test_data.get_input()
        expected = test_data.get_output()
        best_model = None
        best_metric_value = None
        for fitted_model in list_of_fitted_models:
            prediction = fitted_model.predict(input_data).get_output()  # type: ignore[attr-defined, arg-type]
            if best_model is None:
                best_model = fitted_model
                match optimization_metric:
                    case "accuracy":
                        best_metric_value = ClassificationMetrics.accuracy(predicted=prediction, expected=expected)
                    case "precision":
                        best_metric_value = ClassificationMetrics.precision(
                            predicted=prediction,
                            expected=expected,
                            positive_class=positive_class,
                        )
                    case "recall":
                        best_metric_value = ClassificationMetrics.recall(
                            predicted=prediction,
                            expected=expected,
                            positive_class=positive_class,
                        )
                    case "f1_score":
                        best_metric_value = ClassificationMetrics.f1_score(
                            predicted=prediction,
                            expected=expected,
                            positive_class=positive_class,
                        )
            else:
                match optimization_metric:
                    case "accuracy":
                        error_of_fitted_model = ClassificationMetrics.accuracy(predicted=prediction, expected=expected)
                        if error_of_fitted_model > best_metric_value:
                            best_model = fitted_model  # pragma: no cover
                            best_metric_value = error_of_fitted_model  # pragma: no cover
                    case "precision":
                        error_of_fitted_model = ClassificationMetrics.precision(
                            predicted=prediction,
                            expected=expected,
                            positive_class=positive_class,
                        )
                        if error_of_fitted_model > best_metric_value:
                            best_model = fitted_model  # pragma: no cover
                            best_metric_value = error_of_fitted_model  # pragma: no cover
                    case "recall":
                        error_of_fitted_model = ClassificationMetrics.recall(
                            predicted=prediction,
                            expected=expected,
                            positive_class=positive_class,
                        )
                        if error_of_fitted_model > best_metric_value:
                            best_model = fitted_model  # pragma: no cover
                            best_metric_value = error_of_fitted_model  # pragma: no cover
                    case "f1_score":
                        error_of_fitted_model = ClassificationMetrics.f1_score(
                            predicted=prediction,
                            expected=expected,
                            positive_class=positive_class,
                        )
                        if error_of_fitted_model > best_metric_value:
                            best_model = fitted_model  # pragma: no cover
                            best_metric_value = error_of_fitted_model  # pragma: no cover
        assert best_model is not None  # just for linter
        best_model._is_fitted = True
        return best_model

    def _get_best_cnn_model_table(
        self,
        list_of_fitted_models: list[Self],
        test_data: ImageDataset,
        optimization_metric: Literal["accuracy", "precision", "recall", "f1_score"],
        positive_class: Any = None,
    ) -> Self:
        input_data = test_data.get_input()
        labels = test_data.get_output()
        expected = self._inverse_one_hot_encode_by_index_of_column(labels)

        # Set positive class to index of positive column, to be able to calculate Classification Metrics
        if positive_class is not None:
            for column_index in range(labels.column_count):
                # TODO Find out if a column is the positive class, maybe by the column name?
                if labels.column_names[column_index] == positive_class:
                    positive_class = labels.column_names[column_index]
                    break

        best_model = None
        best_metric_value = None
        for fitted_model in list_of_fitted_models:
            prediction = self._inverse_one_hot_encode_by_index_of_column(
                fitted_model.predict(input_data).get_output(),  # type: ignore[attr-defined, arg-type]
            )
            if best_model is None:
                best_model = fitted_model
                match optimization_metric:
                    case "accuracy":
                        best_metric_value = ClassificationMetrics.accuracy(
                            predicted=prediction,
                            expected=expected,  # type: ignore[arg-type]
                        )  # type: ignore[arg-type]
                    case "precision":
                        best_metric_value = ClassificationMetrics.precision(
                            predicted=prediction,  # type: ignore[arg-type]
                            expected=expected,
                            positive_class=positive_class,
                        )  # type: ignore[arg-type]
                    case "recall":
                        best_metric_value = ClassificationMetrics.recall(
                            predicted=prediction,  # type: ignore[arg-type]
                            expected=expected,
                            positive_class=positive_class,
                        )  # type: ignore[arg-type]
                    case "f1_score":
                        best_metric_value = ClassificationMetrics.f1_score(
                            predicted=prediction,  # type: ignore[arg-type]
                            expected=expected,
                            positive_class=positive_class,
                        )  # type: ignore[arg-type]
            else:
                match optimization_metric:
                    case "accuracy":
                        error_of_fitted_model = ClassificationMetrics.accuracy(
                            predicted=prediction,
                            expected=expected,  # type: ignore[arg-type]
                        )  # type: ignore[arg-type]
                        if error_of_fitted_model > best_metric_value:
                            best_model = fitted_model  # pragma: no cover
                            best_metric_value = error_of_fitted_model  # pragma: no cover
                    case "precision":
                        error_of_fitted_model = ClassificationMetrics.precision(
                            predicted=prediction,
                            expected=expected,  # type: ignore[arg-type]
                            positive_class=positive_class,
                        )  # type: ignore[arg-type]
                        if error_of_fitted_model > best_metric_value:
                            best_model = fitted_model  # pragma: no cover
                            best_metric_value = error_of_fitted_model  # pragma: no cover
                    case "recall":
                        error_of_fitted_model = ClassificationMetrics.recall(
                            predicted=prediction,  # type: ignore[arg-type]
                            expected=expected,
                            positive_class=positive_class,
                        )  # type: ignore[arg-type]
                        if error_of_fitted_model > best_metric_value:
                            best_model = fitted_model  # pragma: no cover
                            best_metric_value = error_of_fitted_model  # pragma: no cover
                    case "f1_score":
                        error_of_fitted_model = ClassificationMetrics.f1_score(
                            predicted=prediction,
                            expected=expected,  # type: ignore[arg-type]
                            positive_class=positive_class,
                        )  # type: ignore[arg-type]
                        if error_of_fitted_model > best_metric_value:
                            best_model = fitted_model  # pragma: no cover
                            best_metric_value = error_of_fitted_model  # pragma: no cover
        assert best_model is not None  # just for linter
        best_model._is_fitted = True
        return best_model

    def _inverse_one_hot_encode_by_index_of_column(self, table: Table) -> Column:
        indices = []
        for row_index in range(table.row_count):
            for column_index in range(table.column_count):
                if table.get_column(table.column_names[column_index]).get_value(row_index) == 1.0:
                    indices.append(column_index)
                    break
        return Column("class_index", indices)

    def _get_models_for_all_choices(self) -> list[Self]:
        all_possible_layer_combinations: list[list] = [[]]
        for layer in self._layers:
            if not layer._contains_choices():
                for item in all_possible_layer_combinations:
                    item.append(layer)
            else:
                updated_combinations = []
                versions_of_one_layer = layer._get_layers_for_all_choices()
                for version in versions_of_one_layer:
                    copy_of_all_current_possible_combinations = copy.deepcopy(all_possible_layer_combinations)
                    for combination in copy_of_all_current_possible_combinations:
                        combination.append(version)
                        updated_combinations.append(combination)
                all_possible_layer_combinations = updated_combinations

        models = []
        for combination in all_possible_layer_combinations:
            new_model = NeuralNetworkClassifier(input_conversion=self._input_conversion, layers=combination)
            models.append(new_model)
        return models  # type: ignore[return-value]

    def predict(self, test_data: IPT) -> IFT:
        """
        Make a prediction for the given test data.

        The original Model is not modified.

        Parameters
        ----------
        test_data:
            The data the network should predict.

        Returns
        -------
        prediction:
            The given test_data with an added "prediction" column at the end

        Raises
        ------
        ModelNotFittedError
            If the Model has not been fitted yet
        """
        import torch

        _init_default_device()

        if not self._is_fitted or self._model is None:
            raise ModelNotFittedError
        if not self._input_conversion._is_predict_data_valid(test_data):
            raise FeatureDataMismatchError
        dataloader = self._input_conversion._data_conversion_predict(test_data, self._batch_size)
        predictions = []
        with torch.no_grad():
            for x in dataloader:
                elem = self._model(x)
                if not isinstance(elem, torch.Tensor) and hasattr(elem, "logits"):
                    elem = elem.logits  # pragma: no cover
                elif not isinstance(elem, torch.Tensor):
                    raise ValueError(f"Output of model has unsupported type: {type(elem)}")  # pragma: no cover
                if self._num_of_classes > 1:
                    predictions.append(torch.argmax(elem, dim=1))
                else:
                    predictions.append(elem.squeeze(dim=1).round())
        return self._input_conversion._data_conversion_output(
            test_data,
            torch.cat(predictions, dim=0),
        )

    @property
    def is_fitted(self) -> bool:
        """Whether the model is fitted."""
        return self._is_fitted

    @property
    def input_size(self) -> int | ModelImageSize | None:
        """The input size of the model."""
        # TODO: raise if not fitted, don't return None
        return self._input_size

    def _contains_choices(self) -> bool:
        """Whether the model contains choices in any layer."""
        return any(layer._contains_choices() for layer in self._layers)

input_size: int | ModelImageSize | None

The input size of the model.

is_fitted: bool

Whether the model is fitted.

fit

Train the neural network with given training data.

The original model is not modified.

Parameters:

Name Type Description Default
train_data IFT

The data the network should be trained on.

required
epoch_count int

The number of times the training cycle should be done.

25
batch_size int

The size of data batches that should be loaded at one time.

1
learning_rate float

The learning rate of the neural network.

0.001
callback_on_batch_completion Callable[[int, float], None] | None

Function used to view metrics while training. Gets called after a batch is completed with the index of the last batch and the overall loss average.

None
callback_on_epoch_completion Callable[[int, float], None] | None

Function used to view metrics while training. Gets called after an epoch is completed with the index of the last epoch and the overall loss average.

None

Returns:

Name Type Description
trained_model Self

The trained Model

Raises:

Type Description
ValueError

If epoch_count < 1 If batch_size < 1

Source code in src/safeds/ml/nn/_model.py
def fit(
    self,
    train_data: IFT,
    epoch_count: int = 25,
    batch_size: int = 1,
    learning_rate: float = 0.001,
    callback_on_batch_completion: Callable[[int, float], None] | None = None,
    callback_on_epoch_completion: Callable[[int, float], None] | None = None,
) -> Self:
    """
    Train the neural network with given training data.

    The original model is not modified.

    Parameters
    ----------
    train_data:
        The data the network should be trained on.
    epoch_count:
        The number of times the training cycle should be done.
    batch_size:
        The size of data batches that should be loaded at one time.
    learning_rate:
        The learning rate of the neural network.
    callback_on_batch_completion:
        Function used to view metrics while training. Gets called after a batch is completed with the index of the
        last batch and the overall loss average.
    callback_on_epoch_completion:
        Function used to view metrics while training. Gets called after an epoch is completed with the index of the
        last epoch and the overall loss average.

    Returns
    -------
    trained_model:
        The trained Model

    Raises
    ------
    ValueError
        If epoch_count < 1
        If batch_size < 1
    """
    import torch
    from torch import nn

    from ._internal_model import _InternalModel  # Slow import on global level

    _init_default_device()

    if self._contains_choices():
        raise FittingWithChoiceError

    if isinstance(train_data, TimeSeriesDataset) and train_data.continuous:
        raise NotImplementedError(
            "Continuous Predictions are currently not supported for Time Series Classification.",
        )

    if not self._input_conversion._is_fit_data_valid(train_data):
        raise FeatureDataMismatchError

    _check_bounds("epoch_count", epoch_count, lower_bound=_ClosedBound(1))
    _check_bounds("batch_size", batch_size, lower_bound=_ClosedBound(1))

    copied_model = copy.deepcopy(self)
    # TODO: How is this supposed to work with pre-trained models? Should the old weights be kept or discarded?
    copied_model._model = _InternalModel(self._input_conversion, self._layers, is_for_classification=True)
    copied_model._batch_size = batch_size
    copied_model._input_size = copied_model._model.input_size

    # TODO: Re-enable or remove depending on how the above TODO is resolved
    # if copied_model._input_conversion._data_size != copied_model._input_size:
    #     raise InputSizeError(copied_model._input_conversion._data_size, copied_model._input_size)

    dataloader = copied_model._input_conversion._data_conversion_fit(
        train_data,
        copied_model._batch_size,
        copied_model._num_of_classes,
    )

    if copied_model._num_of_classes > 1:
        loss_fn = nn.CrossEntropyLoss()
    else:
        loss_fn = nn.BCELoss()

    optimizer = torch.optim.SGD(copied_model._model.parameters(), lr=learning_rate)
    for _ in range(epoch_count):
        loss_sum = 0.0
        amount_of_loss_values_calculated = 0
        for x, y in iter(dataloader):
            optimizer.zero_grad()
            pred = copied_model._model(x)
            loss = loss_fn(pred, y)
            loss_sum += loss.item()
            amount_of_loss_values_calculated += 1
            loss.backward()
            optimizer.step()

            copied_model._total_number_of_batches_done += 1
            if callback_on_batch_completion is not None:
                callback_on_batch_completion(
                    copied_model._total_number_of_batches_done,
                    loss_sum / amount_of_loss_values_calculated,
                )
        copied_model._total_number_of_epochs_done += 1
        if callback_on_epoch_completion is not None:
            callback_on_epoch_completion(
                copied_model._total_number_of_epochs_done,
                loss_sum / amount_of_loss_values_calculated,
            )
    copied_model._is_fitted = True
    copied_model._model.eval()
    return copied_model

from_pretrained_model

Load a pretrained model from a Huggingface repository.

Parameters:

Name Type Description Default
huggingface_repo str

the name of the huggingface repository

required

Returns:

Name Type Description
pretrained_model NeuralNetworkClassifier

the pretrained model as a NeuralNetworkClassifier

Source code in src/safeds/ml/nn/_model.py
@staticmethod
def from_pretrained_model(huggingface_repo: str) -> NeuralNetworkClassifier:  # pragma: no cover
    """
    Load a pretrained model from a [Huggingface repository](https://huggingface.co/models/).

    Parameters
    ----------
    huggingface_repo:
        the name of the huggingface repository

    Returns
    -------
    pretrained_model:
        the pretrained model as a NeuralNetworkClassifier
    """
    from transformers import AutoConfig, AutoImageProcessor, AutoModelForImageClassification, PretrainedConfig
    from transformers.models.auto.modeling_auto import MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES

    _init_default_device()

    config: PretrainedConfig = AutoConfig.from_pretrained(huggingface_repo)

    if config.model_type not in MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES:
        raise ValueError("This model is not supported")

    model: Module = AutoModelForImageClassification.from_pretrained(huggingface_repo)

    image_processor: BaseImageProcessor = AutoImageProcessor.from_pretrained(huggingface_repo)
    if hasattr(image_processor, "size") and hasattr(config, "num_channels"):
        if "shortest_edge" in image_processor.size:
            input_size = ConstantImageSize(
                image_processor.size.get("shortest_edge"),
                image_processor.size.get("shortest_edge"),
                config.num_channels,
            )
        else:
            input_size = ConstantImageSize(
                image_processor.size.get("width"),
                image_processor.size.get("height"),
                config.num_channels,
            )
    else:  # Should never happen due to model check
        raise ValueError("This model is not supported")  # pragma: no cover

    label_dict: dict[str, str] = config.id2label
    column_name = "label"
    labels_table = Table({column_name: [label for _, label in label_dict.items()]})
    one_hot_encoder = OneHotEncoder(column_names=[column_name]).fit(labels_table)

    in_conversion = InputConversionImageToColumn(input_size)

    in_conversion._column_name = column_name
    in_conversion._one_hot_encoder = one_hot_encoder
    in_conversion._input_size = input_size
    in_conversion._output_type = _ColumnAsTensor
    num_of_classes = labels_table.row_count

    network = NeuralNetworkClassifier.__new__(NeuralNetworkClassifier)
    network._input_conversion = in_conversion
    network._model = model
    network._input_size = input_size
    network._batch_size = 1
    network._is_fitted = True
    network._num_of_classes = num_of_classes
    network._total_number_of_epochs_done = 0
    network._total_number_of_batches_done = 0

    return network

predict

Make a prediction for the given test data.

The original Model is not modified.

Parameters:

Name Type Description Default
test_data IPT

The data the network should predict.

required

Returns:

Name Type Description
prediction IFT

The given test_data with an added "prediction" column at the end

Raises:

Type Description
ModelNotFittedError

If the Model has not been fitted yet

Source code in src/safeds/ml/nn/_model.py
def predict(self, test_data: IPT) -> IFT:
    """
    Make a prediction for the given test data.

    The original Model is not modified.

    Parameters
    ----------
    test_data:
        The data the network should predict.

    Returns
    -------
    prediction:
        The given test_data with an added "prediction" column at the end

    Raises
    ------
    ModelNotFittedError
        If the Model has not been fitted yet
    """
    import torch

    _init_default_device()

    if not self._is_fitted or self._model is None:
        raise ModelNotFittedError
    if not self._input_conversion._is_predict_data_valid(test_data):
        raise FeatureDataMismatchError
    dataloader = self._input_conversion._data_conversion_predict(test_data, self._batch_size)
    predictions = []
    with torch.no_grad():
        for x in dataloader:
            elem = self._model(x)
            if not isinstance(elem, torch.Tensor) and hasattr(elem, "logits"):
                elem = elem.logits  # pragma: no cover
            elif not isinstance(elem, torch.Tensor):
                raise ValueError(f"Output of model has unsupported type: {type(elem)}")  # pragma: no cover
            if self._num_of_classes > 1:
                predictions.append(torch.argmax(elem, dim=1))
            else:
                predictions.append(elem.squeeze(dim=1).round())
    return self._input_conversion._data_conversion_output(
        test_data,
        torch.cat(predictions, dim=0),
    )