Skip to content

Commit

Permalink
update files
Browse files Browse the repository at this point in the history
  • Loading branch information
hyllll committed Apr 12, 2021
1 parent 054f135 commit fc48c03
Show file tree
Hide file tree
Showing 13 changed files with 19 additions and 90 deletions.
16 changes: 4 additions & 12 deletions experiment/run_ddpg.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,10 @@
import torch.backends.cudnn as cudnn

import sys
# sys.path.append('/home/xinghua/Hongyang/Code-submit')
sys.path.append('/home/workshop/lhy/code-submit')

from collections import defaultdict
# from memory_profiler import profile
from tqdm import tqdm
from daisy.model.KNNCFRecommender import ItemKNNCF
# from daisy.model.TRDRecommender import TRD, get_next_state, get_reward, create_initial_state
from daisy.utils.loader import load_rate, split_test, get_ur, PairMFData, get_ur_l
from daisy.utils.metrics import precision_at_k, recall_at_k, map_at_k, hr_at_k, mrr_at_k, ndcg_at_k

Expand All @@ -35,13 +31,13 @@ def __init__(self, user_num, item_num, factor_num, pretrain_model, pretrain):

if pretrain:
if method == 'Item2Vec':
item_weights = np.random.normal(size=(item_num + 1, factor_num), scale=0.01)
item_weights = np.zeros((item_num + 1, factor_num))
for k, v in model.item2vec.items():
if isinstance(k, int):
item_weights[k] = v
self.embed_item.weight.data.copy_(torch.from_numpy(item_weights))

user_weights = np.random.normal(size=(user_num, factor_num), scale=0.01)
user_weights = np.zeros((user_num, factor_num))
for k, v in model.user_vec_dict.items():
if isinstance(k, int):
user_weights[k] = v
Expand All @@ -51,18 +47,14 @@ def __init__(self, user_num, item_num, factor_num, pretrain_model, pretrain):

if method == 'bprmf':
weight = model.embed_item.weight.cpu().detach()
pad = np.random.normal(size=(1, factor_num), scale=0.01)
pad = np.zeros((1, factor_num))
pad = torch.FloatTensor(pad)
weight = torch.cat([weight, pad])
self.embed_item.weight.data.copy_(weight)

weight = model.embed_user.weight.cpu().detach()
self.embed_user.weight.data.copy_(weight)
print("embedding load completed")
else:
print("not load item2vec")
nn.init.normal_(self.embed_user.weight, std=0.01)
nn.init.normal_(self.embed_item.weight, std=0.01)
print("embedding load completed")

class Actor(nn.Module):
def __init__(self, embedding, factor_num=32, gpuid='0'):
Expand Down
14 changes: 4 additions & 10 deletions experiment/run_dqn.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,10 @@
import torch.backends.cudnn as cudnn

import sys
sys.path.append('/home/xinghua/Hongyang/Code-submit')
# sys.path.append('/home/workshop/lhy/code-submit')

from collections import defaultdict
# from memory_profiler import profile
from tqdm import tqdm
from daisy.model.KNNCFRecommender import ItemKNNCF
# from daisy.model.TRDRecommender import TRD, get_next_state, get_reward, create_initial_state
from daisy.utils.loader import load_rate, split_test, get_ur, PairMFData, get_ur_l
from daisy.utils.metrics import precision_at_k, recall_at_k, map_at_k, hr_at_k, mrr_at_k, ndcg_at_k

