pygmtools.utils.inner_prod_aff_fn

pygmtools.utils.inner_prod_aff_fn(feat1, feat2, backend=None)[source]

Inner product affinity function. The affinity is defined as

\[\mathbf{f}_1^\top \cdot \mathbf{f}_2\]
Parameters
  • feat1\((b\times n_1 \times f)\) the feature vectors \(\mathbf{f}_1\)

  • feat2\((b\times n_2 \times f)\) the feature vectors \(\mathbf{f}_2\)

  • backend – (default: pygmtools.BACKEND variable) the backend for computation.

Returns

\((b\times n_1\times n_2)\) element-wise inner product affinity matrix

Numpy Implementation Example

This is an example of Numpy implementation for your reference if you want to customize the affinity function:

import numpy as np

def inner_prod_aff_fn(feat1, feat2): # feat1 has shape (n_1, f), feat2 has shape (n_2, f)
    return np.matmul(feat1, feat2.swapaxes(1,2))

The most important thing to bear in mind when customizing is to write an affinity function that respects the input & output dimensions:

  • Input feat1: \((b\times n_1 \times f)\),

  • Input feat2: \((b\times n_2 \times f)\),

  • Output: \((b\times n_1\times n_2)\).

Another example can be found at gaussian_aff_fn().