nvidia-docker + greta

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 = 1 
## 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
## int  4.3062 0.08244 0.0026070      0.0053595
## coef 0.4089 0.02012 0.0006362      0.0011826
## sd   0.4086 0.02407 0.0007611      0.0009736
## 
## 2. Quantiles for each variable:
## 
##        2.5%    25%    50%    75%  97.5%
## int  4.1542 4.2486 4.3030 4.3591 4.4686
## coef 0.3700 0.3961 0.4094 0.4229 0.4482
## sd   0.3657 0.3914 0.4070 0.4240 0.4588
Ignacio Martinez avatar
About Ignacio Martinez
research economist, tech enthusiast
comments powered by Disqus