-
Notifications
You must be signed in to change notification settings - Fork 54
/
main.py
executable file
·212 lines (170 loc) · 6.86 KB
/
main.py
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# -*- encoding: utf-8 -*-
import time
import random
import math
import fire
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
from dataset import ReviewData
from framework import Model
import models
import config
def now():
return str(time.strftime('%Y-%m-%d %H:%M:%S'))
def collate_fn(batch):
data, label = zip(*batch)
return data, label
def train(**kwargs):
if 'dataset' not in kwargs:
opt = getattr(config, 'Digital_Music_data_Config')()
else:
opt = getattr(config, kwargs['dataset'] + '_Config')()
opt.parse(kwargs)
random.seed(opt.seed)
np.random.seed(opt.seed)
torch.manual_seed(opt.seed)
if opt.use_gpu:
torch.cuda.manual_seed_all(opt.seed)
if len(opt.gpu_ids) == 0 and opt.use_gpu:
torch.cuda.set_device(opt.gpu_id)
model = Model(opt, getattr(models, opt.model))
if opt.use_gpu:
model.cuda()
if len(opt.gpu_ids) > 0:
model = nn.DataParallel(model, device_ids=opt.gpu_ids)
if model.net.num_fea != opt.num_fea:
raise ValueError(f"the num_fea of {opt.model} is error, please specific --num_fea={model.net.num_fea}")
# 3 data
train_data = ReviewData(opt.data_root, mode="Train")
train_data_loader = DataLoader(train_data, batch_size=opt.batch_size, shuffle=True, collate_fn=collate_fn)
val_data = ReviewData(opt.data_root, mode="Val")
val_data_loader = DataLoader(val_data, batch_size=opt.batch_size, shuffle=False, collate_fn=collate_fn)
print(f'train data: {len(train_data)}; test data: {len(val_data)}')
optimizer = optim.Adam(model.parameters(), lr=opt.lr, weight_decay=opt.weight_decay)
scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=5, gamma=0.8)
# training
print("start training....")
min_loss = 1e+10
best_res = 1e+10
mse_func = nn.MSELoss()
mae_func = nn.L1Loss()
smooth_mae_func = nn.SmoothL1Loss()
for epoch in range(opt.num_epochs):
total_loss = 0.0
total_maeloss = 0.0
model.train()
print(f"{now()} Epoch {epoch}...")
for idx, (train_datas, scores) in enumerate(train_data_loader):
if opt.use_gpu:
scores = torch.FloatTensor(scores).cuda()
else:
scores = torch.FloatTensor(scores)
train_datas = unpack_input(opt, train_datas)
optimizer.zero_grad()
output = model(train_datas)
mse_loss = mse_func(output, scores)
total_loss += mse_loss.item() * len(scores)
mae_loss = mae_func(output, scores)
total_maeloss += mae_loss.item()
smooth_mae_loss = smooth_mae_func(output, scores)
if opt.loss_method == 'mse':
loss = mse_loss
if opt.loss_method == 'rmse':
loss = torch.sqrt(mse_loss) / 2.0
if opt.loss_method == 'mae':
loss = mae_loss
if opt.loss_method == 'smooth_mae':
loss = smooth_mae_loss
loss.backward()
optimizer.step()
if opt.fine_step:
if idx % opt.print_step == 0 and idx > 0:
print("\t{}, {} step finised;".format(now(), idx))
val_loss, val_mse, val_mae = predict(model, val_data_loader, opt)
if val_loss < min_loss:
model.save(name=opt.dataset, opt=opt.print_opt)
min_loss = val_loss
print("\tmodel save")
if val_loss > min_loss:
best_res = min_loss
scheduler.step()
mse = total_loss * 1.0 / len(train_data)
print(f"\ttrain data: loss:{total_loss:.4f}, mse: {mse:.4f};")
val_loss, val_mse, val_mae = predict(model, val_data_loader, opt)
if val_loss < min_loss:
model.save(name=opt.dataset, opt=opt.print_opt)
min_loss = val_loss
print("model save")
if val_mse < best_res:
best_res = val_mse
print("*"*30)
print("----"*20)
print(f"{now()} {opt.dataset} {opt.print_opt} best_res: {best_res}")
print("----"*20)
def test(**kwargs):
if 'dataset' not in kwargs:
opt = getattr(config, 'Digital_Music_data_Config')()
else:
opt = getattr(config, kwargs['dataset'] + '_Config')()
opt.parse(kwargs)
assert(len(opt.pth_path) > 0)
random.seed(opt.seed)
np.random.seed(opt.seed)
torch.manual_seed(opt.seed)
if opt.use_gpu:
torch.cuda.manual_seed_all(opt.seed)
if len(opt.gpu_ids) == 0 and opt.use_gpu:
torch.cuda.set_device(opt.gpu_id)
model = Model(opt, getattr(models, opt.model))
if opt.use_gpu:
model.cuda()
if len(opt.gpu_ids) > 0:
model = nn.DataParallel(model, device_ids=opt.gpu_ids)
if model.net.num_fea != opt.num_fea:
raise ValueError(f"the num_fea of {opt.model} is error, please specific --num_fea={model.net.num_fea}")
model.load(opt.pth_path)
print(f"load model: {opt.pth_path}")
test_data = ReviewData(opt.data_root, mode="Test")
test_data_loader = DataLoader(test_data, batch_size=opt.batch_size, shuffle=False, collate_fn=collate_fn)
print(f"{now()}: test in the test datset")
predict_loss, test_mse, test_mae = predict(model, test_data_loader, opt)
def predict(model, data_loader, opt):
total_loss = 0.0
total_maeloss = 0.0
model.eval()
with torch.no_grad():
for idx, (test_data, scores) in enumerate(data_loader):
if opt.use_gpu:
scores = torch.FloatTensor(scores).cuda()
else:
scores = torch.FloatTensor(scores)
test_data = unpack_input(opt, test_data)
output = model(test_data)
mse_loss = torch.sum((output-scores)**2)
total_loss += mse_loss.item()
mae_loss = torch.sum(abs(output-scores))
total_maeloss += mae_loss.item()
data_len = len(data_loader.dataset)
mse = total_loss * 1.0 / data_len
mae = total_maeloss * 1.0 / data_len
print(f"\tevaluation reslut: mse: {mse:.4f}; rmse: {math.sqrt(mse):.4f}; mae: {mae:.4f};")
model.train()
return total_loss, mse, mae
def unpack_input(opt, x):
uids, iids = list(zip(*x))
uids = list(uids)
iids = list(iids)
user_reviews = opt.users_review_list[uids]
user_item2id = opt.user2itemid_list[uids] # 检索出该user对应的item id
user_doc = opt.user_doc[uids]
item_reviews = opt.items_review_list[iids]
item_user2id = opt.item2userid_list[iids] # 检索出该item对应的user id
item_doc = opt.item_doc[iids]
data = [user_reviews, item_reviews, uids, iids, user_item2id, item_user2id, user_doc, item_doc]
data = list(map(lambda x: torch.LongTensor(x).cuda(), data))
return data
if __name__ == "__main__":
fire.Fire()