Перейти к содержимому

Как установить pymc на windows 10 x64

  • автор:

Installing PyMC3 and Theano on Windows 10* using WSL

I really, really like the PyMC3 package for proabilistic programming in Python. It seems to hit a sweet spot of allowing near endless customisation of bespoke Bayesian models, while maintaining relatively concise and readable syntax. It samples super fast, and the PyMC3 community are great.

The problem is that installing it successfully on Windows 10 can be a serious headache. There seems to be various issues successfully installing the g++ compiler and connecting it with Theano, and/or issues with multiprocessing making sampling incredibly slow (if it runs successfully at all) and limited to one core. Trying to install it last week, I felt I’d somehow managed to replicate every Windows-related install error reported across GitHub issues, StackOverflow, and the PyMC3 Discourse!

The solution for me was to install PyMC3 within Ubuntu using Windows Subsystem for Linux (WSL), thereby bypassing weird errors and conflicts within Windows. I’m not overly tech-savvy, so maybe there is a nice way to have an entirely Windows-based solution. I tried so many different ways and for the life of me I couldn’t do it!

Here’s the process I used, step by step:

1. Install WSL and Ubuntu

This is a relatively straight-forward process in itself following Microsoft’s own guide, so I won’t bother repeating it. Feel free to go with whatever distribution of Linux you prefer, but I stuck with Ubuntu and it’s working great.

If you’re able, go ahead and upgrade to WSL2. On my machine some Windows requirements for the ugrade weren’t available quite yet, so I’m just running WSL1. I skipped the part of their instructions involving this upgrade.

2. Install g++ and miniconda

