fairlib.src.networks.adv

fairlib.src.networks.adv.customized_loss

class fairlib.src.networks.adv.customized_loss.DiffLoss

compute the Frobenius norm of two tensors

forward(D1, D2)

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

fairlib.src.networks.adv.utils

class fairlib.src.networks.adv.utils.BaseDiscriminator
class fairlib.src.networks.adv.utils.GradientReversal(lambda_)
forward(x)

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class fairlib.src.networks.adv.utils.GradientReversalFunction(*args, **kwargs)

From: https://github.com/jvanvugt/pytorch-domain-adaptation/blob/cb65581f20b71ff9883dd2435b2275a1fd4b90df/utils.py#L26 Gradient Reversal Layer from: Unsupervised Domain Adaptation by Backpropagation (Ganin & Lempitsky, 2015) Forward pass is the identity function. In the backward pass, the upstream gradients are multiplied by -lambda (i.e. gradient is reversed)

static backward(ctx, grads)

Defines a formula for differentiating the operation with backward mode automatic differentiation (alias to the vjp function).

This function is to be overridden by all subclasses.

It must accept a context ctx as the first argument, followed by as many outputs as the forward() returned (None will be passed in for non tensor outputs of the forward function), and it should return as many tensors, as there were inputs to forward(). Each argument is the gradient w.r.t the given output, and each returned value should be the gradient w.r.t. the corresponding input. If an input is not a Tensor or is a Tensor not requiring grads, you can just pass None as a gradient for that input.

The context can be used to retrieve tensors saved during the forward pass. It also has an attribute ctx.needs_input_grad as a tuple of booleans representing whether each input needs gradient. E.g., backward() will have ctx.needs_input_grad[0] = True if the first input to forward() needs gradient computated w.r.t. the output.

static forward(ctx, x, lambda_)

Performs the operation.

This function is to be overridden by all subclasses.

It must accept a context ctx as the first argument, followed by any number of arguments (tensors or other types).

The context can be used to store arbitrary data that can be then retrieved during the backward pass. Tensors should not be stored directly on ctx (though this is not currently enforced for backward compatibility). Instead, tensors should be saved either with ctx.save_for_backward() if they are intended to be used in backward (equivalently, vjp) or ctx.save_for_forward() if they are intended to be used for in jvp.

class fairlib.src.networks.adv.utils.SubDiscriminator(args)
forward(input_data, group_label=None)

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

fairlib.src.networks.adv.discriminator

fairlib.src.networks.adv.discriminator.adv_eval_epoch(model, discriminators, iterator, args)

evaluate the discriminator

Parameters
  • model (torch.nn.Module) – the main task model.

  • discriminators (torch.nn.Module) – the discriminator

  • iterator (dataloader) – torch data iterator.

  • args (namespace) – arguments for training.

Returns

(evaluation loss, evaluation metrics)

Return type

tuple

fairlib.src.networks.adv.discriminator.adv_train_batch(model, discriminators, batch, args)

train the discriminator one batch

Parameters
  • model (torch.nn.Module) – the main task model

  • discriminators (torch.nn.Module) – the discriminator

  • batch (tuple) – bach data, including inputs, target labels, protected labels, etc.

  • args (namespace) – arguments for training

Returns

training loss

Return type

float

fairlib.src.networks.adv.discriminator.adv_train_epoch(model, discriminators, iterator, args)

train the discriminator one epoch

Parameters
  • model (torch.nn.Module) – the main task model.

  • discriminators (torch.nn.Module) – the discriminator

  • iterator (dataloader) – torch data iterator.

  • args (namespace) – arguments for training.

Returns

training loss.

Return type

float