Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to do incremental training? #31

Open
tfzxyinhao opened this issue May 26, 2017 · 8 comments
Open

How to do incremental training? #31

tfzxyinhao opened this issue May 26, 2017 · 8 comments

Comments

@tfzxyinhao
Copy link

generator new data and new user every day,how to do incremental training

@leodesigner
Copy link

You have to maintain your own item/user matrix and manage new/expired users or items.
Then run matrix factorization periodically.

@tfzxyinhao
Copy link
Author

@leodesigner
thanks for your answer
matrix factorization once again waste time
your mean that can't reuse the last time result of matrix factorization

@leodesigner
Copy link

leodesigner commented May 27, 2017

You can actually reuse last results as an initialization - this is better than starting from random initialization. In this case only new item/users should be initialized randomly or as an average of similar items/users.
In my case my input matrix is about 10000x10000 and one iteration of the MF algorithm takes about 0.0125s on my server. (I am reusing last results).

@tfzxyinhao
Copy link
Author

@leodesigner
are you use implicit to do real-time recommend? or it's can do it

@leodesigner
Copy link

I am using implicit to do calculations once a 10-60 minutes. However the results are used in realtime web app (client browser side item sorting based on cousine distance).

@benfred
Copy link
Owner

benfred commented May 28, 2017

Like @leodesigner was saying - this isn't supported in this library right now, but you can build this on top of implicit with some effort.

Adding support for incremental training would be a good feature for this library.

@jbochi
Copy link
Contributor

jbochi commented Jun 23, 2017

If you update the user_items matrix, you can now recalculate a user factor and get updated recommendations by running model.recommend(userid, user_items, recalculate_user=True).

You can also get recommendations for new users by passing a column vector as user_items and userid=0.

It's still not incremental training because item factors are not recalculated, but maybe it's helpful.

@marcusklaas
Copy link

marcusklaas commented Mar 29, 2018

Setting recalculate_user=True works, but seems to be quite slow, since it does a complete matrix inversion. In my tests, over 99% of the time would be spent on this operation.

One could speed this up by the conjugate gradient method, detailed here: https://www.benfrederickson.com/fast-implicit-matrix-factorization/.

The relevant part of that page would give something like this.

def factor_user_cg(Cui, X, Y, regularization, cg_steps=3):
	users, factors = X.shape
	# we could cache this
	YtY = Y.T.dot(Y) + regularization * np.eye(factors)

	# random start
	x = np.random.rand(factors) * 0.01

	# calculate residual r = (YtCuPu - (YtCuY.dot(Xu), without computing YtCuY
	r = -YtY.dot(x)
	for i, confidence in nonzeros(Cui, u):
		r += (confidence - (confidence - 1) * Y[i].dot(x)) * Y[i]

	p = r.copy()
	rsold = r.dot(r)

	for _ in range(cg_steps):
		# calculate Ap = YtCuYp - without actually calculating YtCuY
		Ap = YtY.dot(p)
		for i, confidence in nonzeros(Cui, u):
		Ap += (confidence - 1) * Y[i].dot(p) * Y[i]

		# standard CG update
		alpha = rsold / p.dot(Ap)
		x += alpha * p
		r -= alpha * Ap
		rsnew = r.dot(r)
		p = r + (rsnew / rsold) * p
		rsold = rsnew

	return x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants