TL;DR
- source : https://www.ismll.uni-hildesheim.de/pub/pdfs/Rendle2010FM.pdf
- MF ์ผ๋ฐํ
๋ฐ์ดํฐ ํํ
- supervised learning์ ์ํด์ ์ ํํ๋ก ๋ง๋ค์ด์ผ ํจ
- โUser 5๊ฐ Item 4๋ฅผ ๋ณด๊ณ Rating 4๋ฅผ ์ค ๋ฐ์ดํฐโ์ ๊ฒฝ์ฐ ๋ค์๊ณผ ๊ฐ์ด ํํ ๊ฐ๋ฅ (์ ์ฒด User 10๋ช
, ์ ์ฒด Item 5๊ฐ ๊ฐ์ )
๊ธฐ์กด ๋ชจ๋ธ๋ค์ ๋ฌธ์
- ๊ธฐ๋ณธ์ ์ผ๋ก ์ถ์ฒ์์คํ ์์ ํ์ฉํ๋ ๋ฐ์ดํฐ๋ ๋ง์ ๋ฒ์ฃผํ ๋ณ์๋ฅผ ๋ค๋ฃจ๊ธฐ ๋๋ฌธ์ ์ 0 ๋ง์ sparseํ ์ํ
- ํฌ๊ฒ SVM ๋ชจ๋ธ๊ณผ Factorization ๋ชจ๋ธ๋ค์ด ์๋๋ฐ ๊ฐ๊ฐ ํ๊ณ๊ฐ ์์
SVM models
- Linear Kernel
- user ,item์ bias๋ง ๊ณ ๋ คํ ์์ฃผ ๊ธฐ๋ณธ์ ์ธ CF ๋ชจํ์ผ๋ก ์๊ฐํ ์ ์์
- ๋งค์ฐ ๊ฐ๋จํด์ sparseํด๋ ์ถ์ ์๋๊ธดํ๋๋ฐ ๋น์ฐํ ์ฑ๋ฅ์ ์์ข์
- Polynomial Kernel
- : symmetric matrix
- ๋ชจ๋ ์ํธ์์ฉ ํ๋ผ๋ฏธํฐ ๋ฅผ ๋
๋ฆฝ์ผ๋ก ์ทจ๊ธ
- ๊ฐ ์ ๋ฐ์ดํธ๋๋ ค๋ฉด User u๊ฐ Item i๋ฅผ ํ๊ฐํ ํ๋ จ๋ฐ์ดํฐ๊ฐ ์์ด์ผ ํจ
- ๋ฐ๋ผ์ Sparseํ ์ํฉ์์ non-linear ์ํธ์์ฉ์ ํ์ตํ๊ธฐ๊ฐ ์ด๋ ค์
Factorization models
- Sparseํ User - Item Interaction ํ๋ ฌ์ด input์ด๋ผ์ ์ผ๋ฐ์ ์ธ ์ค์ feature vector ์ฌ์ฉํ ์ ์์
- User, Item ์ด์ธ์ ์๋ก์ด feature๋ฅผ ๋ฃ๊ณ ์ถ์ผ๋ฉด ๋ฒ๊ฑฐ๋ก์
Factorization Machine
- high sparsity ์์๋ ๋ฏฟ์๋งํ ํ๋ผ๋ฏธํฐ ์ถ์ ์ด ๊ฐ๋ฅํ๋ฉฐ SVM์ฒ๋ผ ์ผ๋ฐ์ ์ธ predictor๋ก ์ฌ์ฉ๊ฐ๋ฅํ ๋ชจ๋ธ
์ฅ์
- Sparseํ ๋ฐ์ดํฐ์์ non-linear ๊ด๊ณ ํ๋ผ๋ฏธํฐ ์ถ์ ๊ฐ๋ฅ
- polynomial kernel SVM ์ฒ๋ผ ๋ชจ๋ nested variable interaction ๋ชจ๋ธ๋ง์ ํ ์ ์๋ ๊ฒ์ ๋์ผ
- ํ์ง๋ง scalar ํ๋ผ๋ฏธํฐ ๋ฅผ ๋๋ ๊ฒ์ด ์๋ MF์ฒ๋ผ ๋ฒกํฐ์ ๋ด์ ์ผ๋ก ํํํ โfactorized parameterizationโ ์ฌ์ฉ
- Linear time์ ๊ณ์ฐ๊ฐ๋ฅํ๋ฉฐ linear number of parameter ๋ณด์
- ์์์ ์ค์ feature vector ์ฌ์ฉ ๊ฐ๋ฅ
- feature vector ์ ์กฐ์ ํ๋ฉด ๋ค์ํ ๋ชจ๋ธ ํํ ๊ฐ๋ฅํ๋ฉฐ ์ค์ ๋ก ์ฌ๋ฌ CF ๋ชจ๋ธ์ ์ผ๋ฐํํ ๋ชจ๋ธ
์์ ๋ฐ์ดํฐ ํํ
-
7๊ฐ์ ๋ฐ์ดํฐ
- #1 : User โBโ๊ฐ Movie โSWโ๋ฅผ Time โ5โ์ ์์ฒญ โ ๋ณ์ 4
- #2 : User โBโ๊ฐ Movie โSTโ๋ฅผ Time โ8โ์ ์์ฒญ โ ๋ณ์ 5
- #3 : User โCโ๊ฐ Movie โTIโ๋ฅผ Time โ9โ์ ์์ฒญ โ ๋ณ์ 1
- #4 : User โCโ๊ฐ Movie โSWโ๋ฅผ Time โ12โ์ ์์ฒญ โ ๋ณ์ 5
- #5 : User โAโ๊ฐ Movie โTIโ๋ฅผ Time โ13โ์ ์์ฒญ โ ๋ณ์ 5
- #6 : User โAโ๊ฐ Movie โNHโ๋ฅผ Time โ14โ์ ์์ฒญ โ ๋ณ์ 3
- #7 : User โAโ๊ฐ Movie โSWโ๋ฅผ Time โ16โ์ ์์ฒญ โ ๋ณ์ 1
-
๊ธฐ๋ณธ์ ์ผ๋ก User, Item์ ์ฌ์ฉํ๊ณ , ๊ทธ ์ธ์ Auxiliary Features๋ ์์ ๋กญ๊ฒ ์ฌ์ฉ โ ์ด๊ฒ์ด FM์ ์ฅ์
- User: one hot encoded ์ ์
- Movies: ํด๋น ์ ์ ๊ฐ ํ๊ฐํ one hot encoded ์ํ
- Time: ๋ฐ์ดํฐ ๋ค์ด์จ ์์
- Other Movies rated: ๊ทธ ์ ์ ๊ฐ ๋ณธ ๋ชจ๋ ์ํ ํ์
- Last Movie rated: ์ง์ ์ ํ๊ฐํ ์ํ
2-way Factorization Model
- Notation
- : data sample์ feature ๊ฐ์. ์ด interaction ๋ฐ์ดํฐ ๊ฐ์๊ฐ ์๋๊ณ ์์ ๊ทธ๋ฆผ์์ column์ ๊ธธ์ด
- : ith feature
- ์ถ์ ํด์ผํ ํ๋ผ๋ฏธํฐ๋
- : global bias
- : i ๋ฒ์งธ ๋ณ์์ strength
- : ith, jth ๋ณ์๊ฐ์ interaction โ sparsityํ์์ ๊ณ ์ฐจ์ ์ํธ์์ฉ ํ๋ผ๋ฏธํฐ ์ถ์ ์ ํต์ฌ
- : dot product
- 2-way FM (d=2)์ ๋ชจ๋ single, pairwise interaction ์ก์๋
Training ์์ : ์ ์ฒด User 10๋ช , Item 5๊ฐ์ธ ์ํฉ์์ User ID 5๊ฐ Item ID 4์ Rating 4๋ฅผ ๋ถ์ฌ
- User์ Item ๋ง ํ์ฉํ๋ ๊ฐ๋จํ ์์
- Training ๊ณผ์
- Interaction ๋ฐ์ดํฐ๋ฅผ ์์ ๊ทธ๋ฆผ ํํ๋ก ์์
- ์์๋ก ์ด๊ธฐํ๋ ํ๋ผ๋ฏธํฐ๋ค๋ก ๊ณ์ฐ
- ์ค์ Rating๊ณผ์ ์ฐจ์ด๋ก ํ๋ผ๋ฏธํฐ ์
๋ฐ์ดํธ
- ์ค์ ๋ก ์์ ์์์ฒ๋ผ User, Item 2๊ฐ์ง feature๋ง ์ด์ฉํด์ FM์ ํ๋ ๊ฒ์ Matrix Factorization๊ณผ ๋์ผ
- polynomial ํญ์์ ๋ง 1์ด๊ณ ๋๋จธ์ง๋ ๋ชจ๋ 0
- polynomial ํญ์์ ๋ง 1์ด๊ณ ๋๋จธ์ง๋ ๋ชจ๋ 0
์ Sparseํ ๊ฒฝ์ฐ ๋ ์ ๋ฆฌํ ๊น?
- FM์์๋ factorization์ ํตํด ๋ฅผ ๊ณต์ ํจ์ผ๋ก์จ ํ๋ผ๋ฏธํฐ์ ๋ ๋ฆฝ์ฑ์ ์ ๊ฑฐ โ sparseํจ์๋ interaction์ ์ถ์ ํ ์ ์๋ ๊ฒ
์์ : User-Item๋ง ํ์ฉ : User 100๋ช , Item 20๊ฐ, ์๋ฒ ๋ฉ ์ฐจ์ = 4
- ํ์ตํด์ผํ๋ ํ๋ผ๋ฏธํฐ (2์ฐจ์๋ง)
- FM : โ
- SVM : โ
- ํ์ต ๊ณผ์ : (user 5, item 4) ๊ด์ธก ์์
- FM โ ๋ ๋ค ๊ฐฑ์ โ (user 5, ๋ค๋ฅธ item), (๋ค๋ฅธ user, item 4) ์์ธก์ ๋ฐ์
- SVM โ ๋ง ๊ฐฑ์
Linear Complexity
- Pairwise Interaction์ ์๊ฐ๋ณต์ก๋๋ ์ผ๋ก ๋ณด์ด์ง๋ง ์ค์ ๋ก๋ ์ ๊ณ์ฐ ๊ฐ๋ฅ
Training : ์์์ ์ผ๋ก ๋ณํํ ์์ ๋ฏธ๋ถํด์ SGD
์ฝ๋
Info
- ์ฐธ๊ณ : https://github.com/rixwew/pytorch-fm/blob/master/torchfm/model/fm.py#L7
- User, Item ์ด์ธ์ Auxiliary๋ฅผ ์ถ๊ฐํ ์ ์๋๋ฐ ์๋ง one-hot encoding๋๋ ๊ฒ๋ง ๊ฐ๋ฅํ ๋ฏ
import torch
from torchfm.layer import FactorizationMachine, FeaturesEmbedding, FeaturesLinear
class FactorizationMachineModel(torch.nn.Module):
"""
A pytorch implementation of Factorization Machine.
Reference:
S Rendle, Factorization Machines, 2010.
"""
def __init__(self, field_dims, embed_dim):
super().__init__()
self.embedding = FeaturesEmbedding(field_dims, embed_dim)
self.linear = FeaturesLinear(field_dims)
self.fm = FactorizationMachine(reduce_sum=True)
def forward(self, x):
"""
:param x: Long tensor of size ``(batch_size, num_fields)``
"""
x = self.linear(x) + self.fm(self.embedding(x))
return torch.sigmoid(x.squeeze(1))
class FeaturesEmbedding(torch.nn.Module):
def __init__(self, field_dims, embed_dim):
super().__init__()
self.embedding = torch.nn.Embedding(sum(field_dims), embed_dim)
self.offsets = np.array((0, *np.cumsum(field_dims)[:-1]), dtype=np.long)
torch.nn.init.xavier_uniform_(self.embedding.weight.data)
def forward(self, x):
"""
:param x: Long tensor of size ``(batch_size, num_fields)``
"""
x = x + x.new_tensor(self.offsets).unsqueeze(0)
return self.embedding(x)
class FeaturesLinear(torch.nn.Module):
def __init__(self, field_dims, output_dim=1):
super().__init__()
self.fc = torch.nn.Embedding(sum(field_dims), output_dim)
self.bias = torch.nn.Parameter(torch.zeros((output_dim,)))
self.offsets = np.array((0, *np.cumsum(field_dims)[:-1]), dtype=np.long)
def forward(self, x):
"""
:param x: Long tensor of size ``(batch_size, num_fields)``
"""
x = x + x.new_tensor(self.offsets).unsqueeze(0)
return torch.sum(self.fc(x), dim=1) + self.bias
class FactorizationMachine(torch.nn.Module):
def __init__(self, reduce_sum=True):
super().__init__()
self.reduce_sum = reduce_sum
def forward(self, x):
"""
:param x: Float tensor of size ``(batch_size, num_fields, embed_dim)``
"""
# O(kn^2) -> O(kn)
square_of_sum = torch.sum(x, dim=1) ** 2
sum_of_square = torch.sum(x ** 2, dim=1)
ix = square_of_sum - sum_of_square
if self.reduce_sum:
ix = torch.sum(ix, dim=1, keepdim=True)
return 0.5 * ix