Skip to content

Commit 662a84c

Browse files
committed
fixed max_iters bug
1 parent f5d5ced commit 662a84c

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

src/hamerly.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,13 @@ function kmeans!(alg::Hamerly, containers, design_matrix, k;
4141
@parallelize n_threads ncol chunk_initialize!(alg, containers, centroids, design_matrix)
4242

4343
converged = false
44-
niters = 1
44+
niters = 0
4545
J_previous = 0.0
4646
p = containers.p
4747

4848
# Update centroids & labels with closest members until convergence
49-
while niters <= max_iters
49+
while niters < max_iters
50+
niters += 1
5051
update_containers!(containers, alg, centroids, n_threads)
5152
@parallelize n_threads ncol chunk_update_centroids!(centroids, containers, alg, design_matrix)
5253
collect_containers(alg, containers, n_threads)
@@ -58,7 +59,7 @@ function kmeans!(alg::Hamerly, containers, design_matrix, k;
5859
@parallelize n_threads ncol chunk_update_bounds!(containers, r1, r2, pr1, pr2)
5960

6061
if verbose
61-
# Show progress and terminate if J stopped decreasing.
62+
# Show progress and terminate if J stops decreasing as specified by the tolerance level.
6263
println("Iteration $niters: Jclust = $J")
6364
end
6465

@@ -69,7 +70,7 @@ function kmeans!(alg::Hamerly, containers, design_matrix, k;
6970
end
7071

7172
J_previous = J
72-
niters += 1
73+
7374
end
7475

7576
@parallelize n_threads ncol sum_of_squares(containers, design_matrix, containers.labels, centroids)

src/kmeans.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,14 @@ function kmeans!(alg, containers, design_matrix, k;
189189
centroids = init == nothing ? smart_init(design_matrix, k, n_threads, init=k_init).centroids : deepcopy(init)
190190

191191
converged = false
192-
niters = 1
192+
niters = 0
193193
J_previous = 0.0
194194

195195
# Update centroids & labels with closest members until convergence
196196

197-
while niters <= max_iters
197+
while niters < max_iters
198+
niters += 1
199+
198200
update_containers!(containers, alg, centroids, n_threads)
199201
J = update_centroids!(centroids, containers, alg, design_matrix, n_threads)
200202

@@ -210,7 +212,7 @@ function kmeans!(alg, containers, design_matrix, k;
210212
end
211213

212214
J_previous = J
213-
niters += 1
215+
214216
end
215217

216218
totalcost = sum_of_squares(design_matrix, containers.labels, centroids)

test/test05_hamerly.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ end
2929

3030
@test res.totalcost 14.16198704459199
3131
@test !res.converged
32-
@test res.iterations == 11
32+
@test res.iterations == 10
3333

3434
Random.seed!(2020)
3535
X = rand(3, 100)
@@ -48,7 +48,7 @@ end
4848

4949
@test res.totalcost 14.16198704459199
5050
@test !res.converged
51-
@test res.iterations == 11
51+
@test res.iterations == 10
5252

5353
Random.seed!(2020)
5454
X = rand(3, 100)

0 commit comments

Comments
 (0)