Skip to content

Commit

Permalink
代码合并
Browse files Browse the repository at this point in the history
  • Loading branch information
deipss committed Sep 23, 2022
2 parents 2648ca7 + f66f253 commit 2d90522
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 4 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
*.un~
*.pyc
*.swp
# *.json
*.json
res/
.DS_Store

Expand Down Expand Up @@ -189,6 +189,7 @@ pip-selfcheck.json

.vscode/

<<<<<<< HEAD

Data/ml-20m
Data/ml-10m
Expand All @@ -202,3 +203,7 @@ experiments/
!*.json
*.nohup
Data/preprocessed
=======
Data/
experiments/
>>>>>>> f66f2534ebfd937778c7174b5f9f216efdebe5de
4 changes: 2 additions & 2 deletions dataloaders/ae.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ def __init__(self, user2items, item_count):
for user, useritem in enumerate(user2items.values()):
for _ in range(len(useritem)):
user_row.append(user)

# Column indices for sparse matrix
item_col = []
for useritem in user2items.values():
Expand All @@ -102,6 +101,7 @@ def __init__(self, user2items, item_count):
sparse_data = sparse.csr_matrix((np.ones(len(user_row)), (user_row, item_col)),
dtype='float64', shape=(len(user2items), item_count))


# Convert to torch tensor
self.data = torch.FloatTensor(sparse_data.toarray())

Expand All @@ -127,7 +127,6 @@ def __init__(self, user2items, item_count):
for _ in range(len(label_items)):
label_user_row.append(user)
input_user_row, label_user_row = np.array(input_user_row), np.array(label_user_row)

# Column indices for sparse matrix
input_item_col = np.hstack(input_list)
label_item_col = np.hstack(label_list)
Expand All @@ -142,6 +141,7 @@ def __init__(self, user2items, item_count):
self.input_data = torch.FloatTensor(sparse_input.toarray())
self.label_data = torch.FloatTensor(sparse_label.toarray())


def split_input_label_proportion(self, data, label_prop=0.2):
input_list, label_list = [], []

Expand Down
3 changes: 3 additions & 0 deletions dataloaders/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ def __init__(self, args, dataset):
self.test = dataset['test']
self.umap = dataset['umap']
self.smap = dataset['smap']
<<<<<<< HEAD
self.top50 = dataset['top50']
=======
>>>>>>> f66f2534ebfd937778c7174b5f9f216efdebe5de
self.user_count = len(self.umap)
self.item_count = len(self.smap)

Expand Down
1 change: 0 additions & 1 deletion datasets/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import numpy as np
import pandas as pd
from tqdm import tqdm

tqdm.pandas()

from abc import *
Expand Down
3 changes: 3 additions & 0 deletions templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def set_template(args):

# args.dataset_code = 'ml-' + input('Input 1 for ml-1m, 20 for ml-20m: ') + 'm'
args.dataset_code = 'ml-1m'

args.min_rating = 0 if args.dataset_code == 'ml-1m' else 4
args.min_uc = 5
args.min_sc = 0
Expand All @@ -25,6 +26,7 @@ def set_template(args):
args.train_negative_sampling_seed = 0
args.test_negative_sampler_code = 'random'
args.test_negative_sample_size = 100 if args.dataset_code == 'ml-1m' else 500
args.test_negative_sample_size = 100
args.test_negative_sampling_seed = 98765

args.trainer_code = 'bert'
Expand All @@ -41,6 +43,7 @@ def set_template(args):
args.best_metric = 'NDCG@10'

args.model_code = 'bert_cnn'

args.model_init_seed = 0

args.bert_dropout = 0.1
Expand Down
6 changes: 6 additions & 0 deletions trainers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
from .bert import BERTTrainer
from .dae import DAETrainer
from .vae import VAETrainer
<<<<<<< HEAD
from .ncf import NCFTrainer
=======
>>>>>>> f66f2534ebfd937778c7174b5f9f216efdebe5de


TRAINERS = {
BERTTrainer.code(): BERTTrainer,
DAETrainer.code(): DAETrainer,
<<<<<<< HEAD
NCFTrainer.code(): NCFTrainer,
=======
>>>>>>> f66f2534ebfd937778c7174b5f9f216efdebe5de
VAETrainer.code(): VAETrainer
}

Expand Down
34 changes: 34 additions & 0 deletions trainers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
import json
from abc import *
from pathlib import Path
<<<<<<< HEAD
import math
import numpy as np
=======
>>>>>>> f66f2534ebfd937778c7174b5f9f216efdebe5de


class AbstractTrainer(metaclass=ABCMeta):
Expand Down Expand Up @@ -97,15 +100,23 @@ def train_one_epoch(self, epoch, accum_iter):

average_meter_set.update('loss', loss.item())
tqdm_dataloader.set_description(
<<<<<<< HEAD
'Epoch {}, loss {:.3f} '.format(epoch + 1, average_meter_set['loss'].avg))
=======
'Epoch {}, loss {:.3f} '.format(epoch+1, average_meter_set['loss'].avg))
>>>>>>> f66f2534ebfd937778c7174b5f9f216efdebe5de

accum_iter += batch_size

