推荐算法
推荐算法可以预测用户评分,并根据评分推荐数据
推荐算法与其他预测算法的区别在于:推荐算法中的数据大多都不完整,用户只对几个电影评分;而预测算法则要求数据完整,便于拟合和预测
协同过滤
评分矩阵Y,左侧索引是名称,栏目是用户名
协同过滤的基本原理就是利用已有的评分数据,对未有的评分数据进行预测,根据评分大小推荐给用户
本质上用的算法还是线性回归那套
计算公式
J
(
x
(
0
)
,
.
.
.
,
x
(
n
m
−
1
)
,
w
(
0
)
,
b
(
0
)
,
.
.
.
,
w
(
n
u
−
1
)
,
b
(
n
u
−
1
)
)
=
[
1
2
∑
(
i
,
j
)
:
r
(
i
,
j
)
=
1
(
w
(
j
)
⋅
x
(
i
)
+
b
(
j
)
−
y
(
i
,
j
)
)
2
]
+
[
λ
2
∑
j
=
0
n
u
−
1
∑
k
=
0
n
−
1
(
w
k
(
j
)
)
2
+
λ
2
∑
i
=
0
n
m
−
1
∑
k
=
0
n
−
1
(
x
k
(
i
)
)
2
]
⏟
r
e
g
u
l
a
r
i
z
a
t
i
o
n
J({\mathbf{x}^{(0)},...,\mathbf{x}^{(n_m-1)},\mathbf{w}^{(0)},b^{(0)},...,\mathbf{w}^{(n_u-1)},b^{(n_u-1)}})= \left[ \frac{1}{2}\sum_{(i,j):r(i,j)=1}(\mathbf{w}^{(j)} \cdot \mathbf{x}^{(i)} + b^{(j)} - y^{(i,j)})^2 \right]+ \underbrace{\left[\frac{\lambda}{2}\sum_{j=0}^{n_u-1}\sum_{k=0}^{n-1}(\mathbf{w}^{(j)}_k)^2+ \frac{\lambda}{2}\sum_{i=0}^{n_m-1}\sum_{k=0}^{n-1}(\mathbf{x}_k^{(i)})^2\right]}_{regularization}
J(x(0),...,x(nm−1),w(0),b(0),...,w(nu−1),b(nu−1))=
21(i,j):r(i,j)=1∑(w(j)⋅x(i)+b(j)−y(i,j))2
+regularization
[2λj=0∑nu−1k=0∑n−1(wk(j))2+2λi=0∑nm−1k=0∑n−1(xk(i))2]
The first summation in (1) is “for all
i
i
i,
j
j
j where
r
(
i
,
j
)
r(i,j)
r(i,j) equals
1
1
1” and could be written:
= [ 1 2 ∑ j = 0 n u − 1 ∑ i = 0 n m − 1 r ( i , j ) ∗ ( w ( j ) ⋅ x ( i ) + b ( j ) − y ( i , j ) ) 2 ] + regularization = \left[ \frac{1}{2}\sum_{j=0}^{n_u-1} \sum_{i=0}^{n_m-1}r(i,j)*(\mathbf{w}^{(j)} \cdot \mathbf{x}^{(i)} + b^{(j)} - y^{(i,j)})^2 \right] +\text{regularization} =[21j=0∑nu−1i=0∑nm−1r(i,j)∗(w(j)⋅x(i)+b(j)−y(i,j))2]+regularization
代码
本质上就是一个矩阵的运算,利用np.sum()
化简代码
自定义计算函数
cofi_cost_func_v
def cofi_cost_func_v(X, W, b, Y, R, lambda_):
"""
Returns the cost for the content-based filtering
Vectorized for speed. Uses tensorflow operations to be compatible with custom training loop.
Args:
X (ndarray (num_movies,num_features)): matrix of item features
W (ndarray (num_users,num_features)) : matrix of user parameters
b (ndarray (1, num_users) : vector of user parameters
Y (ndarray (num_movies,num_users) : matrix of user ratings of movies
R (ndarray (num_movies,num_users) : matrix, where R(i, j) = 1 if the i-th movies was rated by the j-th user
lambda_ (float): regularization parameter
Returns:
J (float) : Cost
"""
j = (tf.linalg.matmul(X, tf.transpose(W)) + b - Y)*R
J = 0.5 * tf.reduce_sum(j**2) + (lambda_/2) * (tf.reduce_sum(X**2) + tf.reduce_sum(W**2))
return J
利用tensorflow求导
iterations = 200
lambda_ = 1
for iter in range(iterations):
with tf.GradientTape() as tape:
cost_value = cofi_cost_func_v(X, W, b, Y, R, lambda_)
grads = tape.gradient( cost_value, [X,W,b] )
optimizer.apply_gradients( zip(grads, [X,W,b]) )
if iter % 5 == 0:
print(f"Training loss at iteration {iter}: {cost_value:0.1f}")
预测变量
- 矩阵乘法:
y_pred=X@W.T+b
- 合并原始数据:
Y_res=R*Y+(1-R)*y_pred
预测二进制变量
原来的 f ( w , b , x ) → f = s i g m o i d ( z ) f(w,b,x)\to f=sigmoid(z) f(w,b,x)→f=sigmoid(z)函数
损失函数改为交叉熵即可!
技巧:平均值正常化
模型评估
- 不适合冷启动问题
- 需要额外的信息,很难解释这些额外的信息的含义
- 梯度下降速度极慢,参数太多