From 026945e66942088a9c3ede980b8639a0a20a40cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 25 Sep 2021 20:54:27 +0200 Subject: [PATCH] Remove breaks to improve readability --- .../notebook/explore/intro_to_nx.livemd | 52 ------------------- 1 file changed, 52 deletions(-) diff --git a/lib/livebook/notebook/explore/intro_to_nx.livemd b/lib/livebook/notebook/explore/intro_to_nx.livemd index db6ddbc6b64..31d845ee402 100644 --- a/lib/livebook/notebook/explore/intro_to_nx.livemd +++ b/lib/livebook/notebook/explore/intro_to_nx.livemd @@ -39,8 +39,6 @@ Systems of equations are a central theme in numerical computing. These equations are often expressed and solved with multidimensional arrays. For example, this is a two dimensional array: - - $$ \begin{bmatrix} 1 & 2 \\ @@ -48,8 +46,6 @@ $$ \end{bmatrix} $$ - - Elixir programmers typically express a similar data structure using a list of lists, like this: @@ -77,8 +73,6 @@ than `Enum.map/2` and `Enum.reduce/3`. In this section, we'll look at some of the various tools for creating and interacting with tensors. - - To get started, we'll first need to include the Nx dependency. In Livebook, we can simply use Mix install. At the time of this writing, there's no Hex package for Nx, so we'll use a git @@ -102,8 +96,6 @@ Now, everything is set up, so we're ready to create some tensors. ### Creating tensors - - Start out by getting a feel for Nx through its documentation. Do so through the IEx helpers, like this: @@ -138,8 +130,6 @@ The result shows all of the major fields that make up a tensor: * The shape of the tensor, going left to right, with the outside dimensions listed first. * The names of each dimension. - - We can easily convert it to a binary: ```elixir @@ -168,8 +158,6 @@ because they must often be processed. Elixir binaries make quick work of dealing with numerical data structured for platforms other than Elixir. - - We can get any cell of the tensor: ```elixir @@ -200,8 +188,6 @@ Now, * return a `{2, 2}` tensor containing the first two columns of the first two rows - - We can get information about this most recent term with the IEx helper `i`, like this: @@ -223,8 +209,6 @@ to get more documentation about tensors. We could also open an Elixir cell, type Nx.tensor, and hover the cursor over the word `tensor` to see the help about that function. - - We can get the shape of the tensor with `Nx.shape/1`: ```elixir @@ -242,8 +226,6 @@ changes the metadata, so it has no notable cost. The new tensor has the same type, but a new shape. - - Now, reshape the tensor to contain three dimensions with one batch, one row, and four columns. @@ -263,8 +245,6 @@ Nx.tensor([[1, 2, 3]], names: [:rows, :cols], type: {:u, 8}) We created a tensor of the shape `{1, 3}`, with the type `u8`, the values `[1, 2, 3]`, and two axes named `rows` and `cols`. - - Now we know how to create tensors, so it's time to do something with them. ## Tensor aware functions @@ -324,8 +304,6 @@ Nx.sum(tensor, axes: [:x]) Nx sums the values across the `x` dimension: `1 + 2` in the first row and `3 + 4` in the second row. - - Now, * create a `{2, 2, 2}` tensor @@ -341,8 +319,6 @@ Sometimes, we need to combine two tensors together with an operator. Let's say we wanted to subtract one tensor from another. Mathematically, the expression looks like this: - - $$ \begin{bmatrix} 5 & 6 \\ @@ -358,8 +334,6 @@ $$ \end{bmatrix} $$ - - To solve this problem, add each integer on the left with the corresponding integer on the right. Unfortunately, we cannot use Elixir's built-in subtraction operator as it is not tensor-aware. @@ -383,8 +357,6 @@ Often, the dimensions of tensors in an operator don't match. For example, you might want to subtract a `1` from every element of a `{2, 2}` tensor, like this: - - $$ \begin{bmatrix} 1 & 2 \\ @@ -396,8 +368,6 @@ $$ \end{bmatrix} $$ - - Mathematically, it's the same as this: $$ @@ -415,8 +385,6 @@ $$ \end{bmatrix} $$ - - That means we need a way to convert `1` to a `{2, 2}` tensor. `Nx.broadcast/2` solves that problem. This function takes a tensor or a scalar and a shape. @@ -451,8 +419,6 @@ Nx.subtract(10, tensor) Or subtract a row or column. Mathematically, it would look like this: - - $$ \begin{bmatrix} 1 & 2 \\ @@ -467,12 +433,8 @@ $$ \end{bmatrix} $$ - - which is the same as this: - - $$ \begin{bmatrix} 1 & 2 \\ @@ -488,8 +450,6 @@ $$ \end{bmatrix} $$ - - This rewrite happens in Nx too, also through a broadcast. We want to broadcast the tensor `[1, 2]` to match the `{2, 2}` shape, like this: @@ -531,8 +491,6 @@ h Nx.broadcast Much of the time, you won't have to broadcast yourself. Many of the functions and operators Nx supports will do so automatically. - - We can use tensor-aware operators via various `Nx` functions and many of them implicitly broadcast tensors. @@ -556,8 +514,6 @@ over classic Elixir functions. individual operations and using a just-in-time (JIT) compiler to emit highly specialized native code for the desired computation unit. - - We don't have to do anything special to get access to get tensor awareness beyond importing `Nx.Defn` and writing our code within a `defn` block. @@ -612,8 +568,6 @@ defn subtract(a, b) do end ``` - - Now, it's your turn. Add a `defn` to `TensorMath` that accepts two tensors representing the lengths of sides of a right triangle and uses the pythagorean theorem to return the @@ -647,20 +601,14 @@ a function returning the gradient. We have two functions: `poly/1` is a simple numerical definitin, and `poly_slope_at/1` returns its gradient: - - $$ poly: f(x) = 3x^2 + 2x + 1 \\ $$ - - $$ polySlopeAt: g(x) = 6x + 2 $$ - - Here's the Elixir equivalent of those functions: ```elixir