Jan 10, 2016

[ML] Vectorization

i.e:
row * col



m 為 training set
n 為features

X0^(i=1) 是 X0的第1個training set value.
X1^(i=2) 是 X1的第2個training set value.
...
Xn^(i=m) 是 Xn的第m個training set value.

Design Matrix X為:
X = [
 X0 X1 ... Xn
 X0 X1 ... Xn
 X0 X1 ... Xn
 X0 X1 ... Xn
.
.
.
 X0 X1 ... Xn
]  為 m X n Matrix

X^T , 即X transpose:
X^T=[
X0 X0 X0 ... X0
X1 X1 X1 ... X1
...
Xn Xn Xn ... Xn] 為 n X m Matrix

theta為:
theta = [
theta0
theta1] 為(n+1) X 1 Matrix

h(X) 為:
X * theta=[
val1
val2
...
valm
] 為 m X 1 Matrix

difference:
y=[
real_val1
real_val2
...
real_valm] 為 m X 1 Matrix

diff = h(x) - y 為 m X 1 Matrix

原本的方程:
diff(i) * X0^(i) + diff(i)*X0^(i) ... + diff(i)*X0^(i)

Vectorize!!:
也就是要:
X0^1 * diff(1) + X0^2*diff(2) + ... + X0^m * diff(m)
X0^1 ~ X0^m 可由 X^T, 即 design matrix X transpose 這個 n X m matrix的第一個row得到。
diff(1) ~ diff(m) 可由 diff 這個 m X 1 matrix 得到

X^T * diff 為 n X 1 matrix.
第1個row 為
diff(i) * X0^(i) + diff(i)*X0^(i) ... + diff(i)*X0^(i) 的和
第2個row 為
diff(i) * X1^(i) + diff(i)*X1^(i) ... + diff(i)*X1^(i) 的和
第n個row 為
diff(i) * Xn^(i) + diff(i)*Xn^(i) ... + diff(i)*Xn^(i) 的和

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.