Expand All @@ -38,31 +34,29 @@ def __init__(self, user_num, item_num, model, method, factor_num=32, pretrain=1,

if self.pretrain:
if method == 'Item2Vec':
item_weights = np.random.normal(size=(item_num + 1, factor_num), scale=0.01)
item_weights = np.zeros((item_num + 1, factor_num))
for k, v in model.item2vec.items():
if isinstance(k, int):
item_weights[k] = v
self.embed_item.weight.data.copy_(torch.from_numpy(item_weights))

user_weights = np.random.normal(size=(user_num, factor_num), scale=0.01)
user_weights = np.zeros((user_num, factor_num))
for k, v in model.user_vec_dict.items():
if isinstance(k, int):
user_weights[k] = v
self.embed_user.weight.data.copy_(torch.from_numpy(user_weights))
del item_weights, user_weights
if method == 'bprmf':
weight = model.embed_item.weight.cpu().detach()
pad = np.random.normal(size=(1, factor_num), scale=0.01)
pad = np.zeros((1, factor_num))
pad = torch.FloatTensor(pad)
weight = torch.cat([weight, pad])
self.embed_item.weight.data.copy_(weight)

weight = model.embed_user.weight.cpu().detach()
self.embed_user.weight.data.copy_(weight)
print("embedding load completed")
else:
nn.init.normal_(self.embed_user.weight, std=0.01)
nn.init.normal_(self.embed_item.weight, std=0.01)


self.fc1 = nn.Linear(self.factor_num * 7, 256)
self.fc1.weight.data.normal_(0,0.1)
Expand Down
2 changes: 0 additions & 2 deletions experiment/run_item2Vec.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import torch.utils.data as data

import sys
sys.path.append('/home/xinghua/Hongyang/Code-submit')
# sys.path.append('/home/workshop/lhy/code-submit')

from daisy.model.Item2VecRecommender import Item2Vec
from daisy.utils.loader import load_rate, split_test, get_ur, BuildCorpus, PermutedSubsampledCorpus
Expand Down
2 changes: 0 additions & 2 deletions experiment/run_item2Vec_train.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import torch.utils.data as data

import sys
sys.path.append('/home/xinghua/Hongyang/Code-submit')
# sys.path.append('/home/workshop/lhy/code-submit')

from daisy.model.Item2VecRecommender import Item2Vec
from daisy.utils.loader import load_rate, split_test, get_ur, BuildCorpus, PermutedSubsampledCorpus
Expand Down
7 changes: 1 addition & 6 deletions experiment/run_itemknncf.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
import torch.utils.data as data

import sys
# sys.path.append('/home/xinghua/Hongyang/Code-submit')
sys.path.append('/home/workshop/lhy/code-submit')


from daisy.model.KNNCFRecommender import ItemKNNCF
from daisy.utils.loader import load_rate, split_test, get_ur
Expand Down Expand Up @@ -51,9 +50,6 @@
help='The (min) number of neighbors to take into account')
args = parser.parse_args()

'''Test Process for Metrics Exporting'''
# df, user_num, item_num = load_rate(args.dataset, args.prepro, binary=False)
# train_set, test_set = split_test(df, args.test_method, args.test_size)

#temporary used for tuning test result
train_set1 = pd.read_csv(f'../experiment_data/train1_{args.dataset}_{args.prepro}.dat')
Expand Down Expand Up @@ -83,7 +79,6 @@

# get ground truth
test_ur = get_ur(test_set)
# validate_ur = get_ur(validate_set)
total_train_ur = get_ur(train_set)

# initial candidate item pool
Expand Down
5 changes: 0 additions & 5 deletions experiment/run_itemknncf_train.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import torch.utils.data as data

import sys
sys.path.append('/home/xinghua/Hongyang/Code-submit')
# sys.path.append('/home/workshop/lhy/code-submit')

from daisy.model.KNNCFRecommender import ItemKNNCF
from daisy.utils.loader import load_rate, split_test, get_ur
Expand Down Expand Up @@ -51,9 +49,6 @@
help='The (min) number of neighbors to take into account')
args = parser.parse_args()

'''Test Process for Metrics Exporting'''
# df, user_num, item_num = load_rate(args.dataset, args.prepro, binary=False)
# train_set, test_set = split_test(df, args.test_method, args.test_size)

#temporary used for tuning test result
train_set1 = pd.read_csv(f'../experiment_data/train1_{args.dataset}_{args.prepro}.dat')
Expand Down
13 changes: 0 additions & 13 deletions experiment/run_mostpop.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,12 @@
from tqdm import tqdm

import sys
# sys.path.append('/home/xinghua/Hongyang/Code-submit')
sys.path.append('/home/workshop/lhy/code-submit')

from daisy.model.MostPopRecommender import MostPop
from daisy.utils.loader import load_rate, split_test, get_ur
from daisy.utils.metrics import precision_at_k, recall_at_k, map_at_k, hr_at_k, mrr_at_k, ndcg_at_k

if __name__ == '__main__':
# load logger configuration
# logging.config.fileConfig("./log.conf")
# logger_name = 'MostPop'
# logger = logging.getLogger(logger_name)
# logger.debug('MostPop experiment running...')

parser = argparse.ArgumentParser(description='Most-Popular recommender test')
# common settings
Expand All @@ -45,10 +38,6 @@
help='Initial selected number of Most-popular')
args = parser.parse_args()

