Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e9ce389

Browse files
author
Jonathan Chang
committed
Some bugfixes. This seems functional now.
1 parent 3f064a7 commit e9ce389

File tree

3 files changed

+71
-8
lines changed

3 files changed

+71
-8
lines changed

pkg/R/Flim.R

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@ Flim <- function(singleton.counts,
2222
pairwise.counts[,3],
2323
document.count)
2424
for (ii in 1:num.iterations) {
25-
flim.obj$optimizeAll()
25+
cat("Iteration ")
26+
print(ii)
27+
print(system.time({
28+
flim.obj$estimateExpectations()
29+
cat("Total change: ")
30+
print(flim.obj$optimizeAll())
31+
}))
2632
}
2733
return(flim.obj)
2834
}

pkg/src/flim.cpp

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <gsl/gsl_blas.h>
33
#include <gsl/gsl_sf_lambert.h>
44
#include <vector>
5+
#include <math.h>
56

67
class Flim {
78
// Actual parameters.
@@ -40,7 +41,7 @@ class Flim {
4041
empirical_pair_(N, N),
4142
empirical_singleton_(N) {
4243
gsl_matrix_float_set_zero(lambda_);
43-
gsl_vector_float_set_zero(ones_);
44+
gsl_vector_float_set_all(ones_, 1.0);
4445
}
4546

4647
~Flim() {
@@ -108,7 +109,6 @@ class Flim {
108109
initializeKappa(num_documents);
109110
}
110111

111-
112112
float sigmoid(float x) {
113113
return 1.0 / (1.0 + exp(-x));
114114
}
@@ -130,19 +130,66 @@ class Flim {
130130
return exp(p11) / (exp(p11) + exp(p10) + exp(p01) + 1);
131131
}
132132

133-
void optimizeAll() {
133+
double optimizeAll() {
134+
double total_delta = 0.0;
134135
for (int x = 0; x < lambda_.nrow(); ++x) {
135136
for (int y = 0; y < x; ++y) {
136-
optimizeLambda(y, x);
137+
total_delta += optimizeLambda(y, x);
137138
}
138139
}
140+
return total_delta;
139141
}
140142

141-
void optimizeLambda(unsigned int x, unsigned int y) {
143+
/**
144+
* Proof of lemma 3.2.1:
145+
*
146+
* x = a - b e^x
147+
*
148+
* iff
149+
*
150+
* x = a - W(be^a)
151+
* => W(be^a) = a - x
152+
* => (a-x) exp(a - x) = be^a
153+
*
154+
*/
155+
156+
double optimizeLambda(unsigned int x, unsigned int y) {
157+
#ifdef NEW_WAY
142158
double A = getComputedExpectation(x, y) * exp(-lambda_(x, y));
143159
double B = 2 * beta2_;
144160
double C = empirical_pair_(x, y) - beta1_;
161+
#else
162+
double A = getComputedExpectation(x, y);
163+
double B = 2 * beta2_;
164+
double C = empirical_pair_(x, y) - beta1_ - 2 * beta2_ * lambda_(x, y);
165+
#endif
166+
167+
// #define DEBUGGING_NOISE
168+
#ifdef DEBUGGING_NOISE
169+
if (empirical_pair_(x, y) > 0 && rand() < RAND_MAX / 1000) {
170+
double overcount_x = lambda_(x,y) * singleton_expectation_[y];
171+
double overcount_y = lambda_(x,y) * singleton_expectation_[x];
145172

173+
double p10 = q_lambda_[x] + kappa_[x] - overcount_x;
174+
double p01 = q_lambda_[y] + kappa_[y] - overcount_y;
175+
176+
double p11 = estimates_(x,y) - overcount_x - overcount_y;
177+
178+
std::cout << getComputedExpectation(x, y) << " "
179+
<< empirical_pair_(x, y) << " "
180+
<< kappa_[x] << " "
181+
<< kappa_[y] << " "
182+
<< lambda_(x,y) << " "
183+
<< x << " " << y << " "
184+
<< q_lambda_[x] << " "
185+
<< q_lambda_[y] << " "
186+
<< estimates_(x,y) << " "
187+
<< overcount_x << " " << overcount_y << " "
188+
<< p10 << " " << p01 << " " << p11 << " "
189+
<< std::endl;
190+
}
191+
#endif
192+
146193
double delta1 = 0;
147194
double delta2 = 0;
148195
if (beta2_ == 0) {
@@ -171,8 +218,14 @@ class Flim {
171218
new_lambda = 0;
172219
}
173220

221+
double delta = fabs(new_lambda - lambda_(x,y));
222+
if (delta - delta != 0) {
223+
delta = 0.0;
224+
}
225+
174226
lambda_(x,y) = new_lambda;
175227
lambda_(y,x) = new_lambda;
228+
return delta;
176229
}
177230

178231
RcppGSL::matrix<float> getLambda() {

tests/cora.test.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ require(Matrix)
55
data(cora.documents)
66
data(cora.vocab)
77

8-
counts <- count.pairs(cora.documents)
8+
## documents <- head(cora.documents)
9+
documents <- cora.documents
10+
11+
counts <- count.pairs(documents)
912
singleton.counts <- diag(counts)
1013
counts <- as(counts, 'dgTMatrix')
1114
pairwise.counts <- subset(data.frame(
@@ -17,7 +20,8 @@ save(pairwise.counts, singleton.counts, file="counts.Rdata")
1720

1821
flim.instance <- Flim(singleton.counts,
1922
pairwise.counts,
20-
length(cora.documents))
23+
length(documents),
24+
0.0, 0.0, 15)
2125

2226
lambda <- flim.instance$getLambda()
2327
save(lambda, file="lambda.Rdata")

0 commit comments

Comments
 (0)