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

module 'tensorflow' has no attribute 'contrib' #32

Closed
njtierney opened this issue Nov 6, 2024 · 3 comments
Closed

module 'tensorflow' has no attribute 'contrib' #32

njtierney opened this issue Nov 6, 2024 · 3 comments

Comments

@njtierney
Copy link
Contributor

First reported here https://forum.greta-stats.org/t/odes-and-config-attribute-in-tensorflow/366

This appears to be an issue with using the contrib slot in tensorflow, specifically in greta.dynamics, probably here:

tf_int <- tf$contrib$integrate

It is likely just an API change, but here is the reprex:

library(greta.dynamics)
#> Loading required package: greta
#> 
#> Attaching package: 'greta'
#> The following objects are masked from 'package:stats':
#> 
#>     binomial, cov2cor, poisson
#> The following objects are masked from 'package:base':
#> 
#>     %*%, apply, backsolve, beta, chol2inv, colMeans, colSums, diag,
#>     eigen, forwardsolve, gamma, identity, rowMeans, rowSums, sweep,
#>     tapply
greta_sitrep()
#> ℹ checking if python available
#> ✔ python (v3.10) available
#> 
#> ℹ checking if TensorFlow available
#> ✔ TensorFlow (v2.15.0) available
#> 
#> ℹ checking if TensorFlow Probability available
#> ✔ TensorFlow Probability (v0.23.0) available
#> 
#> ℹ checking if greta conda environment available
#> ✔ greta conda environment available
#> 
#> ℹ Initialising python and checking dependencies, this may take a moment.
#> ✔ Initialising python and checking dependencies ... done!
#> 
#> ℹ greta is ready to use!
# replicate the Lotka-Volterra example from deSolve
library(deSolve)
LVmod <- function(Time, State, Pars) {
  with(as.list(c(State, Pars)), {
    Ingestion <- rIng * Prey * Predator
    GrowthPrey <- rGrow * Prey * (1 - Prey / K)
    MortPredator <- rMort * Predator
    
    dPrey <- GrowthPrey - Ingestion
    dPredator <- Ingestion * assEff - MortPredator
    
    return(list(c(dPrey, dPredator)))
  })
}

pars <- c(
  rIng = 0.2, # /day, rate of ingestion
  rGrow = 1.0, # /day, growth rate of prey
  rMort = 0.2, # /day, mortality rate of predator
  assEff = 0.5, # -, assimilation efficiency
  K = 10
) # mmol/m3, carrying capacity

yini <- c(Prey = 1, Predator = 2)
times <- seq(0, 30, by = 1)
out <- ode(yini, times, LVmod, pars)

# simulate observations
jitter <- rnorm(2 * length(times), 0, 0.1)
y_obs <- out[, -1] + matrix(jitter, ncol = 2)

# fit a greta model to infer the parameters from this simulated data

# greta version of the function
lotka_volterra <- function(y, t, rIng, rGrow, rMort, assEff, K) {
  Prey <- y[1, 1]
  Predator <- y[1, 2]
  
  Ingestion <- rIng * Prey * Predator
  GrowthPrey <- rGrow * Prey * (1 - Prey / K)
  MortPredator <- rMort * Predator
  
  dPrey <- GrowthPrey - Ingestion
  dPredator <- Ingestion * assEff - MortPredator
  
  cbind(dPrey, dPredator)
}

# priors for the parameters
rIng <- uniform(0, 2) # /day, rate of ingestion
rGrow <- uniform(0, 3) # /day, growth rate of prey
rMort <- uniform(0, 1) # /day, mortality rate of predator
assEff <- uniform(0, 1) # -, assimilation efficiency
K <- uniform(0, 30) # mmol/m3, carrying capacity

# initial values and observation error
y0 <- uniform(0, 5, dim = c(1, 2))
obs_sd <- uniform(0, 1)

# solution to the ODE
y <- ode_solve(lotka_volterra, y0, times, rIng, rGrow, rMort, assEff, K)

# sampling statement/observation model
distribution(y_obs) <- normal(y, obs_sd)

# we can use greta to solve directly, for a fixed set of parameters (the true
# ones in this case)
values <- c(
  list(y0 = t(1:2)),
  as.list(pars)
)
vals <- calculate(y, values = values)[[1]]
#> module 'tensorflow' has no attribute 'contrib'

