greta-dev / greta-dev/greta

add for loop-type constructor

Open
#210 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
607
Forks
67
Avg merge
3d 8h
Merged PRs (30d)
1

Description

Lots of people want for loop functionality for models, but they are very slow to define and execute. There are more efficient representations for each of these (e.g. vectorisation/linear algebra or adding an iteration-type method for dynamical models), but not all. It would be useful to have functionality for for loop -like code that uses Tensorflow's while loop syntax. Something like:

```r
fun <- function (i, arglist) {
arglist$x <- i ^ abs(arglist$x)
arglist
}

a <- normal(0, 1)
out <- loop(i = 1:10, fun, x = a)
```
I.e. the interface could be quite similar to lapply, but would behave cumulatively, updating arguments in the arglist. It would be equivalent to the R for loop:
```r
out <- list <- list(x = a)
for (i in 1:10) {
list <- fun(i, list)
out <- mapply(c, out, list, SIMPLIFY = FALSE)
}
```
I.e. returning a list, with vectors for the replicated elements (each with length 10 in this case).

This could use the same internal function creation trick as the ODE gist to create a tensorflow function, then map it to `tf.while_loop()` internally. Rather than spooling out the for loop into a really complex greta DAG/TF graph, this would iterate over the same piece of graph on the tensorflow side.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.