Shortcuts

tensor

trojanzoo.utils.tensor.tanh_func(x)[source]

tanh object function.

return x.tanh().add(1).mul(0.5)
Parameters:

x (torch.Tensor) – The tensor ranging from [,+][-\infty, +\infty].

Returns:

torch.Tensor – The tensor ranging in [0, 1]

trojanzoo.utils.tensor.atan_func(x)[source]

arctan object function.

return x.atan().div(math.pi).add(0.5)
Parameters:

x (torch.Tensor) – The tensor ranging from [],+][-\infty], +\infty].

Returns:

torch.Tensor – The tensor ranging in [0, 1]

trojanzoo.utils.tensor.repeat_to_batch(x, batch_size=1)[source]

Repeat a single input tensor to a batch.

Parameters:
  • x (torch.Tensor) – The single input tensor to process with shape (*).

  • batch_size (int) – Batch size. Defaults to 1.

Returns:

torch.Tensor – The batched input tensor with shape (batch_size, *)

trojanzoo.utils.tensor.add_noise(x, noise=None, mean=0.0, std=1.0, universal=False, clip_min=0.0, clip_max=1.0)[source]

Add noise to a batched input tensor.

Parameters:
  • x (torch.Tensor) – The input tensor to process with shape (N, *).

  • noise (torch.Tensor | None) – The pre-defined noise. If None, generate Gaussian noise using torch.normal. Defaults to be None.

  • mean (float) – The mean of generated Gaussian noise. Defaults to 0.0.

  • std (float) – The std of generated Gaussian noise. Defaults to 1.0.

  • universal (bool) – Whether the noise is universal for all samples in the batch. Defaults to False.

  • clip_min (float | torch.Tensor) – The min value of available input region. Defaults to 0.0.

  • clip_max (float | torch.Tensor) – The max value of available input region. Defaults to 1.0.

Returns:

torch.Tensor – The noisy batched input tensor with shape (N, *) ((*) when universal=True).

Docs

Access comprehensive developer documentation for TrojanZoo

View Docs