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 predict in sequential models ? #172

Open
kshitijyad opened this issue Jan 14, 2020 · 3 comments
Open

How to predict in sequential models ? #172

kshitijyad opened this issue Jan 14, 2020 · 3 comments

Comments

@kshitijyad
Copy link

I am trying to predict the implicit sequential model.

data=[['useID','docid','timestamp']]

from sklearn import preprocessing
le_usr = preprocessing.LabelEncoder() # user encoder
le_itm = preprocessing.LabelEncoder() # item encoder

# shift item_ids with +1 (but not user_ids):
item_ids = (le_itm.fit_transform(data['docid'])+1).astype('int32')
user_ids = (le_usr.fit_transform(data['userID'])).astype('int32')


from spotlight.interactions import Interactions
implicit_interactions = Interactions(user_ids, item_ids, timestamps=data.timestamp)

from spotlight.cross_validation import user_based_train_test_split, random_train_test_split
train, test = user_based_train_test_split(implicit_interactions, 0.3)

Now I use the following code to train:

from spotlight.sequence.implicit import ImplicitSequenceModel

sequential_interaction = train.to_sequence()
implicit_sequence_model = ImplicitSequenceModel(use_cuda=False, n_iter=1, loss='bpr', representation='pooling')
implicit_sequence_model.fit(sequential_interaction, verbose=True)

But after this lets say i want to predict for "user_ids" (one hot encoded at the top) 1000, I dont know how to use the predict function.

As per my understanding, predict function should take the input of sequence for that user, but how to find out that sequence for that user?

sequential_interaction when used with .sequences() doesnt give me which row belongs to which user.

Can you please provide an example to use predict on this data ? Any example using implicit sequence model would be helpful.

Thanks

@errisnotnil
Copy link

predictions = model.predict(ids)

item_ids= (-predictions).argsort()[:10] # last 10 items
print(item_ids)
print(predictions[item_ids])

@ddofer
Copy link

ddofer commented Jan 24, 2021

In the above, how would we get the topk predictions per user? i.e for the last item in a sequence, I would want the top k predictions, not just the last x predictions?
Thanks!

@FrancescoI
Copy link

The predict function only needs a (sorted) array of item indices as input to recommend the "next best items" given the input sequence.

It doesn't need any reference of the user_id or session_id, because it encodes the input sequence in the embedding space and then it matches its representation with the items one, using the dot product operator.

I am trying to predict the implicit sequential model.

data=[['useID','docid','timestamp']]

from sklearn import preprocessing
le_usr = preprocessing.LabelEncoder() # user encoder
le_itm = preprocessing.LabelEncoder() # item encoder

# shift item_ids with +1 (but not user_ids):
item_ids = (le_itm.fit_transform(data['docid'])+1).astype('int32')
user_ids = (le_usr.fit_transform(data['userID'])).astype('int32')


from spotlight.interactions import Interactions
implicit_interactions = Interactions(user_ids, item_ids, timestamps=data.timestamp)

from spotlight.cross_validation import user_based_train_test_split, random_train_test_split
train, test = user_based_train_test_split(implicit_interactions, 0.3)

Now I use the following code to train:

from spotlight.sequence.implicit import ImplicitSequenceModel

sequential_interaction = train.to_sequence()
implicit_sequence_model = ImplicitSequenceModel(use_cuda=False, n_iter=1, loss='bpr', representation='pooling')
implicit_sequence_model.fit(sequential_interaction, verbose=True)

But after this lets say i want to predict for "user_ids" (one hot encoded at the top) 1000, I dont know how to use the predict function.

As per my understanding, predict function should take the input of sequence for that user, but how to find out that sequence for that user?

sequential_interaction when used with .sequences() doesnt give me which row belongs to which user.

Can you please provide an example to use predict on this data ? Any example using implicit sequence model would be helpful.

Thanks

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

No branches or pull requests

4 participants