Skip to content

nst_math

gram_matrix(matrix)

Compute the Gram matrix of input 2D matrix.

In Linear Algebra the Gram matrix G of a set of vectors (u_1, u_2, .. , u_n) is the matrix of dot products, whose entries are:

G_{ij} = u^T_i * u_j = numpy.dot(u_i, u_j) OR GA = A * A^T

Uses tenforflow to compute the Gram matrix of the input 2D matrix.

Parameters:

Name Type Description Default
matrix type

matrix of shape (n_C, n_H * n_W)

required

Returns:

Type Description
tensor

Gram matrix of A, of shape (n_C, n_C)

Source code in src/artificial_artwork/nst_math.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def gram_matrix(matrix: VolumeType) -> VolumeType:
    """Compute the Gram matrix of input 2D matrix.

    In Linear Algebra the Gram matrix G of a set of vectors (u_1, u_2, .. , u_n)
    is the matrix of dot products, whose entries are:

    G_{ij} = u^T_i * u_j = numpy.dot(u_i, u_j)
    OR
    GA = A * A^T

    Uses tenforflow to compute the Gram matrix of the input 2D matrix.

    Args:
        matrix (type): matrix of shape (n_C, n_H * n_W)

    Returns:
        (tf.tensor): Gram matrix of A, of shape (n_C, n_C)
    """
    return tf.matmul(matrix, tf.transpose(matrix))