Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions C1-TensorFlow.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,14 @@ microbenchmark::microbenchmark(
```{r chunk_chapter3_task_12, eval=TRUE, include=TRUE}
do_something_TF = function(x = matrix(0.0, 10L, 10L)){
x = tf$constant(x) # Remember, this is a local copy!
mean_per_row = tf$reduce_mean(x, axis = 0L)
result = x - mean_per_row
return(result)
mean_per_row = tf$reduce_mean(x, axis = 1L)
result = tf$transpose(x) - mean_per_row
return(tf$transpose(result))
}
```

Short explanation: A 2 dimensional tensor in tensorflow can be seen as an array of arrays. E.g. a 20x30 (rows x columns) matrix is an array which consists of 20 arrays of length 30. If we write result = matrix - vector, tensorflow tries to subtract from every inner array of the matrix (so from every row) the vector. Therefore if the want to substract the first number of mean_per_row from the first column of the matrix, we have to transpose this matrix, do the subtraction and then of course transpose it back to the original dimensions. Alternativly one could transform the vector to an matrix of size 20x1: `result = x - tf$reshape(mean_per_row, shape = list(20L,1L))` and then omit the second transpose in the return statement. You could now also try to guess, which one of the two is faster and then check that!

```{r chunk_chapter3_task_13, eval=TRUE, include=TRUE}
test = matrix(0.0, 100L, 100L)
microbenchmark::microbenchmark(do_something_R(test), do_something_TF(test))
Expand Down Expand Up @@ -1067,4 +1069,3 @@ cat("Original intercept: ", intercept, "\n")

`r unhide()`
:::