Sequential Recommendation for cold-start users with meta transitional learning
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hyper_main.py 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import os
  2. os.environ['CUDA_VISIBLE_DEVICES'] = "1"
  3. from ray.tune.schedulers import ASHAScheduler
  4. from ray.tune import CLIReporter
  5. from ray import tune
  6. from functools import partial
  7. from hyper_tunning import train_metatl
  8. import argparse
  9. import numpy as np
  10. import torch
  11. import random
  12. from trainer import *
  13. from utils import *
  14. from sampler import *
  15. import copy
  16. def get_params():
  17. args = argparse.ArgumentParser()
  18. args.add_argument("-data", "--dataset", default="electronics", type=str)
  19. args.add_argument("-seed", "--seed", default=None, type=int)
  20. args.add_argument("-K", "--K", default=3, type=int) #NUMBER OF SHOT
  21. # args.add_argument("-dim", "--embed_dim", default=100, type=int)
  22. # args.add_argument("-bs", "--batch_size", default=1024, type=int)
  23. # args.add_argument("-lr", "--learning_rate", default=0.001, type=float)
  24. args.add_argument("-epo", "--epoch", default=100000, type=int)
  25. # args.add_argument("-prt_epo", "--print_epoch", default=100, type=int)
  26. # args.add_argument("-eval_epo", "--eval_epoch", default=1000, type=int)
  27. # args.add_argument("-b", "--beta", default=5, type=float)
  28. # args.add_argument("-m", "--margin", default=1, type=float)
  29. # args.add_argument("-p", "--dropout_p", default=0.5, type=float)
  30. # args.add_argument("-gpu", "--device", default=1, type=int)
  31. args = args.parse_args()
  32. params = {}
  33. for k, v in vars(args).items():
  34. params[k] = v
  35. # params['device'] = torch.device('cuda:1')
  36. params['device'] = 0
  37. return params, args
  38. def main(num_samples, gpus_per_trial=2):
  39. print("===============",torch.cuda.device_count(),"=======")
  40. params, args = get_params()
  41. if params['seed'] is not None:
  42. SEED = params['seed']
  43. torch.manual_seed(SEED)
  44. torch.cuda.manual_seed(SEED)
  45. torch.backends.cudnn.deterministic = True
  46. np.random.seed(SEED)
  47. random.seed(SEED)
  48. user_train, usernum_train, itemnum, user_input_test, user_test, user_input_valid, user_valid = data_load(args.dataset, args.K)
  49. # batch_size = params['batch_size']
  50. # sampler = WarpSampler(user_train, usernum_train, itemnum, batch_size=batch_size, maxlen=args.K, n_workers=1)
  51. # sampler_test = DataLoader(user_input_test, user_test, itemnum, params)
  52. # sampler_valid = DataLoader(user_input_valid, user_valid, itemnum, params)
  53. config = {
  54. # "l1": tune.sample_from(lambda _: 2 ** np.random.randint(2, 9)),
  55. # "l2": tune.sample_from(lambda _: 2 ** np.random.randint(2, 9)),
  56. # "lr": tune.loguniform(1e-4, 1e-1),
  57. "embed_dim" : tune.choice([50,75,100,125,150,200,300]),
  58. # "batch_size" : tune.choice([128,256,512,1024,2048]),
  59. "learning_rate" : tune.choice([0.1,0.01,0.004,0.005,0.007,0.001,0.0001]),
  60. "beta" : tune.choice([0.05,0.1,1,4,4.5,5,5.5,6,10]),
  61. "margin" : tune.choice([1,0.9,0.8,1.1,1.2]),
  62. "batch_size" : tune.choice([128,256,512,1024,2048]),
  63. "number_of_neg" : tune.choice([1,3,5,7,10,20,30,50,70]),
  64. "loss_function" : tune.choice(["bpr"]),
  65. "eval_epoch" : tune.choice([100,250,500,1000,1500]),
  66. 'device' : params['device'],
  67. "itemnum":itemnum,
  68. "params":params,
  69. }
  70. print("===============", torch.cuda.device_count(), "=======")
  71. scheduler = ASHAScheduler(
  72. metric="MRR",
  73. mode="max",
  74. max_t=params['epoch'],
  75. grace_period=200,
  76. reduction_factor=2)
  77. reporter = CLIReporter(
  78. # parameter_columns=["l1", "l2", "lr", "batch_size"],
  79. metric_columns=["MRR","NDCG10","NDCG5","NDCG1","Hits10","Hits5","Hits1","training_iteration"])
  80. result = tune.run(
  81. train_metatl,
  82. resources_per_trial={"cpu": 4, "gpu": gpus_per_trial},
  83. config=config,
  84. num_samples=num_samples,
  85. scheduler=scheduler,
  86. progress_reporter=reporter,
  87. log_to_file=True,
  88. resume=True,
  89. local_dir="/media/external_10TB/10TB/maheri/metaTL_ray/ray_local_dir",
  90. name="bpr_rnn",
  91. )
  92. best_trial = result.get_best_trial("MRR", "max", "last")
  93. print("Best trial config: {}".format(best_trial.config))
  94. print("Best trial final validation loss: {}".format(
  95. best_trial.last_result["loss"]))
  96. print("Best trial final validation MRR: {}".format(
  97. best_trial.last_result["MRR"]))
  98. print("Best trial final validation NDCG@1: {}".format(
  99. best_trial.last_result["NDCG@1"]))
  100. #
  101. print("=======================================================")
  102. print(result.results_df)
  103. print("=======================================================\n")
  104. if __name__ == "__main__":
  105. # You can change the number of GPUs per trial here:
  106. main(num_samples=150, gpus_per_trial=0.5)