'''Test Process for Metrics Exporting'''
# df, user_num, item_num = load_rate(args.dataset, args.prepro)
# train_set, test_set = split_test(df, args.test_method, args.test_size)

#temporary used for tuning test result
train_set1 = pd.read_csv(f'../experiment_data/train1_{args.dataset}_{args.prepro}.dat')
train_set2 = pd.read_csv(f'../experiment_data/train2_{args.dataset}_{args.prepro}.dat')
Expand All @@ -57,10 +46,8 @@

train_set1['rating'] = 1.0
train_set2['rating'] = 1.0
# validate_set['rating'] = 1.0
test_set['rating'] = 1.0
train_set = pd.concat([train_set1, train_set2], ignore_index=True)
# df = pd.concat([train_set, validate_set, test_set], ignore_index=True)

split_idx = len(train_set)
df = pd.concat([train_set, test_set], ignore_index=True)
Expand Down
13 changes: 2 additions & 11 deletions experiment/run_mostpop_train.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,14 @@
from tqdm import tqdm

import sys
sys.path.append('/home/xinghua/Hongyang/Code-submit')
# sys.path.append('/home/workshop/lhy/code-submit')


from daisy.model.MostPopRecommender import MostPop
from daisy.utils.loader import load_rate, split_test, get_ur
from daisy.utils.metrics import precision_at_k, recall_at_k, map_at_k, hr_at_k, mrr_at_k, ndcg_at_k

if __name__ == '__main__':
# load logger configuration
# logging.config.fileConfig("./log.conf")
# logger_name = 'MostPop'
# logger = logging.getLogger(logger_name)
# logger.debug('MostPop experiment running...')


parser = argparse.ArgumentParser(description='Most-Popular recommender test')
# common settings
Expand All @@ -46,11 +41,7 @@
help='Initial selected number of Most-popular')
args = parser.parse_args()

'''Test Process for Metrics Exporting'''
# df, user_num, item_num = load_rate(args.dataset, args.prepro)
# train_set, test_set = split_test(df, args.test_method, args.test_size)

#temporary used for tuning test result
train_set1 = pd.read_csv(f'../experiment_data/train1_{args.dataset}_{args.prepro}.dat')
train_set2 = pd.read_csv(f'../experiment_data/train2_{args.dataset}_{args.prepro}.dat')

Expand Down
12 changes: 3 additions & 9 deletions experiment/run_pair_mf.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
import torch.utils.data as data

import sys
# sys.path.append('/home/xinghua/Hongyang/Code-submit')
sys.path.append('/home/workshop/lhy/code-submit')

from daisy.model.pairwise.MFRecommender import PairMF
from daisy.utils.loader import load_rate, split_test, get_ur, PairMFData
from daisy.utils.metrics import precision_at_k, recall_at_k, map_at_k, hr_at_k, mrr_at_k, ndcg_at_k
Expand Down Expand Up @@ -81,18 +80,13 @@
help='save model or not')
args = parser.parse_args()

'''Test Process for Metrics Exporting'''
# df, user_num, item_num = load_rate(args.dataset, args.prepro)
# train_set, test_set = split_test(df, args.test_method, args.test_size)

#temporary used for tuning test result

train_set1 = pd.read_csv(f'../experiment_data/train1_{args.dataset}_{args.prepro}.dat')
train_set2 = pd.read_csv(f'../experiment_data/train2_{args.dataset}_{args.prepro}.dat')
# validate_set = pd.read_csv(f'../experiment_data/validate_{args.dataset}_{args.prepro}_{args.test_method}.dat')
test_set = pd.read_csv(f'../experiment_data/test_{args.dataset}_{args.prepro}.dat')
# if args.dataset in ['yelp']:
# train_set['timestamp'] = pd.to_datetime(train_set['timestamp'])
# test_set['timestamp'] = pd.to_datetime(test_set['timestamp'])

train_set1['rating'] = 1.0
train_set2['rating'] = 1.0
# validate_set['rating'] = 1.0
Expand Down
2 changes: 0 additions & 2 deletions experiment/run_pair_mf_train.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import torch.utils.data as data

import sys
# sys.path.append('/home/xinghua/Hongyang/Code-submit')
sys.path.append('/home/workshop/lhy/code-submit')

from daisy.model.pairwise.MFRecommender import PairMF
from daisy.utils.loader import load_rate, split_test, get_ur, PairMFData
Expand Down
6 changes: 0 additions & 6 deletions experiment/run_pair_neumf.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import torch.utils.data as data

import sys
# sys.path.append('/home/xinghua/Hongyang/Code-submit')
sys.path.append('/home/workshop/lhy/code-submit')

from daisy.model.pairwise.NeuMFRecommender import PairNeuMF
from daisy.utils.loader import load_rate, split_test, get_ur, PairMFData
Expand Down Expand Up @@ -91,11 +89,7 @@
help='gpu card ID')
args = parser.parse_args()

'''Test Process for Metrics Exporting'''
# df, user_num, item_num = load_rate(args.dataset, args.prepro)
# train_set, test_set = split_test(df, args.test_method, args.test_size)

# temporary used for tuning test result
train_set1 = pd.read_csv(f'../experiment_data/train1_{args.dataset}_{args.prepro}.dat')
train_set2 = pd.read_csv(f'../experiment_data/train2_{args.dataset}_{args.prepro}.dat')

Expand Down
3 changes: 0 additions & 3 deletions experiment/run_pair_neumf_train.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import torch.utils.data as data

import sys
sys.path.append('/home/xinghua/Hongyang/Code-submit')
# sys.path.append('/home/workshop/lhy/code-submit')

from daisy.model.pairwise.NeuMFRecommender import PairNeuMF
from daisy.utils.loader import load_rate, split_test, get_ur, PairMFData
Expand Down Expand Up @@ -164,7 +162,6 @@
test_ucands = defaultdict(list)
for k, v in train2_ur.items():
sample_num = candidates_num - len(v) if len(v) < candidates_num else 0
# sub_item_pool = item_pool - v - total_train_ur[k] - validate_ur[k]# remove GT & interacted
sub_item_pool = item_pool - v - train1_ur[k] - test_ur[k]
sample_num = min(len(sub_item_pool), sample_num)
if sample_num == 0:
Expand Down
14 changes: 5 additions & 9 deletions experiment/run_trd.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import torch.backends.cudnn as cudnn

import sys
# sys.path.append('/home/xinghua/Hongyang/Code-submit')
sys.path.append('/home/workshop/lhy/code-submit')

from collections import defaultdict
from tqdm import tqdm
Expand All @@ -34,13 +32,13 @@ def __init__(self, user_num, item_num, model, method, factor_num=32, gpuid='0'):
self.embed_item = nn.Embedding(item_num + 1, factor_num, padding_idx=item_num)

if method == 'Item2Vec' or method == 'mostpop' or method == 'itemknn' :
item_weights = np.random.normal(size=(item_num + 1, factor_num), scale=0.01)
item_weights = np.zeros((item_num + 1, factor_num))
for k, v in model.item2vec.items():
if isinstance(k, int):
item_weights[k] = v
self.embed_item.weight.data.copy_(torch.from_numpy(item_weights))

user_weights = np.random.normal(size=(user_num, factor_num), scale=0.01)
user_weights = np.zeros((user_num, factor_num))
for k, v in model.user_vec_dict.items():
if isinstance(k, int):
user_weights[k] = v
Expand All @@ -49,7 +47,7 @@ def __init__(self, user_num, item_num, model, method, factor_num=32, gpuid='0'):
del item_weights, user_weights
elif method == 'bprmf':
weight = model.embed_item.weight.cpu().detach()
pad = np.random.normal(size=(1, factor_num), scale=0.01)
pad = np.zeros((1, factor_num))
pad = torch.FloatTensor(pad)
weight = torch.cat([weight, pad])
self.embed_item.weight.data.copy_(weight)
Expand All @@ -58,16 +56,14 @@ def __init__(self, user_num, item_num, model, method, factor_num=32, gpuid='0'):
self.embed_user.weight.data.copy_(weight)
elif method == 'neumf':
weight = model.embed_item_GMF.weight.cpu().detach()
pad = np.random.normal(size=(1, factor_num), scale=0.01)
pad = np.zeros((1, factor_num))
pad = torch.FloatTensor(pad)
weight = torch.cat([weight, pad])
self.embed_item.weight.data.copy_(weight)

weight = model.embed_user_GMF.weight.cpu().detach()
self.embed_user.weight.data.copy_(weight)
else:
nn.init.normal_(self.embed_user.weight, std=0.01)
nn.init.normal_(self.embed_item.weight, std=0.01)


if self.factor_num == 32:
self.fc1 = nn.Linear(self.factor_num * 7, 256)
Expand Down

0 comments on commit fc48c03

Please sign in to comment.