Title: | Interface for Masked Autoregressive Flows |
---|---|
Description: | Interfaces the Python library 'zuko' implementing Masked Autoregressive Flows. See Rozet, Divo and Schnake (2023) <doi:10.5281/zenodo.7625672> and Papamakarios, Pavlakou and Murray (2017) <doi:10.48550/arXiv.1705.07057>. |
Authors: | Jean-Michel Marin [aut, cph], François Rousset [aut, cre, cph] |
Maintainer: | François Rousset <[email protected]> |
License: | GPL (>= 2) |
Version: | 1.1.6 |
Built: | 2024-10-27 05:04:20 UTC |
Source: | https://github.com/cran/mafR |
(Currently not used nor exported) utility converting an R object to a torch tensor.
.r_to_torch(x, py_handle, device)
.r_to_torch(x, py_handle, device)
x |
An R object suitable for use in |
py_handle |
The return value of |
device |
Character: |
r_to_torch
returns a 32-bit floating-point torch tensor allocated on the given device
.
my_env <- list2env(list(is_set=FALSE),parent = emptyenv()) my_env <- get_py_MAF_handle(my_env, reset=FALSE, torch_device="cpu")
my_env <- list2env(list(is_set=FALSE),parent = emptyenv()) my_env <- get_py_MAF_handle(my_env, reset=FALSE, torch_device="cpu")
Interface to control variables in a Python environment possibly used by Infusion. Currently the only implemented control is that of the torch random seed.
control_py_env(py_handle, seed = NULL)
control_py_env(py_handle, seed = NULL)
py_handle |
An R environment that provides access to a Python evaluation environment,
as produced by |
seed |
Numeric: passed (as integer value) to |
Returns NULL invisibly.
## Initialization of Python session: my_env <- list2env(list(is_set=FALSE),parent = emptyenv()) py_handle <- get_py_MAF_handle(my_env, reset=FALSE, torch_device="cpu") if (inherits(py_handle,"environment")) control_py_env(py_handle, seed=0L)
## Initialization of Python session: my_env <- list2env(list(is_set=FALSE),parent = emptyenv()) py_handle <- get_py_MAF_handle(my_env, reset=FALSE, torch_device="cpu") if (inherits(py_handle,"environment")) control_py_env(py_handle, seed=0L)
Utility initializing a Python environment for running zuko.flows.MAF
and retrieving it.
get_py_MAF_handle(envir, reset=FALSE, torch_device="cpu", GPU_mem=NULL, verbose = TRUE)
get_py_MAF_handle(envir, reset=FALSE, torch_device="cpu", GPU_mem=NULL, verbose = TRUE)
envir |
An environment (in the R sense) initialized as shown in the Examples. |
reset |
Boolean: Whether to reinitialize the Python session or not. |
torch_device |
Character: |
GPU_mem |
For development purposes (effect is complicated). An amount of (dedicated) GPU memory, in bytes. |
verbose |
Boolean. Whether to print some messages or not. |
If successful, get_py_MAF_handle
returns the modified input environment.
If sourcing the Python code provided by mafR failed (presumably from trying to use an improperly set-up Python environment), the error condition message is returned.
# Initialization of Python session: my_env <- list2env(list(is_set=FALSE),parent = emptyenv()) my_env <- get_py_MAF_handle(my_env, reset=FALSE, torch_device="cpu") if (inherits(my_env,"environment")) { # => provides access to: my_env$torch # Imported Python package (result of reticulate::import("torch")) my_env$device # the torch_device # and to internal definitions for MAF training }
# Initialization of Python session: my_env <- list2env(list(is_set=FALSE),parent = emptyenv()) my_env <- get_py_MAF_handle(my_env, reset=FALSE, torch_device="cpu") if (inherits(my_env,"environment")) { # => provides access to: my_env$torch # Imported Python package (result of reticulate::import("torch")) my_env$device # the torch_device # and to internal definitions for MAF training }
This wraps Python procedures to train Masked Autoregressive Flows (MAFs, Paramakarios et al. 2017) using the Python package zuko
. It has been tested with version 1.1.0 and 1.2.0 of that package. Note that objects created by its version 1.2.0 cannot be read with its version 1.1.0 (i.e., when saved in and read from pickle
files).
The simplest portable way to get mafR working may be to install it in a conda environment. Below is a complete installation recipe. More information about alternative installation procedure may be found on the Git repository for mafR, https://github.com/f-rousset/mafR.
mkdir -p ~/miniconda3 wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda3/miniconda.sh bash ~/miniconda3/miniconda.sh -b -u -p ~/miniconda3 rm ~/miniconda3/miniconda.sh ~/miniconda3/bin/conda init bash conda create --name maf-conda python==3.10 conda activate maf-conda pip install zuko conda install R conda install conda-forge::r-gmp conda install conda-forge::gsl
and, in an R session within the maf-conda
environment:
install.packages("reticulate") library(reticulate) use_condaenv(condaenv="maf-conda", conda="~/miniconda3/bin/conda") install.packages("mafR") # 'mafR' was first designed for use with 'Infusion': install.packages("Infusion") install.packages("Rmixmod") # only a Suggested dependency of Infusion, but needed.
Papamakarios, G., D. Sterratt, and I. Murray. 2019. Sequential Neural Likelihood: Fast Likelihood-free Inference with Autoregressive Flows. Proceedings of the Twenty-Second International Conference on Artificial Intelligence and Statistics, PMLR 89:837-848, 2019. https://doi.org/10.48550/arXiv.1705.07057 ; https://proceedings.mlr.press/v89/papamakarios19a.html
Rozet, F., Divo, F., Schnake, S (2023) Zuko: Normalizing flows in PyTorch. https://doi.org/10.5281/zenodo.7625672