-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
192 lines (152 loc) · 4.52 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import argparse
from pathlib import Path
import os
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from tabulate import tabulate
from evaluate import evaluate
from jwnmf import jwnmf
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"-d",
"--dataset",
help="Dataset Path; Must Contain A.csv and S.csv.",
type=Path,
required=True,
)
parser.add_argument(
"-k",
"--latent_dim",
help="Dimension of latent factor",
type=int,
default=10,
)
parser.add_argument(
"-l",
"--lambda_coefficient",
help="Lambda",
type=float,
default=0.1,
)
parser.add_argument(
"-i",
"--max_iterations",
type=int,
default=4000,
)
parser.add_argument(
"-e",
"--epsilon",
type=float,
default=1e-6,
)
parser.add_argument(
"-o",
"--output_dir",
help="Output Directory",
type=Path,
)
return parser.parse_args()
def read_matrix(path):
return pd.read_csv(path, header=None, index_col=False).to_numpy()
def write_matrix(matrix, path):
pd.DataFrame(matrix).to_csv(path, header=None, index=False)
def plot_loss(losses, path):
plt.figure()
plt.plot(losses)
plt.xlabel("Iterations")
plt.ylabel("Losses")
plt.savefig(path)
def main():
args = parse_args()
dataset = args.dataset
A_Ob = read_matrix(dataset / "A_Ob.csv")
S_Ob = read_matrix(dataset / "S_Ob.csv")
S_height, S_width = S_Ob.shape
n, m = A_Ob.shape
assert S_height == S_width == n
k = args.latent_dim
lambda_coefficient = args.lambda_coefficient
max_iters = args.max_iterations
epsilon = args.epsilon
output_dir = args.output_dir if args.output_dir else dataset / "JWNMF_results"
os.mkdir(output_dir)
table_data = [
("dataset", dataset),
("output", output_dir),
("num users", n),
("num attributes", m),
("latent dim", k),
("lambda", lambda_coefficient),
("max iterations", max_iters),
("epsilon", epsilon),
]
table = tabulate(
table_data,
headers=["Parameter", "Value"],
tablefmt="orgtbl",
)
print(table)
V, U, W, losses, i = jwnmf.train(
S_Ob, A_Ob, m, n, k, lambda_coefficient, max_iters, epsilon
)
print("Terminated")
table_data.append(("iterations", i))
table = tabulate(
table_data,
headers=["Parameter", "Value"],
tablefmt="orgtbl",
)
print(table)
with open(output_dir / "info.txt", "w") as f:
f.write(table)
write_matrix(V, output_dir / "V.csv")
write_matrix(U, output_dir / "U.csv")
write_matrix(W, output_dir / "W.csv")
S_Pr = V @ V.T
A_Pr = V @ U.T
S_Pr[S_Pr > 1] = 1
S_Pr[S_Pr < 0] = 0
A_Pr[A_Pr > 1] = 1
A_Pr[A_Pr < 0] = 0
write_matrix(S_Pr, output_dir / "S_Pr.csv")
write_matrix(A_Pr, output_dir / "A_Pr.csv")
plot_loss(losses, output_dir / "losses.png")
try:
A_Gt = read_matrix(dataset / "A_GT.csv")
S_Gt = read_matrix(dataset / "S_GT.csv")
except:
print("Ground Truth Not Found")
return
assert A_Gt.shape == A_Ob.shape
assert S_Gt.shape == S_Ob.shape
predicted_for_eval_S = evaluate.predicted_scores_for_eval(S_Pr, S_Ob)
ground_truth_for_eval_S = evaluate.ground_truth_for_eval(S_Gt, S_Ob)
with open(output_dir / "unobserved_inferred.txt", "w") as f:
f.write(",".join(map(str, list(predicted_for_eval_S))))
with open(output_dir / "unobserved_gt.txt", "w") as f:
f.write(",".join(map(str, list(ground_truth_for_eval_S))))
metrics = evaluate.evaluate(ground_truth_for_eval_S, predicted_for_eval_S)
table = tabulate(
{(k, v) for k, v in metrics.items()},
headers=["Parameter", "Value"],
tablefmt="orgtbl",
)
print(table)
with open(output_dir / "metrics.txt", "w") as f:
f.write(table)
predicted_for_eval_A = evaluate.predicted_scores_for_eval(A_Pr, A_Ob)
ground_truth_for_eval_A = evaluate.ground_truth_for_eval(A_Gt, A_Ob)
metrics = evaluate.evaluate_cascade(ground_truth_for_eval_A, predicted_for_eval_A)
table = tabulate(
{(k, v) for k, v in metrics.items()},
headers=["Parameter", "Value"],
tablefmt="orgtbl",
)
print(table)
with open(output_dir / "metrics_A.txt", "a") as f:
f.write(table)
if __name__ == "__main__":
main()