[선형대수학] 벡터와 행렬의 연산 - 내적과 내적의 활용

벡터와 행렬의 연산 - 내적과 내적의 활용 (가중합/코사인유사도/직교/이차형식) 관련 내용을 정리합니다.

내적 : 벡터-벡터 곱셈

Inner product / Dot product 내적은 벡터간의 곱을 의미한다.

$\mathbf{v} \cdot \mathbf{u}=\mathbf{v}^{\top} \mathbf{u}=\sum_{i} u_{i} v_{i}$
$x^T y = \begin{bmatrix} x_{1} & x_{2} & \cdots & x_{N} \end{bmatrix} \begin{bmatrix} y_{1} \\ y_{2} \\ \vdots \\ y_{N} \\ \end{bmatrix} = x_1 y_1 + \cdots + x_N y_N = \sum_{i=1}^N x_i y_i$

  • 내적 연산을 위해서는 2가지 조건을 만족해야한다.
    1. 두 벡터의 차원(길이)가 같아야 한다.
    2. 앞의 벡터가 행벡터이고 뒤의 벡터가 열벡터여야 한다.
  • 두 벡터의 내적을 요소별 곱셈 후 합을 수행해서도 구할 수 있다. ( 2. tf.reduce_sum코드 참고 )

tf.tensordot/tf.reduce_sum

## 1. tf.tensordot : Dot Products
y = tf.ones(4, dtype=tf.float32) 
x, y, tf.tensordot(x, y, axes=1) 
(<tf.Tensor: shape=(4,), dtype=float32, numpy=array([0., 1., 2., 3.], dtype=float32)>, 
<tf.Tensor: shape=(4,), dtype=float32, numpy=array([1., 1., 1., 1.], dtype=float32)>, 
<tf.Tensor: shape=(), dtype=float32, numpy=6.0>) 

## 2. tf.reduce_sum : elementwise multiplication and then a sum
tf.reduce_sum(x* y)  
<tf.Tensor: shape=(), dtype=float32, numpy=6.0>

내적의 활용

벡터의 내적은 유용하게 사용되는데, 그 예시들을 살펴보자.

1. 가중합/가중평균

하나의 벡터에 대한 가중합을 벡터의 내적으로 표현해보자.

가중합

weighted sum 가중합은 $\mathbf{x}$벡터와 $\mathbf{w}$가중치 벡터 $(\in \mathbb{R}^d)$ 가중치가 반영된 $\mathbf{x}$벡터 값의 합이다.
$w_1 x_1 + \cdots + w_N x_N = \sum_{i=1}^N w_i x_i$

가중평균

weighted average 가중평균은 $\sum_{i=1}^{d} w_{i}=1$ 가중치 총 합이 1인 경우의 가중합을 의미한다.

가중합과 가중평균을 $\mathbf{x}^\top \mathbf{w}$ 내적으로 표현하면 다음과 같다.

$\begin{aligned}
\sum_{i=1}^{N} w_{i} x_{i} &=\left[\begin{array}{llll}
w_{1} & w_{2} & \cdots & w_{N}
\end{array}\right]\left[\begin{array}{c}
x_{1} \\
x_{2} \\
\vdots \\
x_{N}
\end{array}\right] & &=w^{T} x \\
&=\left[\begin{array}{llll}
x_{1} & x_{2} & \cdots & x_{N}
\end{array}\right]\left[\begin{array}{c}
w_{1} \\
w_{2} \\
\vdots \\
w_{N}
\end{array}\right] & &=x^{T} w
\end{aligned}
$

여러 개의 벡터에 대한 가중합 계산

여러 벡터 $x_{1}, \cdots, x_{M}$에 대해서 가중합을 동시에 계산하는 식을 벡터의 내적으로 표현해보자. (선형 회귀 모형에서 사용되는 수식)

$\hat{y} = X w \rightarrow \ \hat{y}= \begin{bmatrix}
\hat{y}_1 \\
\hat{y}_2 \\
\vdots \\
\hat{y}_M \\
\end{bmatrix}
$

