训练数据集: { x i , y i } i = 1 N \{x_i,y_i\}^N_{i=1} {xi,yi}i=1N
机器学习中的分类
假定输入x是固定的,训练参数W,预测 P ( y ∣ x ) = W y , x ) ∑ c = 1 C e x p ( W c , x ) P(y|x)=\frac{W_y,x)}{\sum^C_{c=1}exp(W_c,x)} P(y∣x)=∑c=1Cexp(Wc,x)Wy,x)
具体过程:
C:总类别数。
W:模型参数矩阵。
对c=1…C都要计算
f
c
f_c
fc。
W
y
⋅
x
=
∑
i
=
1
d
W
y
i
x
i
=
f
y
W_y\cdot{x}=\sum^d_{i=1}W_{y_i}x_i=f_y
Wy⋅x=i=1∑dWyixi=fy
归一化:
P
(
y
∣
x
)
=
e
x
p
(
f
y
)
∑
c
=
1
C
e
x
p
(
f
c
)
=
s
o
f
t
m
a
x
(
f
)
y
P(y|x)=\frac{exp(f_y)}{\sum^C_{c=1}exp(f_c)}=softmax(f)_y
P(y∣x)=∑c=1Cexp(fc)exp(fy)=softmax(f)y
总体损失函数:
J
(
θ
)
=
1
N
∑
i
=
1
N
−
l
o
g
(
e
f
y
i
∑
c
=
1
C
e
f
c
)
+
λ
∑
k
θ
k
2
J(\theta)=\frac{1}{N}\sum^N_{i=1}-log(\frac{e^{f_{y_i}}}{\sum^C_{c=1}e^{f_c}})+\lambda{\sum_k\theta_k^2}
J(θ)=N1i=1∑N−log(∑c=1Cefcefyi)+λk∑θk2
其中
λ
∑
k
θ
k
2
\lambda{\sum_k\theta_k^2}
λ∑kθk2是正则项:使模型权值尽可能小。
f
=
W
x
f=Wx
f=Wx:
f
f
f是模型。
词窗口分类(Window Classification)
训练一个softmax分类器,给中心词分配一个标签,然后用一个窗口把它前后的单词连接起来。
例:窗口长度为2=>中心词左右各两个单词,加中心词,窗口内共五个单词。
.
.
.
m
u
s
e
u
m
s
i
n
P
a
r
i
s
a
r
e
a
m
a
z
i
n
g
.
.
.
...museums\ in\ Paris\ are\ amazing...
...museums in Paris are amazing...
x
w
i
n
d
o
w
=
[
X
m
u
s
e
u
m
s
X
i
n
X
P
a
r
i
s
X
a
r
e
X
a
m
a
z
i
n
g
]
T
∈
R
5
d
x_{window}=[X_museums\ X_{in}\ X_{Paris}\ X_{are}\ X_{amazing}]^T\in{R^{5d}}
xwindow=[Xmuseums Xin XParis Xare Xamazing]T∈R5d
x
=
x
w
i
n
d
o
w
x=x_{window}
x=xwindow:
y
y
^
=
P
(
y
∣
x
)
=
e
x
p
(
W
y
⋅
x
)
∑
c
=
1
C
e
x
p
(
W
c
⋅
x
)
\widehat{y_y}=P(y|x)=\frac{exp(W_y\cdot{x})}{\sum^C_{c=1}exp(W_c\cdot{x})}
yy
=P(y∣x)=∑c=1Cexp(Wc⋅x)exp(Wy⋅x)
其中
y
y
^
\widehat{y_y}
yy
指模型预测的正确类别。
更新词向量
标记:
t
t
t:目标概率分布。独热向量:只有在正确类别y的值为1,其余为零。
f
=
f
(
x
)
=
W
x
∈
R
C
f=f(x)=Wx\in{R^C}
f=f(x)=Wx∈RC:C维向量,C是类别的数量。
链式法则:
∂
∂
x
−
l
o
g
(
s
o
f
t
m
a
x
(
f
y
(
x
)
)
\frac{\partial}{\partial{x}}-log(softmax(f_y(x))
∂x∂−log(softmax(fy(x))
=
∑
c
=
1
C
−
∂
l
o
g
(
s
o
f
t
m
a
x
(
f
y
(
x
)
∂
f
c
⋅
∂
f
c
(
x
)
∂
x
=\sum^C_{c=1}-\frac{\partial{log(softmax(f_y(x)}}{\partial{f_c}}\cdot{\frac{\partial{f_c(x)}}{\partial{x}}}
=c=1∑C−∂fc∂log(softmax(fy(x)⋅∂x∂fc(x)
考虑两种情况:
(1)
c
=
y
c=y
c=y。
(2)
c
≠
y
c\neq{y}
c̸=y
∂
∂
x
−
l
o
g
(
s
o
f
t
m
a
x
(
f
y
(
x
)
)
=
[
y
1
^
,
y
2
^
,
.
.
.
,
y
y
^
−
1
,
.
.
.
y
C
^
]
T
\frac{\partial}{\partial{x}}-log(softmax(f_y(x))=[\widehat{y_1},\widehat{y_2},...,\widehat{y_y}-1,...\widehat{y_C}]^T
∂x∂−log(softmax(fy(x))=[y1
,y2
,...,yy
−1,...yC
]T
在正确类别减一,其他什么也不做。
公式向量化:
∂
∂
x
−
l
o
g
(
s
o
f
t
m
a
x
(
f
y
(
x
)
)
=
[
y
^
−
t
]
=
δ
\frac{\partial}{\partial{x}}-log(softmax(f_y(x))=[\widehat{y}-t]=\delta
∂x∂−log(softmax(fy(x))=[y
−t]=δ
δ
\delta
δ:误差信号
=
∑
c
=
1
C
−
∂
l
o
g
(
s
o
f
t
m
a
x
(
f
y
(
x
)
∂
f
c
⋅
∂
f
c
(
x
)
∂
x
=
∑
c
=
1
C
δ
c
W
c
T
=\sum^C_{c=1}-\frac{\partial{log(softmax(f_y(x)}}{\partial{f_c}}\cdot{\frac{\partial{f_c(x)}}{\partial{x}}}=\sum^C_{c=1}\delta_cW_c^T
=c=1∑C−∂fc∂log(softmax(fy(x)⋅∂x∂fc(x)=c=1∑CδcWcT
∂
∂
x
−
l
o
g
P
(
y
∣
x
)
=
∑
c
=
1
C
δ
c
W
c
T
=
W
T
δ
∈
R
5
d
\frac{\partial}{\partial{x}}-logP(y|x)=\sum^C_{c=1}\delta_cW_c^T=W^T\delta\in{R^{5d}}
∂x∂−logP(y∣x)=c=1∑CδcWcT=WTδ∈R5d
这里的x指窗口,则:
∇
x
J
=
W
T
δ
=
δ
x
w
i
n
d
o
w
=
[
∇
x
m
u
s
e
u
m
s
,
∇
x
i
n
,
∇
x
P
a
r
i
s
,
∇
x
a
r
e
,
∇
x
a
m
a
z
i
n
g
]
T
∈
R
5
d
\nabla_xJ=W^T\delta=\delta_{x_{window}}=[\nabla_{x_{museums}},\nabla_{x_{in}},\nabla_{x_{Paris}},\nabla_{x_{are}},\nabla_{x_{amazing}}]^T\in{R^{5d}}
∇xJ=WTδ=δxwindow=[∇xmuseums,∇xin,∇xParis,∇xare,∇xamazing]T∈R5d
这个窗口中有
i
n
in
in,则这个梯度会出现在所有包含
i
n
in
in的窗口中。