| Total Complexity | 1 |
| Total Lines | 29 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 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 |