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

backup_grad()[source]

Backup the gradients of the model’s parameters.

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

restore()[source]

Restore the original embeddings of the model.

restore_grad()[source]

Restore the original gradients of the model’s parameters.

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.data2gpu(batch, device)[source]
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

faknow.utils.util.seconds2str(seconds: float) str[source]

Convert seconds to a human-readable time format.

Parameters:

seconds (float) – The duration in seconds.

Returns:

The duration in the format ‘h:mm:ss’ or ‘m:ss’.

Return type:

str

faknow.utils.util.set_seed(seed: int)[source]

Set the random seed for the current environment for python, numpy and pytorch

Parameters:

seed (int) – The random seed to use.