# 一、分解机模型(Factorization Machine, FM)(2010年)

FM的模型方程为:
y = w 0 + ∑ i = 1 n w i x i x j + ∑ i = 1 n ∑ j = i + 1 n &lt; v i , v j &gt; x i x j y=w_0 + \sum_{i=1}^{n}{w_ix_ix_j}+\sum_{i=1}^{n}\sum_{j=i+1}^{n}{&lt;v_i,v_j&gt;x_ix_j}
X = ( x 1 , x 2 , … , x n ) ′ , X ∈ R n × 1 X=(x_1,x_2,…,x_n)^{&#x27;}, X\in R^{n\times1} 。构造交叉项的权值向量为 V i = ( v i 1 , v i 2 , … , v i k ) ′ , V i ∈ R k × 1 ​ V_i=(v_{i1},v_{i2},…,v_{ik})^{&#x27;},V_i\in R^{k\times 1}​

T = [ V 1 ′ V 1 x 1 x 1 . . . V 1 ′ V n x 1 x n . . . V i ′ V j x i x j . . . V n ′ V 1 x n x 1 . . . V n ′ V n x n x n ] T=\left[ \begin{array}{ccc} V_1^{&#x27;}V_1x_1x_1 &amp; ... &amp; V_1^{&#x27;}V_nx_1x_n\\ ... &amp; V_i^{&#x27;}V_jx_ix_j &amp; ...\\ V_n^{&#x27;}V_1x_nx_1 &amp; ... &amp; V_n^{&#x27;}V_nx_nx_n \end{array} \right ]

V = [ V 1 ′ V 2 ′ . . . V n ′ ] n × k = [ ( v 11 v 12 . . . v 1 k ) ( v 21 v 22 . . . v 2 k ) . . . ( v n 1 v n 2 . . . v n k ) ] n × k = [ v 11 v 12 . . . v 1 k v 21 v 22 . . . v 2 k . . . . . . v i j . . . v n 1 v n 2 . . . v n k ] n × k V= \left[ \begin{array}{c} V_{1}^{&#x27;} \\ V_{2}^{&#x27;} \\ ... \\ V_{n}^{&#x27;} \end{array} \right ]_{n\times k} = \left[ \begin{array}{c} (v_{11} &amp; v_{12} &amp; ... &amp; v_{1k}) \\ (v_{21} &amp; v_{22} &amp; ... &amp; v_{2k}) \\ ... \\ (v_{n1} &amp; v_{n2} &amp; ... &amp; v_{nk}) \end{array} \right ]_{n\times k} =\left[ \begin{array}{cccc} v_{11} &amp; v_{12} &amp; ... &amp; v_{1k}\\ v_{21} &amp; v_{22} &amp; ... &amp; v_{2k}\\ ... &amp; ... &amp; v_{ij} &amp; ...\\ v_{n1} &amp; v_{n2} &amp; ... &amp; v_{nk} \end{array} \right ]_{n \times k}

### 解：

∑ i = 1 n ∑ j = i + 1 n &lt; v i , v j &gt; x i x j = 1 2 ∑ i = 1 n ∑ j = 1 n &lt; v i , v j &gt; x i x j − 1 2 ∑ i = 1 n &lt; v i , v i &gt; x i x i = 1 2 ∑ i = 1 n ∑ j = 1 n ∑ k = 1 K v i k v j k x i x j − 1 2 ∑ i = 1 n ∑ k = 1 K v i k v i k x i x i = 1 2 ∑ k = 1 K [ ∑ i = 1 n ( v i k x i ) ∑ j = 1 n ( v j k x j ) ] − 1 2 ∑ k = 1 K ∑ i = 1 n x i 2 v i k 2 = 1 2 ∑ k = 1 K ( ∑ i = 1 n v i k x i ) 2 − 1 2 ∑ k = 1 K ( ∑ i = 1 n v i k 2 x i 2 ) = 1 2 ∑ k = 1 K [ ( ∑ i = 1 n v i k x i ) 2 − ∑ i = 1 n v i k 2 x i 2 ] \sum_{i=1}^{n}\sum_{j=i+1}^{n}&lt;v_i,v_j&gt;x_ix_j = \\ \frac{1}{2}\sum_{i=1}^{n}\sum_{j=1}^{n}&lt;v_i,v_j&gt;x_ix_j - \frac{1}{2}\sum_{i=1}^{n}&lt;v_i,v_i&gt;x_ix_i \\ = \frac{1}{2}\sum_{i=1}^{n}\sum_{j=1}^{n}\sum_{k=1}^{K}{v_{ik}v_{jk}x_ix_j} - \frac{1}{2}\sum_{i=1}^{n}\sum_{k=1}^{K}{v_{ik}v_{ik}x_ix_i} \\ = \frac{1}{2}\sum_{k=1}^{K}[\sum_{i=1}^{n}(v_{ik}x_i)\sum_{j=1}^{n}(v_{jk}x_j)] - \frac{1}{2}\sum_{k=1}^{K}\sum_{i=1}^{n}x_i^2v_{ik}^2 \\ = \frac{1}{2}\sum_{k=1}^{K}(\sum_{i=1}^{n}v_{ik}x_i)^2 - \frac{1}{2}\sum_{k=1}^{K}(\sum_{i=1}^{n}v_{ik}^2x_i^2) \\ = \frac{1}{2}\sum_{k=1}^{K}[(\sum_{i=1}^{n}v_{ik}x_i)^2-\sum_{i=1}^{n}v_{ik}^2x_i^2]

f m 1 = ( v ′ ⋅ i n p u t ) 2 v ∈ R n × k f m 1 ∈ R k × 1 f m 2 = ( v ′ ) 2 ⋅ i n p u t 2 f m 2 ∈ R k × 1 o u t = W ′ ⋅ i n p u t + 1 2 ⋅ 1 ( f m 1 − f m 2 ) W ∈ R n × 1 fm_1 = (v^{&#x27;} \cdot input)^2 \qquad v \in R^{n \times k} \quad fm_1 \in R^{k \times 1} \\ fm_2 = (v^{&#x27;})^2 \cdot input^2 \qquad fm_2 \in R^{k \times 1} \\ out = W^{&#x27;} \cdot input + \frac{1}{2} \cdot \mathbf{1}(fm_1-fm_2) \qquad W \in R^{n \times 1}

1 = ( 1 , 1 , 1 , . . . , 1 ) ∈ R 1 × k \mathbf{1} = (1,1,1,...,1) \in R^{1 \times k}

class FM_model(nn.Module):
def __init__(self, n, k):
super(FM_model, self).__init__()
self.n = n # len(items) + len(users)
self.k = k
self.linear = nn.Linear(self.n, 1, bias=True)
self.v = nn.Parameter(torch.randn(self.k, self.n))

def fm_layer(self, x):
# x 属于 R^{batch*n}
linear_part = self.linear(x)
# 矩阵相乘 (batch*p) * (p*k)
inter_part1 = torch.mm(x, self.v.t())  # out_size = (batch, k)
# 矩阵相乘 (batch*p)^2 * (p*k)^2
inter_part2 = torch.mm(torch.pow(x, 2), torch.pow(self.v, 2).t()) # out_size = (batch, k)
output = linear_part + 0.5 * torch.sum(torch.pow(inter_part1, 2) - inter_part2)
# 这里torch求和一定要用sum
return output  # out_size = (batch, 1)

def forward(self, x):
output = self.fm_layer(x)
return output


# 三、Deep FM(2017)

10-22 3470
05-18 7674
10-17 4517

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

·清尘·

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。