Skip to content

Commit

Permalink
Add an example to the README
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Oct 20, 2024
1 parent dc3567f commit c3bbedb
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,45 @@ import Pkg
Pkg.add("MathOptAI")
```

## Example

Here's an example of using MathOptAI to embed a trained neural network from Flux
into a JuMP model. The vector of JuMP variables `x` is fed as input to the
neural network. The output `y` is a vector of JuMP variables that represents the
output layer of the neural network. The `formulation` object stores the
additional variables and constraints that were added to `model`.

```julia
julia> using JuMP, MathOptAI, Flux

julia> predictor = Flux.Chain(
Flux.Dense(28^2 => 32, Flux.sigmoid),
Flux.Dense(32 => 10),
Flux.softmax,
);

julia> #= Train the Flux model. Code not shown for simplicity =#

julia> model = JuMP.Model();

julia> JuMP.@variable(model, 0 <= x[1:28^2] <= 1);

julia> y, formulation = MathOptAI.add_predictor(model, predictor, x);

julia> y
10-element Vector{VariableRef}:
moai_SoftMax[1]
moai_SoftMax[2]
moai_SoftMax[3]
moai_SoftMax[4]
moai_SoftMax[5]
moai_SoftMax[6]
moai_SoftMax[7]
moai_SoftMax[8]
moai_SoftMax[9]
moai_SoftMax[10]
```

## Documentation

Documentation is available at
Expand Down
39 changes: 39 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,45 @@ import Pkg
Pkg.add("MathOptAI")
```

## Example

Here's an example of using MathOptAI to embed a trained neural network from Flux
into a JuMP model. The vector of JuMP variables `x` is fed as input to the
neural network. The output `y` is a vector of JuMP variables that represents the
output layer of the neural network. The `formulation` object stores the
additional variables and constraints that were added to `model`.

```julia
julia> using JuMP, MathOptAI, Flux

julia> predictor = Flux.Chain(
Flux.Dense(28^2 => 32, Flux.sigmoid),
Flux.Dense(32 => 10),
Flux.softmax,
);

julia> #= Train the Flux model. Code not shown for simplicity =#

julia> model = JuMP.Model();

julia> JuMP.@variable(model, 0 <= x[1:28^2] <= 1);

julia> y, formulation = MathOptAI.add_predictor(model, predictor, x);

julia> y
10-element Vector{VariableRef}:
moai_SoftMax[1]
moai_SoftMax[2]
moai_SoftMax[3]
moai_SoftMax[4]
moai_SoftMax[5]
moai_SoftMax[6]
moai_SoftMax[7]
moai_SoftMax[8]
moai_SoftMax[9]
moai_SoftMax[10]
```

## Getting help

This package is under active development. For help, questions, comments, and
Expand Down

0 comments on commit c3bbedb

Please sign in to comment.