def norm_fro_err(X, W, H, norm_X):
""" Compute the approximation error in Frobeinus norm
norm(X - W.dot(H.T)) is efficiently computed based on trace() expansion
when W and H are thin.
Parameters
----------
X : numpy.array or scipy.sparse matrix, shape (m,n)
W : numpy.array, shape (m,k)
H : numpy.array, shape (n,k)
norm_X : precomputed norm of X
Returns
-------
float
"""
sum_squared = norm_X * norm_X - 2 * np.trace(H.T.dot(X.T.dot(W))) \
+ np.trace((W.T.dot(W)).dot(H.T.dot(H)))
return math.sqrt(np.maximum(sum_squared, 0))
评论列表
文章目录