logo

Julia's Automatic Differentiation Package Zygote.jl 📂Julia

Julia's Automatic Differentiation Package Zygote.jl

Overview

In Julia, the Zygote.jl package is used for automatic differentiation, especially in the field of machine learning, and particularly for deep learning. The developers promote this package as the next-generation automatic differentiation system that enables differentiable programming in Julia, and indeed, using it can be surprisingly intuitive.

If you are curious about packages related to the derivative itself, not automatic differentiation, check out the Calculus.jl package.

Code

Univariate Functions

It’s incredibly simple. Just like when we differentiate normally, appending a prime ' to the function name computes the derivative as if we are working with an actual derivative.

julia> using Zygote

julia> p(x) = 2x^2 + 3x + 1
p (generic function with 1 method)

julia> p(2)
15

julia> p'(2)
11.0

julia> p''(2)
4.0

Multivariate Functions

Use the gradient() function.

julia> g(x,y) = 3x^2 + 2y + x*y
g (generic function with 1 method)

julia> gradient(g, 2,-1)
(11.0, 4.0)

If you want to write code a bit more intuitively, you can redefine the function using \nabla, or , like below.

julia> ∇(f, v...) = gradient(f, v...)
∇ (generic function with 1 method)

julia> ∇(g, 2, -1)
(11.0, 4.0)

Full Code

using Zygote

p(x) = 2x^2 + 3x + 1

p(2)
p'(2)
p''(2)

g(x,y) = 3x^2 + 2y + x*y
gradient(g, 2,-1)

∇(f, v...) = gradient(f, v...)
∇(g, 2, -1)

Environment

  • OS: Windows
  • julia: v1.9.0
  • Zygote: v0.6.62