-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexperiment.py
More file actions
164 lines (103 loc) · 3.58 KB
/
experiment.py
File metadata and controls
164 lines (103 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env python
# coding: utf-8
# In[16]:
# import os
# import warnings
import random
# from os.path import join
import torch
import numpy as np
import pandas as pd
# from scipy import sparse
# import torchvision
import pickle
# In[17]:
torch.__version__
# In[18]:
# torchvision.__version__
# In[19]:
import torch
print(torch.__version__)
print(torch.cuda.is_available())
print(torch.cuda.current_device())
print(torch.cuda.device(0))
print(torch.cuda.device_count())
print(torch.cuda.get_device_name(0))
print(torch.version.cuda)
# In[25]:
# import warnings
# import random
# from os.path import join
# import torch
# import numpy as np
# warnings.filterwarnings('ignore')
# set seed
def seed_everything(random_seed):
torch.manual_seed(random_seed)
torch.cuda.manual_seed(random_seed)
torch.cuda.manual_seed_all(random_seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(random_seed)
random.seed(random_seed)
seed = 1
seed_everything(seed)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(device)
top_k = 20
# In[21]:
import pickle
with open('data/playlist.data', 'rb') as f:
playlists = pickle.load(f)
# In[22]:
num_users = len(playlists)
num_items = 70229
print(f"# of users: {num_users}, # of items: {num_items}")
music_data = dict()
for idx, i in enumerate(playlists):
music_data[idx] = i
user_train_music = dict()
user_valid_music = dict()
user_test_music = dict()
for u in music_data:
user_valid_music[u] = [music_data[u][-2]]
user_test_music[u] = [music_data[u][-1]]
user_train_music[u] = music_data[u][:-2]
# In[23]:
top_k = 500
# In[ ]:
# data_dir = 'data/embeddings/'
# embedding = np.load(data_dir + 'lyrics_glove_128.npy')
# embedding = np.append(np.array([np.random.normal(0, 1, 128)]), embedding, axis = 0)
# embedding = np.append(embedding, np.array([np.random.normal(0, 1, 128)]), axis = 0)
# embedding = torch.FloatTensor(embedding)
# print(embedding.size())
# In[ ]:
##########################################################################################################################
# data_dir = 'data/embeddings/'
# with open(data_dir + 'audio_embeddings.p', 'rb') as f:
# embedding = pickle.load(f)
# embedding = np.append(np.array([np.random.normal(0, 1, 150)]), embedding, axis = 0)
# embedding = np.append(embedding, np.array([np.random.normal(0, 1, 150)]), axis = 0)
# embedding = torch.FloatTensor(embedding)
# print(embedding.size())
# ############################################################################################################################
# In[31]:
# get_ipython().run_line_magic('load_ext', 'autoreload')
# get_ipython().run_line_magic('autoreload', '2')
"""
BERT4Rec
"""
from models.Bert4rec_custom import BERTRec_sequential
# from models.BERTRec_sequential import BERTRec_sequential
seed_everything(seed)
#####################################################################################################################
'''
Change hidden to the corresponding embedding dimension
hidden % head should be 0
'''
BERTRec = BERTRec_sequential(user_train_music, user_valid_music, user_num=num_users, item_num=num_items, hidden=300, maxlen=50, n_layers=2, heads=8, mask_prob=0.1,
num_epochs=100, eval_every=1, early_stop_trial=5, learning_rate=0.001, reg_lambda=0.0, batch_size=256, device=device, top_k = top_k)
#####################################################################################################################
# In[32]:
BERTRec.fit()