Skip to content

Lens and derivatives #23

@jw3126

Description

@jw3126

@tkf's amazing gist shows how we can use lenses to compute partial derivatives.

Often I am in the situation, where I want (partial) gradients, hessians etc. instead. Optimizing a struct is a typical example.

Basic case

We have

struct Widget{T <: Number}
    parameter1::T
    parameter2::T
end

cost(w::Widget) = ...

and want to optimize cost. However many algorithms won't accept the cost function as is,
but need cost(::AbstractVector). Also they often need gradients+hessians in the same form as well.

So to use such algorithms, a way is needed to convert our struct to a vector and back.

Variations:

  • Instead of a flat struct, one often has nested structs.
  • Some parameters of the struct might be fixed, while others can be optimized.

Currently I write a lot of ad hoc functions to pack unpack the nested fields I need into vector form and back.

I think there should be a better way. I would like to just provide for each of my structs an
isomorphism (e.g. a special type of lens) with some type of vectors and the lens machinery should
be able to do the following for me:

  • It should be easy to define the isomorphism for a big new struct, in terms of the isomorphisms of its fields.
  • If should be easy to construct lenses that focus on a selection of nested fields.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions