Skip to content
This repository has been archived by the owner on Apr 18, 2023. It is now read-only.

Fix and test asymmetric_quadratic example #193

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 7 additions & 8 deletions examples/asymmetric_quadratic.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using Nabla
using LinearAlgebra
using Random

# Generate the values required to compute a matrix quadratic form.
N = 5
B = randn(5, 5)
A = B.'B + UniformScaling(1e-6)
A = B'B + UniformScaling(1e-6)

# Low-level API computation of derivatives.

Expand All @@ -12,13 +14,10 @@ x_, y_ = randn(N), randn(N)
x, y = Leaf.(Tape(), (x_, y_))

# Compute the forward pass.
z = x.' * (A * y) # Temporary bracketting because we don't support RowVectors yet.
z = x' * (A * y) # Temporary bracketting because we don't support RowVectors yet.

println("Output of the forward pass is:")
println(z)
println()
println("y is $(Nabla.unbox(z)).")
println()
println("Output of the forward pass is:\n $z\n")
println("y is $(Nabla.unbox(z)).\n")

# Get the reverse tape.
z̄ = ∇(z)
Expand All @@ -42,7 +41,7 @@ println("Gradient of z w.r.t. y at $y_ is $ȳ")
# Define the function to be differentiated. Parameters w.r.t. which we want gradients must
# be arguments. Parameters that we don't want gradients w.r.t. should be passed in via a
# closure.
@unionise f(x::AbstractVector, y::AbstractVector) = x.'A * y
@unionise f(x::AbstractVector, y::AbstractVector) = x'A * y

# Compute a function `∇f` which computes the derivative of `f` w.r.t. the inputs.
∇f = ∇(f)
Expand Down
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,9 @@ end
include("checkpointing.jl")
end

@testset "examples" begin
# make sure the examples don't throw errors
include("../examples/asymmetric_quadratic.jl")
end

end