gianlp.models.base_model.BaseModel

class gianlp.models.base_model.BaseModel

Bases: ABC

Serializable keras wrapper model class.

Guarantees serializing and deserializing from and to objects with the same behaviour. All base models contains at least one model from Keras.

Methods

build

Builds the whole chain of models in a recursive manner using the functional API.

deserialize

Deserializes a model

preprocess_texts

Given texts returns the array representation needed for forwarding the keras model

serialize

Serializes the model to be deserialized with the deserialize method

Attributes

inputs

Method for getting all models that serve as input

inputs_shape

Returns the shapes of the inputs of the model

outputs_shape

Returns the output shape of the model

trainable_weights_amount

Computes the total amount of trainable weights

weights_amount

Computes the total amount of weights

abstract property inputs: ModelInputsWrapper

Method for getting all models that serve as input

Returns

a ModelInputsWrapper

abstract property outputs_shape: Union[List[ModelIOShape], ModelIOShape]

Returns the output shape of the model

Returns

a list of shape tuple or shape tuple

abstract property inputs_shape: Union[List[ModelIOShape], ModelIOShape]

Returns the shapes of the inputs of the model

Returns

a list of shape tuple or shape tuple

property weights_amount: Optional[int]

Computes the total amount of weights

Returns

the total amount of weights or none if not built

property trainable_weights_amount: Optional[int]

Computes the total amount of trainable weights

Returns

the total amount of trainable weights or none if not built

build(texts: Union[List[str], Series, Dict[str, List[str]], DataFrame]) None

Builds the whole chain of models in a recursive manner using the functional API. Some operations may need the model to be built.

Parameters

texts – the texts for building if needed, some models have to learn from a sample corpus before working

Raises

ValueError – If the multi-text input keys do not match with the ones in a multi-text model

serialize() bytes

Serializes the model to be deserialized with the deserialize method

Returns

a byte array

classmethod deserialize(data: bytes) BaseModel

Deserializes a model

Parameters

data – the data for deserializing

Returns

a BaseModel object

static get_bytes_from_model(model: Model, copy: bool = False) bytes

Transforms a keras model into bytes

Parameters
  • model – the keras model

  • copy – whether to copy the model before saving. copying the model is needed for complex nested models because the keras save/load can fail

Returns

a byte array

static get_model_from_bytes(data: bytes) Model

Given bytes from keras model serialized with get_bytes_from_model method returns the model

Parameters

data – the model bytes

Returns

a keras model

abstract preprocess_texts(texts: Union[List[str], Series, Dict[str, List[str]], DataFrame]) Union[List[ndarray], ndarray]

Given texts returns the array representation needed for forwarding the keras model

Parameters

texts – the texts to preprocess

Returns

a numpy array or list of numpy arrays representing the texts