Skip to content

Commit 38f3526

Browse files
author
captain
committed
update code
1 parent cf88cfa commit 38f3526

File tree

11 files changed

+60
-204
lines changed

11 files changed

+60
-204
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
*.pyc
22
data/
3+
.data/
34
.idea/
45
result/
5-
venv/
66
.ipynb_checkpoints/

__pycache__/models.cpython-36.pyc

287 Bytes
Binary file not shown.

models.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
2、batch
1717
3、give_item_weight到底是做什么用的
1818
4、模型中seed的用法
19+
5、一些参数的设置
20+
6、添加cuda参数来控制是否使用GPU
1921
'''
2022

2123

2224
def ConvMF(res_dir, train_user, train_item, valid_user, test_user,
23-
R, CNN_X, vocab_size, init_W=None, give_item_weight=True,
25+
R, CNN_X, vocab_size, if_cuda, init_W=None, give_item_weight=True,
2426
max_iter=50, lambda_u=1, lambda_v=100, dimension=50,
2527
dropout_rate=0.2, emb_dim=200, max_len=300, num_kernel_per_ws=100):
2628
# explicit setting
@@ -40,7 +42,6 @@ def ConvMF(res_dir, train_user, train_item, valid_user, test_user,
4042
Test_R = test_user[1]
4143
Valid_R = valid_user[1]
4244

43-
# 这一部分到底是做什么用的
4445
if give_item_weight is True:
4546
item_weight = np.array([math.sqrt(len(i))
4647
for i in Train_R_J], dtype=float)
@@ -49,15 +50,18 @@ def ConvMF(res_dir, train_user, train_item, valid_user, test_user,
4950
item_weight = np.ones(num_item, dtype=float)
5051

5152
pre_val_eval = 1e10
53+
best_tr_eval, best_val_eval, best_te_eval = 1e10, 1e10, 1e10
5254

5355
# dimension: 用户和物品的隐特征维数
5456
# emb_dim: 词向量的维数
57+
# if_cuda: 是否用GPU训练CNN
5558
cnn_module = CNN(dimension, vocab_size, dropout_rate,
56-
emb_dim, max_len, num_kernel_per_ws, init_W)
59+
emb_dim, max_len, num_kernel_per_ws, if_cuda, init_W)
5760

58-
# return the output of CNN
61+
# 返回CNN的output
5962
# size of V is (num_item, dimension)
60-
cnn_module = cnn_module.cuda()
63+
if if_cuda:
64+
cnn_module = cnn_module.cuda()
6165
theta = cnn_module.get_projection_layer(CNN_X)
6266
U = np.random.uniform(size=(num_user, dimension))
6367
V = theta
@@ -104,7 +108,6 @@ def ConvMF(res_dir, train_user, train_item, valid_user, test_user,
104108
sub_loss[j] = sub_loss[j] - 0.5 * np.dot(V[j].dot(tmp_A), V[j])
105109

106110
loss = loss + np.sum(sub_loss)
107-
seed = np.random.randint(100000)
108111

109112
# 用V训练CNN模型,更新V
110113
cnn_module.train(CNN_X, V)
@@ -126,9 +129,10 @@ def ConvMF(res_dir, train_user, train_item, valid_user, test_user,
126129
# 计算Loss下降率
127130
converge = abs((loss - PREV_LOSS) / PREV_LOSS)
128131

129-
# 存储模型参数
132+
# 存储效果最好的模型参数
130133
if val_eval < pre_val_eval:
131-
# cnn_module.save_model(res_dir + '/CNN_weights.hdf5')
134+
torch.save(cnn_module, res_dir+'CNN_model.pt')
135+
best_tr_eval, best_val_eval, best_te_eval = tr_eval, val_eval, te_eval
132136
np.savetxt(res_dir + '/U.dat', U)
133137
np.savetxt(res_dir + '/V.dat', V)
134138
np.savetxt(res_dir + '/theta.dat', theta)
@@ -137,13 +141,17 @@ def ConvMF(res_dir, train_user, train_item, valid_user, test_user,
137141

138142
pre_val_eval = val_eval
139143

140-
print("Elpased: %.4fs Converge: %.6f Tr: %.5f Val: %.5f Te: %.5f" % (
141-
elapsed, converge, tr_eval, val_eval, te_eval))
142-
f1.write("Elpased: %.4fs Converge: %.6f Tr: %.5f Val: %.5f Te: %.5f\n" % (
144+
print("Elpased: %.4fs Converge: %.6f Train: %.5f Valid: %.5f Test: %.5f" % (
145+
elapsed, converge, tr_eval, val_eval, te_eval))
146+
f1.write("Elpased: %.4fs Converge: %.6f Train: %.5f Valid: %.5f Test: %.5f\n" % (
143147
elapsed, converge, tr_eval, val_eval, te_eval))
144148

145149
# 超过五次则退出迭代训练
146150
if count == endure_count:
151+
print("\n\nBest Model: Train: %.5f Valid: %.5f Test: %.5f" % (
152+
best_tr_eval, best_val_eval, best_te_eval))
153+
f1.write("\n\nBest Model: Train: %.5f Valid: %.5f Test: %.5f\n" % (
154+
best_tr_eval, best_val_eval, best_te_eval))
147155
break
148156

149157
PREV_LOSS = loss

run.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
parser.add_argument("-a", "--aux_path", type=str, help="Path to R, D_all sets")
3636

3737
# Option for running ConvMF
38+
parser.add_argument("-cu", "--if_cuda", type=bool,
39+
help="if use GPU to train in pytorch", default=False)
3840
parser.add_argument("-o", "--res_dir", type=str,
3941
help="Path to ConvMF's result")
4042
parser.add_argument("-e", "--emb_dim", type=int,
@@ -110,6 +112,7 @@
110112
print("\t aux path - %s" % aux_path)
111113
print("\t data path - %s" % data_path)
112114
print("\t result path - %s" % res_dir)
115+
print("\t if cuda - %s" % args.if_cuda)
113116
print("\t pretrained w2v data path - %s" % pretrain_w2v)
114117
print("\t dimension: %d\n\t lambda_u: %.4f\n\t lambda_v: %.4f\n\t max_iter: %d\n\t num_kernel_per_ws: %d" %
115118
(dimension, lambda_u, lambda_v, max_iter, num_kernel_per_ws))
@@ -147,4 +150,5 @@
147150
ConvMF(max_iter=max_iter, res_dir=res_dir,
148151
lambda_u=lambda_u, lambda_v=lambda_v, dimension=dimension, vocab_size=vocab_size, init_W=init_W,
149152
give_item_weight=give_item_weight, CNN_X=input_array, emb_dim=emb_dim, num_kernel_per_ws=num_kernel_per_ws,
150-
train_user=train_user, train_item=train_item, valid_user=valid_user, test_user=test_user, R=R)
153+
train_user=train_user, train_item=train_item, valid_user=valid_user, test_user=test_user, R=R,
154+
if_cuda=args.if_cuda)

run_test_ConvMF.sh renamed to run_ConvMF.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#!/usr/bin/env bash
22
python ./run.py \
3+
-cu True \
34
-d ./data/preprocessed/ml-1m/0.2/ \
45
-a ./data/preprocessed/ml-1m/ \
56
-o ./result/ml-1m/1_100_200 \
67
-e 50 \
7-
-u 10 \
8-
-v 100 \
8+
-u 100 \
9+
-v 10 \
10+
-w 100 \
911
-g True
1012

1113

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/usr/bin/env bash
22
python ./run.py \
3-
-d ./data/preprocessed/ml-1m/0.2/ \
4-
-a ./data/preprocessed/ml-1m/ \
53
-c True \
64
-r ./data/movielens/ml-1m_ratings.dat \
75
-i ./data/movielens/ml_plot.dat \
8-
-m 1
6+
-m 1 \
7+
-d ./data/preprocessed/ml-1m/0.2/ \
8+
-a ./data/preprocessed/ml-1m/
-4.02 KB
Binary file not shown.

text_analysis/cnn_model.py

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# coding:utf-8
22

3-
# Pytorch
3+
# 导入Pytorch相关模块
44
import torch
55
import torch.nn as nn
66
import torch.nn.functional as F
@@ -14,13 +14,14 @@ class CNN(nn.Module):
1414
# More than this epoch cause easily over-fitting on our data sets
1515
nb_epoch = 5
1616

17-
def __init__(self, output_dimesion, vocab_size, dropout_rate, emb_dim, max_len, n_filters, init_W=None):
17+
def __init__(self, output_dimesion, vocab_size, dropout_rate, emb_dim, max_len, n_filters, if_cuda, init_W=None):
1818
# n_filter为卷积核个数
1919
super(CNN, self).__init__()
2020

2121
self.max_len = max_len
2222
self.emb_dim = emb_dim
23-
vanila_dimension = 200 # 倒数第二层的节点数
23+
self.if_cuda = if_cuda
24+
vanila_dimension = 2*n_filters # 倒数第二层的节点数
2425
projection_dimension = output_dimesion # 输出层的节点数
2526
self.qual_conv_set = {}
2627

@@ -49,13 +50,10 @@ def __init__(self, output_dimesion, vocab_size, dropout_rate, emb_dim, max_len,
4950
)
5051

5152
'''Dropout Layer'''
52-
# layer = Dense(vanila_dimension, activation='tanh')(flatten_layer)
53-
# layer = Dropout(dropout_rate)(layer)
54-
self.layer = nn.Linear(300, vanila_dimension)
53+
self.layer = nn.Linear(n_filters*3, vanila_dimension)
5554
self.dropout = nn.Dropout(dropout_rate)
5655

5756
'''Projection Layer & Output Layer'''
58-
# output_layer = Dense(projection_dimension, activation='tanh')(layer)
5957
self.output_layer = nn.Linear(vanila_dimension, projection_dimension)
6058

6159
def forward(self, inputs):
@@ -82,19 +80,23 @@ def train(self, X_train, V):
8280
optimizer = torch.optim.Adam(self.parameters(), lr=0.001)
8381

8482
for epoch in range(1, self.nb_epoch + 1):
85-
86-
print('<---epoch' + str(epoch))
8783
n_batch = len(X_train) // self.batch_size
8884

8985
# 这里会漏掉一些训练集,先这样写
90-
for i in range(n_batch):
86+
for i in range(n_batch+1):
9187
begin_idx, end_idx = i * self.batch_size, (i + 1) * self.batch_size
92-
feature = X_train[begin_idx:end_idx][...]
93-
target = V[begin_idx:end_idx][...]
88+
89+
if i<n_batch:
90+
feature = X_train[begin_idx:end_idx][...]
91+
target = V[begin_idx:end_idx][...]
92+
else:
93+
feature = X_train[begin_idx:][...]
94+
target = V[begin_idx:][...]
9495

9596
feature = Variable(torch.from_numpy(feature.astype('int64')).long())
9697
target = Variable(torch.from_numpy(target))
97-
feature, target = feature.cuda(), target.cuda()
98+
if self.if_cuda:
99+
feature, target = feature.cuda(), target.cuda()
98100

99101
optimizer.zero_grad()
100102
logit = self(feature)
@@ -105,34 +107,7 @@ def train(self, X_train, V):
105107

106108
def get_projection_layer(self, X_train):
107109
inputs = Variable(torch.from_numpy(X_train.astype('int64')).long())
108-
inputs = inputs.cuda()
110+
if self.if_cuda:
111+
inputs = inputs.cuda()
109112
outputs = self(inputs)
110113
return outputs.cpu().data.numpy()
111-
112-
113-
# 获取CNN模型的输出
114-
115-
# def train(self, X_train, V, item_weight, seed):
116-
# # X_train is CNN_X
117-
# X_train = sequence.pad_sequences(X_train, maxlen=self.max_len)
118-
# np.random.seed(seed)
119-
# X_train = np.random.permutation(X_train)
120-
# np.random.seed(seed)
121-
# V = np.random.permutation(V)ojecti
122-
# np.random.seed(seed)
123-
# item_weight = np.random.permutation(item_weight)
124-
#
125-
# print("Train...CNN module")
126-
# history = self.model.fit(X_train, V, verbose=0, batch_size=self.batch_size,
127-
# epochs=self.nb_epoch, sample_weight=item_weight)
128-
#
129-
# # cnn_loss_his = history.history['loss']
130-
# # cmp_cnn_loss = sorted(cnn_loss_his)[::-1]
131-
# # if cnn_loss_his != cmp_cnn_loss:
132-
# # self.nb_epoch = 1
133-
# return history
134-
#
135-
# def get_projection_layer(self, X_train):
136-
# X_train = sequence.pad_sequences(X_train, maxlen=self.max_len)
137-
# Y = self.model.predict(X_train, batch_size=len(X_train))
138-
# return Y

text_analysis/models.py

Lines changed: 0 additions & 144 deletions
This file was deleted.

text_analysis/models.pyc

-5.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)