nvidia-docker + greta

nvidia-docker + greta
Page content

Goal: Use greta with nvidia-docker

Docker file:


## Based on work by https://github.com/earthlab/dockerfiles/blob/master/r-greta/Dockerfile
## https://github.com/rocker-org/ml
## rocker
##

FROM nvidia/cuda:9.0-cudnn7-runtime

MAINTAINER "Ignacio Martinez" ignacio@protonmail.com

RUN echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections

## Prepare R installation from 
RUN sh -c 'echo "deb https://cloud.r-project.org/bin/linux/ubuntu xenial-cran35/" >> /etc/apt/sources.list' \
    && apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9 

RUN apt-get update \
    && apt-get upgrade -y -q \
    && apt-get install -y --no-install-recommends \
           libapparmor1 \
           r-base \
           r-base-dev \
           littler \
           r-cran-littler \
           libxml2-dev \
           libxt-dev \
           libssl-dev \
           libcurl4-openssl-dev \
           imagemagick \
           python-pip \
           libpython2.7 \
	   build-essential \
	   curl \
           libfreetype6-dev \
           libzmq3-dev \
           pkg-config \
           python \
           python-dev \
           rsync \
           software-properties-common \
           unzip \
           htop \
  && Rscript -e "install.packages(c('littler', 'docopt'), repo = 'https://cloud.r-project.org')" \
  && ln -s /usr/local/lib/R/site-library/littler/examples/install2.r /usr/local/bin/install2.r \
  && ln -s /usr/local/lib/R/site-library/littler/examples/installGithub.r /usr/local/bin/installGithub.r \
  && ln -s /usr/local/lib/R/site-library/littler/bin/r /usr/local/bin/r \
        && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/* \
    && echo 'options(repos = c(CRAN = "https://cloud.r-project.org"))' >> /etc/R/Rprofile.site 

RUN curl -O https://bootstrap.pypa.io/get-pip.py && \
    python get-pip.py && \
    rm get-pip.py

RUN pip --no-cache-dir install \
        ipykernel \
        jupyter \
        matplotlib \
        numpy \
        scipy \
        && \
    python -m ipykernel.kernelspec

RUN install2.r --error \
    devtools  
    
RUN R -e "devtools::install_github('rstudio/tensorflow')"
RUN R -e "devtools::install_github('rstudio/keras')"

RUN install2.r --error \
    devtools \
    rstan \
    tictoc \
    bayesplot \
    DiagrammeR \
    greta \
    rgexf \
    XML \
    tidyverse 

RUN pip install virtualenv

## Need to configure non-root user for RStudio
RUN useradd rstudio \
  && echo "rstudio:rstudio" | chpasswd \
	&& mkdir /home/rstudio \
	&& chown rstudio:rstudio /home/rstudio \
	&& addgroup rstudio staff

USER rstudio

RUN R -e "tensorflow::install_tensorflow(version = 'gpu')"

USER root

Build the container:

$ nvidia-docker build -t r-gpu .

Example:

$ nvidia-docker run -it r-gpu bash
root@46f38776714a:/# su rstudio
rstudio@46f38776714a:/$ R
library(greta)
x <- iris$Petal.Length
y <- iris$Sepal.Length
int <- normal(0, 5)
coef <- normal(0, 3)
sd <- lognormal(0, 3)
mean <- int + coef * x
distribution(y) <- normal(mean, sd)
m <- model(int, coef, sd)
draws <- mcmc(m, n_samples = 1000)
summary(draws)   
## 
## Iterations = 1:1000
## Thinning interval = 1 
## Number of chains = 4 
## Sample size per chain = 1000 
## 
## 1. Empirical mean and standard deviation for each variable,
##    plus standard error of the mean:
## 
##        Mean      SD  Naive SE Time-series SE
## sd   0.4084 0.02383 0.0003768      0.0004125
## coef 0.4097 0.01890 0.0002988      0.0003128
## int  4.3038 0.07852 0.0012415      0.0012286
## 
## 2. Quantiles for each variable:
## 
##        2.5%    25%    50%    75%  97.5%
## sd   0.3637 0.3925 0.4069 0.4236 0.4591
## coef 0.3734 0.3974 0.4096 0.4221 0.4461
## int  4.1495 4.2497 4.3046 4.3552 4.4588