$= \begin{bmatrix}
w_1 x_{1,1} + w_2 x_{1,2} + \cdots + w_N x_{1,N} \\
w_1 x_{2,1} + w_2 x_{2,2} + \cdots + w_N x_{2,N} \\
\vdots \\
w_1 x_{M,1} + w_2 x_{M,2} + \cdots + w_N x_{M,N} \\
\end{bmatrix}
$

$=\begin{bmatrix}
x_{1,1} & x_{1,2} & \cdots & x_{1,N} \\
x_{2,1} & x_{2,2} & \cdots & x_{2,N} \\
\vdots & \vdots & \vdots & \vdots \\
x_{M,1} & x_{M,2} & \cdots & x_{M,N} \\
\end{bmatrix} \begin{bmatrix}
w_1 \\ w_2 \\ \vdots \\ w_N
\end{bmatrix}$

$=\begin{bmatrix}
x_1^T \\
x_2^T \\
\vdots \\
x_M^T \\
\end{bmatrix}
\begin{bmatrix}
w_1 \\ w_2 \\ \vdots \\ w_N
\end{bmatrix}
$

2. 코사인 유사도

Cosine Similarity 코사인 유사도를 통해 두 벡터의 유사도를 측정할 수 있다.
$\cos (\theta)=\frac{\mathbf{a} \cdot \mathbf{b}}{|\mathbf{a}||\mathbf{b}|}$

  • $-1≤\cos (\theta)≤1$
    • $\cos (\theta)= 1$ : 두 벡터 동일한 방향
    • $\cos (\theta)= -1$ : 두 벡터 정반대 방향
    • $\cos (\theta)= 0$ : 두 벡터가 직교 orthogonal
  • 고차원의 벡터들의 원소의 랜덤 표본 평균이 0인 경우, 코사인 유사도 또한 0 에 가깝다.

3. 직교

$x^Ty=0$ : 두 벡터$x,y \in \mathbb{R}^{n}$ 내적이 0이면, 두 벡터는 Orthogonal 직교한다.

  • 직교 또한 고차원에 대한 expansion of perpendicular 수직확장으로 볼 수 있다.
  • $x_{1}, x_{2}, \ldots, x_{n}$ $n$-차원의 벡터로 가정하면, $x_{1}, x_{2}, \ldots, x_{n}$의 선형결합을 $c_{1} x_{1}+c_{2} x_{2}+\ldots+c_{n} x_{n}$로 표현할 수 있다. ($c_{1} x_{1}+c_{2} x_{2}+\ldots+c_{n} x_{n}$ : 스칼라)
  • $U^{T} U=I=U U^{T}$
  • 두 벡터 $g,h$는 직교이다.
    $g \cdot h =\left(\begin{array}{lll}
    2 & 3 & -2
    \end{array}\right) *\left(\begin{array}{c}
    4 \\
    -2 \\
    1
    \end{array}\right)\\=2 * 4+(3) *(-2)+(-2) * 1\\=8-6-2=0
    $

4. 이차형식

벡터의 이차형식 Quadratic form어떤 벡터와 정방행렬이 (행벡터 × 정방행렬 × 열벡터) 형식으로 된 것을 의미한다.

$x^{T} A x=\left[\begin{array}{llll}
x_{1} & x_{2} & \cdots & x_{N}
\end{array}\right]\left[\begin{array}{cccc}
a_{1,1} & a_{1,2} & \cdots & a_{1, N} \\
a_{2,1} & a_{2,2} & \cdots & a_{2, N} \\
\vdots & \vdots & \ddots & \vdots \\
a_{N, 1} & a_{N, 2} & \cdots & a_{N, N}
\end{array}\right]\left[\begin{array}{c}
x_{1} \\
x_{2} \\
\vdots \\
x_{N}
\end{array}\right]\\=\sum_{i=1}^{N} \sum_{j=1}^{N} a_{i, j} x_{i} x_{j}$

⇒ $i=1, \ldots, N, j=1, \ldots, N$에 대해 가능한 모든 $i,j$쌍의 조합에 해당하는 원소 $x_{i}, x_{j}$를 가중치 $a_{i, j}$ 와 곱한 값인 $a_{i, j} x_{i}x_{j}$ 총합이 된다.


Source&Reference: 김도형의 데이터사이언스 스쿨