diff --git a/docs/src/manual/PyTorch.md b/docs/src/manual/PyTorch.md index 9eb49af..f56d76b 100644 --- a/docs/src/manual/PyTorch.md +++ b/docs/src/manual/PyTorch.md @@ -38,27 +38,37 @@ torch.save(model, "saved_pytorch_model.pt") MathOptAI uses [PythonCall.jl](https://github.com/JuliaPy/PythonCall.jl) to call from Julia into Python. -See [CondaPkg.jl](https://github.com/JuliaPy/CondaPkg.jl) for more control over -how to link Julia to an existing Python environment. For example, if you have an -existing Python installation (with PyTorch installed), and it is available in -the current Conda environment, set: +To use [`PytorchModel`](@ref) your code must load the `PythonCall` package: +```julia +import PythonCall +``` + +PythonCall uses [CondaPkg.jl](https://github.com/JuliaPy/CondaPkg.jl) to manage +Python dependencies. See [CondaPkg.jl](https://github.com/JuliaPy/CondaPkg.jl) +for more control over how to link Julia to an existing Python environment. For +example, if you have an existing Python installation (with PyTorch installed), +and it is available in the current Conda environment, do: ```julia ENV["JULIA_CONDAPKG_BACKEND"] = "Current" +import PythonCall ``` -before importing PythonCall.jl. If the Python installation can be found on the -path and it is not in a Conda environment, set: +If the Python installation can be found on the path and it is not in a Conda +environment, do: ```julia ENV["JULIA_CONDAPKG_BACKEND"] = "Null" +import PythonCall ``` If `python` is not on your path, you may additionally need to set -`JULIA_PYTHONCALL_EXE`, for example, to: +`JULIA_PYTHONCALL_EXE`, for example, do: ```julia ENV["JULIA_PYTHONCALL_EXE"] = "python3" +ENV["JULIA_CONDAPKG_BACKEND"] = "Null" +import PythonCall ``` ## Basic example @@ -67,7 +77,7 @@ Use [`MathOptAI.add_predictor`](@ref) to embed a PyTorch model into a JuMP model: ```@repl -using JuMP, MathOptAI +using JuMP, MathOptAI, PythonCall model = Model(); @variable(model, x[1:1]); predictor = MathOptAI.PytorchModel("saved_pytorch_model.pt"); @@ -81,7 +91,7 @@ formulation Use the `reduced_space = true` keyword to formulate a reduced-space model: ```@repl -using JuMP, MathOptAI +using JuMP, MathOptAI, PythonCall model = Model(); @variable(model, x[1:1]); predictor = MathOptAI.PytorchModel("saved_pytorch_model.pt"); @@ -96,7 +106,7 @@ formulation Use the `gray_box = true` keyword to embed the network as a nonlinear operator: ```@repl -using JuMP, MathOptAI +using JuMP, MathOptAI, PythonCall model = Model(); @variable(model, x[1:1]); predictor = MathOptAI.PytorchModel("saved_pytorch_model.pt"); @@ -112,7 +122,7 @@ Pass a dictionary to the `config` keyword that maps the `Symbol` name of each PyTorch layer to a MathOptAI predictor: ```@repl -using JuMP, MathOptAI +using JuMP, MathOptAI, PythonCall model = Model(); @variable(model, x[1:1]); predictor = MathOptAI.PytorchModel("saved_pytorch_model.pt"); diff --git a/docs/src/tutorials/pytorch.jl b/docs/src/tutorials/pytorch.jl index 2f1d149..e43cab1 100644 --- a/docs/src/tutorials/pytorch.jl +++ b/docs/src/tutorials/pytorch.jl @@ -11,30 +11,40 @@ # ## Python integration -# MathOptAI uses [PythonCall.jl](https://github.com/JuliaPy/PythonCall.jl) -# to call from Julia into Python. +# MathOptAI uses [PythonCall.jl](https://github.com/JuliaPy/PythonCall.jl) to call +# from Julia into Python. + +# To use [`PytorchModel`](@ref) your code must load the `PythonCall` package: +# ```julia +# import PythonCall +# ``` # -# See [CondaPkg.jl](https://github.com/JuliaPy/CondaPkg.jl) for more control -# over how to link Julia to an existing Python environment. For example, if you -# have an existing Python installation (with PyTorch installed), and it is -# available in the current Conda environment, set: +# PythonCall uses [CondaPkg.jl](https://github.com/JuliaPy/CondaPkg.jl) to manage +# Python dependencies. See [CondaPkg.jl](https://github.com/JuliaPy/CondaPkg.jl) +# for more control over how to link Julia to an existing Python environment. For +# example, if you have an existing Python installation (with PyTorch installed), +# and it is available in the current Conda environment, do: # # ```julia # ENV["JULIA_CONDAPKG_BACKEND"] = "Current" +# import PythonCall # ``` # -# before importing PythonCall.jl. If the Python installation can be found on -# the path and it is not in a Conda environment, set: +# If the Python installation can be found on the path and it is not in a Conda +# environment, do: # # ```julia # ENV["JULIA_CONDAPKG_BACKEND"] = "Null" +# import PythonCall # ``` # # If `python` is not on your path, you may additionally need to set -# `JULIA_PYTHONCALL_EXE`, for example, to: +# `JULIA_PYTHONCALL_EXE`, for example, do: # # ```julia # ENV["JULIA_PYTHONCALL_EXE"] = "python3" +# ENV["JULIA_CONDAPKG_BACKEND"] = "Null" +# import PythonCall # ``` # ## Required packages @@ -46,6 +56,7 @@ using Test import Ipopt import MathOptAI import Plots +import PythonCall # ## Training a model diff --git a/src/predictors/PytorchModel.jl b/src/predictors/PytorchModel.jl index ea94b10..d33470d 100644 --- a/src/predictors/PytorchModel.jl +++ b/src/predictors/PytorchModel.jl @@ -12,10 +12,18 @@ A wrapper struct for loading a PyTorch model. The only supported file extension is `.pt`, where the `.pt` file has been created using `torch.save(model, filename)`. +!!! warning + To use [`PytorchModel`](@ref), your code must load the `PythonCall` package: + ```julia + import PythonCall + ``` + ## Example ```jldoctest -julia> using PythonCall, MathOptAI +julia> using MathOptAI + +julia> using PythonCall # This line is important! julia> predictor = PytorchModel("model.pt"); ```