View Source Nx.Defn.Composite (Nx v0.4.0)

Functions to deal with composite data types according to Nx.Container.

The functions in this module can be used both inside and outside defn. Note the functions in this module traverses tensors, but it does not automatically convert values to tensors. For example, the tuple {1, 2, 3} once traversed will emit the numbers 1, 2, and 3. If desired, you can invoke Nx.to_tensor/1 to normalize them.

Note that, when a value is given to defn, it is first converted to tensors and containers via Nx.LazyContainer. Inside defn, there are no lazy containers, only containers.

Link to this section Summary

Functions

Traverses two composite types to see if they are compatible.

Counts the number of non-composite types in the composite type.

Flattens recursively the given list of composite types.

Reduces recursively the given composite types with acc and fun.

Traverses recursively the given composite types with fun.

Traverses recursively the given composite types with acc and fun.

Link to this section Functions

Link to this function

compatible?(left, right, fun)

View Source

Traverses two composite types to see if they are compatible.

For non-composite types, the given fun will be called to compare numbers/tensors pairwise.

Counts the number of non-composite types in the composite type.

examples

Examples

iex> Nx.Defn.Composite.count(123)
1
iex> Nx.Defn.Composite.count({1, {2, 3}})
3
iex> Nx.Defn.Composite.count({Complex.new(1), {Nx.tensor(2), 3}})
3
Link to this function

flatten_list(args, tail \\ [])

View Source

Flattens recursively the given list of composite types.

Elements that are not tensors (i.e. numbers and Complex numbers) are kept as is unless a custom function is given.

examples

Examples

iex> Nx.Defn.Composite.flatten_list([1, {2, 3}])
[1, 2, 3]

iex> Nx.Defn.Composite.flatten_list([1, {2, 3}], [Nx.tensor(4)])
[1, 2, 3, Nx.tensor(4)]

Reduces recursively the given composite types with acc and fun.

If composite tensor expressions are given, such as a tuple, the composite type is recursively traversed and returned.

If a non-composite tensor expression is given, the function is invoked for it but not for its arguments.

Traverses recursively the given composite types with fun.

If a composite tensor is given, such as a tuple, the composite type is recursively traversed and returned.

Otherwise the function is invoked with the tensor (be it a number, complex, or actual tensor).

Link to this function

traverse(expr, acc, fun)

View Source

Traverses recursively the given composite types with acc and fun.

If a composite tensor is given, such as a tuple, the composite type is recursively traversed and returned.

Otherwise the function is invoked with the tensor (be it a number, complex, or actual tensor).