-
Notifications
You must be signed in to change notification settings - Fork 12
/
sub-.tensorflow.tensor.qmd
123 lines (90 loc) · 3.13 KB
/
sub-.tensorflow.tensor.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
---
execute:
freeze: true
---
[R/extract.R](https://github.com/rstudio/tensorflow//blob/main/R/extract.R#L89)
# [.tensorflow.tensor
## Subset tensors with `[`
## Description
Subset tensors with `[`
## Usage
```r
## S3 method for class 'tensorflow.tensor'
[(
x,
...,
drop = TRUE,
style = getOption("tensorflow.extract.style"),
options = tf_extract_opts(style)
)
```
## Arguments
|Arguments|Description|
|---|---|
| x | Tensorflow tensor |
| ... | slicing specs. See examples and details. |
| drop | whether to drop scalar dimensions |
| style | One of `"python"` or `"R"`. |
| options | An object returned by `tf_extract_opts()` |
## Examples
```{r, eval=ecodown::examples_not_run()}
library(tensorflow)
x <- as_tensor(array(1:15, dim = c(3, 5)))
x
# by default, numerics supplied to [...] are interpreted R style
x[,1] # first column
x[1:2,] # first two rows
x[,1, drop = FALSE] # 1 column matrix
# strided steps can be specified in R syntax or python syntax
x[, seq(1, 5, by = 2)]
x[, 1:5:2]
# if you are unfamiliar with python-style strided steps, see:
# https://numpy.org/doc/stable/reference/arrays.indexing.html#basic-slicing-and-indexing
# missing arguments for python syntax are valid, but they must by backticked
# or supplied as NULL
x[, `::2`]
x[, NULL:NULL:2]
x[, `2:`]
# all_dims() expands to the shape of the tensor
# (equivalent to a python ellipsis `...`)
# (not to be confused with R dots `...`)
y <- as_tensor(array(1:(3^5), dim = c(3,3,3,3,3)))
all.equal(y[all_dims(), 1],
y[, , , , 1])
# tf$newaxis are valid (equivalent to a NULL)
x[,, tf$newaxis]
x[,, NULL]
# negative numbers are always interpreted python style
# The first time a negative number is supplied to `[`, a warning is issued
# about the non-standard behavior.
x[-1,] # last row, with a warning
x[-1,] # the warning is only issued once
# specifying `style = 'python'` changes the following:
# + zero-based indexing is used
# + slice sequences in the form of `start:stop` do not include `stop`
# in the returned value
# + out-of-bounds indices in a slice are valid
# The style argument can be supplied to individual calls of `[` or set
# as a global option
# example of zero based indexing
x[0, , style = 'python'] # first row
x[1, , style = 'python'] # second row
# example of slices with exclusive stop
options(tensorflow.extract.style = 'python')
x[, 0:1] # just the first column
x[, 0:2] # first and second column
# example of out-of-bounds index
x[, 0:10]
options(tensorflow.extract.style = NULL)
# slicing with tensors is valid too, but note, tensors are never
# translated and are always interpreted python-style.
# A warning is issued the first time a tensor is passed to `[`
x[, tf$constant(0L):tf$constant(2L)]
# just as in python, only scalar tensors are valid
# https://www.tensorflow.org/api_docs/python/tf/Tensor#__getitem__
# To silence the warnings about tensors being passed as-is and negative numbers
# being interpreted python-style, set
options(tensorflow.extract.style = 'R')
# clean up from examples
options(tensorflow.extract.style = NULL)
```