Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example with ADOLC #535

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions examples/adolc_example.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using FrankWolfe
using ADOLC
using LinearAlgebra
using Test

## Data setup
n = Int(10000)

xpi = rand(n);
total = sum(xpi);
const xp = xpi ./ total;

f(x) = sum(abs2, x-xp)

println("Automatic differentiation")

const tape_id = 1
ADOLC.derivative(f, zeros(n), :jac, tape_id=tape_id)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe an explanation on the line here, since this call is not returning any variable

c = CxxVector(zeros(n))

function grad!(storage, x)
ADOLC.gradient!(c, f, n, x, tape_id, true) # derivate in the direction y
@. storage = c
end

lmo_radius = 2.5
lmo = FrankWolfe.FrankWolfe.ProbabilitySimplexOracle(lmo_radius)

x00 = FrankWolfe.compute_extreme_point(lmo, zeros(n))
gradient = collect(x00)

x_au, _, primal_au, dual_gap_au, _ = FrankWolfe.frank_wolfe(
f,
grad!,
lmo,
collect(copy(x00)),
line_search=FrankWolfe.Adaptive(),
memory_mode=FrankWolfe.InplaceEmphasis(),
verbose=true,
trajectory=false,
);

println("\nHandwritten Gradient")

f(x) = norm(x-xp)^2

function grad!(storage, x)
@. storage = 2 * (x - xp)
return nothing
end

lmo_radius = 2.5
lmo = FrankWolfe.FrankWolfe.ProbabilitySimplexOracle(lmo_radius)

x00 = FrankWolfe.compute_extreme_point(lmo, zeros(n))
gradient = collect(x00)

x, _, primal, dual_gap, _ = FrankWolfe.frank_wolfe(
f,
grad!,
lmo,
collect(copy(x00)),
line_search=FrankWolfe.Adaptive(),
memory_mode=FrankWolfe.InplaceEmphasis(),
verbose=true,
trajectory=false,
);

@testset "Automatic differentiation" begin
@test primal >= primal_au - dual_gap_au
@test primal_au >= primal - dual_gap
end
Loading