PlainModel#
- class PlainModel#
A plaintext representation of an ML model.
- compare_weights(self: pyhelayers.PlainModel, plain: pyhelayers.PlainModel, verbose: bool, eps: float) None #
Compare weights of this PlainModel with the PlainModel pass as an argument. :param plain: The PlainModel object, :param verbose: bool, :param eps: double
- static create(*args, **kwargs)#
Overloaded function.
create(hyper_params: pyhelayers.PlainModelHyperParams) -> pyhelayers.PlainModel
Creates and returns a plain model given hyperparameters.
- param hyper_params:
The hyperparameters object.
create(hyper_params: pyhelayers.PlainModelHyperParams, files: List[str]) -> pyhelayers.PlainModel
Creates and returns a plain model given hyperparameters and names of files with contents to load.
- param hyper_params:
The hyperparameters object.
- param files:
List of file names with content to load, depending on the underlying model.
create(hyper_params: pyhelayers.PlainModelHyperParams, buffers: List[bytes], files: List[str]) -> pyhelayers.PlainModel
Creates and returns a plain model given hyperparameters, buffers with contents to load, and file names corresponding to each stream.
- param hyper_params:
The hyperparameters object.
- param buffers:
List of buffers with content to load, depending on the underlying model. All buffers must be rewindable.
- param files:
List of file names corresponding to each buffer.
- fit(self: pyhelayers.PlainModel, inputs: List[numpy.ndarray[numpy.float64]]) None #
Fit (train) the model.
- Parameters:
inputs – The training inputs.
- get_empty_he_model(self: pyhelayers.PlainModel, he_context: pyhelayers.HeContext) pyhelayers.HeModel #
Returns an empty (not initialized) HE model from the underlying type corresponding to this plain model.
- Parameters:
he_context – The HE context.
- get_hyper_params(self: pyhelayers.PlainModel) pyhelayers.PlainModelHyperParams #
Returns the model’s hyperparameters.
- get_inputs_plain_tensor_metadata(self: pyhelayers.PlainModel) List[pyhelayers.PlainTensorMetadata] #
Returns a vector of PlainTensorMetadata objects. The i-th element of this vector contains metadata relating to the i-th input of this PlainModel (such as shape and batch dimension). If this PlainModel is initialized for prediction, the returned vector describes inputs for the the predict() method. If this PlainModel is initialized for fitting, the returned vector describes the inputs for the fit() method.
- init(self: pyhelayers.PlainModel, hyper_params: pyhelayers.PlainModelHyperParams, buffers: List[bytes], files: List[str]) None #
Initializes a plain model given hyperparameters, buffers with contents to load, and file names corresponding to each stream.
- Parameters:
hyper_params – The hyperparameters object.
buffers – List of buffers with content to load, depending on the underlying model. All buffers must be rewindable.
files – List of file names corresponding to each buffer.
- init_from_files(self: pyhelayers.PlainModel, hyper_params: pyhelayers.PlainModelHyperParams, files: List[str]) None #
Initializes a plain model given hyperparameters and names of files with contents to load.
- Parameters:
hyper_params – The hyperparameters object.
files – List of file names with content to load, depending on the underlying model.
- is_fit_mode(self: pyhelayers.PlainModel) bool #
Returns indication for whether the initialized model is on fit-only or on predict-only mode.
- is_verbose(self: pyhelayers.PlainModel) bool #
Returns indication for whether the initialized model verbosity is turned on or off.
- predict(self: pyhelayers.PlainModel, inputs: List[numpy.ndarray[numpy.float64]]) List[numpy.ndarray[numpy.float64]] #
Run prediction (inference) over the model. Returns predictions.
- Parameters:
inputs – The inputs.
- validate_init(self: pyhelayers.PlainModel) None #
Validates this model has been initialized.
-
class PlainModel : public helayers::Printable#
A plaintext representation of an ML model.
Subclassed by helayers::AmlPlain, helayers::ArimaPlain, helayers::DTreePlain, helayers::KMeansPlain, helayers::LogisticRegressionPlain, helayers::NeuralNetPlain, helayers::XGBoostPlain
Public Functions
-
PlainModel()#
Construct a new plain model object.
-
virtual ~PlainModel()#
Destroy the plain model object.
-
PlainModel(const PlainModel &src) = delete#
Deleted copy constructor.
-
PlainModel &operator=(const PlainModel &src) = delete#
Deleted operator=.
Initializes a plain model given hyperparameters, streams with contents to load, and file names corresponding to each stream.
- Parameters:
hyperParams – The hyperparameters object.
streams – List of streams with content to load, depending on the underlying model. All streams must be rewindable.
files – List of file names corresponding to each stream.
-
void initFromFiles(const PlainModelHyperParams &hyperParams, const std::vector<std::string> &files)#
Initializes a plain model given hyperparameters and names of files with contents to load.
- Parameters:
hyperParams – The hyperparameters object.
files – List of file names with content to load, depending on the underlying model.
-
virtual std::vector<PlainTensorMetadata> getInputsPlainTensorMetadata() const = 0#
Returns a vector of PlainTensorMetadata objects.
The i-th element of this vector contains metadata relating to the i-th input of this PlainModel (such as shape and batch dimension). If this PlainModel is initialized for prediction, the returned vector describes inputs for the the predict() method. If this PlainModel is initialized for fitting, the returned vector describes the inputs for the fit() method.
-
virtual std::optional<DimInt> getInputsBatchDim() const#
Returns the batch dimension of the inputs, if exist.
-
std::optional<DimInt> getRequirementForFixedBatchSize() const#
Returns a value if the model has a requirement for a fixed batch size.
If so, it is only able to process batches of the given size.
-
std::vector<DoubleTensorCPtr> predict(const std::vector<DoubleTensorCPtr> &inputs) const#
Run prediction (inference) over the model.
Returns predictions.
- Parameters:
inputs – The inputs.
-
void fit(const std::vector<DoubleTensorCPtr> &inputs)#
Fit (train) the model.
- Parameters:
inputs – The training inputs.
-
bool isFitMode() const#
Returns indication for whether the initialized model is on fit-only or on predict-only mode.
-
void setPredictMode()#
Set the model to predict mode.
-
bool isVerbose() const#
Returns indication for whether the initialized model verbosity is turned on or off.
-
const PlainModelHyperParams &getHyperParams() const#
Returns the model’s hyperparameters.
-
bool isInitialized() const#
Returns indication for whether the model is initialized.
-
void validateInit() const#
Validates this model has been initialized.
-
virtual std::shared_ptr<HeModel> getEmptyHeModel(const HeContext &he) const = 0#
Returns an empty (not initialized) HE model from the underlying type corresponding to this plain model.
- Parameters:
he – The HE context.
-
virtual std::string getClassName() const = 0#
Returns the name of this class.
-
virtual void debugPrint(const std::string &title = "", Verbosity verbosity = VERBOSITY_REGULAR, std::ostream &out = std::cout) const override#
Prints the content of this object.
- Parameters:
title – Text to add to the print
verbosity – Verbosity level
out – Output stream
-
virtual void compareWeights(const PlainModel &other, bool verbose = false, double eps = 1e-6) const#
Compares the internal weights of this model with the weights of the other model.
- Parameters:
other – The other plain model.
verbose – Whether to report comparison results in verbose way.
eps – Comparison tolerance.
Public Static Functions
-
static std::shared_ptr<PlainModel> create(const PlainModelHyperParams &hyperParams)#
Creates and returns a plain model given hyperparameters.
- Parameters:
hyperParams – The hyperparameters object.
Creates and returns a plain model given hyperparameters, streams with contents to load, and file names corresponding to each stream.
- Parameters:
hyperParams – The hyperparameters object.
streams – List of streams with content to load, depending on the underlying model. All streams must be rewindable.
files – List of file names corresponding to each stream.
-
static std::shared_ptr<PlainModel> create(const PlainModelHyperParams &hyperParams, const std::vector<std::string> &files)#
Creates and returns a plain model given hyperparameters and names of files with contents to load.
- Parameters:
hyperParams – The hyperparameters object.
files – List of file names with content to load, depending on the underlying model.
-
PlainModel()#