-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
176 lines (143 loc) · 5.85 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
# -*- encoding: utf-8 -*-
import time
import random
import math
import fire
import os
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.utils.data import DataLoader
from collections import defaultdict
from dataset import *
from framework import Model
import models
import config
import pickle
from ranking.evaluate import NDCG, HitRate
from ranking.metric import RankingMetric
from attack import do_attack, test_attack, find_most_important_review, do_attack_add_random_review
import gensim
from utils import *
from train_rl_ps_aspect import train_rl_ps_aspect
torch.set_printoptions(precision=4)
bce_func = nn.BCELoss()
margin_func = nn.MarginRankingLoss()
nll_loss = F.nll_loss
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)
if len(opt.gpu_ids) == 0 and opt.use_gpu:
torch.cuda.set_device(opt.gpu_id)
print(opt.model)
print(opt.dataset)
accumulation_steps = 1
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
best_mae = 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)
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 = loss / accumulation_steps
loss.backward()
if (idx + 1) % accumulation_steps == 0 or (idx + 1) == len(train_data_loader):
optimizer.step()
optimizer.zero_grad()
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
best_mae = min(best_mae, val_mae)
print("*"*30)
print("----"*20)
print(f"{now()} {opt.dataset} {opt.model} {opt.print_opt} best_res: {best_res} best mae: {best_mae}")
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)
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)
# test_data_loader = DataLoader(test_data, batch_size=1, shuffle=False, collate_fn=collate_fn)
print(len(test_data))
print(f"{now()}: test in the test datset")
predict_loss, test_mse, test_mae = predict(model, test_data_loader, opt)
if __name__ == "__main__":
fire.Fire()