| 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] (ORCID: <https://orcid.org/0000-0003-4670-0371>) |
| Maintainer: | François Rousset <[email protected]> |
| License: | GPL (>= 2) |
| Version: | 1.1.14 |
| Built: | 2026-06-06 05:48:04 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.
# See help("get_py_MAF_handle") for session setup, providing 'py_handle' argument in particular.# See help("get_py_MAF_handle") for session setup, providing 'py_handle' argument in particular.
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.
# See help("get_py_MAF_handle") for basic usage # The following test of the first argument may be useful in broader contexts: ## Not run: if (inherits(py_handle,"environment")) control_py_env(py_handle, seed=0L) ## End(Not run)# See help("get_py_MAF_handle") for basic usage # The following test of the first argument may be useful in broader contexts: ## Not run: if (inherits(py_handle,"environment")) control_py_env(py_handle, seed=0L) ## End(Not run)
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.
## Not run: # too slow for CRAN checks # Initialization of python virtual environment: init_py_env(test_cuda=FALSE) # Initialization of python session (defines functions etc. # in python session and provide access to them from R): # my_env <- list2env(list(is_set=FALSE),parent = emptyenv()) py_handle <- get_py_MAF_handle(my_env, reset=FALSE, torch_device="cpu") # => get_py_MAF_handle() has added objects in myenv: my_env$torch # Imported Python package (result of reticulate::import("torch")) my_env$device # the torch_device # and functions for MAF training, such as myenv$MAF_density_estimation. # The returned 'py_handle' can be used by oher functions, e.g.: control_py_env(py_handle, seed=0L) # alters the seed in the python session. ## End(Not run)## Not run: # too slow for CRAN checks # Initialization of python virtual environment: init_py_env(test_cuda=FALSE) # Initialization of python session (defines functions etc. # in python session and provide access to them from R): # my_env <- list2env(list(is_set=FALSE),parent = emptyenv()) py_handle <- get_py_MAF_handle(my_env, reset=FALSE, torch_device="cpu") # => get_py_MAF_handle() has added objects in myenv: my_env$torch # Imported Python package (result of reticulate::import("torch")) my_env$device # the torch_device # and functions for MAF training, such as myenv$MAF_density_estimation. # The returned 'py_handle' can be used by oher functions, e.g.: control_py_env(py_handle, seed=0L) # alters the seed in the python session. ## End(Not run)
py_require
Initialize the required Python environment with the required modules, using procedures introduced in reticulate 1.42.0.
init_py_env( cuda=FALSE, test_cuda=cuda, test_dynamo=TRUE, force=FALSE, test_torch=test_cuda || test_dynamo, verbose=interactive())init_py_env( cuda=FALSE, test_cuda=cuda, test_dynamo=TRUE, force=FALSE, test_torch=test_cuda || test_dynamo, verbose=interactive())
cuda |
Boolean: Whether to install cuda device (only needed once). |
test_cuda |
Boolean: whether to test that cuda device can be used. |
test_dynamo |
Boolean: whether to test that |
test_torch |
Boolean: whether to test that |
force |
Boolean: Whether to force re-installation of miniconda. |
verbose |
Boolean: Whether to reprot progress of the different steps of the procedure. |
The the attempt to import the "torch._dynamo" module may fail on linux with message
ImportError: cannot import name 'NP_SUPPORTED_MODULES' from 'torch._dynamo.utils'.
If so, seek the libstdc++ library in the r-reticulate architecture (something like
/home/<MY_USER_NAME>/.local/share/r-miniconda/lib/libstdc++.so.6.0.29),
and either create a symbolic link to it
as libstdc++.so.6 in (likely path) /usr/lib/x86_64-linux-gnu/,
or change the PATH variable:
export LD_LIBRARY_PATH=/home/<MY_USER_NAME>/.local/share/r-miniconda/lib/:$LD_LIBRARY_PATH.
On the WSL, I found that the symbolic link is overwritten each time the WSL is restarted.
Returns NULL invisibly.
# See help("get_py_MAF_handle") for basic usage # The following test may be useful in broader contexts: ## Not run: # too slow for CRAN checks if ( ! reticulate::py_available(initialize = FALSE)) init_py_env(test_cuda=FALSE) ## End(Not run)# See help("get_py_MAF_handle") for basic usage # The following test may be useful in broader contexts: ## Not run: # too slow for CRAN checks if ( ! reticulate::py_available(initialize = FALSE)) init_py_env(test_cuda=FALSE) ## End(Not run)
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, 1.2.0 and 1.4.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. Alternative installation procedures (notably, using reticulate functions) may be found in files not necessarily included in this package but available on its Git repository, 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