-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtrain_leml.py
executable file
·98 lines (81 loc) · 2.63 KB
/
train_leml.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
#!/bin/python
import sys
import os
path = os.path.split(os.path.realpath(__file__))[0]
from latent_factor import *
from arffio import *
from common import *
import copy
import logging, Logger
import pickle
import numpy as np
import scipy.sparse as sp
import sampler
import random
import time
from common import *
from train_common import *
np.random.seed(0)
random.seed(0)
def printUsages():
print "Usage: train_rep.py [options] train_file model_file"
print "options"
print " -h hidden_space_dimension: set the hidden space dimension (default 100)"
print " -l2 l2_regularization: set the l2 regularization(default 0.001)"
print " -i number_of_iter: set the number of iteration(default 10)"
def parseParameter(argv):
if len(argv) < 3: #at least 4 paramters: train.py train_file model_file
printUsages()
exit(1)
parameters = copy.deepcopy(leml_default_params)
parameters["train_file"] = argv[len(argv) - 2]
parameters["model_file"] = argv[len(argv) - 1]
i = 1
while i + 1 < len(argv) - 2:
if "-h" == argv[i]:
parameters["h"] = int(argv[i+1])
elif "-l2" == argv[i]:
parameters["l2"] = float(argv[i+1])
elif "-i" == argv[i]:
parameters["i"] = int(argv[i+1])
else:
print argv[i]
printUsages()
exit(1)
i += 2
if False == checkParamValid(parameters):
printUsages()
exit(1)
return parameters
def main(argv):
parameters = parseParameter(argv)
train_file = parameters["train_file"]
model_file = parameters["model_file"]
# read a instance to know the number of features and labels
train_reader = SvmReader(train_file, 1)
x, y, has_next = train_reader.read()
parameters["nx"] = x.shape[1]
parameters["ny"] = y.shape[1]
train_reader.close()
model = Model(parameters)
rater = AdaGrad(model)
model.rater = rater
thrsel = ThresholdSel()
model.thrsel = thrsel
if m.internal_memory == parameters["m"]:
model = train_internal(model, train_file, parameters)
elif m.external_memory == parameters["m"]:
model = train_external(model, train_file, parameters)
else:
logger = logging.getLogger(Logger.project_name)
logger.error("Invalid m param")
raise Exception("Invalid m param")
#write the model
#model.clear_for_save()
model.save(model_file)
#s = pickle.dumps(model)
#f = open(model_file, "w")
#f.write(s)
#f.close()
if __name__ == "__main__":
main(sys.argv)