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.ABC

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

Bases: sensai.vector_model.VectorRegressionModel, sensai.tensor_model.TensorModel, abc.ABC

__init__(check_input_shape=True, check_input_columns=True)

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: pandas.core.frame.DataFrame) pandas.core.frame.DataFrame

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: sensai.vector_model.VectorClassificationModel, sensai.tensor_model.TensorModel, abc.ABC

__init__(check_input_shape=True, check_input_columns=True)

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: pandas.core.frame.DataFrame) pandas.core.frame.DataFrame

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: sensai.vector_model.VectorRegressionModel, sensai.tensor_model.TensorModel, abc.ABC

__init__(check_input_shape=True, check_output_shape=True, check_input_columns=True)

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: pandas.core.frame.DataFrame) pandas.core.frame.DataFrame

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: sensai.vector_model.VectorModel, sensai.tensor_model.TensorModel, abc.ABC

__init__(check_input_shape=True, check_output_shape=True, check_input_columns=True)

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
get_num_predicted_classes()
fit(x: pandas.core.frame.DataFrame, y: pandas.core.frame.DataFrame, fit_preprocessors=True, fit_model=True)
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()
convert_class_probabilities_to_predictions(df: pandas.core.frame.DataFrame)

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: pandas.core.frame.DataFrame) pandas.core.frame.DataFrame
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: pandas.core.frame.DataFrame) pandas.core.frame.DataFrame

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