Please Test This Experimental greta Install Script

Please reply here as to your success or failure using the below install script - Its been moderately tested on Windows, but needs more Mac OS and Linux Testing

Installing greta requires the successful configuration of a compatible conda Python environment. The default configurations offered by any out-of-the-box R functions like tensorflow::install_tensorflow() do not provide for greta-compatible configurations.

I have had much success in the past with my textbook’s install instructions (robust install instructions link) as hundreds of students get things working properly without issue using these instructions. The challenge with those instructions is that students have to leave the comfort of RStudio and venture to using command prompts which setup environments that RStudio then needs to find.

To make things easier and to avoid students venturing into using the command prompt, I have created a greta-installation script to LOCALLY install greta and that can be run interactively on any OS and all within RStudio. The script is now a separate section within my textbook (experimental install script link) and is pasted below (note: script will also install the causact package which I developed and provides R functions for visualizing and running inference via greta on generative directed acyclic graphs).

## INSTALLATION SCRIPT TO GET GRETA, CAUSACT,
## and TENSORFLOW ALL WORKING TOGETHER HAPPILY

## NOTE:  Run each line one at a time using CTRL+ENTER.
##        Await completion of one line
##        before running the next.
##        If prompted to "Restart R", say YES.

#### STEP 0:  Restart R in a Clean Session
#### use RStudio menu:  SESSION -> RESTART R

#### STEP 1: INSTALL PACKAGES WITH PYTHON DEPENDENCIES
install.packages("reticulate",dependencies = TRUE)
install.packages("greta",dependencies = TRUE)
install.packages("causact",dependencies = TRUE)

#### STEP 2: INSTALL & UPDATE MINICONDA AS NEEDED
## test for available conda binary
condaPath = try(reticulate::conda_binary()) #error is OK
if (is.null(condaPath) | class(condaPath) == "try-error") {
  ## install miniconda in default location if possible
  condaInstall = try(reticulate::install_miniconda())
  condaPath = try(reticulate::miniconda_path())
  ## if install still fails install miniconda in C:\miniconda
  if(class(condaInstall) == "try-error") {
    condaPath = file.path("/", "miniconda")
    reticulate::install_miniconda(path = condaPath,
                                  force = TRUE)}
  reticulate::miniconda_update(path = condaPath) #update new install
  newCondaInstall = TRUE  ## flag to update env var if needed
} else {
  reticulate::miniconda_update() ## update existing conda
  newCondaInstall = FALSE
  ### this environment is added automatically by install_miniconda()
  reticulate::conda_create(envname = "r-reticulate") ## add environment
}

#### STEP 3: RESTART R WITH CONDA ENVIRONMENT HINT
#### GIVEN IN EDITED .Rprofile FILE
## capture environment variable for inclusion in .Rprofile
newProfileLines = c() ## initialize config lines for .Rprofile
if(newCondaInstall) {  ## help R find new install location
  newProfileLines = c(
    newProfileLines,
    paste0("Sys.setenv(RETICULATE_MINICONDA_PATH = '",
           condaPath, "')")
  )
  rProfilePath = file.path("~", ".Rprofile")
  profileLines = c()  ## init blank lines
  if (file.exists(rProfilePath)) {
    ## is there a .RProfile file?
    profileLines = readLines(rProfilePath)# get rProfile
  }
  ## add new line to top of file
  profileLines = c(newProfileLines, profileLines)
  writeLines(profileLines, rProfilePath)
  .rs.restartR()  ## run & wait for R session to resume 
                  ## (at least 15 seconds) before continuing
}

#### STEP 4:  Update "r-reticulate" CONDA ENVIRONMENT
####          FOR TENSORFLOW
## Install the specific versions of modules
## for the TensorFlow installation via CONDA.
## this line takes a few minutes to execute
reticulate::conda_install(envname = "r-reticulate",
                          packages =
                            c(
                              "python=3.7",
                              "tensorflow=1.14",
                              "pyyaml",
                              "requests",
                              "Pillow",
                              "pip",
                              "numpy=1.16",
                              "h5py=2.8",
                              "tensorflow-probability=0.7"
                            ))

