def objective(trial): start = time.time() import gc gc.collect() score_sum = 0 data_dirs = [ '../' + data_path + '/valid1/bpr', '../' + data_path + '/valid2/bpr' ] for data_dir in data_dirs: dataset = dataloader.AmazonDataset(data_dir) embedding_dim = trial.suggest_discrete_uniform('embedding_dim', 16, 128, 16) user_size = len(dataset.user_list) item_size = len(dataset.item_list) layer_size = trial.suggest_int('layer_size', 1, 3) nfm = model.NFM(int(embedding_dim), user_size, item_size, layer_size).to(device) batch_size = int( trial.suggest_discrete_uniform('batch_size', 128, 512, 128)) lr = trial.suggest_loguniform('lr', 1e-4, 1e-2) weight_decay = trial.suggest_loguniform('weight_decay', 1e-6, 1e-2) warmup = 350 lr_decay_every = 2 lr_decay_rate = trial.suggest_uniform('lr_decay_rate', 0.5, 1) iterater = training.TrainIterater(batch_size=batch_size, data_dir=data_dir) score = iterater.iterate_epoch(nfm, lr, epoch=2000, weight_decay=weight_decay, warmup=warmup, lr_decay_rate=lr_decay_rate, lr_decay_every=lr_decay_every, eval_every=1e+5, early_stop=True) torch.cuda.empty_cache() score_sum += score mi, sec = time_since(time.time() - start) print('{}m{}sec'.format(mi, sec)) return -1 * score_sum / 2
dataset = dataloader.AmazonDataset(data_dir) #print(dataset.user_items_test_dict) embedding_dim = params['embedding_dim'] user_size = len(dataset.user_list) item_size = len(dataset.item_list) layer_size = int(params['layer_size']) batch_size = int(params['batch_size']) lr = params['lr'] weight_decay = params['weight_decay'] warmup = 350 lr_decay_every = 2 lr_decay_rate = params['lr_decay_rate'] nfm = model.NFM(int(embedding_dim), user_size, item_size, layer_size).to(device) iterater = training.TrainIterater(batch_size=batch_size, data_dir=data_dir) score = iterater.iterate_epoch(nfm, lr, epoch=300, weight_decay=weight_decay, warmup=warmup, lr_decay_rate=lr_decay_rate, lr_decay_every=lr_decay_every, eval_every=3) #evaluater = evaluate.Evaluater(data_dir) #score = evaluater.topn_map(nfm) #print(score)
batch_size=args.batch_size, shuffle=False, num_workers=0) ############################## CREATE MODEL ########################### if args.pre_train: assert os.path.exists(config.FM_model_path), 'lack of FM model' assert config.model == 'NFM', 'only support NFM for now' FM_model = torch.load(config.FM_model_path) else: FM_model = None if config.model == 'FM': model = model.FM(num_features, args.hidden_factor, args.batch_norm, eval(args.dropout)) else: model = model.NFM( num_features, args.hidden_factor, config.activation_function, eval(args.layers), args.batch_norm, eval(args.dropout), FM_model) model.cuda() if config.optimizer == 'Adagrad': optimizer = optim.Adagrad( model.parameters(), lr=args.lr, initial_accumulator_value=1e-8) elif config.optimizer == 'Adam': optimizer = optim.Adam(model.parameters(), lr=args.lr) elif config.optimizer == 'SGD': optimizer = optim.SGD(model.parameters(), lr=args.lr) elif config.optimizer == 'Momentum': optimizer = optim.SGD(model.parameters(), lr=args.lr, momentum=0.95) if config.loss_type == 'square_loss': criterion = nn.MSELoss(reduction='sum') else: # log_loss
return loss_total / len(self.user_item_train_df) def valid_loss(self, batch, loss_func, model): with torch.no_grad(): batch, y_train = batch user_tensor = torch.tensor(batch[:, 0], dtype=torch.long, device=device) item_tensor = torch.tensor(batch[:, 1], dtype=torch.long, device=device) y_train = torch.tensor(y_train, dtype=torch.float, device=device) pred = model(user_tensor, item_tensor) loss = loss_func(pred, y_train) return loss def valid_metric(self, model): return 0 if __name__ == '__main__': import model dataset = AmazonDataset('../data_beauty_2core_es/valid1/bpr/') user_size = len(dataset.user_list) item_size = len(dataset.item_list) nfm = model.NFM(32, user_size, item_size, 2) es = EarlyStop('../data_beauty_2core_es/early_stopping/bpr', 10) es.early_stop(nfm)