Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Installation Issue: tensor flow in rocker/ml docker image, ModuleNotFoundError: No module named 'tensorflow' #612

Closed
sneumann opened this issue Dec 5, 2024 · 1 comment

Comments

@sneumann
Copy link

sneumann commented Dec 5, 2024

[thanks @t-kalinowski for your response, I was since editing my issue and adding the workaround]
Hi,
I am installing (and testing) tensorflow using

R -e 'install.packages("reticulate"); remotes::install_github("rstudio/tensorflow", upgrade="never"); library(tensorflow); install.packages("keras"); library(keras); install_keras(); model.sla <- keras_model_sequential(input_shape = c(NULL, 12906, 1))'

This works nicely in an older rocker/rstudio image based on Ubuntu22.04.4, with R-python 3.10.12, R-4.4.1
This fails on the rocker/ml images based on Ubuntu22.04.3 with nvidia cuda stuff installed, also with R-python 3.10.12 and R-4.4.1

I have rocker-org/rocker-versioned2#880 as initially I thought that would be a rocker/ml issue.
I have a workaround now to install as shown above, and for usage

## Does not work:
R -e 'tensorflow::as_tensor("Hello World")' ; 

## Workaround:
R -e 'reticulate::use_virtualenv("/root/.virtualenvs/r-tensorflow", required = TRUE); library(tensorflow); tensorflow::as_tensor("Hello World")'

So all left now is possibly to add your suggestions to https://tensorflow.rstudio.com/install/

I also leave below my output from debugging. I was curious about the error for tf_config(),
this comes because python_version can be print()ed, but not cat()ed.

Yours,
Steffen

> reticulate::py_config()
python:         /root/.virtualenvs/r-tensorflow/bin/python
libpython:      /usr/lib/python3.10/config-3.10-x86_64-linux-gnu/libpython3.10.so
pythonhome:     /root/.virtualenvs/r-tensorflow:/root/.virtualenvs/r-tensorflow
version:        3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0]
numpy:          /root/.virtualenvs/r-tensorflow/lib/python3.10/site-packages/numpy
numpy_version:  1.26.4
# R -e 'reticulate::use_virtualenv("/root/.virtualenvs/r-tensorflow", required = TRUE); library(tensorflow);  tensorflow::tf_config()'
> tensorflow::tf_config()
TensorFlow v2.15.1 ()
Python vError in cat("Python v", x$python_version, " (", aliased(x$python), ")\n",  : 
  argument 2 (type 'list') cannot be handled by 'cat'

Digging into the tf_config() error above:

> cat(tensorflow::tf_config()$python_version)
Error in cat(tensorflow::tf_config()$python_version) : 
  argument 1 (type 'list') cannot be handled by 'cat'
> print(tensorflow::tf_config()$python_version)
[1] ‘3.10’
> str(tensorflow::tf_config()$python_version)
Classes 'package_version', 'numeric_version'  hidden list of 1
 $ : int [1:2] 3 10
@t-kalinowski
Copy link
Member

t-kalinowski commented Dec 5, 2024

[Sorry, premature send, there was a new-style GitHub send button, which I mis-interpreted as "save draft". I'll update and polish this issue, until then please consider as WIP]

At a glance, it seems this is a "reticulate is finding the wrong Python installation" issue. Consulting https://rstudio.github.io/reticulate/articles/versions.html#order-of-discovery is usually the quickest way to resolve these.

Also, it looks like you're using {keras} with the latest tensorflow. Note that the latest tensorflow depends on {keras3}, and new code today should generally prefer to use {keras3}. If you really want to use "legacy" keras with the latest tensorflow, you have to take some extra steps (py_install("tf-keras"); Sys.setenv(TF_USE_LEGACY_KERAS=1) probably some other steps too (not tested, but it wouldn't surprise me if import("tf_keras", as = "keras") is also needed).

@sneumann sneumann closed this as completed Dec 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants