35. Dynamic Programming による効率化
Kn(sx, t) = Kn(s, t) +(u の末尾が x で終わるものの追加分)
文字列 s の末尾に文字 x を追加した場合
s
t
u
u x
x
x
|s| + 1i1
j1
t
x
jn−1 + 1 k
|s| + 1 − i1 + 1
u が x より手前にある t の部分文字列の集合
Kn(sx, t) = Kn(s, t) +
u∈Σn−1
i:u=s[i]
k:tk=x
j:u=t[j],jn−1k
λ|s|+1−i1+1
λk−j1+1
= Kn(s, t) +
k:tk=x
λ2
u∈Σn−1
i:u=s[i]
j:u=t[j],jn−1k
λ|s|−i1+1
λk−1−j1+1
= Kn(s, t) +
k:tk=x
λ2
K
n−1(s, t[1 : k − 1])
K
n(s, t) =
u∈Σn
i:u=s[i]
j:u=t[j]
λ|s|−i1+1
λ|t|−j1+1
※ は文字列 の長さ|s| s
36. Dynamic Programming による効率化
s
x
x
x
K
n(s, t) =
u∈Σn
i:u=s[i]
j:u=t[j]
λ|s|−i1+1
λ|t|−j1+1
t[1 : k − 1]
t[1 : k − 1]
Kn(sx, t) = Kn(s, t) +
k:tk=x
λ2
K
n−1(s, t[1 : k − 1])
s
t
j1
i1
|s| + i1 + 1
|t| + j1 + 1
u
k
の意味K
n(s, t)
改めて の意味Kn(s, t)
37. Dynamic Programming による効率化
K
n(sx, t) = λK
n(s, t) +
u∈Σn−1
i:u=s[i]
k:tk=x
j:u=t[j],jn−1k
λ|s|+1−i1+1
λ|t|−j1+1
= λK
n(s, t) +
k:tk=x
λ|t|−k+2
u∈Σn−1
i:u=s[i]
j:u=t[j],jn−1k
λ|s|−i1+1
λk−1−j1+1
= λK
n(s, t) +
k:tk=x
λ|t|−k+2
K
n−1(s, t[1 : k − 1])
= λK
n(s, t) + K
n(sx, t)
K
n(s, t) の更新式( と同様)Kn(s, t)
K
n(sx, t) =
k:tk=x
λ|t|−k+2
K
n−1(s, t[1 : k − 1])
38. K
n(sx, ty) =
λK
n(sx, t) if x = y
λK
n(sx, t) + λ2
K
n−1(s, t) otherwise
Dynamic Programming による効率化
の更新式
単純に2つの項に分解
t
= ty とおくと
よって
K
n(s, t)
K
n(sx, t
) =
k:t
k=x
λ|t
|−k+2
K
n−1(s, t
[1 : k − 1])
= λ
k:t
k=x,k|t|
λ|t|−k+2
K
n−1(s, t[1 : k − 1]) + λ2
K
n−1(s, t)[[t
k = x, k = |t
|]]
= λK
n(sx, t) + λ2
K
n−1(s, t)[[t
k = x, k = |t
|]]
39. K
i (sx, ty) =
λK
i (sx, t) if x = y
λK
i (sx, t) + λ2
K
i−1(s, t) otherwise
Gap-weighted String Kernel の更新式
i = 1, · · · , n − 1
K
0(s, t) = 1 ∀
s, t
K
i (s, t) = 0 if min(|s|, |t|) i
K
i(s, t) = 0 if min(|s|, |t|) i
K
i(sx, t) = λK
i(s, t) + K
i (sx, t)
Ki(s, t) = 0 if min(|s|, |t|) i
Kn(sx, t) = Kn(s, t) +
k:tk=x
λ2
K
n−1(s, t[1 : k − 1])
40. Gap-weighted String Kernel の”擬似”コード(1)
l = 0.7 # lambda
def indices(t, x):
ret = [];
pos = -1;
while 1:
pos = t.find(x, pos + 1)
if pos != -1:
ret.append(pos)
else:
break
return ret
def K(i, s, t):
if min(len(s), len(t)) i:
return 0
return K(i, s[0:-1], t) + l ** 2 * sum([K1(i - 1, s[0:-1], t[0:j]) for j in
indices(t, s[-1])])
Ki(s, t) = 0 if min(|s|, |t|) i
Kn(sx, t) = Kn(s, t) +
k:tk=x
λ2
K
n−1(s, t[1 : k − 1])
41. K
i (sx, ty) =
λK
i (sx, t) if x = y
λK
i (sx, t) + λ2
K
i−1(s, t) otherwise
def K1(i, s, t):
if i == 0:
return 1
if min(len(s), len(t)) i:
return 0
return l * K1(i, s[0:-1], t) + K2(i, s, t)
Gap-weighted String Kernel の”擬似”コード(2)
K
0(s, t) = 1 ∀
s, t
K
i (s, t) = 0 if min(|s|, |t|) i
K
i(s, t) = 0 if min(|s|, |t|) i
K
i(sx, t) = λK
i(s, t) + K
i (sx, t)
def K2(i, s, t):
if min(len(s), len(t)) i:
return 0
if s[-1] == t[-1]:
return l * (K2(i, s, t[0:-1]) + l * K1(i -1, s[0:-1], t[0:-1]))
else:
return l * K2(i, s, t[0:-1])
50. • 赤穂昭太郎, カーネル多変量解析, 岩波書店, 2008
前半部分は全てこの内容
• H. Lodhi, C. Saunders, J. Shawe-Taylor, N. Cristianini, and C.Watkins.Text
Classification using String Kernels. Journal of Machine Learning Research,Vol. 2, pp.
419–444, 2002.
Gap-weighted String Kernel の元論文
• カーネル法の応用 http://www.ism.ac.jp/~fukumizu/ISM_lecture_2006/
Lecture2006_application.pdf
• 文書分類と Kernel あれこれ(仮称) http://www.ism.ac.jp/~fukumizu/
ISM_lecture_2006/Lecture2006_application.pdf
• Juho Rousu , John Shawe-Taylor. Efficient Computation of Gapped Substring
Kernels on Large Alphabets,The Journal of Machine Learning Research,Vol. 6, pp.
1323-1344, 2005.
余裕があればこっちの方を理解して紹介したかった・・・
参考文献