Created on 2024-11-06 with reprex v2.1.1

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.4.1 Patched (2024-07-08 r86915)
#>  os       macOS Sonoma 14.5
#>  system   aarch64, darwin20
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       Australia/Hobart
#>  date     2024-11-06
#>  pandoc   3.2 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/aarch64/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package        * version  date (UTC) lib source
#>  abind            1.4-5    2016-07-21 [1] CRAN (R 4.4.0)
#>  backports        1.5.0    2024-05-23 [1] CRAN (R 4.4.0)
#>  base64enc        0.1-3    2015-07-28 [1] CRAN (R 4.4.0)
#>  callr            3.7.6    2024-03-25 [1] CRAN (R 4.4.0)
#>  cli              3.6.3    2024-06-21 [1] CRAN (R 4.4.0)
#>  coda             0.19-4.1 2024-01-31 [1] CRAN (R 4.4.0)
#>  codetools        0.2-20   2024-03-31 [2] CRAN (R 4.4.1)
#>  crayon           1.5.3    2024-06-20 [1] CRAN (R 4.4.0)
#>  deSolve        * 1.40     2023-11-27 [1] CRAN (R 4.4.0)
#>  digest           0.6.37   2024-08-19 [1] CRAN (R 4.4.1)
#>  evaluate         1.0.1    2024-10-10 [1] CRAN (R 4.4.1)
#>  fansi            1.0.6    2023-12-08 [1] CRAN (R 4.4.0)
#>  fastmap          1.2.0    2024-05-15 [1] CRAN (R 4.4.0)
#>  fs               1.6.5    2024-10-30 [1] CRAN (R 4.4.1)
#>  future           1.34.0   2024-07-29 [1] CRAN (R 4.4.0)
#>  globals          0.16.3   2024-03-08 [1] CRAN (R 4.4.0)
#>  glue             1.8.0    2024-09-30 [1] CRAN (R 4.4.1)
#>  greta          * 0.5.0    2024-11-06 [1] local
#>  greta.dynamics * 0.2.1    2024-10-15 [1] https://greta-dev.r-universe.dev (R 4.4.1)
#>  hms              1.1.3    2023-03-21 [1] CRAN (R 4.4.0)
#>  htmltools        0.5.8.1  2024-04-04 [1] CRAN (R 4.4.0)
#>  jsonlite         1.8.9    2024-09-20 [1] CRAN (R 4.4.1)
#>  knitr            1.48     2024-07-07 [1] CRAN (R 4.4.0)
#>  lattice          0.22-6   2024-03-20 [2] CRAN (R 4.4.1)
#>  lifecycle        1.0.4    2023-11-07 [1] CRAN (R 4.4.0)
#>  listenv          0.9.1    2024-01-29 [1] CRAN (R 4.4.0)
#>  magrittr         2.0.3    2022-03-30 [1] CRAN (R 4.4.0)
#>  Matrix           1.7-0    2024-04-26 [2] CRAN (R 4.4.1)
#>  parallelly       1.38.0   2024-07-27 [1] CRAN (R 4.4.0)
#>  pillar           1.9.0    2023-03-22 [1] CRAN (R 4.4.0)
#>  pkgconfig        2.0.3    2019-09-22 [1] CRAN (R 4.4.0)
#>  png              0.1-8    2022-11-29 [1] CRAN (R 4.4.0)
#>  prettyunits      1.2.0    2023-09-24 [1] CRAN (R 4.4.0)
#>  processx         3.8.4    2024-03-16 [1] CRAN (R 4.4.0)
#>  progress         1.2.3    2023-12-06 [1] CRAN (R 4.4.0)
#>  ps               1.8.1    2024-10-28 [1] CRAN (R 4.4.1)
#>  R6               2.5.1    2021-08-19 [1] CRAN (R 4.4.0)
#>  Rcpp             1.0.13-1 2024-11-02 [1] CRAN (R 4.4.1)
#>  reprex           2.1.1    2024-07-06 [1] CRAN (R 4.4.0)
#>  reticulate       1.38.0   2024-06-19 [1] CRAN (R 4.4.0)
#>  rlang            1.1.4    2024-06-04 [1] CRAN (R 4.4.0)
#>  rmarkdown        2.28     2024-08-17 [1] CRAN (R 4.4.0)
#>  rstudioapi       0.16.0   2024-03-24 [1] CRAN (R 4.4.0)
#>  sessioninfo      1.2.2    2021-12-06 [1] CRAN (R 4.4.0)
#>  tensorflow       2.16.0   2024-04-15 [1] CRAN (R 4.4.0)
#>  tfautograph      0.3.2    2021-09-17 [1] CRAN (R 4.4.0)
#>  tfruns           1.5.3    2024-04-19 [1] CRAN (R 4.4.0)
#>  utf8             1.2.4    2023-10-22 [1] CRAN (R 4.4.0)
#>  vctrs            0.6.5    2023-12-01 [1] CRAN (R 4.4.0)
#>  whisker          0.4.1    2022-12-05 [1] CRAN (R 4.4.0)
#>  withr            3.0.2    2024-10-28 [1] CRAN (R 4.4.1)
#>  xfun             0.49     2024-10-31 [1] CRAN (R 4.4.1)
#>  yaml             2.3.10   2024-07-26 [1] CRAN (R 4.4.0)
#> 
#>  [1] /Users/nick/Library/R/arm64/4.4/library
#>  [2] /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library
#> 
#> ─ Python configuration ───────────────────────────────────────────────────────
#>  python:         /Users/nick/Library/r-miniconda-arm64/envs/greta-env-tf2/bin/python
#>  libpython:      /Users/nick/Library/r-miniconda-arm64/envs/greta-env-tf2/lib/libpython3.10.dylib
#>  pythonhome:     /Users/nick/Library/r-miniconda-arm64/envs/greta-env-tf2:/Users/nick/Library/r-miniconda-arm64/envs/greta-env-tf2
#>  version:        3.10.14 | packaged by conda-forge | (main, Mar 20 2024, 12:51:49) [Clang 16.0.6 ]
#>  numpy:          /Users/nick/Library/r-miniconda-arm64/envs/greta-env-tf2/lib/python3.10/site-packages/numpy
#>  numpy_version:  1.26.4
#>  tensorflow:     /Users/nick/Library/r-miniconda-arm64/envs/greta-env-tf2/lib/python3.10/site-packages/tensorflow
#>  
#>  NOTE: Python version was forced by use_python() function
#> 
#> ──────────────────────────────────────────────────────────────────────────────

