greta-dev / greta-dev/greta

speed up greta:::tf_extract() and greta:::tf_replace()

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

Description

greta's extraction (e.g. `a <- x[2]`) and replacement (e.g. `x[2] <- 0`) syntax uses the internal tensorflow functions `tf_extract()` and `tf_replace()` to do the operations on tensors, with a shim to map R's extract/replace syntax onto TensorFlow's. This shim doesn't always use the most efficient operations for common extraction methods.

For example:
`x[2, ]` could use `tf$slice()`, which might be more efficient than the current general approach or reshaping to a vector, using `tf$gather()` and then reshaping to a matrix.

`x[2, ] <- 0` could use `tf$tensor_scatter_nd_update()`, which would probably be much more efficient than the current (`greta:::tf_recombine()`) approach of flattening the vector, breaking it up into vectors, replacing some, and then recombining them with `tf$concat()` before reshaping the vector into a matrix 😓.

This would particularly help when people write for loops to that alter elements in matrices (e.g. for timeseries models), and reduce the need for nasty hacks like storing the iterated components in lists.

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.