Passed
Pull Request — master (#1)
by Konstantinos
01:11
created

artificial_artwork.nst_math.gram_matrix()   A

Complexity

Conditions 1

Size

Total Lines 19
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 19
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
from typing import Union
2
from numpy.typing import NDArray
3
import tensorflow as tf
4
5
6
# Define type alias
7
VolumeType = Union[NDArray, tf.python.framework.ops.Tensor]
8
9
10
def gram_matrix(A: VolumeType) -> VolumeType:
11
    """Compute the Gram matrix of input 2D matrix A.
12
13
    In Linear Algebra the Gram matrix G of a set of vectors (u_1, u_2, .. , u_n)
14
    is the matrix of dot products, whose entries are:
15
    
16
    G_{ij} = u^T_i * u_j = numpy.dot(u_i, u_j)
17
    OR
18
    GA = A * A^T
19
20
    Uses tenforflow to compute the Gram matrix of the input 2D matrix.
21
22
    Args:
23
        A (type): matrix of shape (n_C, n_H * n_W)
24
    
25
    Returns:
26
        (tf.tensor): Gram matrix of A, of shape (n_C, n_C)
27
    """
28
    return tf.matmul(A, tf.transpose(A))
29