faknow.utils
faknow.utils.pgd
- class faknow.utils.pgd.PGD(model: Module, emb_name, epsilon=1.0, alpha=0.3)[source]
Bases:
object
- __init__(model: Module, emb_name, epsilon=1.0, alpha=0.3)[source]
Projected Gradient Descent (PGD) attack on a PyTorch model.
- Parameters:
model (nn.Module) – The PyTorch model to be attacked.
emb_name – The name of the embedding parameter to be perturbed.
epsilon (float, optional) – The maximum perturbation allowed (default: 1.0).
alpha (float, optional) – The step size for each iteration of the attack (default: 0.3).
- attack(is_first_attack=False)[source]
Perform the PGD attack on the model.
- Parameters:
is_first_attack (bool, optional) – If True, it creates a backup of the model’s embeddings before performing the first attack (default: False).
- project(param_name: str, param_data, epsilon: float)[source]
Project the perturbed embeddings to stay within the allowed epsilon neighborhood.
- Parameters:
param_name (str) – Name of the embedding parameter.
param_data – The perturbed embedding data.
epsilon (float) – The maximum allowed perturbation.
- Returns:
The projected embedding data.
- Return type:
Tensor
faknow.utils.util
- class faknow.utils.util.EarlyStopping(patience=10, delta=1e-06, mode='max')[source]
Bases:
object
Early stopping to stop the training when the score does not improve after certain epochs.
- __init__(patience=10, delta=1e-06, mode='max')[source]
- Parameters:
patience (int) – number of epochs to wait for improvement, default=10
delta (float) – minimum change in the monitored quantity to qualify as an improvement, default=0.000001
mode (str) – minimize or maximize score, one of {min, max}, default=max
- faknow.utils.util.check_loss_type(result)[source]
Check the type of the loss and convert it to a tensor if necessary.
- Parameters:
result (Union[torch.Tensor, dict]) – The loss value or a dictionary of losses.
- Returns:
A tuple containing the loss tensor and a boolean indicating if the loss was a dictionary.
- Return type:
Tuple[torch.Tensor, bool]
- faknow.utils.util.dict2str(result_dict: Dict[str, float]) str [source]
Convert a dictionary of metrics to a string.
- Parameters:
result_dict (dict) – A dictionary containing metric names and corresponding values.
- Returns:
The formatted string representation of the dictionary.
- Return type:
str
- faknow.utils.util.now2str() str [source]
Get the current time and convert it to a formatted string.
- Returns:
The current time in the format ‘%Y-%m-%d-%H_%M_%S’.
- Return type:
str