Tensors
Mix.install([
{:nx, "~> 0.5"}
])
Playing With Tensors
Nx.tensor(44)
The tensor arg can be one of many things
- A number (aka scalar)
- A boolean (synatic sugar on a special 0/1 tensor)
- A list of numbers, booleans, or list of numbers and booleans
-
A tensor (why? well the
opts
we haven’t talke about yet would then be used to tweak an existing tensor)
scalar = Nx.tensor(48)
true_scalar = Nx.tensor(true)
false_scalar = Nx.tensor(false)
vector = Nx.tensor([4, 8])
matrix = Nx.tensor([[1, 1, 2], [2, 2, 4]])
{scalar, true_scalar, false_scalar, vector, matrix}
Tensor Options
You can explicitly specify the data type
-
:u8
-
:u16
-
:u32
-
:u64
-
:s8
-
:s16
-
:s32
-
:s64
-
:bf16
-
:f16
-
:f32
-
:f64
implicit_bool = Nx.tensor(1, type: :u8)
named_columns = Nx.tensor([[0, 1], [1, 3], [2, 5]], names: [:rows, :cols])
{implicit_bool, named_columns}
Tensor Operations
You will be manipulating data a lot with Tensors, and it will take some getting used to. Try changing the second tensor between
- 10
- [10]
- [10, 20, 30]
- [10, 20, 30, 40]
[1,2,3]
|> Nx.tensor()
|> Nx.add(Nx.tensor([10, 20, 30]))