And the output of reticulate::py_last_error()

> reticulate::py_last_error()

── Python Exception Message ─────────────────────────────────────────────────────────────────────────
AttributeError: module 'tensorflow' has no attribute 'contrib'

── R Traceback ──────────────────────────────────────────────────────────────────────────────────────
     ▆
  1. └─greta::calculate(y, values = values)
  2.   ├─base::with(...) at greta/R/calculate.R:163:3
  3.   ├─reticulate:::with.python.builtin.object(...)
  4.   │ ├─base::tryCatch(...)
  5.   │ │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
  6.   │ └─base::force(expr)
  7.   └─greta:::calculate_list(...) at greta/R/calculate.R:252:7
  8.     └─greta:::calculate_target_tensor_list(...) at greta/R/calculate.R:428:3
  9.       └─dag$define_tf(target_nodes = target_nodes) at greta/R/calculate.R:499:3
 10.         └─self$define_tf_body(target_nodes = target_nodes) at greta/R/dag_class.R:374:7
 11.           └─base::lapply(...) at greta/R/dag_class.R:349:7
 12.             └─greta (local) FUN(X[[i]], ...)
 13.               └─x$define_tf(self) at greta/R/dag_class.R:351:9
 14.                 └─self$tf(dag) at greta/R/node_class.R:211:9
 15.                   ├─base::do.call(operation, tf_args) at greta/R/node_types.R:192:9
 16.                   └─greta.dynamics (local) `<fn>`(...)
 17.                     ├─tf$contrib
 18.                     └─reticulate:::`$.python.builtin.module`(tf, "contrib")
 19.                       └─reticulate::py_get_attr(x, name, FALSE)
See `reticulate::py_last_error()$r_trace$full_call` for more details.
@goldingn
Copy link
Member

goldingn commented Nov 6, 2024

Just flagging that there's a greta_2 branch of greta.dynamics. That might be the solution if this error is due to the main greta.dynamics (which I think is for TF1 greta) being run with TF2 greta.

@njtierney
Copy link
Contributor Author

Thanks, Nick! I'll make sure to branch changes off from that TF2 branch.

Currently the issue seems to be

tf_int <- tf$contrib$integrate

Just trying to track down if they've left some backwards compatibility in there somewhere - so far the best I've come up with is the ODE solver in TFP: https://www.tensorflow.org/probability/api_docs/python/tfp/math/ode/Solver

But nothing that is just a drop-in

@njtierney
Copy link
Contributor Author

Resolved in #33

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