22
33## Prerequisites
44
5- You will need to know a bit of Elixir. For a refresher, check out the
5+ To properly use Nx, you will need to know a bit of Elixir. For a refresher, check out the
66[ Elixir Getting Started Guide] ( https://hexdocs.pm/elixir/introduction.html ) .
77
8- To work the examples you can run using the livebook buttom in this page.
8+ To work on the examples you can run using the "Run in Livebook" button in this page.
99
1010#### Learning Objectives
1111
1212This is a overview of Nx tensors. In this section, we'll look at some of the various tools for
13- creating and interacting with tensors. The IEx helpers will assist our
14- exploration of the core tensor concepts.
15-
16- ``` elixir
17- import IEx .Helpers
18- ```
13+ creating and interacting with tensors.
1914
2015After reading, you should be able to understand:
2116
@@ -27,30 +22,36 @@ After reading, you should be able to understand:
2722
2823## The Basics
2924
30- Now, everything is set up, so we're ready to create some tensors .
25+ First, let's install Nx with ` Mix.install ` .
3126
3227``` elixir
3328Mix .install ([
3429 {:nx , " ~> 0.5" }
3530])
3631```
3732
33+ The ` IEx.Helpers ` module will assist our exploration of the core tensor concepts.
34+
35+ ``` elixir
36+ import IEx .Helpers
37+ ```
38+
3839### Creating tensors
3940
40- The argument must be one of:
41+ The argument for ` Nx.tensor/1 ` must be one of:
4142
42- - a tensor
43- - a number (which means the tensor is scalar/zero-dimensional)
44- - a boolean (also scalar/zero-dimensional)
43+ - a tensor;
44+ - a number (which means the tensor is scalar/zero-dimensional);
45+ - a boolean (also scalar/zero-dimensional);
4546- an arbitrarily nested list of numbers and booleans
47+ - the special atoms ` :nan ` , ` :infinity ` , ` :neg_infinity ` , which represent values not supported by Elixir floats.
4648
47- If a new tensor is allocated, it will be allocated in the backend defined by
48- ` Nx.default_backend/0 ` , unless the ` :backend ` option is given, which overrides the
49- default.
49+ If a new tensor is allocated, it will be allocated in the backend defined by the ` :backend ` option.
50+ If it is not provided, ` Nx.default_backend/0 ` will be used instead.
5051
5152#### Examples
5253
53- A number returns a tensor of zero dimensions:
54+ A number returns a tensor of zero dimensions, also known as a scalar :
5455
5556``` elixir
5657Nx .tensor (0 )
@@ -60,7 +61,7 @@ Nx.tensor(0)
6061Nx .tensor (1.0 )
6162```
6263
63- Giving a list returns a vector (a one-dimensional tensor) :
64+ A list returns a one-dimensional tensor, also known as a vector :
6465
6566``` elixir
6667Nx .tensor ([1 , 2 , 3 ])
@@ -70,7 +71,7 @@ Nx.tensor([1, 2, 3])
7071Nx .tensor ([1.2 , 2.3 , 3.4 , 4.5 ])
7172```
7273
73- Multi- dimensional tensors are also possible:
74+ Higher dimensional tensors are also possible:
7475
7576``` elixir
7677Nx .tensor ([[1 , 2 , 3 ], [4 , 5 , 6 ]])
@@ -105,14 +106,14 @@ Names make your code more expressive:
105106Nx .tensor ([[[1 , 2 , 3 ], [4 , 5 , 6 ], [7 , 8 , 9 ]]], names: [:batch , :height , :width ])
106107```
107108
108- You can also leave dimension names as ` nil ` :
109+ You can also leave dimension names as ` nil ` (which is the default) :
109110
110111``` elixir
111112Nx .tensor ([[[1 , 2 , 3 ], [4 , 5 , 6 ], [7 , 8 , 9 ]]], names: [:batch , nil , nil ])
112113```
113114
114115However, you must provide a name for every dimension in the tensor. For example,
115- the following code snippet raises an error:
116+ the following code snippet raises an error because 1 name is given, but there are 3 dimensions :
116117
117118``` elixir
118119Nx .tensor ([[[1 , 2 , 3 ], [4 , 5 , 6 ], [7 , 8 , 9 ]]], names: [:batch ])
@@ -127,6 +128,9 @@ tensor = Nx.tensor([[1, 2], [3, 4]], names: [:y, :x])
127128tensor[[0 , 1 ]]
128129```
129130
131+ Negative indices will start counting from the end of the axis.
132+ ` -1 ` is the last entry, ` -2 ` the second to last and so on.
133+
130134``` elixir
131135tensor = Nx .tensor ([[1 , 2 ], [3 , 4 ], [5 , 6 ]], names: [:y , :x ])
132136tensor[[- 1 , - 1 ]]
@@ -169,36 +173,36 @@ Besides single-precision (32 bits), floats can have other kinds of precision, su
169173double-precision (64):
170174
171175``` elixir
172- Nx .tensor ([1 , 2 , 3 ], type: :f16 )
176+ Nx .tensor ([0.0 , 0. 2 , 0.4 , 1.0 ], type: :f16 )
173177```
174178
175179``` elixir
176- Nx .tensor ([1 , 2 , 3 ] , type: :f64 )
180+ Nx .tensor ([0.0 , 0. 2 , 0.4 , 1.0 , type: :f64 )
177181```
178182
179- Brain-floating points are also supported:
183+ Brain floats are also supported:
180184
181185``` elixir
182- Nx .tensor ([1 , 2 , 3 ] , type: :bf16 )
186+ Nx .tensor ([0.0 , 0. 2 , 0.4 , 1.0 , type: :bf16 )
183187```
184188
185189Certain backends and compilers support 8-bit floats. The precision
186190implementation of 8-bit floats may change per backend, so you must be careful
187- when transferring data across. The binary backend implements F8E5M2:
191+ when transferring data across different backends . The binary backend implements F8E5M2:
188192
189193``` elixir
190194Nx .tensor ([1 , 2 , 3 ], type: :f8 )
191195```
192196
193197In all cases, the non-finite values negative infinity (-Inf), infinity (Inf),
194198and "not a number" (NaN) can be represented by the atoms ` :neg_infinity ` ,
195- ` :infinity ` , and ` :nan respectively ` :
199+ ` :infinity ` , and ` :nan ` , respectively:
196200
197201``` elixir
198202Nx .tensor ([:neg_infinity , :nan , :infinity ])
199203```
200204
201- Finally, complex numbers are also supported in tensors:
205+ Finally, complex numbers are also supported in tensors, in both 32-bit and 64-bit precision :
202206
203207``` elixir
204208Nx .tensor (Complex .new (1 , - 1 ))
@@ -217,7 +221,7 @@ Nx supports element-wise arithmetic operations for tensors and broadcasting when
217221``` elixir
218222a = Nx .tensor ([1 , 2 , 3 ])
219223b = Nx .tensor ([0 , 1 , 2 ])
220- Nx .add (a , b)
224+ Nx .add (a, b)
221225```
222226
223227### Subtraction
@@ -227,7 +231,7 @@ Nx.add(a , b)
227231``` elixir
228232a = Nx .tensor ([10 , 20 , 30 ])
229233b = Nx .tensor ([0 , 1 , 2 ])
230- Nx .subtract (a , b)
234+ Nx .subtract (a, b)
231235```
232236
233237### Multiplication
@@ -237,7 +241,7 @@ Nx.subtract(a , b)
237241``` elixir
238242a = Nx .tensor ([2 , 3 , 4 ])
239243b = Nx .tensor ([0 , 1 , 2 ])
240- Nx .multiply (a , b)
244+ Nx .multiply (a, b)
241245```
242246
243247### Division
@@ -247,7 +251,7 @@ Nx.multiply(a , b)
247251``` elixir
248252a = Nx .tensor ([10 , 30 , 40 ])
249253b = Nx .tensor ([5 , 6 , 8 ])
250- Nx .divide (a , b)
254+ Nx .divide (a, b)
251255```
252256
253257### Exponentiation
@@ -257,12 +261,12 @@ Nx.divide(a , b)
257261``` elixir
258262a = Nx .tensor ([2 , 3 , 4 ])
259263b = Nx .tensor ([2 ])
260- Nx .pow (a , b)
264+ Nx .pow (a, b)
261265```
262266
263267### Quotient
264268
265- ` Nx.quotient/2 ` : Returns a new tensor where each element is the integer division (div/2) of left by right .
269+ ` Nx.quotient/2 ` : Returns a new tensor where each element is the integer division (` div/2 ` ) .
266270
267271``` elixir
268272a = Nx .tensor ([10 , 20 , 30 ])
@@ -273,7 +277,7 @@ Nx.quotient(a, b)
273277
274278### Remainder
275279
276- ` Nx.remainder/2 ` : Computes the remainder of the division of two integer tensors .
280+ ` Nx.remainder/2 ` : Computes the remainder of the integer division .
277281
278282``` elixir
279283a = Nx .tensor ([27 , 32 , 43 ])
@@ -292,7 +296,7 @@ Nx.negate(a)
292296
293297### Square Root
294298
295- ` Nx.sqrt/1 ` : It computes the element-wise square root of the given tensor .
299+ ` Nx.sqrt/1 ` : Computes the element-wise square root.
296300
297301``` elixir
298302a = Nx .tensor ([4 , 9 , 16 ])
@@ -301,21 +305,21 @@ Nx.sqrt(a)
301305
302306## Element-Wise Comparison
303307
304- Returns 1 when true and 0 when false
308+ The following operations returns a u8 tensor where 1 represents ` true ` and 0 represents ` false ` .
305309
306310### Equality and Inequality
307311
308312` Nx.equal/2 ` , ` Nx.not_equal/2 `
309313
310314``` elixir
311315a = Nx .tensor ([4 , 9 , 16 ])
312- b = Nx .tensor ([4 , 9 , 16 ])
316+ b = Nx .tensor ([4 , 9 , - 16 ])
313317Nx .equal (a, b)
314318```
315319
316320``` elixir
317321a = Nx .tensor ([4 , 9 , 16 ])
318- b = Nx .tensor ([4.0 , 9.0 , 16.0 ])
322+ b = Nx .tensor ([4.0 , 9.0 , - 16.0 ])
319323Nx .not_equal (a, b)
320324```
321325
@@ -331,7 +335,7 @@ Nx.greater(a, b)
331335
332336``` elixir
333337a = Nx .tensor ([4 , 9 , 16 ])
334- b = Nx .tensor ([4.2 , 9.0 , 16.7 ])
338+ b = Nx .tensor ([4.2 , 9.0 , 15.9 ])
335339Nx .less (a, b)
336340```
337341
@@ -340,15 +344,15 @@ Nx.less(a, b)
340344` Nx.greater_equal/2 ` , ` Nx.less_equal/2 `
341345
342346``` elixir
343- a = Nx .tensor ([3 , 5 , 2 ])
344- b = Nx .tensor ([2 , 5 , 4 ])
347+ a = Nx .tensor ([4 , 9 , 16 ])
348+ b = Nx .tensor ([4 , 8 , 17 ])
345349
346350Nx .greater_equal (a, b)
347351```
348352
349353``` elixir
350- a = Nx .tensor ([3 , 5 , 2 ])
351- b = Nx .tensor ([2 , 5 , 4 ])
354+ a = Nx .tensor ([4 , 9 , 16 ])
355+ b = Nx .tensor ([4. 2 , 9.0 , 15.9 ])
352356
353357Nx .less_equal (a, b)
354358```
@@ -359,7 +363,7 @@ These operations aggregate values across tensor axes.
359363
360364### Sum
361365
362- ` Nx.sum/1 ` : Sums all elements
366+ ` Nx.sum/1 ` : Sums all elements.
363367
364368``` elixir
365369a = Nx .tensor ([[4 , 9 , 16 ], [4.2 , 9.0 , 16.7 ]])
@@ -368,7 +372,7 @@ Nx.sum(a)
368372
369373### Mean
370374
371- ` Nx.mean/1 ` : Computes the mean value of the tensor
375+ ` Nx.mean/1 ` : Computes the mean value of the elements.
372376
373377``` elixir
374378a = Nx .tensor ([[4 , 9 , 16 ], [4.2 , 9.0 , 16.7 ]])
@@ -377,7 +381,7 @@ Nx.mean(a)
377381
378382### Product
379383
380- ` Nx.product/1 ` : Computes the product of all elements.
384+ ` Nx.product/1 ` : Computes the product of the elements.
381385
382386``` elixir
383387a = Nx .tensor ([[4 , 9 , 16 ], [4.2 , 9.0 , 16.7 ]])
@@ -386,7 +390,7 @@ Nx.product(a)
386390
387391## Matrix Multiplication
388392
389- ` Nx.dot/4 ` : Computes the generalized dot product between two tensors, given the contracting axes.hyunnnn
393+ ` Nx.dot/4 ` : Computes the generalized dot product between two tensors, given the contracting axes.
390394
391395``` elixir
392396t1 = Nx .tensor ([[1 , 2 ], [3 , 4 ]], names: [:x , :y ])
0 commit comments