tensor_model#


This module contains base classes for models that input and output tensors, for examples CNNs. The fitting and predictions will still be performed on data frames, like in VectorModel, but now it will be expected that all entries of the input data frame passed to the model are tensors of the same shape. Lists of scalars of the same lengths are also accepted. The same is expected of the ground truth data frames. Everything will work as well if the entries are just scalars but in this case it is recommended to use VectorModel instead.

If we denote the shapes of entries in the dfs as inputTensorShape and outputTensorShape, the model will be fit on input tensors of shape (N_rows, N_inputColumns, inputTensorShape) and output tensors of shape (N_rows, N_outputColumns, outputTensorShape), where empty dimensions (e.g. for one-column data frames) will be stripped.

exception InvalidShapeError[source]#

Bases: Exception

class TensorModel[source]#

Bases: ABC

get_model_input_shape() Optional[Tuple][source]#
get_model_output_shape()[source]#
class TensorToScalarRegressionModel(check_input_shape=True, check_input_columns=True)[source]#

Bases: VectorRegressionModel, TensorModel, ABC

Base class for regression models that take tensors as input and output scalars. They can be evaluated in the same way as non-tensor regression models

Parameters:
  • check_input_shape – Whether to check if during predict input tensors have the same shape as during fit. For certain applications, e.g. using CNNs on larger inputs than the training set, this has to be disabled

  • check_input_columns – Whether to check if input columns at predict time coincide with those at fit time

predict(x: DataFrame) DataFrame[source]#

Applies the model to the given input data frame

Parameters:

x – the input data frame

Returns:

the model outputs in the form of a data frame whose index corresponds to the index of x

class TensorToScalarClassificationModel(check_input_shape=True, check_input_columns=True)[source]#

Bases: VectorClassificationModel, TensorModel, ABC

Base class for classification models that take tensors as input and output scalars. They can be evaluated in the same way as non-tensor classification models

Parameters:
  • check_input_shape – Whether to check if during predict input tensors have the same shape as during fit. For certain applications, e.g. using CNNs on larger inputs than the training set, this has to be disabled

  • check_input_columns – Whether to check if input columns at predict time coincide with those at fit time

predict(x: DataFrame) DataFrame[source]#

Applies the model to the given input data frame

Parameters:

x – the input data frame

Returns:

the model outputs in the form of a data frame whose index corresponds to the index of x

class TensorToTensorRegressionModel(check_input_shape=True, check_output_shape=True, check_input_columns=True)[source]#

Bases: VectorRegressionModel, TensorModel, ABC

Base class for regression models that output tensors. Multiple targets can be used by putting them into separate columns. In that case it is required that all target tensors have the same shape.

Parameters:
  • check_input_shape – Whether to check if during predict tensors have the same shape as during fit. For certain applications, e.g. using CNNs on larger inputs than the training set, this has to be disabled

  • check_output_shape – Whether to check if predictions have the same shape as ground truth data during fit. For certain applications, e.g. using CNNs on larger inputs than the training set, this has to be disabled

  • check_input_columns – Whether to check if input columns at predict time coincide with those at fit time

predict(x: DataFrame) DataFrame[source]#

Applies the model to the given input data frame

Parameters:

x – the input data frame

Returns:

the model outputs in the form of a data frame whose index corresponds to the index of x

class TensorToTensorClassificationModel(check_input_shape=True, check_output_shape=True, check_input_columns=True)[source]#

Bases: VectorModel, TensorModel, ABC

Base class for classification models that output tensors, e.g. for semantic segregation. The models can be fit on a ground truth data frame with a single column. The entries in this column should be binary tensors with one-hot-encoded labels, i.e. of shape (*predictionShape, numLabels)

Parameters:
  • check_input_shape – Whether to check if during predict tensors have the same shape as during fit. For certain applications, e.g. using CNNs on larger inputs than the training set, this has to be disabled

  • check_output_shape – Whether to check if predictions have the same shape as ground truth data during fit. For certain applications, e.g. using CNNs on larger inputs than the training set, this has to be disabled

  • check_input_columns – Whether to check if input columns at predict time coincide with those at fit time

is_regression_model() bool[source]#
get_num_predicted_classes()[source]#
fit(x: DataFrame, y: DataFrame, fit_preprocessors=True, fit_model=True)[source]#
Parameters:
  • x – data frame containing input tensors on which to train

  • y – ground truth has to be an array containing only zeroes and ones (one-hot-encoded labels) of the shape (*prediction_shape, numLabels)

  • fit_preprocessors – whether the model’s preprocessors (feature generators and data frame transformers) shall be fitted

  • fit_model – whether the model itself shall be fitted

get_model_output_shape()[source]#
convert_class_probabilities_to_predictions(df: DataFrame)[source]#

Converts from a result returned by predictClassProbabilities to a result as return by predict.

Parameters:

df – the output data frame from predictClassProbabilities

Returns:

an output data frame as it would be returned by predict

predict_class_probabilities(x: DataFrame) DataFrame[source]#
Parameters:

x – the input data

Returns:

a data frame with a single column containing arrays of shape (*tensorShape, numLabels). Raises an exception if the classifier cannot predict probabilities.

predict(x: DataFrame) DataFrame[source]#

Returns an array of integers. If the model was fitted on binary ground truth arrays of shape (*tensorShape, numLabels), predictions will have the shape tensorShape and contain integers 0, 1, …, numLabels - 1. They correspond to the predicted labels