Now you’ll want to open up Ubuntu and install some pre-requisites: g++ and miniconda. I’m not installing a full distribution of Anaconda because it’s horribly bloated. Go in either order you like:

  • Download g++: This one’s easy. Go for:
    • sudo apt-get install g++
    • wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
    • bash Miniconda3-latest-Linux-x86_64.sh
    • rm Miniconda3-latest-Linux-x86_64.sh

    3. Create a fresh environment and install your packages

    Strictly speaking, you could just start installing PyMC3 (in the base Miniconda environment) right away.

    But I’ve found virtual environments really useful if I mess up my install process or find my package management getting out of hand. You can just delete everything and start again!

    • Create and activate a conda virtual environment: I’ve called my virtual environment pm3, but call yours whatever you want.
      • conda create -n pm3 python
      • conda activate pm3
      • conda install mkl-service numpy scipy jupyter
      • conda install -c conda-forge pymc3

      4. Optional Steps for Jupyter Notebooks

      For data analysis, I mostly work out of Jupyter Notebooks mostly (hence including it in the instructions above). I wanted to get Jupyter working nicely too.

      Full credit here (and above) should go to Emily Kauffman, Prayson Wilfred Daniel & Michael C. Grant in this GitHub Gist — I’m basically just reproducing their instructions.

      4a. Set up your browser to open Jupyter automatically

      In Windows, typing ‘jupyter notebook’ will automatically open it up in your default browser. If you want the same behaviour your Ubuntu-based conda virtual environment, you’ll want to:

      • Define the browser environment variable: Make sure this is the path to your browser of choice. I use Firefox, so mine was:
        • export BROWSER="/mnt/c/Program Files/Mozilla Firefox/firefox.exe"

        Done! Running ‘jupyter notebook’ in your virtual environment within miniconda should automatically launch it in your browser.

        4b. Create a symlink for your Windows Jupyter Folder within Ubuntu

        If you’ve got an existing folder where you store your Jupyter Notebooks in Windows, creating a link between this and Ubuntu is really helpful.

          Navigate to Home Directory and set up the link: Replace the path to your own folder (e.g. ‘/Users/Your_name/Whatever’) here, and name the folder whatever you like:

            cd

          You should now see a folder called Jupyter_Notebooks (or whatever you called it) when Jupyter opens!

          pymc3 3.11.5

          PyMC3 is a Python package for Bayesian statistical modeling and Probabilistic Machine Learning focusing on advanced Markov chain Monte Carlo (MCMC) and variational inference (VI) algorithms. Its flexibility and extensibility make it applicable to a large suite of problems.

          Check out the getting started guide, or interact with live examples using Binder! For questions on PyMC3, head on over to our PyMC Discourse forum.

          The future of PyMC3 & Theano

          There have been many questions and uncertainty around the future of PyMC3 since Theano stopped getting developed by the original authors, and we started experiments with a PyMC version based on tensorflow probability.

          Installation Guide (Windows)

          If you are a Windows user who downloaded Anaconda or Miniforge in the days between 2022-06-06 to 2022-06-08 by clicking on the links on our Wiki installation page: https://github.com/pymc-devs/pymc/wiki/Installation-Guide-(Windows), your system might be compromised.

          PyMC Installation on Windows

          The following instructions rely on having Anaconda, Mamba or Miniforge installed, which provide Python environments from which you can install and run PyMC in a controlled way, using the conda utility.

          The Simple Install process for PyMC under Windows is recommended for most users, with conda being used to set up an environment that contains required dependencies, including the GCC compiler toolchain. Using conda allows Aesara and PyMC to easily access MKL and also confines the install of the GCC compiler toolchain into the conda environment rather than placing it onto the global Windows PATH .

          It is usually a good idea to install into a fresh conda environment, which we will call pymc_env :

          Next, you can activate the environment in which you just installed PyMC.

          The Fancy Install of PyMC is for those who would prefer to manage the GCC compiler installation on their own rather than rely on conda to do it. You can install an up-to-date copy of GCC yourself and make sure it is available on the global Windows PATH . An easy way to do this (though not the only way) is via the Chocolatey package manager:

          Once GCC installation has completed, you can then pickup the creation of a conda environment as described above, replacing the conda environment creation command with this one, which omits the m2w64-toolchain :

          2. Installation¶

          PyMC is known to run on Mac OS X, Linux and Windows, but in theory should be able to work on just about any platform for which Python, a Fortran compiler and the NumPy module are available. However, installing some extra depencies can greatly improve PyMC’s performance and versatility. The following describes the required and optional dependencies and takes you through the installation process.

          2.1. Dependencies¶

          PyMC requires some prerequisite packages to be present on the system. Fortunately, there are currently only a few hard dependencies, and all are freely available online.

            version 2.6 or later. (1.6 or newer): The fundamental scientific programming package, it provides a multidimensional array type and many useful functions for numerical analysis. (1.0 or newer): 2D plotting library which produces publication quality figures in a variety of image formats and interactive environments (optional): Library of algorithms for mathematics, science and engineering. (optional): Package for managing hierarchical datasets and designed to efficiently and easily cope with extremely large amounts of data. Requires the HDF5 library. (optional): Python interface to Graphviz’s Dot language, it allows PyMC to create both directed and non-directed graphical representations of models. Requires the Graphviz library. (optional): An enhanced interactive Python shell and an architecture for interactive parallel computing. (optional): A test discovery-based unittest extension (required to run the test suite).

          There are prebuilt distributions that include all required dependencies. For Mac OS X and Windows users, we recommend the Anaconda Python distribution. Anaconda comes bundled with most of these prerequisites. Note that depending on the currency of these distributions, some packages may need to be updated manually.

          If, instead of installing the prebuilt binaries, you prefer (or have) to build pymc yourself, make sure you have a Fortran and a C compiler. There are free compilers (gfortran, gcc) available on all platforms. Other compilers have not been tested with PyMC but may work nonetheless.

          2.2. Installation using EasyInstall¶

          The easiest way to install PyMC is to type in a terminal:

          Provided EasyInstall (part of the setuptools module) is installed and in your path, this should fetch and install the package from the Python Package Index. Make sure you have the appropriate administrative privileges to install software on your computer.

          2.3. Installing from pre-built binaries¶

          Pre-built binaries are available for Windows XP and Mac OS X. These can be installed as follows:

          1. Download the installer for your platform from PyPI or the GitHub download page.

          For other platforms, you will need to build the package yourself from source. Fortunately, this should be relatively straightforward.

          2.3.1. Anaconda¶

          If you are running the Anaconda Python distribution you can install a PyMC binary from the Binstar package management service, using the conda utility:

          2.4. Compiling the source code¶

          First download the source code from GitHub and unpack it. Then move into the unpacked directory and follow the platform specific instructions.

          2.4.1. Windows¶

          One way to compile PyMC on Windows is to install MinGW and MSYS. MinGW is the GNU Compiler Collection (GCC) augmented with Windows specific headers and libraries. MSYS is a POSIX-like console (bash) with UNIX command line tools. Download the Automated MinGW Installer and double-click on it to launch the installation process. You will be asked to select which components are to be installed: make sure the g77 compiler is selected and proceed with the instructions. Then download and install MSYS-1.0.exe, launch it and again follow the on-screen instructions.

          Once this is done, launch the MSYS console, change into the PyMC directory and type:

          This will build the C and Fortran extension and copy the libraries and python modules in the site-packages directory of your Python distribution.

          Some Windows users have reported problems building PyMC with MinGW, particularly under Enthought Python. An alternative approach in this case is to use the gcc and gfortran compilers that are bundled with EPD (located in the Scripts directory). In order to do this, you should add the EPD “Scripts” directory to your PATH environment variable (ensuring that it appears ahead of the MinGW binary directory, if it exists on your PATH). Then build PyMC using the install command above.

          Alternatively, one may build the currently-available release of PyMC using pip.

          2.4.2. Mac OS X or Linux¶

          In a terminal, type:

          The above syntax also assumes that you have gFortran installed and available. The sudo command may be required to install PyMC into the Python site-packages directory if it has restricted privileges.

          In addition, the python-dev package may be required to install PyMC on Linux systems.

          2.5. Installing from GitHub¶

          You can check out PyMC from the GitHub repository:

          Previous versions are available in the /tags directory.

          2.6. Running the test suite¶

          pymc comes with a set of tests that verify that the critical components of the code work as expected. To run these tests, users must have nose installed. The tests are launched from a python shell:

          In case of failures, messages detailing the nature of these failures will appear. In case this happens (it shouldn’t), please report the problems on the issue tracker (the issues tab on the GitHub page), specifying the version you are using and the environment.

          2.7. Bugs and feature requests¶

          Report problems with the installation, bugs in the code or feature request at the issue tracker. Comments and questions are welcome and should be addressed to PyMC’s mailing list.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *