-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_functions.py
154 lines (130 loc) · 4.74 KB
/
helper_functions.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
from __future__ import division
import itertools
import numpy as np
def warn(condition, string):
if condition == False:
print "WARNING: "+string
# Normalizes the rows of a matrix M to sum up to 1
def normalize_rows(M):
row_sums = M.sum(1)
return M/row_sums[:, np.newaxis]
# Normalizes the columns of a matrix M to sum up to 1
def normalize_columns(M):
col_sums = M.sum(0)
return M/col_sums[np.newaxis, :]
# Calculates the maximum difference between entries of two matrices A and B
def max_diff(A, B):
C = A-B
return max([abs(x) for x in list(C.flatten())])
# Calculates the L1 difference between two matrices A and B
def L1_diff(A,B):
C = A-B
return sum([abs(x) for x in list(C.flatten())])
# Calculates lower bound on L1 error between A and B
def min_error(A, B):
K = A[0,:].size
if K != B[0, :].size:
print "Matrices have different numbers of columns"
total_err = 0
for colA in range(K):
min_err = float("inf")
for colB in range(K):
err = (abs(A[:, colA] - B[:, colB])).sum()
if err < min_err:
min_err = err
total_err = total_err + min_err
return total_err
# Calculates a greedy L1 error between A and B
def greedy_error(A, B):
K = A[0,:].size
if K != B[0, :].size:
print "Matrices have different numbers of columns"
total_err = 0
columns_B = range(K)
for colA in range(K):
min_err = float("inf")
col_index = -1
for colB in columns_B:
err = (abs(A[:, colA] - B[:, colB])).sum()
if err < min_err:
min_err = err
col_index = colB
total_err = total_err + min_err
columns_B.remove(col_index)
return total_err
# Saves the L1 error between all pairs of columns of A and B
def save_colerrors(A, B, filename):
K = A[0, :].size
if K != B[0, :].size:
print "Matrices have different numbers of columns"
errors = np.zeros(K*K)
for colA in range(K):
for colB in range(K):
errors[colA*K + colB] = (abs(A[:, colA] - B[:, colB])).sum()
np.savetxt(filename, errors)
# Appends greedy and min L1 errors between the true and recovered topic matrices to the given text file
def save_L1_errors1(A_proj_estimate, true_A, output_filename, numdocs, seed_W, numwords, time):
file = open(output_filename, 'a')
file.write(str(numdocs))
file.write('\t')
file.write(str(seed_W))
file.write('\t')
file.write(str(min(greedy_error(true_A, A_proj_estimate), greedy_error(A_proj_estimate, true_A))))
file.write('\t')
file.write(str(min_error(true_A, A_proj_estimate)))
file.write('\t')
file.write(str(np.amin(A_proj_estimate)))
file.write('\t')
file.write(str(np.sum(np.absolute(A_proj_estimate))))
file.write('\t')
file.write(str(numwords - len(true_A[:, 0])))
file.write('\t')
file.write(str(time))
file.write('\n')
file.close()
# Appends greedy and min L1 errors between the true and recovered topic matrices to the given text file
def save_L1_errors(A_proj_estimate, true_A, output_filename, epsilon, numdocs, seed_W, numwords, time):
file = open(output_filename, 'a')
file.write(str(epsilon))
file.write('\t')
file.write(str(numdocs))
file.write('\t')
file.write(str(seed_W))
file.write('\t')
file.write(str(min(greedy_error(true_A, A_proj_estimate), greedy_error(A_proj_estimate, true_A))))
file.write('\t')
file.write(str(min_error(true_A, A_proj_estimate)))
file.write('\t')
file.write(str(np.amin(A_proj_estimate)))
file.write('\t')
file.write(str(np.sum(np.absolute(A_proj_estimate))))
file.write('\t')
file.write(str(numwords - len(true_A[:, 0])))
file.write('\t')
file.write(str(time))
file.write('\n')
file.close()
# Appends greedy and min L1 errors between the true and recovered topic matrices to the given text file
def save_L1_errors2(A_proj_estimate, true_A, output_filename, epsilon, step, numdocs, seed_W, numwords, time):
file = open(output_filename, 'a')
file.write(str(epsilon))
file.write('\t')
file.write(str(step))
file.write('\t')
file.write(str(numdocs))
file.write('\t')
file.write(str(seed_W))
file.write('\t')
file.write(str(min(greedy_error(true_A, A_proj_estimate), greedy_error(A_proj_estimate, true_A))))
file.write('\t')
file.write(str(min_error(true_A, A_proj_estimate)))
file.write('\t')
file.write(str(np.amin(A_proj_estimate)))
file.write('\t')
file.write(str(np.sum(np.absolute(A_proj_estimate))))
file.write('\t')
file.write(str(numwords - len(true_A[:, 0])))
file.write('\t')
file.write(str(time))
file.write('\n')
file.close()