#### STEP 4:  TEST THE INSTALLATION
library(greta)  ## should work now
library(causact)
graph = dag_create() %>%
  dag_node("Normal RV",
           rhs =normal(0,10))
graph %>% dag_render()  ## see oval
drawsDF = graph %>% dag_greta() ## see "running X chains..."
drawsDF %>% dagp_plot(densityPlot = TRUE)  ## see plot

Dear Adam, thank you so much: this works great for me, under Windows. I will try it under Linux in the days to come. Yet, it does not solve a pending issue that I posted here: Problem with coding a new probability distribution in Greta. I supect this is related on how R6/greta and tensorflow are interconnected, but I can’t solve it. If you see how to solve this, this would be very fine. Sincerecly, Frédéric

Glad it worked. Thanks for the feedback. In terms of your other issue, I am probably not very useful, but will respond with something that might help in that thread.

Best,
Adam

Dear Adam, actually the installation under Linux did not work. It seemed to work for the 4 first steps but then when I tried the last commands I had an error:

drawsDF = graph %>% dag_greta() ## see “running X chains…”
Couldn’t find a suitable web browser!
Set the BROWSER environment variable to your desired browser.
Warning: program returned non-zero exit code #1
/usr/bin/xdg-open: 870: /usr/bin/xdg-open: www-browser: not found
/usr/bin/xdg-open: 870: /usr/bin/xdg-open: links2: not found
/usr/bin/xdg-open: 870: /usr/bin/xdg-open: elinks: not found
/usr/bin/xdg-open: 870: /usr/bin/xdg-open: links: not found
/usr/bin/xdg-open: 870: /usr/bin/xdg-open: lynx: not found
/usr/bin/xdg-open: 870: /usr/bin/xdg-open: w3m: not found
xdg-open: no method available for opening ‘/tmp/RtmpLS92vH/viewhtml3bf04e04d9f9/index.html’
Error in py_call_impl(callable, dots$args, dots$keywords) :
RuntimeError: Evaluation error: ValueError: Tensor(“leapfrog_integrate_one_step/maybe_call_fn_and_grads/value_and_gradients/strided_slice:0”, shape=(), dtype=int32) must be from the same graph as Tensor(“mcmc_sample_chain/trace_scan/while/Const:0”, shape=(1,), dtype=float64) (graphs are FuncGraph(name=mh_one_step_hmc_kernel_one_step_leapfrog_integrate_while_body_364, id=140075058233232) and <tensorflow.python.framework.ops.Graph object at 0x7f65c64ffcd0>).

Detailed traceback:
File “/home/irstea.priv/frederic.gosselin/.local/share/r-miniconda/envs/r-reticulate/lib/python3.7/site-packages/tensorflow/python/ops/gen_array_ops.py”, line 11468, in tile
“Tile”, input=input, multiples=multiples, name=name)
File “/home/irstea.priv/frederic.gosselin/.local/share/r-miniconda/envs/r-reticulate/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py”, line 330, in _apply_op_helper
g = ops._get_graph_from_inputs(_Flatten(keywords.values()))
File "/home/

Sincerely, Frédéric

Thanks for testing this. This Linux debugging is beyond my abilities. Maybe someone else on the forum can help us. :slight_smile:

This script does not seem to work for Linux or Mac distributions. I would stick to the longer install instructions of here: https://www.causact.com/install-tensorflow-greta-and-causact.html#install-tensorflow-greta-and-causact

If anyone can fix the above script so it works robustly on a MAC as well, that would be greatly appreciated. Thanks.

Dear Adam,
actually there seems to have a small efficiency problem with the windows installation above: when I run the greta model, I get the following message:
2021-04-12 17:49:03.619930: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2

This might mean that Tensorflow is not as efficient as it could be. It there a way to control the precise build of tensorflow such as here: https://github.com/fo40225/tensorflow-windows-wheel?

Thank you very much.

All the best,

Frédéric

Tuning a Tensorflow installation to the specifics of your machine is quite challenging to do and I would not recommend it. The message you see is pretty standard for PC installs and I am not sure how much efficiency is being sacrificed as a result.


For other readings this post, please use the update install instructions here: Installing greta - new script to overcome reticulate's auto_configure