Skip to content

Commit 43a85d5

Browse files
committed
[DOCS] Updated the 'Tensors' guide in the website.
1 parent 6fca645 commit 43a85d5

File tree

2 files changed

+93
-42
lines changed

2 files changed

+93
-42
lines changed

docs/src/main/paradox/guides/tensors.md

Lines changed: 64 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ VARIANT // Variant
7373

7474
TensorFlow Scala also provides value classes for the types
7575
that are not natively supported by Scala (e.g.,
76-
@scaladoc[UByte](org.platanios.tensorflow.api.types.UByte)
76+
@scaladoc[UByte](org.platanios.tensorflow.api.core.types.UByte)
7777
corresponds to `UINT8`).
7878

7979
It is also possible to cast tensors from one data type to
@@ -98,11 +98,50 @@ which stands for *TensorFlow Imperative*
9898

9999
@@@
100100

101-
## Rank
101+
## Shape
102+
103+
The shape of a tensor is the number of elements it contains
104+
in each dimension. The TensorFlow documentation uses two
105+
notational conventions to describe tensor dimensionality:
106+
*rank*, and *shape*. The following table shows how these
107+
relate to one another:
108+
109+
| Rank | Shape | Example |
110+
|:-----|:-------------------|:----------------------------------------|
111+
| 0 | [] | A 0-D tensor. A scalar. |
112+
| 1 | [D0] | A 1-D tensor with shape [5]. |
113+
| 2 | [D0, D1] | A 2-D tensor with shape [3, 4]. |
114+
| 3 | [D0, D1, D2] | A 3-D tensor with shape [1, 4, 3]. |
115+
| n | [D0, D1, ... Dn-1] | A tensor with shape [D0, D1, ... Dn-1]. |
116+
117+
Shapes can be automatically converted to integer tensors,
118+
if necessary.
119+
120+
@@@ note
121+
122+
Shapes are automatically converted to `Tensor[Int]` and not
123+
`Tensor[Long]` in order to improve performance when working
124+
with GPUs. The reason is that TensorFlow treats integer
125+
tensors in a special manner, if they are placed on GPUs,
126+
assuming that they represent shapes.
127+
128+
@@@
129+
130+
For example:
131+
132+
@@snip [Tensors.scala](/docs/src/main/scala/Tensors.scala) { #tensor_shape_examples }
102133

103-
The rank of a [`Tensor`][tensor] is its number of dimensions. Synonyms for rank include order or degree or
104-
`n`-dimension. Note that rank in TensorFlow is not the same as matrix rank in mathematics. As the following table shows,
105-
each rank in TensorFlow corresponds to a different mathematical entity:
134+
The shape of a tensor can be inspected using:
135+
136+
@@snip [Tensors.scala](/docs/src/main/scala/Tensors.scala) { #tensor_inspect_shape_example }
137+
138+
### Rank
139+
140+
The rank of of a tensor is its number of dimensions.
141+
Synonyms for rank include order or degree or `n`-dimension.
142+
Note that rank in TensorFlow is not the same as matrix rank
143+
in mathematics. As the following table shows, each rank in
144+
TensorFlow corresponds to a different mathematical entity:
106145

107146
| Rank | Math Entity |
108147
|:-----|:---------------------------------|
@@ -112,50 +151,33 @@ each rank in TensorFlow corresponds to a different mathematical entity:
112151
| 3 | 3-Tensor (cube of numbers) |
113152
| n | n-Tensor (you get the idea) |
114153

115-
For example:
116-
```tut:silent
117-
val t0 = Tensor.ones(INT32, Shape()) // Creates a scalar equal to the value 1
118-
val t1 = Tensor.ones(INT32, Shape(10)) // Creates a vector with 10 elements, all of which are equal to 1
119-
val t2 = Tensor.ones(INT32, Shape(5, 2)) // Creates a matrix with 5 rows with 2 columns
120-
121-
// You can also create tensors in the following way:
122-
val t3 = Tensor(2.0, 5.6) // Creates a vector that contains the numbers 2.0 and 5.6
123-
val t4 = Tensor(Tensor(1.2f, -8.4f), Tensor(-2.3f, 0.4f)) // Creates a matrix with 2 rows and 2 columns
124-
```
125-
126-
A rank of a tensor can be obtained in one of two ways:
127-
```tut:silent
128-
t4.rank // Returns the value 2
129-
tfi.rank(t4) // Also returns the value 2
130-
```
131-
132-
## Shape
154+
The rank of a tensor can be inspected using:
133155

156+
@@snip [Tensors.scala](/docs/src/main/scala/Tensors.scala) { #tensor_inspect_rank_example }
134157

135158
## Indexing / Slicing
136159

137-
Similar to NumPy, tensors can be indexed/sliced in various ways:
160+
Similar to NumPy, tensors can be indexed/sliced in various
161+
ways. An indexer can be one of:
138162

139-
An indexer can be one of:
140-
- `Ellipsis`: Corresponds to a full slice over multiple dimensions of a tensor. Ellipses are used to represent
141-
zero or more dimensions of a full-dimension indexer sequence.
142-
- `NewAxis`: Corresponds to the addition of a new dimension.
143-
- `Slice`: Corresponds to a slice over a single dimension of a tensor.
163+
- `Ellipsis`: Full slice over multiple dimensions of a
164+
tensor. Ellipses are used to represent zero or more
165+
dimensions of a full-dimension indexer sequence.
166+
- `NewAxis`: Addition of a new dimension.
167+
- `Slice`: Slice over a single dimension of a tensor.
144168

145-
Examples of constructing and using indexers are provided in the `Ellipsis` and the `Slice` class documentation.
146-
Here we provide examples of indexing over tensors using indexers:
169+
Examples of constructing and using indexers are provided in
170+
the @scaladoc[Ellipsis](org.platanios.tensorflow.api.core.Ellipsis)
171+
and the @scaladoc[Slice](org.platanios.tensorflow.api.core.Slice)
172+
documentation. Here we provide examples of indexing over
173+
tensors using indexers:
147174

148-
```scala
149-
val t = Tensor.zeros[Float](Shape(4, 2, 3, 8))
150-
t(::, ::, 1, ::) // Tensor with shape [4, 2, 1, 8]
151-
t(1 :: -2, ---, 2) // Tensor with shape [1, 2, 3, 1]
152-
t(---) // Tensor with shape [4, 2, 3, 8]
153-
t(1 :: -2, ---, NewAxis, 2) // Tensor with shape [1, 2, 3, 1, 1]
154-
t(1 ::, ---, NewAxis, 2) // Tensor with shape [3, 2, 3, 1, 1]
155-
```
175+
@@snip [Tensors.scala](/docs/src/main/scala/Tensors.scala) { #tensor_indexer_examples }
156176

157177
where `---` corresponds to an ellipsis.
158178

159-
Note that each indexing sequence is only allowed to contain at most one Ellipsis. Furthermore, if an ellipsis is not
160-
provided, then one is implicitly appended at the end of indexing sequence. For example, `foo(2 :: 4)` is equivalent to
161-
`foo(2 :: 4, ---)`.
179+
Note that each indexing sequence is only allowed to contain
180+
at most one ellipsis. Furthermore, if an ellipsis is not
181+
provided, then one is implicitly appended at the end of the
182+
indexing sequence. For example, `foo(2 :: 4)` is equivalent
183+
to `foo(2 :: 4, ---)`.

docs/src/main/scala/Tensors.scala

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import org.platanios.tensorflow.api._
22

3+
import scala.language.postfixOps
4+
35
object Tensors {
46
// #zeros_tensor_example
57
val tensor = Tensor.zeros[Int](Shape(2, 5))
@@ -33,4 +35,31 @@ object Tensors {
3335
// #tensor_datatype_example
3436
floatTensor.dataType // Returns FLOAT32
3537
// #tensor_datatype_example
38+
39+
// #tensor_shape_examples
40+
val t0 = Tensor.ones[Int](Shape()) // Creates a scalar equal to the value 1
41+
val t1 = Tensor.ones[Int](Shape(10)) // Creates a vector with 10 elements, all of which are equal to 1
42+
val t2 = Tensor.ones[Int](Shape(5, 2)) // Creates a matrix with 5 rows with 2 columns
43+
44+
// You can also create tensors in the following way:
45+
val t3 = Tensor(2.0, 5.6) // Creates a vector that contains the numbers 2.0 and 5.6
46+
val t4 = Tensor(Tensor(1.2f, -8.4f), Tensor(-2.3f, 0.4f)) // Creates a matrix with 2 rows and 2 columns
47+
// #tensor_shape_examples
48+
49+
// #tensor_inspect_shape_example
50+
t4.shape // Returns the value Shape(2, 2)
51+
// #tensor_inspect_shape_example
52+
53+
// #tensor_inspect_rank_example
54+
t4.rank // Returns the value 2
55+
// #tensor_inspect_rank_example
56+
57+
// #tensor_indexer_examples
58+
val t = Tensor.zeros[Float](Shape(4, 2, 3, 8))
59+
t(::, ::, 1, ::) // Tensor with shape [4, 2, 1, 8]
60+
t(1 :: -2, ---, 2) // Tensor with shape [1, 2, 3, 1]
61+
t(---) // Tensor with shape [4, 2, 3, 8]
62+
t(1 :: -2, ---, NewAxis, 2) // Tensor with shape [1, 2, 3, 1, 1]
63+
t(1 ::, ---, NewAxis, 2) // Tensor with shape [3, 2, 3, 1, 1]
64+
// #tensor_indexer_examples
3665
}

0 commit comments

Comments
 (0)