if self._needs_to_log(accum_iter):
tqdm_dataloader.set_description('Logging to Tensorboard')
log_data = {
'state_dict': (self._create_state_dict()),
<<<<<<< HEAD
'epoch': epoch + 1,
=======
'epoch': epoch+1,
>>>>>>> f66f2534ebfd937778c7174b5f9f216efdebe5de
'accum_iter': accum_iter,
}
log_data.update(average_meter_set.averages())
Expand All @@ -120,15 +131,23 @@ def validate(self, epoch, accum_iter):
average_meter_set = AverageMeterSet()

with torch.no_grad():
<<<<<<< HEAD
tqdm_dataloader = tqdm(self.test_loader)
=======
tqdm_dataloader = tqdm(self.val_loader)
>>>>>>> f66f2534ebfd937778c7174b5f9f216efdebe5de
for batch_idx, batch in enumerate(tqdm_dataloader):
batch = [x.to(self.device) for x in batch]

metrics = self.calculate_metrics(batch)

for k, v in metrics.items():
average_meter_set.update(k, v)
<<<<<<< HEAD
description_metrics = ['NDCG@%d' % k for k in self.metric_ks[:3]] + \
=======
description_metrics = ['NDCG@%d' % k for k in self.metric_ks[:3]] +\
>>>>>>> f66f2534ebfd937778c7174b5f9f216efdebe5de
['Recall@%d' % k for k in self.metric_ks[:3]]
description = 'Val: ' + ', '.join(s + ' {:.3f}' for s in description_metrics)
description = description.replace('NDCG', 'N').replace('Recall', 'R')
Expand All @@ -137,7 +156,11 @@ def validate(self, epoch, accum_iter):

log_data = {
'state_dict': (self._create_state_dict()),
<<<<<<< HEAD
'epoch': epoch + 1,
=======
'epoch': epoch+1,
>>>>>>> f66f2534ebfd937778c7174b5f9f216efdebe5de
'accum_iter': accum_iter,
}
log_data.update(average_meter_set.averages())
Expand All @@ -162,6 +185,7 @@ def test(self):

for k, v in metrics.items():
average_meter_set.update(k, v)
<<<<<<< HEAD
description_metrics = ['NDCG@%d' % k for k in self.metric_ks[:3]] + \
['Recall@%d' % k for k in self.metric_ks[:3]]
description = 'Val: ' + ', '.join(s + ' {:.3f}' for s in description_metrics)
Expand Down Expand Up @@ -189,6 +213,9 @@ def test_top50(self, top50):
for k, v in metrics.items():
average_meter_set.update(k, v)
description_metrics = ['NDCG@%d' % k for k in self.metric_ks[:3]] + \
=======
description_metrics = ['NDCG@%d' % k for k in self.metric_ks[:3]] +\
>>>>>>> f66f2534ebfd937778c7174b5f9f216efdebe5de
['Recall@%d' % k for k in self.metric_ks[:3]]
description = 'Val: ' + ', '.join(s + ' {:.3f}' for s in description_metrics)
description = description.replace('NDCG', 'N').replace('Recall', 'R')
Expand All @@ -205,8 +232,12 @@ def _create_optimizer(self):
if args.optimizer.lower() == 'adam':
return optim.Adam(self.model.parameters(), lr=args.lr, weight_decay=args.weight_decay)
elif args.optimizer.lower() == 'sgd':
<<<<<<< HEAD
return optim.SGD(self.model.parameters(), lr=args.lr, weight_decay=args.weight_decay,
momentum=args.momentum)
=======
return optim.SGD(self.model.parameters(), lr=args.lr, weight_decay=args.weight_decay, momentum=args.momentum)
>>>>>>> f66f2534ebfd937778c7174b5f9f216efdebe5de
else:
raise ValueError

Expand Down Expand Up @@ -238,6 +269,7 @@ def _create_state_dict(self):

def _needs_to_log(self, accum_iter):
return accum_iter % self.log_period_as_iter < self.args.train_batch_size and accum_iter != 0
<<<<<<< HEAD

def _evaluate(self, candidate, top50, ks):

Expand All @@ -257,3 +289,5 @@ def _evaluate(self, candidate, top50, ks):
metrics['Recall@%d' % k], metrics['NDCG@%d' % k] = np.array(hit).mean(), np.array(ndcg).mean()

return metrics
=======
>>>>>>> f66f2534ebfd937778c7174b5f9f216efdebe5de
4 changes: 4 additions & 0 deletions trainers/vae.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ def calculate_metrics(self, batch):

# Annealing beta
if self.finding_best_beta:
<<<<<<< HEAD
if self.current_best_metric <= metrics[self.best_metric]:
=======
if self.current_best_metric < metrics[self.best_metric]:
>>>>>>> f66f2534ebfd937778c7174b5f9f216efdebe5de
self.current_best_metric = metrics[self.best_metric]
self.best_beta = self.__beta

Expand Down
1 change: 1 addition & 0 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def create_experiment_export_folder(args):


def get_name_of_experiment_path(experiment_dir, experiment_description, args):

experiment_path = os.path.join(experiment_dir, (experiment_description + "_" + str(date.today())))
idx = _get_experiment_index(experiment_path)
experiment_path = experiment_path + "_" + str(idx)
Expand Down

0 comments on commit 2d90522

Please sign in to comment.