From d4efac6de4c57f39570b063a1efc267d35a393cb Mon Sep 17 00:00:00 2001 From: Logan Mondal Bhamidipaty <76822456+FlyingWorkshop@users.noreply.github.com> Date: Fri, 3 May 2024 17:59:51 -0700 Subject: [PATCH] checkpoint --- src/explicit.jl | 20 +++++++++++--------- src/implicit.jl | 13 ++++++++----- src/utils.jl | 0 3 files changed, 19 insertions(+), 14 deletions(-) create mode 100644 src/utils.jl diff --git a/src/explicit.jl b/src/explicit.jl index a68fade..dd674a2 100644 --- a/src/explicit.jl +++ b/src/explicit.jl @@ -13,7 +13,7 @@ function PoissonEPCA() # assumes X = {integers} epsilon = eps() @. begin - Bregman(p, q) = p * log((p + epsilon) / (q + epsilon)) + q - p + Bregman(p, q) = p * (log(p + epsilon) - log(q + epsilon)) + q - p g(theta) = exp(theta) end mu = g(0) @@ -25,7 +25,7 @@ function BernoulliEPCA() # assumes X = {0, 1} epsilon = eps() @. begin - Bregman(p, q) = p * log((p + epsilon)/ (q + epsilon)) + (1 - p) * log((1 - p + epsilon) / (1 - q + epsilon)) + Bregman(p, q) = p * (log(p + epsilon) - log(q + epsilon)) + (1 - p) * (log(1 - p + epsilon) - log(1 - q + epsilon)) g(theta) = exp(theta) / (1 + exp(theta)) end mu = g(1) @@ -57,7 +57,7 @@ function fit!( ) B, g = epca.Bregman, epca.g L(theta) = begin - X_hat= g.(theta) + X_hat = g.(theta) sum(B(X, X_hat) + epsilon * B(mu, X_hat)) end n, d = size(X) @@ -66,9 +66,9 @@ function fit!( for i in 1:maxiter V = Optim.minimizer(optimize(V_hat->L(A * V_hat), V)) result = optimize(A_hat->L(A_hat * V), A) - loss = Optim.minimum(result) A = Optim.minimizer(result) if verbose && (i % print_steps == 0 || i == 1) + loss = Optim.minimum(result) println("Iteration: $i/$maxiter | Loss: $loss") end end @@ -81,22 +81,24 @@ function compress( epca::ExplicitEPCA, X; mu=epca.mu, - maxoutdim=1, maxiter=100, verbose=false, - print_stesp=10, + print_steps=10, epsilon=eps() ) B, g, V = epca.Bregman, epca.g, epca.V L(theta) = begin - X_hat= g.(theta) + X_hat = g.(theta) sum(@. B(X, X_hat) + epsilon * B(mu, X_hat)) end n, _ = size(X) - A = ones(n, maxoutdim) + outdim = size(V)[1] + A = ones(n, outdim) for _ in 1:maxiter - A = Optim.minimizer(optimize(A_hat->L(A_hat * V), A)) + result = optimize(A_hat->L(A_hat * V), A) + A = Optim.minimizer(result) if verbose && (i % print_steps == 0 || i == 1) + loss = Optim.minimum(result) println("Iteration: $i/$maxiter | Loss: $loss") end end diff --git a/src/implicit.jl b/src/implicit.jl index e256d3d..509a501 100644 --- a/src/implicit.jl +++ b/src/implicit.jl @@ -84,9 +84,9 @@ function fit!( for i in 1:maxiter V = Optim.minimizer(optimize(V_hat->L(A * V_hat), V)) result = optimize(A_hat->L(A_hat * V), A) - loss = Optim.minimum(result) A = Optim.minimizer(result) if verbose && (i % print_steps == 0 || i == 1) + loss = Optim.minimum(result) println("Iteration: $i/$maxiter | Loss: $loss") end end @@ -96,8 +96,8 @@ end function compress( epca::ImplicitEPCA, - X, - mu; + X; + mu=1, # NOTE: mu = 1 may not be valid for all link functions. maxiter=100, verbose=false, print_steps=10, @@ -106,11 +106,14 @@ function compress( ) L = _make_loss(epca, X, mu, epsilon; tol=tol) n, _ = size(X) - A = ones(n, maxoutdim) V = epca.V + outdim = size(V)[1] + A = ones(n, outdim) for i in 1:maxiter - A = Optim.minimizer(optimize(A_hat->L(A_hat * V), A)) + result = optimize(A_hat->L(A_hat * V), A) + A = Optim.minimizer(result) if verbose && (i % print_steps == 0 || i == 1) + loss = Optim.minimum(result) println("Iteration: $i/$maxiter | Loss: $loss") end end diff --git a/src/utils.jl b/src/utils.jl new file mode 100644 index 0000000..e69de29