Package: ezSASRec
ezSASRecDocumentationhttps://ezsasrec.netlify.appReferencesreposkang205 SASRecnnkkmto/SASRec-tf2microsoft recommenderspapersSelf-Attentive Sequential RecommendationA Case Study on Sampling Strategies for Evaluating Neural Sequential Item Recommendation ModelsQuickStartexample data source:https://www.kaggle.com/datasets/rounakbanik/the-movies-datasetimportpandasaspdimportpicklefromsasrec.utilimportfilter_k_core,SASRecDataSet,load_modelfromsasrec.modelimportSASRECfromsasrec.samplerimportWarpSamplerimportmultiprocessingPreprocessingpath='your path'df=pd.read_csv('ratings.csv')df=df.rename({'userId':'userID','movieId':'itemID','timestamp':'time'},axis=1)\.sort_values(by=['userID','time'])\.drop(['rating','time'],axis=1)\.reset_index(drop=True)df.head()userIDitemID0127621154503211125523196821415577# filter data# every user and item will appear more than 6 times in filtered_dffiltered_df=filter_k_core(df,7)Original: 270896 users and 45115 items
Final: 243377 users and 24068 items# make maps (encoder)user_set,item_set=set(filtered_df['userID'].unique()),set(filtered_df['itemID'].unique())user_map=dict()item_map=dict()foru,userinenumerate(user_set):user_map[user]=u+1fori,iteminenumerate(item_set):item_map[item]=i+1maps=(user_map,item_map)# Encode filtered_dffiltered_df["userID"]=filtered_df["userID"].apply(lambdax:user_map[x])filtered_df["itemID"]=filtered_df["itemID"].apply(lambdax:item_map[x])# save data and maps# save sasrec datafiltered_df.to_csv('sasrec_data.txt',sep="\t",header=False,index=False)# save mapswithopen('maps.pkl','wb')asf:pickle.dump(maps,f)Load data and Train model# load datadata=SASRecDataSet('sasrec_data.txt')data.split()# train, val, test split# the last interactions of each user is used for test# the last but one will be used for validation# others will be used for train# make model and warmsampler for batch trainingmax_len=100hidden_units=128batch_size=2048model=SASREC(item_num=data.itemnum,seq_max_len=max_len,num_blocks=2,embedding_dim=hidden_units,attention_dim=hidden_units,attention_num_heads=2,dropout_rate=0.2,conv_dims=[hidden_units,hidden_units],l2_reg=0.00001)sampler=WarpSampler(data.user_train,data.usernum,data.itemnum,batch_size=batch_size,maxlen=max_len,n_workers=multiprocessing.cpu_count())# train modelmodel.train(data,sampler,num_epochs=3,batch_size=batch_size,lr=0.001,val_epoch=1,val_target_user_n=1000,target_item_n=-1,auto_save=True,path=path,exp_name='exp_example',)epoch 1 / 3 -----------------------------

Evaluating...    

epoch: 1, test (NDCG@10: 0.04607630127474612, HR@10: 0.097)
best score model updated and saved


epoch 2 / 3 -----------------------------

Evaluating...    

epoch: 2, test (NDCG@10: 0.060855185638025944, HR@10: 0.118)
best score model updated and saved


epoch 3 / 3 -----------------------------

Evaluating...   

epoch: 3, test (NDCG@10: 0.07027207563856912, HR@10: 0.139)
best score model updated and savedPredict# load trained modelmodel=load_model(path,'exp_example')get score# get user-item score# make inv_user_mapinv_user_map={v:kfork,vinuser_map.items()}# sample target_usermodel.sample_val_users(data,100)encoded_users=model.val_users# get scoresscore=model.get_user_item_score(data,[inv_user_map[u]foruinencoded_users],# user_list containing raw(not-encoded) userID[1,2,3],# item_list containing raw(not-encoded) itemIDuser_map,item_map,batch_size=10)100%|██████████| 10/10 [00:00<00:00, 29.67batch/s]score.head()user_id123015255.5969444.2416533.804743117564.5356072.6944590.858440224085.8830614.6559604.691791324625.0846952.9420752.773376433415.5324384.3481504.073740get recommendation# get top N recommendationreco=model.recommend_item(data,user_map,[inv_user_map[u]foruinencoded_users],is_test=True,top_n=5)100%|██████████| 100/100 [00:04<00:00, 21.10it/s]# returned tuple contains topN recommendations for each userreco{1525: [(456, 6.0680223),
  (355, 6.033769),
  (379, 5.9833336),
  (591, 5.9718275),
  (776, 5.8978705)],
 1756: [(7088, 5.735977),
  (15544, 5.5946136),
  (5904, 5.500249),
  (355, 5.492655),
  (22149, 5.4117346)],
 2408: [(456, 5.976555),
  (328, 5.8824606),
  (588, 5.8614006),
  (264, 5.7114534),
  (299, 5.649914)],
 2462: [(259, 6.3445344),
  (591, 6.2664876),
  (295, 6.105361),
  (355, 6.0698805),
  (1201, 5.8477645)],
 3341: [(110, 5.510764),
  (1, 5.4927354),
  (259, 5.4851904),
  (161, 5.467624),
  (208, 5.2486935)], ...}