bisection(edge_weights, a, b, n_perturbations, epsilon=1e-05, iter_max=100000.0)

Find root using bisection method for edge weight adjustment.

Parameters:
  • edge_weights (Tensor) –

    Edge weights to adjust

  • a (float) –

    Lower bound

  • b (float) –

    Upper bound

  • n_perturbations (int) –

    Target number of perturbations

  • epsilon (float, default: 1e-05 ) –

    Convergence threshold. Default: 1e-5

  • iter_max (int, default: 100000.0 ) –

    Maximum iterations. Default: 1e5

Returns:
  • float

    Root value for weight adjustment

Notes

Processing Steps:

  • Define target function
  • Iterative bisection
  • Check convergence
  • Update bounds

Features:

  • Numerical optimization
  • Convergence control
  • Iteration limiting
diff(t1, t2)

Compute normalized squared Euclidean distance between tensors.

Parameters:
  • t1 (Tensor) –

    First tensor of shape (batch_size, feature_dim)

  • t2 (Tensor) –

    Second tensor of shape (batch_size, feature_dim)

Returns:
  • Tensor

    Mean normalized squared distance

Notes

Processing Steps:

  • Normalize input tensors
  • Compute squared differences
  • Calculate mean distance

Features:

  • L2 normalization
  • Euclidean distance
  • Batch processing
get_modified_adj(modified_edge_index, perturbed_edge_weight, n, device, edge_index, edge_weight, make_undirected=False)

Create modified adjacency matrix with perturbed edges.

Parameters:
  • modified_edge_index (Tensor) –

    Modified edge indices

  • perturbed_edge_weight (Tensor) –

    Perturbed edge weights

  • n (int) –

    Number of nodes

  • device (device) –

    Device to store tensors

  • edge_index (Tensor) –

    Original edge indices

  • edge_weight (Tensor) –

    Original edge weights

  • make_undirected (bool, default: False ) –

    Whether to make graph undirected. Default: False

Returns:
  • tuple[Tensor, Tensor]

    Modified edge indices and weights

Notes

Processing Steps:

  • Handle undirected option
  • Combine edges
  • Coalesce edges
  • Adjust weights

Features:

  • Graph modification
  • Edge coalescing
  • Weight adjustment
  • Undirected support
grad_with_checkpoint(outputs, inputs)

Compute gradients with checkpointing for memory efficiency.

Parameters:
  • outputs (Tensor) –

    Output tensor requiring gradient computation

  • inputs (torch.Tensor or tuple of torch.Tensor) –

    Input tensor(s) to compute gradients with respect to

Returns:
  • list[Tensor]

    List of gradient tensors for each input

Notes

Processing Steps:

  • Handle single/multiple inputs
  • Retain gradients for non-leaf tensors
  • Compute backward pass
  • Clone and clear gradients

Features:

  • Memory efficient
  • Multiple input support
  • Gradient preservation
  • Clean gradient states
inner(t1, t2)

Compute normalized inner product distance between tensors.

Parameters:
  • t1 (Tensor) –

    First tensor of shape (batch_size, feature_dim)

  • t2 (Tensor) –

    Second tensor of shape (batch_size, feature_dim)

Returns:
  • Tensor

    Mean normalized inner product distance

Notes

Processing Steps:

  • Normalize input tensors
  • Compute inner products
  • Calculate mean distance

Features:

  • L2 normalization
  • Numerical stability
  • Batch processing
inner_margin(t1, t2, margin)

Compute margin-based inner product distance between tensors.

Parameters:
  • t1 (Tensor) –

    First tensor of shape (batch_size, feature_dim)

  • t2 (Tensor) –

    Second tensor of shape (batch_size, feature_dim)

  • margin (float) –

    Margin threshold for distance computation

Returns:
  • Tensor

    Mean margin-based distance

Notes

Processing Steps:

  • Normalize input tensors
  • Compute inner products
  • Apply margin threshold
  • Calculate mean distance

Features:

  • Margin-based learning
  • L2 normalization
  • ReLU activation
linear_to_triu_idx(n, lin_idx)

Convert linear indices to upper triangular matrix indices.

Parameters:
  • n (int) –

    Size of the square matrix

  • lin_idx (Tensor) –

    Linear indices to convert

Returns:
  • Tensor

    Stack of row and column indices (2, num_indices)

Notes

Processing Steps:

  • Calculate row indices
  • Calculate column indices
  • Stack indices together

Features:

  • Matrix coordinate conversion
  • Efficient computation
  • Double precision handling
project(n_perturbations, values, eps, inplace=False)

Project values onto constrained space with perturbation budget.

Parameters:
  • n_perturbations (int) –

    Number of allowed perturbations

  • values (Tensor) –

    Values to project

  • eps (float) –

    Small constant for numerical stability

  • inplace (bool, default: False ) –

    Whether to modify values in-place. Default: False

Returns:
  • Tensor

    Projected values

Notes

Processing Steps:

  • Check perturbation budget
  • Find projection threshold
  • Apply clamping
  • Handle numerical bounds

Features:

  • Constraint satisfaction
  • Memory efficiency
  • Numerical stability
to_symmetric(edge_index, edge_weight, n, op='mean')

Convert directed graph to undirected by symmetrization.

Parameters:
  • edge_index (Tensor) –

    Edge indices

  • edge_weight (Tensor) –

    Edge weights

  • n (int) –

    Number of nodes

  • op (str, default: 'mean' ) –

    Operation for combining weights. Default: 'mean'

Returns:
  • tuple[Tensor, Tensor]

    Symmetric edge indices and weights

Notes

Processing Steps:

  • Mirror edges
  • Duplicate weights
  • Coalesce edges
  • Combine weights

Features:

  • Edge symmetrization
  • Weight handling
  • Efficient coalescing
  • Operation flexibility