robustx.lib.models.pytorch_models package

Submodules

robustx.lib.models.pytorch_models.CustomPyTorchModel module

class robustx.lib.models.pytorch_models.CustomPyTorchModel.CustomPyTorchModel(model, criterion=CrossEntropyLoss(), optimizer_class=<class 'torch.optim.adam.Adam'>, learning_rate=0.001)[source]

Bases: BaseModel

A custom PyTorch model that can be trained, used for predictions, and evaluated for performance.

model

The PyTorch model architecture.

Type:

nn.Module

criterion

The loss function used for training.

Type:

nn.CrossEntropyLoss

optimizer

The optimizer used for training the model.

Type:

optim.Adam

train(X: pd.DataFrame, y: pd.DataFrame, epochs: int = 10, batch_size: int = 32) None:[source]

Trains the model on the provided data for a specified number of epochs and batch size.

predict(X: pd.DataFrame) pd.DataFrame:[source]

Predicts outcomes for multiple instances of the provided feature data.

predict_single(X: pd.DataFrame) int:[source]

Predicts the outcome for a single instance of the provided feature data.

predict_proba(X: pd.DataFrame) pd.DataFrame:[source]

Predicts the probability of each outcome for multiple instances.

evaluate(X: pd.DataFrame, y: pd.DataFrame) float:[source]

Evaluates the model’s accuracy on the provided feature and target data.

evaluate(X, y)[source]

Evaluate the model’s performance on a test set (X, y).

@param X: Feature variables as a pandas DataFrame. @param y: Target variable as a pandas DataFrame. @return: Accuracy of the model as a float.

Return type:

float

predict(X)[source]

Predict outcomes for the given features X (multiple instances).

@param X: Input data as a pandas DataFrame. @return: Predictions as a pandas DataFrame.

Return type:

DataFrame

predict_proba(X)[source]

Predict the probability of outcomes for multiple instances.

@param X: Input data as a pandas DataFrame. @return: Probabilities as a pandas DataFrame.

Return type:

DataFrame

predict_single(X)[source]

Predict the outcome for a single instance.

@param X: Input data for a single instance as a pandas DataFrame. @return: Predicted class as an integer.

Return type:

int

train(X, y, epochs=10, batch_size=32, **kwargs)[source]

Train the PyTorch model using the provided data.

@param X: Feature variables as a pandas DataFrame. @param y: Target variable as a pandas DataFrame. @param epochs: Number of training epochs, default is 10. @param batch_size: Size of each mini-batch, default is 32.

robustx.lib.models.pytorch_models.SimpleNNModel module

class robustx.lib.models.pytorch_models.SimpleNNModel.SimpleNNModel(input_dim, hidden_dim, output_dim, seed=None)[source]

Bases: BaseModel

A simple neural network model using PyTorch. This model can be customized with different numbers of hidden layers and units.

input_dim

The number of input features for the model.

Type:

int

hidden_dim

The number of units in each hidden layer. An empty list means no hidden layers.

Type:

list of int

output_dim

The number of output units for the model.

Type:

int

criterion

The loss function used for training.

Type:

nn.BCELoss

optimizer

The optimizer used for training the model.

Type:

optim.Adam

__create_model() nn.Sequential:

Creates and returns the PyTorch model architecture.

train(X: pd.DataFrame, y: pd.DataFrame, epochs: int = 100) None:[source]

Trains the model on the provided data for a specified number of epochs.

set_weights(weights: Dict[str, torch.Tensor]) None:[source]

Sets custom weights for the model.

predict(X: pd.DataFrame) pd.DataFrame:[source]

Predicts the outcomes for the provided instances.

predict_single(x: pd.DataFrame) int:[source]

Predicts the outcome of a single instance and returns an integer.

evaluate(X: pd.DataFrame, y: pd.DataFrame) float:[source]

Evaluates the model’s accuracy on the provided data.

predict_proba(x: torch.Tensor) pd.DataFrame:[source]

Predicts the probability of outcomes for the provided instances.

predict_proba_tensor(x: torch.Tensor) torch.Tensor:[source]

Predicts the probability of outcomes for the provided instances using tensor input.

get_torch_model() nn.Module:[source]

Returns the underlying PyTorch model.

compute_accuracy(X_test, y_test)[source]
evaluate(X, y)[source]

Evaluates the model’s accuracy.

@param X: Feature variables as a pandas DataFrame. @param y: Target variable as a pandas DataFrame. @return: Accuracy of the model as a float.

get_torch_model()[source]

Retrieves the underlying PyTorch model.

@return: The PyTorch model.

predict(X)[source]

Predicts outcomes for the given input data.

@param X: Input data as a pandas DataFrame or torch tensor. @return: Predictions as a pandas DataFrame.

Return type:

DataFrame

predict_proba(x)[source]

Predicts probabilities of outcomes.

@param x: Input data as a torch tensor. @return: Probabilities of each outcome as a pandas DataFrame.

Return type:

DataFrame

predict_proba_tensor(x)[source]

Predicts probabilities of outcomes using the model.

@param x: Input data as a torch tensor. @return: Probabilities of each outcome as a torch tensor.

Return type:

Tensor

predict_single(x)[source]

Predicts the outcome for a single instance.

@param x: Single input instance as a pandas DataFrame or torch tensor. @return: Prediction as an integer (0 or 1).

Return type:

int

set_weights(weights)[source]

Sets custom weights for the model.

@param weights: Dictionary containing weights and biases for each layer.

train(X, y, epochs=100, **kwargs)[source]

Trains the neural network model.

@param X: Feature variables as a pandas DataFrame. @param y: Target variable as a pandas DataFrame. @param epochs: Number of training epochs.

Module contents