Created
October 22, 2018 23:40
-
-
Save jdyen/9e17ae021cb246eda2060e91dd3ee30c to your computer and use it in GitHub Desktop.
tf function for rep(x, times) with times as a tensor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tf_expand <- function(x, repeats) { | |
x <- tf$constant(x, dtype = tf$float64) | |
repeats <- tf$constant(repeats, dtype = tf$int32) | |
shape <- tf$reduce_sum(repeats) | |
idx <- tf$concat(c(tf$constant(0L, dtype = tf$int32)[tf$newaxis], | |
tf$cumsum(repeats[0L:(tf$size(repeats) - 1L)])), | |
axis = 0L) | |
tmp_index <- tf$concat(c(tf$constant(0, dtype = tf$float64)[tf$newaxis], | |
x[0L:(tf$size(x) - 1L)]), axis = 0L) | |
y <- tf$sparse_to_dense( | |
sparse_indices = idx, | |
output_shape = shape[tf$newaxis], | |
sparse_values = x - tf$concat(c(tf$constant(0, dtype = tf$float64)[tf$newaxis], | |
x[0L:(tf$size(x) - 1L)]), axis = 0L) | |
) | |
tf$cumsum(y) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment