-
Notifications
You must be signed in to change notification settings - Fork 0
/
myfm.py
144 lines (96 loc) · 7.04 KB
/
myfm.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
#coding:utf-8
import sys
import os
from subprocess import *
from metric import evaluate
import numpy as np
class MyFM():
temp_dir = '.\\fm\\temp'
def __init__(self, train_file, test_file, test_file2 = None):
if not os.path.exists(MyFM.temp_dir):
os.mkdir(MyFM.temp_dir)
self.train_file = train_file
self.test_file = test_file
self.test_file2 = test_file2
self.train_tail = os.path.split(self.train_file)[1]
self.test_tail = os.path.split(self.test_file)[1]
self.fm_train_exe = r'.\\fm\\libfm.exe'
assert os.path.exists(self.fm_train_exe), 'train executable not found'
self.predict_file = os.path.join(MyFM.temp_dir, self.test_tail + '.predict')
self.result_file = os.path.join(MyFM.temp_dir, self.test_tail + '.result')
if test_file2:
self.test_file2 = test_file2
self.test_tail2 = os.path.split(self.test_file2)[1]
self.predict_file2 = os.path.join(MyFM.temp_dir, self.test_tail2 + '.predict')
self.result_file2 = os.path.join(MyFM.temp_dir, self.test_tail2 + '.result')
if os.path.exists(self.predict_file):
os.remove(self.predict_file)
if os.path.exists(self.result_file):
os.remove(self.result_file)
if self.test_file2 and os.path.exists(self.predict_file2):
os.remove(self.predict_file2)
if self.test_file2 and os.path.exists(self.result_file2):
os.remove(self.result_file2)
def data_process(self, infile, outfile):
infile = open(infile)
data = infile.readlines()
infile.close()
outfile = open(outfile, 'w')
outfile.write('labels 1 -1' + '\n')
for d in data:
if float(d.strip()) > 0.5:
outfile.write('1 ' + d.strip() + ' ' + str(1 - float(d.strip())) + '\n')
else:
outfile.write('-1 ' + d.strip() + ' ' + str(1 - float(d.strip())) + '\n')
outfile.close()
def train(self):
#=========================================================================MCMC=============================================================================
command_train = "{0} -task c -verbosity 1 -train {1} -test {2} -out {3} -dim '1,1,8' -iter 1000 -method mcmc -init_stdev 0.1".format(self.fm_train_exe, self.train_file, self.test_file, self.predict_file)
# print command_train
Popen(command_train, shell = True, stdout = PIPE).communicate()
if self.test_file2:
command_train = "{0} -task c -verbosity 1 -train {1} -test {2} -out {3} -dim '1,1,8' -iter 1000 -method mcmc -init_stdev 0.1".format(self.fm_train_exe, self.train_file, self.test_file2, self.predict_file2)
Popen(command_train, shell = True, stdout = PIPE).communicate()
#===========================================================================SGD============================================================================
# command_train = "{0} -task c -verbosity 0 -train {1} -test {2} -out {3} -dim '1,1,8' -iter 1000 -method sgd -learn_rate 0.01 -regular '0,0,0.01' -init_stdev 0.1".format(self.fm_train_exe, self.train_file, self.test_file, self.predict_file)
# Popen(command_train, shell = True, stdout = PIPE).communicate()
# if self.test_file2:
# command_train = "{0} -task c -verbosity 0 -train {1} -test {2} -out {3} -dim '1,1,8' -iter 1000 -method sgd -learn_rate 0.01 -regular '0,0,0.01' -init_stdev 0.1".format(self.fm_train_exe, self.train_file, self.test_file2, self.predict_file2)
# Popen(command_train, shell = True, stdout = PIPE).communicate()
#============================================================================ALS===========================================================================
# command_train = "{0} -task c -verbosity 1 -train {1} -test {2} -out {3} -dim '1,1,8' -iter 1000 -method als -regular '0,0,10' -init_stdev 0.1".format(self.fm_train_exe, self.train_file, self.test_file, self.predict_file)
# Popen(command_train, shell = True, stdout = PIPE).communicate()
# if self.test_file2:
# command_train = "{0} -task c -verbosity 1 -train {1} -test {2} -out {3} -dim '1,1,8' -iter 1000 -method als -regular '0,0,10' -init_stdev 0.1".format(self.fm_train_exe, self.train_file, self.test_file2, self.predict_file2)
# Popen(command_train, shell = True, stdout = PIPE).communicate()
#==============================================================================SGDA========================================================================
# command_train = "{0} -task c -verbosity 1 -train {1} -test {2} -out {3} -dim '1,1,8' -iter 1000 -method sgda -learn_rate 0.01 -init_stdev 0.1 -validation {4}".format(self.fm_train_exe, self.train_file, self.test_file, self.predict_file, self.train_file)
# Popen(command_train, shell = True, stdout = PIPE).communicate()
# if self.test_file2:
# command_train = "{0} -task c -verbosity 1 -train {1} -test {2} -out {3} -dim '1,1,8' -iter 1000 -method sgda -learn_rate 0.01 -init_stdev 0.1 -validation {4}".format(self.fm_train_exe, self.train_file, self.test_file2, self.predict_file2, self.train_file)
# Popen(command_train, shell = True, stdout = PIPE).communicate()
def predict(self):
self.data_process(self.predict_file, self.result_file)
if self.test_file2:
self.data_process(self.predict_file2, self.result_file2)
def evaluat(self):
if self.test_file2:
in_file = open(self.test_file2)
gold_data = [int(line.strip().split()[0]) for line in in_file]
in_file.close()
in_file = open(self.result_file2)
classifier_data = [int(line.strip().split()[0]) for line in in_file.readlines()[1:]]
in_file.close()
p, r, f, auc = evaluate(np.asarray(gold_data), np.asarray(classifier_data))
return (p, r, f, auc)
else:
in_file = open(self.test_file)
gold_data = [int(line.strip().split()[0]) for line in in_file]
in_file.close()
in_file = open(self.result_file)
classifier_data = [int(line.strip().split()[0]) for line in in_file.readlines()[1:]]
in_file.close()
p, r, f, auc = evaluate(np.asarray(gold_data), np.asarray(classifier_data))
return (p, r, f, auc)
if __name__ == '__main__':
pass