-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcdl.py
More file actions
73 lines (64 loc) · 2.84 KB
/
cdl.py
File metadata and controls
73 lines (64 loc) · 2.84 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
# pylint: skip-file
# This is the MXNet version of CDL used in the KDD 2016 hands-on tutorial for MXNet. Note that the code is a simplified version of CDL and is used for demonstration only (you can also find the corresponding IPython notebook). We do not use pretrain and sigmoid activation (as used in the matlab/C++ code for CDL) in this version of code, which may harm the performance.
# To run the code, please type in (after installing MXNet):
# python cdl.py
# in the command line.
# Hao WANG, 2016.8.24
import mxnet as mx
import numpy as np
import logging
import data
from math import sqrt
from autoencoder import AutoEncoderModel
import os
if __name__ == '__main__':
lambda_u = 1 # lambda_u in CDL
lambda_v = 10 # lambda_v in CDL
K = 50
p = 4
is_dummy = False
num_iter = 34000
batch_size = 256
np.random.seed(1234) # set seed
lv = 1e-2 # lambda_v/lambda_n in CDL
dir_save = 'cdl%d' % p
if not os.path.isdir(dir_save):
os.system('mkdir %s' % dir_save)
fp = open(dir_save+'/cdl.log','w')
print 'p%d: lambda_v/lambda_u/ratio/K: %f/%f/%f/%d' % (p,lambda_v,lambda_u,lv,K)
fp.write('p%d: lambda_v/lambda_u/ratio/K: %f/%f/%f/%d\n' % \
(p,lambda_v,lambda_u,lv,K))
fp.close()
if is_dummy:
X = data.get_dummy_mult()
R = data.read_dummy_user()
else:
X = data.get_mult()
R = data.read_user()
# set to INFO to see less information during training
logging.basicConfig(level=logging.DEBUG)
#ae_model = AutoEncoderModel(mx.gpu(0), [784,500,500,2000,10], pt_dropout=0.2,
# internal_act='relu', output_act='relu')
ae_model = AutoEncoderModel(mx.cpu(2), [X.shape[1],100,K],
pt_dropout=0.2, internal_act='relu', output_act='relu')
train_X = X
#ae_model.layerwise_pretrain(train_X, 256, 50000, 'sgd', l_rate=0.1, decay=0.0,
# lr_scheduler=mx.misc.FactorScheduler(20000,0.1))
#V = np.zeros((train_X.shape[0],10))
V = np.random.rand(train_X.shape[0],K)/10
lambda_v_rt = np.ones((train_X.shape[0],K))*sqrt(lv)
U, V, theta, BCD_loss = ae_model.finetune(train_X, R, V, lambda_v_rt, lambda_u,
lambda_v, dir_save, batch_size,
num_iter, 'sgd', l_rate=0.1, decay=0.0,
lr_scheduler=mx.misc.FactorScheduler(20000,0.1))
#ae_model.save('cdl_pt.arg')
np.savetxt(dir_save+'/final-U.dat',U,fmt='%.5f',comments='')
np.savetxt(dir_save+'/final-V.dat',V,fmt='%.5f',comments='')
np.savetxt(dir_save+'/final-theta.dat',theta,fmt='%.5f',comments='')
#ae_model.load('cdl_pt.arg')
Recon_loss = lambda_v/lv*ae_model.eval(train_X,V,lambda_v_rt)
print "Training error: %.3f" % (BCD_loss+Recon_loss)
fp = open(dir_save+'/cdl.log','a')
fp.write("Training error: %.3f\n" % (BCD_loss+Recon_loss))
fp.close()
#print "Validation error:", ae_model.eval(val_X)