4.1.2. Functions

This page describes the basic/composite functions implemented in primitiv. They return a template type Var, and take 0 or more number of references of Var as their arguments. Var becomes either Node or Tensor according to the usage:

primitiv::Node x = ...;
primitiv::Tensor w = ...;
auto y = primitiv::functions::tanh(x);  // `y` becomes a `Node`.
auto u = primitiv::functions::exp(w);  // `u` becomes a `Tensor`.

If the function has no argument with type Var, you must specify the template argument appropriately:

auto x = primitiv::functions::input<Node>(...);  // `x` becomes a `Node`.
auto w = primitiv::functions::parameter<Tensor>(...);  // `w` becomes a `Tensor`.
namespace functions

Functions

template <typename Var>
type_traits::Identity<Var> selu(const Var &x, float a = 1.6732632423543772848170429916717, float s = 1.0507009873554804934193349852946)

Applies an elementwise scaled ELU function:

\[\begin{split} \mathrm{SELU}(x) := s \times \left\{ \begin{array}{ll} x, & \mathrm{if} \ x \geq 0, \\ \alpha (e^x - 1), & \mathrm{otherwise}. \end{array} \right. \end{split}\]
Return
A variable representing \( \mathrm{SELU}(x) \).
Remark
This function is implemented as a composite of some other functions.
Parameters
  • x: A variable representing an argument \( x \).
  • a: A scaling factor \( \alpha \).
  • s: Another scaling factor \( s \).

template <typename Container>
type_traits::Reduce<Container> sum(const Container &xs)

Applies summation along variables in the container.

Return
A new variable.
Remark
This function is implemented as a composite of some other functions.
Parameters
  • xs: Iterable container of variables. xs must have both begin() and end() functions that return the begin/end iterators.

template <typename Container>
type_traits::ReducePtr<Container> sum(const Container &xs)

Same as above, but xs has pointers of variables.

Return
A new variable.
Remark
This function is implemented as a composite of some other functions.
Parameters
  • xs: Iterable container of pointers of variables. xs must have both begin() end end() functions that return the begin/end iterators.

template <typename Var>
type_traits::Identity<Var> mean(const Var &x, std::uint32_t dim)

Calculates means along an axis.

Return
A new variable.
Remark
This function is implemented as a composite of some other functions.
Parameters
  • x: A variable representing values before reduction.
  • dim: Axis to be processed.

template <typename Container>
type_traits::Reduce<Container> mean(const Container &xs)

Calculates means along variables in the container.

Return
A new variable.
Remark
This function is implemented as a composite of some other functions.
Parameters
  • xs: Iterable container of variables. xs must have both begin() and end() functions that return the begin/end iterators.

template <typename Container>
type_traits::ReducePtr<Container> mean(const Container &xs)

Same as above, but xs has pointers of variables.

Return
A new variable.
Remark
This function is implemented as a composite of some other functions.
Parameters
  • xs: Iterable container of pointers of variables. xs must have both begin() end end() functions that return the begin/end iterators.

Tensor zeros_tensor(const Shape &shape, Device *dev)

Creates a new Tensor with all values \( 0 \).

Return
A new Tensor.
Remark
This function is implemented as a composite of some other functions.
Parameters

Node zeros_node(const Shape &shape, Device *dev, Graph *g)

Creates a new Node with all values \( 0 \).

Return
A new Node.
Remark
This function is implemented as a composite of some other functions.
Parameters
  • shape: Shape of the new Node.
  • dev: Device to manage the new Node, or nullptr to use the default device.
  • g: Graph to manage the instance of the Node, or nullptr to use the default graph.

template <typename Var>
type_traits::Identity<Var> zeros(const Shape &shape, Device *dev)

Creates a new variable with all values \( 0 \).

Return
A new variable.
Remark
This function uses the default graph when specifying Node as the template variable.
Remark
This function is implemented as a composite of some other functions.
Parameters
  • shape: Shape of the new variable.
  • dev: Device to manage the new variable, or nullptr to use the default defice.

template <typename Var>
type_traits::Identity<Var> zeros(const Shape &shape, Device &dev)

Creates a new variable with all values \( 0 \).

Return
A new variable.
Remark
This function uses the default graph when specifying Node as the template variable.
Remark
This function is implemented as a composite of some other functions.
Parameters
  • shape: Shape of the new variable.
  • dev: Device to manage the new variable.

template <typename Var>
type_traits::Identity<Var> zeros(const Shape &shape)

Creates a new variable with all values \( 0 \).

Return
A new variable.
Remark
This function always uses the default device, and also uses the default graph when specifying Node as the template variable.
Remark
This function is implemented as a composite of some other functions.
Parameters
  • shape: Shape of the new variable.

Tensor ones_tensor(const Shape &shape, Device *dev)

Creates a new Tensor with all values \( 1 \).

Return
A new Tensor.
Remark
This function is implemented as a composite of some other functions.
Parameters

Node ones_node(const Shape &shape, Device *dev, Graph *g)

Creates a new Node with all values \( 1 \).

Return
A new Node.
Remark
This function is implemented as a composite of some other functions.
Parameters
  • shape: Shape of the new Node.
  • dev: Device to manage the new Node, or nullptr to use the default device.
  • g: Graph to manage the instance of the Node, or nullptr to use the default graph.

template <typename Var>
type_traits::Identity<Var> ones(const Shape &shape, Device *dev)

Creates a new variable with all values \( 1 \).

Return
A new variable.
Remark
This function uses the default graph when specifying Node as the template variable.
Remark
This function is implemented as a composite of some other functions.
Parameters
  • shape: Shape of the new variable.
  • dev: Device to manage the new variable, or nullptr to use the default defice.

template <typename Var>
type_traits::Identity<Var> ones(const Shape &shape, Device &dev)

Creates a new variable with all values \( 1 \).

Return
A new variable.
Remark
This function uses the default graph when specifying Node as the template variable.
Remark
This function is implemented as a composite of some other functions.
Parameters
  • shape: Shape of the new variable.
  • dev: Device to manage the new variable.

template <typename Var>
type_traits::Identity<Var> ones(const Shape &shape)

Creates a new variable with all values \( 1 \).

Return
A new variable.
Remark
This function always uses the default device, and also uses the default graph when specifying Node as the template variable.
Remark
This function is implemented as a composite of some other functions.
Parameters
  • shape: Shape of the new variable.

template <typename Var>
type_traits::Identity<Var> dropout(const Var &x, float rate, bool enabled)

Applies the dropout:

\[\begin{split} \begin{array}{rcl} w & \sim & \mathrm{Bernoulli}(w; 1 - r), \\ \mathrm{dropout}(x) & := & \frac{1}{1 - r} \times w \times x. \end{array} \end{split}\]
Return
A new variable.
Remark
This function is implemented as a composite of some other functions.
Parameters
  • x: A variable representing original values.
  • rate: The dropout probability \( r \). 0 maintains all values and 1 discards all values.
  • enabled: If true, this function applies the operation. Otherwise, this function performs nothing.

template <typename Var>
type_traits::Identity<Var> positive(const Var &x)

Applies a unary \( + \) operation. This function does not change any values of the argument, and returns a copy of it.

Return
A variable representing \( +x \).
Parameters
  • x: A variable representing an argument \( x \).

template <typename Var>
type_traits::Identity<Var> negative(const Var &x)

Applies a unary \( - \) operation.

Return
A variable representing \( -x \).
Parameters
  • x: A variable representing an argument \( x \).

template <typename Var>
type_traits::Identity<Var> add(const Var &x, float k)

Applies an elementwise addition between a variable and a constant.

Return
A variable representing \( x + k \).
Parameters
  • x: A variable representing an argument \( x \).
  • k: A constant \( k \).

template <typename Var>
type_traits::Identity<Var> add(float k, const Var &x)

Applies an elementwise addition between a constant and a variable.

Return
A variable representing \( k + x \).
Parameters
  • k: A constant \( k \).
  • x: A variable representing an argument \( x \).

template <typename Var>
type_traits::Identity<Var> add(const Var &a, const Var &b)

Applies an elementwise addition between two variables.

Return
A variable representing \( a + b \).
Parameters
  • a: A variable representing an argument \( a \).
  • b: A variable representing an argument \( b \).

template <typename Var>
type_traits::Identity<Var> subtract(const Var &x, float k)

Applies an elementwise subtraction between a variable and a constant.

Return
A variable representing \( x - k \).
Parameters
  • x: A variable representing an argument \( x \).
  • k: A constant \( k \).

template <typename Var>
type_traits::Identity<Var> subtract(float k, const Var &x)

Applies an elementwise subtraction between a constant and a variable.

Return
A variable representing \( k - x \).
Parameters
  • k: A constant \( k \).
  • x: A variable representing an argument \( x \).

template <typename Var>
type_traits::Identity<Var> subtract(const Var &a, const Var &b)

Applies an elementwise subtraction between two variables.

Return
A variable representing \( a - b \).
Parameters
  • a: A variable representing an argument \( a \).
  • b: A variable representing an argument \( b \).

template <typename Var>
type_traits::Identity<Var> multiply(const Var &x, float k)

Applies an elementwise multiplication between a variable and a constant.

Return
A variable representing \( x \times k \).
Parameters
  • x: A variable representing an argument \( x \).
  • k: A constant \( k \).

template <typename Var>
type_traits::Identity<Var> multiply(float k, const Var &x)

Applies an elementwise multiplication between a constant and a variable.

Return
A variable representing \( k \times x \).
Parameters
  • k: A constant \( k \).
  • x: A variable representing an argument \( x \).

template <typename Var>
type_traits::Identity<Var> multiply(const Var &a, const Var &b)

Applies an elementwise multiplication between two variables.

Return
A variable representing \( a \times b \).
Parameters
  • a: A variable representing an argument \( a \).
  • b: A variable representing an argument \( b \).

template <typename Var>
type_traits::Identity<Var> divide(const Var &x, float k)

Applies an elementwise division between a variable and a constant.

Return
A variable representing \( x / k \).
Parameters
  • x: A variable representing an argument \( x \).
  • k: A constant \( k \).

template <typename Var>
type_traits::Identity<Var> divide(float k, const Var &x)

Applies an elementwise division between a constant and a variable.

Return
A variable representing \( k / x \).
Parameters
  • k: A constant \( k \).
  • x: A variable representing an argument \( x \).

template <typename Var>
type_traits::Identity<Var> divide(const Var &a, const Var &b)

Applies an elementwise division between two variables.

Return
A variable representing \( a / b \).
Parameters
  • a: A variable representing an argument \( a \).
  • b: A variable representing an argument \( b \).

template <typename Var>
type_traits::Identity<Var> pow(const Var &x, float k)

Applies an elementwise exponentation between a variable and a constant.

Return
A variable representing \( x^k \).
Parameters
  • x: A variable representing an argument \( x \).
  • k: A constant \( k \).

template <typename Var>
type_traits::Identity<Var> pow(float k, const Var &x)

Applies an elementwise exponentation between a constant and a variable.

Return
A variable representing \( k^x \).
Parameters
  • k: A constant \( k \).
  • x: A variable representing an argument \( x \).

template <typename Var>
type_traits::Identity<Var> pow(const Var &a, const Var &b)

Applies an elementwise exponentation between two variables.

Return
A variable representing \( a^b \).
Parameters
  • a: A variable representing an argument \( a \).
  • b: A variable representing an argument \( b \).

template <typename Var>
type_traits::Identity<Var> pown(const Var &x, std::int32_t k)

Applies an elementwise exponentation between a variable and an integer constant. This function can be applied correctly when x has some negative values.

Return
A variable representing \( x^k \).
Parameters
  • x: A variable representing an argument \( x \).
  • k: An integer constant \( k \).

Tensor input_tensor(const Shape &shape, const std::vector<float> &data, Device *dev)

Creates a new Tensor from specific shape and data.

Return
A new Tensor.
Parameters
  • shape: Shape of the new Tensor.
  • data: Inner data of the new Tensor. data.size() should be equal to shape.size() and each data is ordered by the column-major order.
  • dev: Device to manage inner data of the Tensor, or nullptr to use the default device.

Node input_node(const Shape &shape, const std::vector<float> &data, Device *dev, Graph *g)

Creates a new Node from specific shape and data.

Return
A new Node.
Parameters
  • shape: Shape of the new Node.
  • data: Inner data of the new Node. data.size() should be equal to shape.size() and each data is ordered by the column-major order.
  • dev: Device to manage inner data of the Node, or nullptr to use the default device.
  • g: Graph to manage the instance of the Node, or nullptr to use the default graph.

template <typename Var>
type_traits::Identity<Var> input(const Shape &shape, const std::vector<float> &data, Device *dev)

Creates a new variable from specific shape and data.

Return
A new variable.
Remark
This function uses the default graph when specifying Node as the template variable.
Parameters
  • shape: Shape of the new variable.
  • data: Inner data of the new variable. data.size() should be equal to shape.size() and each data is ordered by the column-major order.
  • dev: Device to manage inner data of the variable, or nullptr to use the default device.

template <typename Var>
type_traits::Identity<Var> input(const Shape &shape, const std::vector<float> &data, Device &dev)

Creates a new variable from specific shape and data.

Return
A new variable.
Remark
This function uses the default graph when specifying Node as the template variable.
Parameters
  • shape: Shape of the new variable.
  • data: Inner data of the new variable. data.size() should be equal to shape.size() and each data is ordered by the column-major order.
  • dev: Device to manage inner data of the variable.

template <typename Var>
type_traits::Identity<Var> input(const Shape &shape, const std::vector<float> &data)

Creates a new variable from specific shape and data.

Return
A new variable.
Remark
This function always uses the default device, and also uses the default graph when specifying Node as the template variable.
Parameters
  • shape: Shape of the new variable.
  • data: Inner data of the new variable. data.size() should be equal to shape.size() and each data is ordered by the column-major order.

Tensor parameter_tensor(Parameter &param)

Creates a new Tensor from a specific Parameter.

Return
A new Tensor.
Parameters

Node parameter_node(Parameter &param, Graph *g)

Creates a new Node from a specific Parameter.

Return
A new Node.
Parameters
  • param: Parameter to be associated with the Node.
  • g: Graph to manage the instance of the Node, or nullptr to use the default graph.

template <typename Var>
type_traits::Identity<Var> parameter(Parameter &param)

Creates a new variable from a specific Parameter.

Return
A new variable.
Remark
This function uses the default graph when specifying Node as the template variable.
Parameters
  • param: Parameter to be associated with the variable.

template <typename Var>
type_traits::Identity<Var> copy(const Var &x, Device *dev)

Copies a variable onto a specific device.

Return
A new variable managed on dev.
Parameters
  • x: A variable to be copied.
  • dev: Device to manage the new variable, or nullptr to use the default device.

template <typename Var>
type_traits::Identity<Var> copy(const Var &x, Device &dev)

Copies a variable onto a specific device.

Return
A new variable managed on dev.
Parameters
  • x: A variable to be copied.
  • dev: Device to manage the new variable.

template <typename Var>
type_traits::Identity<Var> copy(const Var &x)

Copies a variable onto the default device.

Return
A new variable managed on the default device.
Parameters
  • x: A variable to be copied.

template <typename Var>
type_traits::Identity<Var> pick(const Var &x, const std::vector<std::uint32_t> &ids, std::uint32_t dim)

Lookups subplanes according to the specific axis and addresses. This function can be used to an embedding lookup associated with a fixed vocabulary. Following examples show how this function work:

\[\begin{split} \begin{array}{lcl} x & := & \left( \begin{array}{ccc} 1 & 4 & 7 \\ 2 & 5 & 8 \\ 3 & 6 & 9 \end{array} \right), \\ \mathrm{pick}(x, [0, 0, 1], 0) & = & \left( \begin{array}{ccc} 1 & 4 & 7 \end{array} \right), \left( \begin{array}{ccc} 1 & 4 & 7 \end{array} \right), \left( \begin{array}{ccc} 2 & 5 & 8 \end{array} \right), \\ \mathrm{pick}(x, [1, 2], 1) & = & \left( \begin{array}{c} 4 \\ 5 \\ 6 \end{array} \right), \left( \begin{array}{c} 7 \\ 8 \\ 9 \end{array} \right), \\ \mathrm{pick}(x, [0], 2) & = & \left( \begin{array}{ccc} 1 & 4 & 7 \\ 2 & 5 & 8 \\ 3 & 6 & 9 \end{array} \right). \end{array} \end{split}\]
The minibatch broadcasting rule is applied between the Shape of x and the number of values in ids:
\[\begin{split} \begin{array}{lcl} x & := & \left( \begin{array}{ccc} 1 & 4 & 7 \\ 2 & 5 & 8 \\ 3 & 6 & 9 \end{array} \right), \left( \begin{array}{ccc} 11 & 14 & 17 \\ 12 & 15 & 18 \\ 13 & 16 & 19 \end{array} \right), \left( \begin{array}{ccc} 21 & 24 & 27 \\ 22 & 25 & 28 \\ 23 & 26 & 29 \end{array} \right), \\ \mathrm{pick}(x, [0], 1) & = & \left( \begin{array}{c} 4 \\ 5 \\ 6 \end{array} \right), \left( \begin{array}{c} 14 \\ 15 \\ 16 \end{array} \right), \left( \begin{array}{c} 24 \\ 25 \\ 26 \end{array} \right), \\ \mathrm{pick}(x, [0, 1, 2], 1) & = & \left( \begin{array}{c} 1 \\ 2 \\ 3 \end{array} \right), \left( \begin{array}{c} 14 \\ 15 \\ 16 \end{array} \right), \left( \begin{array}{c} 27 \\ 28 \\ 29 \end{array} \right). \end{array} \end{split}\]
Return
A new variable.
Parameters
  • x: A variable representing an original data.
  • ids: List of subplane IDs according to the axis dim. Each value must be lower than x.shape()[dim].
  • dim: Axis to be processed.

template <typename Var>
type_traits::Identity<Var> slice(const Var &x, std::uint32_t dim, std::uint32_t lower, std::uint32_t upper)

Extracts a specific range \( [L, U) \) of subplanes along a specific axis. Following examples show how this function work:

\[\begin{split} \begin{array}{lcl} x & := & \left( \begin{array}{ccc} 1 & 4 & 7 \\ 2 & 5 & 8 \\ 3 & 6 & 9 \end{array} \right), \\ \mathrm{slice}(x, 0, 0, 1) & = & \left( \begin{array}{ccc} 1 & 4 & 7 \end{array} \right), \\ \mathrm{slice}(x, 1, 1, 3) & = & \left( \begin{array}{ccc} 4 & 7 \\ 5 & 8 \\ 6 & 9 \end{array} \right), \\ \mathrm{slice}(x, 2, 0, 1) & = & \left( \begin{array}{ccc} 1 & 4 & 7 \\ 2 & 5 & 8 \\ 3 & 6 & 9 \end{array} \right). \end{array} \end{split}\]
Return
A new variable.
Parameters
  • x: A variable representing an original data.
  • dim: Axis to be processed.
  • lower: Lower bound \( L \) of dim.
  • upper: Upper bound \( U \) of dim.

template <typename Var>
std::vector<type_traits::Identity<Var>> split(const Var &x, std::uint32_t dim, std::uint32_t n)

Splits a given variable into specified number of partitions along an axis.

Return
A list of n variables. Each variable is identical with: split(x, dim, n)[i] == slice(x, dim, L(i), U(i)), where L(i) := i * x.shape()[dim] / n and U(i) := (i + 1) * x.shape()[dim] / n.
Parameters
  • x: A variable representing an original data.
  • dim: Axis to be processed.
  • n: The number of resulting partitions. n should be able to divide x.shape()[dim] without residue.
Exceptions
  • primitiv::Error: n can not divide s.shape()[dim] without residue.

template <typename Container>
type_traits::Reduce<Container> concat(const Container &xs, std::uint32_t dim)

Concatenates multiple variables along specific axis. Following examples show how this function work:

\[\begin{split} \begin{array}{lcl} x_1 & := & \left( \begin{array}{ccc} 1 & 4 & 7 \\ 2 & 5 & 8 \\ 3 & 6 & 9 \end{array} \right), \\ x_2 & := & \left( \begin{array}{ccc} 11 & 14 & 17 \\ 12 & 15 & 18 \\ 13 & 16 & 19 \end{array} \right), \\ \mathrm{concat}([x_1, x_2], 0) & = & \left( \begin{array}{ccc} 1 & 4 & 7 \\ 2 & 5 & 8 \\ 3 & 6 & 9 \\ 11 & 14 & 17 \\ 12 & 15 & 18 \\ 13 & 16 & 19 \\ \end{array} \right), \\ \mathrm{concat}([x_1, x_2], 1) & = & \left( \begin{array}{cccccc} 1 & 4 & 7 & 11 & 14 & 17 \\ 2 & 5 & 8 & 12 & 15 & 18 \\ 3 & 6 & 9 & 13 & 16 & 19 \\ \end{array} \right), \\ \mathrm{concat}([x_1, x_2], 2) & = & \left( \left( \begin{array}{ccc} 1 & 4 & 7 \\ 2 & 5 & 8 \\ 3 & 6 & 9 \\ \end{array} \right), \left( \begin{array}{ccc} 11 & 14 & 17 \\ 12 & 15 & 18 \\ 13 & 16 & 19 \\ \end{array} \right) \right). \end{array} \end{split}\]
Return
A new variable.
Parameters
  • xs: Iterable container of variables. xs must have both begin() and end() functions that return the begin/end iterators.
  • dim: Axis to be processed.

template <typename Container>
type_traits::ReducePtr<Container> concat(const Container &xs, std::uint32_t dim)

Same as above, but xs has pointers of variables.

Return
A new variable.
Parameters
  • xs: Iterable container of pointers of variables. xs must have both begin() and end() functions that return the begin/end iterators.
  • dim: Axis to be processed.

template <typename Var>
type_traits::Identity<Var> reshape(const Var &x, const Shape &new_shape)

Changes the Shape of the variable.

Return
A new variable.
Parameters
  • x: A variable with an old Shape.
  • new_shape: A new Shape to be applied to the new variable. The volume and the minibatch size must be same as that of x.shape().

template <typename Var>
type_traits::Identity<Var> flatten(const Var &x)

Changes the Shape of the variable to the column vector.

Return
A new variable.
Parameters
  • x: A variable with an old Shape.

template <typename Var>
type_traits::Identity<Var> transpose(const Var &x)

Applies a matrix transposition.

Return
A new variable representing \( X^\top \).
Parameters
  • x: A variable representing an argument \( X \). The shape of x must be either a scalar, a column vector or a matrix.

template <typename Var>
type_traits::Identity<Var> flip(const Var &x, std::uint32_t dim)

Flips elements along an axis. Following examples show how this function work:

\[\begin{split} \begin{array}{lcl} x & := & \left( \begin{array}{ccc} 1 & 4 & 7 \\ 2 & 5 & 8 \\ 3 & 6 & 9 \end{array} \right), \\ \mathrm{flip}(x, 0) & = & \left( \begin{array}{ccc} 3 & 6 & 9 \\ 2 & 5 & 8 \\ 1 & 4 & 7 \end{array} \right), \\ \mathrm{flip}(x, 1) & = & \left( \begin{array}{c} 7 & 4 & 1 \\ 8 & 5 & 2 \\ 9 & 6 & 3 \end{array} \right), \\ \mathrm{flip}(x, 2) & = & \left( \begin{array}{ccc} 1 & 4 & 7 \\ 2 & 5 & 8 \\ 3 & 6 & 9 \end{array} \right). \end{array} \end{split}\]
Return
A new variable representing \( \mathrm{flip} (x) \).
Parameters
  • x: A variable representing an argument \( x \). The shape of x must be either a scalar, a column vector or a matrix.

template <typename Var>
type_traits::Identity<Var> permute_dims(const Var &x, const std::vector<std::uint32_t> &perm)

Permutes dimensions of a tensor.

\[\begin{split} \begin{array}{lcl} x & := & \left( \left( \begin{array}{cc} 1 & 4 \\ 2 & 5 \\ 3 & 6 \end{array} \right), \left( \begin{array}{cc} 11 & 14 \\ 12 & 15 \\ 13 & 16 \end{array} \right), \left( \begin{array}{cc} 21 & 24 \\ 22 & 25 \\ 23 & 26 \end{array} \right), \left( \begin{array}{cc} 31 & 34 \\ 32 & 35 \\ 33 & 36 \end{array} \right) \right), \\ \mathrm{permute\_dims}(x, [1, 2, 0]) & = & \left( \left( \begin{array}{cccc} 1 & 11 & 21 & 31 \\ 4 & 14 & 24 & 34 \end{array} \right), \left( \begin{array}{cccc} 2 & 12 & 22 & 32 \\ 5 & 15 & 25 & 35 \end{array} \right), \left( \begin{array}{cccc} 3 & 13 & 23 & 33 \\ 6 & 16 & 26 & 36 \end{array} \right) \right), \\ \mathrm{permute\_dims}(x, [2, 0, 1]) & = & \left( \left( \begin{array}{ccc} 1 & 2 & 3 \\ 11 & 12 & 13 \\ 21 & 22 & 23 \\ 31 & 32 & 33 \end{array} \right), \left( \begin{array}{ccc} 4 & 5 & 6 \\ 14 & 15 & 16 \\ 24 & 35 & 36 \\ 34 & 35 & 36 \end{array} \right) \right), \\ \mathrm{permute\_dims}(x, [0, 1, 2]) & = & x. \\ \end{array} \end{split}\]
Return
A new variable.
Parameters
  • x: A variable representing an original data.
  • perm: A list of dimensions for specifying permutation.

template <typename Var>
type_traits::Identity<Var> permute_dims(const Var &x, const std::initializer_list<std::uint32_t> perm)

Permutes dimensions of a tensor.

Return
A new variable.
Parameters
  • x: A variable representing an original data.
  • perm: A list of dimensions for specifying permutation.

template <typename Var>
type_traits::Identity<Var> matmul(const Var &a, const Var &b)

Applies a matrix multiplication between two matrices.

Return
A new variable representing \( AB \).
Parameters
  • a: A variable representing an argument \( A \). The shape of a must be either a scalar, a column vector or a matrix.
  • b: A variable representing an argument \( B \). The shape of b must be either a scalar, a column vector or a matrix, and b.shape()[0] must be equal to a.shape()[1].

template <typename Var>
type_traits::Identity<Var> abs(const Var &x)

Applies an elementwise absolute function.

Return
A variable representing \( \vert x \vert \).
Parameters
  • x: A variable representing an argument \( x \).

template <typename Var>
type_traits::Identity<Var> sqrt(const Var &x)

Applies an elementwise square root function.

Return
A variable representing \( \sqrt{x} \).
Parameters
  • x: A variable representing an argument \( x \).

template <typename Var>
type_traits::Identity<Var> exp(const Var &x)

Applies an elementwise exponential function.

Return
A variable representing \( e^x \).
Parameters
  • x: A variable representing an argument \( x \).

template <typename Var>
type_traits::Identity<Var> log(const Var &x)

Applies an elementwise natural logarithm function.

Return
A variable representing \( \ln (x) \).
Parameters
  • x: A variable representing an argument \( x \).

template <typename Var>
type_traits::Identity<Var> tanh(const Var &x)

Applies an elementwise hyperbolic tangent function.

Return
A variable representing \( \tanh (x) \).
Parameters
  • x: A variable representing an argument \( x \).

template <typename Var>
type_traits::Identity<Var> sigmoid(const Var &x)

Applies an elementwise logistic sigmoid function:

\[ \mathrm{sigmoid}(x) := \frac{1}{1 + e^{-x}}. \]
Return
A variable representing \( \mathrm{sigmoid}(x) \).
Parameters
  • x: A variable representing an argument \( x \).

template <typename Var>
type_traits::Identity<Var> softplus(const Var &x)

Applies an elementwise softplus function:

\[ \mathrm{softplus}(x) := \ln (1 + e^x). \]
Return
A variable representing \( \mathrm{softplus}(x) \).
Parameters
  • x: A variable representing an argument \( x \).

template <typename Var>
type_traits::Identity<Var> sin(const Var &x)

Applies an elementwise sin function.

Return
A variable representing \( \sin (x) \).
Parameters
  • x: A variable representing an argument \( x \).

template <typename Var>
type_traits::Identity<Var> cos(const Var &x)

Applies an elementwise cos function.

Return
A variable representing \( \cos (x) \).
Parameters
  • x: A variable representing an argument \( x \).

template <typename Var>
type_traits::Identity<Var> tan(const Var &x)

Applies an elementwise tangent function.

Return
A variable representing \( \tan (x) \).
Parameters
  • x: A variable representing an argument \( x \).

template <typename Var>
type_traits::Identity<Var> relu(const Var &x)

Applies an elementwise rectified linear unit (ReLU) function:

\[ \mathrm{ReLU}(x) := \max (x, 0). \]
Return
A variable representing \( \mathrm{ReLU}(x) \).
Parameters
  • x: A variable representing an argument \( x \).

template <typename Var>
type_traits::Identity<Var> lrelu(const Var &x)

Applies an elementwise leaky ReLU function:

\[ \mathrm{LReLU}(x) := \max (x, 0.01x). \]
Return
A variable representing \( \mathrm{LReLU}(x) \).
Parameters
  • x: A variable representing an argument \( x \).

template <typename Var>
type_traits::Identity<Var> prelu(const Var &x, float a)

Applies an elementwise parameterized ReLU function:

\[\begin{split} \mathrm{PReLU}(x) := \left\{ \begin{array}{ll} x, & \mathrm{if} \ x \geq 0, \\ \alpha x, & \mathrm{otherwise}. \end{array} \right. \end{split}\]
Return
A variable representing \( \mathrm{PReLU}(x) \).
Parameters
  • x: A variable representing an argument \( x \).
  • a: A scaling factor \( \alpha \).

template <typename Var>
type_traits::Identity<Var> elu(const Var &x, float a)

Applies an elementwise exponential linear unit (ELU) function:

\[\begin{split} \mathrm{ELU}(x) := \left\{ \begin{array}{ll} x, & \mathrm{if} \ x \geq 0, \\ \alpha (e^x - 1), & \mathrm{otherwise}. \end{array} \right. \end{split}\]
Return
A variable representing \( \mathrm{ELU}(x) \).
Parameters
  • x: A variable representing an argument \( x \).
  • a: A scaling factor \( \alpha \).

template <typename Var>
type_traits::Identity<Var> max(const Var &x, std::uint32_t dim)

Retrieves maximum values along an axis. Following examples show how this function work:

\[\begin{split} \begin{array}{lcl} x & := & \left( \begin{array}{ccc} 1 & 6 & 7 \\ 2 & 5 & 9 \\ 3 & 4 & 8 \end{array} \right), \\ \mathrm{max}(x, 0) & = & \left( \begin{array}{ccc} 3 & 6 & 9 \end{array} \right), \\ \mathrm{max}(x, 1) & = & \left( \begin{array}{c} 7 \\ 9 \\ 8 \end{array} \right), \\ \mathrm{max}(x, 2) & = & \left( \begin{array}{ccc} 1 & 6 & 7 \\ 2 & 5 & 9 \\ 3 & 4 & 8 \end{array} \right). \end{array} \end{split}\]
Return
A new variable.
Parameters
  • x: A variable representing values before reduction.
  • dim: Axis to be processed.

template <typename Var>
type_traits::Identity<Var> min(const Var &x, std::uint32_t dim)

Retrieves minimum values along an axis. Following examples show how this function work:

\[\begin{split} \begin{array}{lcl} x & := & \left( \begin{array}{ccc} 1 & 6 & 7 \\ 2 & 5 & 9 \\ 3 & 4 & 8 \end{array} \right), \\ \mathrm{min}(x, 0) & = & \left( \begin{array}{ccc} 1 & 4 & 7 \end{array} \right), \\ \mathrm{min}(x, 1) & = & \left( \begin{array}{c} 1 \\ 2 \\ 3 \end{array} \right), \\ \mathrm{min}(x, 2) & = & \left( \begin{array}{ccc} 1 & 6 & 7 \\ 2 & 5 & 9 \\ 3 & 4 & 8 \end{array} \right). \end{array} \end{split}\]
Return
A new variable.
Parameters
  • x: A variable representing values before reduction.
  • dim: Axis to be processed.

template <typename Var>
type_traits::Identity<Var> sum(const Var &x, std::uint32_t dim)

Applies summation along an axis. Following examples show how this function work:

\[\begin{split} \begin{array}{lcl} x & := & \left( \begin{array}{ccc} 1 & 4 & 7 \\ 2 & 5 & 8 \\ 3 & 6 & 9 \end{array} \right), \\ \mathrm{sum}(x, 0) & = & \left( \begin{array}{ccc} 6 & 15 & 24 \end{array} \right), \\ \mathrm{sum}(x, 1) & = & \left( \begin{array}{c} 12 \\ 15 \\ 18 \end{array} \right), \\ \mathrm{sum}(x, 2) & = & \left( \begin{array}{ccc} 1 & 4 & 7 \\ 2 & 5 & 8 \\ 3 & 6 & 9 \end{array} \right). \end{array} \end{split}\]
Return
A new variable.
Parameters
  • x: A variable representing values before reduction.
  • dim: Axis to be processed.

template <typename Var>
type_traits::Identity<Var> broadcast(const Var &x, std::uint32_t dim, std::uint32_t size)

Applies broadcasting along an axis. Following examples show how this function work:

\[\begin{split} \begin{array}{lcl} x_1 & := & \left( \begin{array}{ccc} 1 & 2 & 3 \end{array} \right), \\ \mathrm{broadcast}(x_1, 0, 3) & = & \left( \begin{array}{ccc} 1 & 2 & 3 \\ 1 & 2 & 3 \\ 1 & 2 & 3 \end{array} \right), \\ x_2 & := & \left( \begin{array}{c} 1 \\ 2 \\ 3 \end{array} \right), \\ \mathrm{broadcast}(x_2, 1, 3) & = & \left( \begin{array}{ccc} 1 & 1 & 1 \\ 2 & 2 & 2 \\ 3 & 3 & 3 \end{array} \right), \\ \mathrm{broadcast}(x_2, 2, 3) & = & \left( \left( \begin{array}{c} 1 \\ 2 \\ 3 \end{array} \right), \left( \begin{array}{c} 1 \\ 2 \\ 3 \end{array} \right), \left( \begin{array}{c} 1 \\ 2 \\ 3 \end{array} \right) \right). \end{array} \end{split}\]
Return
A new variable.
Parameters
  • x: A variable representing values before reduction.
  • dim: Axis to be processed.
  • size: New size of the axis dim.

template <typename Var>
type_traits::Identity<Var> logsumexp(const Var &x, std::uint32_t dim)

Applies a logsumexp reduction along an axis. This function performs similarly to primitiv::functions::sum w.r.t. the axis.

Return
A new variable.
Parameters
  • x: A variable representing values before expansion.
  • dim: Axis to be processed.

template <typename Var>
type_traits::Identity<Var> log_softmax(const Var &x, std::uint32_t dim)

Applies a softmax operation along an axis, and returns the natural logarithm of resulting values.

Return
A new variable.
Parameters
  • x: A variable representing original values.
  • dim: Axis to be processed.

template <typename Var>
type_traits::Identity<Var> softmax(const Var &x, std::uint32_t dim)

Applies a softmax operation along an axis.

Return
A new variable.
Parameters
  • x: A variable representing original values.
  • dim: Axis to be processed.

template <typename Var>
type_traits::Identity<Var> softmax_cross_entropy(const Var &x, const Var &t, std::uint32_t dim)

Applies a softmax cross entropy function between two variables along an axis.

Return
A new variable.
Parameters
  • x: A variable representing logit values.
  • t: A variable representing ground-truth distribution along the axis dim.
  • dim: Axis to be processed.

template <typename Var>
type_traits::Identity<Var> softmax_cross_entropy(const Var &x, const std::vector<std::uint32_t> &ids, std::uint32_t dim)

Applies a softmax cross entropy function between logits and one-hot distributions along an axis.

Return
A new variable.
Parameters
  • x: A variable representing logit values.
  • ids: List of one-hot IDs along the axis dim. Each value must be lower than x.shape()[dim].
  • dim: Axis to be processed.

template <typename Var>
type_traits::Identity<Var> stop_gradient(const Var &x)

Blocks the gradient propagation beyond this function. This function does not modify any values in the input variable, and force to make all gradients \( 0 \).

Return
A new variable.
Parameters
  • x: A variable representing original values.

template <typename Var>
type_traits::Identity<Var> conv2d(const Var &x, const Var &w, std::uint32_t padding0, std::uint32_t padding1, std::uint32_t stride0, std::uint32_t stride1, std::uint32_t dilation0, std::uint32_t dilation1)

Applies a 2D convolution between two variables.

Return
A new variable with Shape \( [d'_0, d'_1, c_2] \). The first and second dimension are calculated as following:
\[ d'_i := \frac{d_i + 2 \times \mathrm{padding}_i - (u_i - 1) \times \mathrm{dilation}_i + 1}{\mathrm{stride}_i} + 1. \]
Parameters
  • x: A variable with Shape \( [d_0, d_1, c_1] \).
  • w: A variable with Shape \( [u_0, u_1, c_1, c_2] \).
  • padding0: Width of zero-padding along the first axis.
  • padding1: Width of zero-padding along the second axis.
  • stride0: Stride along the first axis.
  • stride1: Stride along the second axis.
  • dilation0: Dilation factor along the first axis.
  • dilation1: Dilation factor along the second axis.

template <typename Var>
type_traits::Identity<Var> max_pool2d(const Var &x, std::uint32_t window0, std::uint32_t window1, std::uint32_t padding0, std::uint32_t padding1, std::uint32_t stride0, std::uint32_t stride1)

Applies a 2D max-pooling operation.

Return
A new variable with Shape \( [d'_0, d'_1, c] \). The first and second dimension are calculated as following:
\[ d'_i := \frac{d_i + 2 \times \mathrm{padding}_i - \mathrm{window}_i}{\mathrm{stride}_i} + 1. \]
Parameters
  • x: A variable with Shape \( [d_0, d_1, c] \).
  • window0: Window size along the first axis.
  • window1: Window size along the second axis.
  • padding0: Width of \( -\infty \) padding along the first axis.
  • padding1: Width of \( -\infty \) padding along the second axis.
  • stride0: Stride along the first axis.
  • stride1: Stride along the second axis.

Tensor constant_tensor(const Shape &shape, float k, Device *dev)

Creates a new Tensor with all values the constant \( k \).

Return
A new Tensor.
Parameters
  • shape: Shape of the new Tensor.
  • k: The constant \( k \) of values in the Tensor.
  • dev: Device to manage the new Tensor, or nullptr to use the default device.

Node constant_node(const Shape &shape, float k, Device *dev, Graph *g)

Creates a new Node with all values the constant \( k \).

Return
A new Node.
Parameters
  • shape: Shape of the new Node.
  • k: The constant \( k \) of values in the Node.
  • dev: Device to manage the new Node, or nullptr to use the default device.
  • g: Graph to manage the instance of the Node, or nullptr to use the default graph.

template <typename Var>
type_traits::Identity<Var> constant(const Shape &shape, float k, Device *dev)

Creates a new variable with all values the constant \( k \).

Return
A new variable.
Remark
This function uses the default graph when specifying Node as the template variable.
Parameters
  • shape: Shape of the new variable.
  • k: The constant \( k \) of values in the variable.
  • dev: Device to manage the new variable, or nullptr to use the default device.

template <typename Var>
type_traits::Identity<Var> constant(const Shape &shape, float k, Device &dev)

Creates a new variable with all values the constant \( k \).

Remark
This function uses the default graph when specifying Node as the template variable.
Parameters
  • shape: Shape of the new variable.
  • k: The constant \( k \) of values in the variable.
  • dev: Device to manage the new variable.

template <typename Var>
type_traits::Identity<Var> constant(const Shape &shape, float k)

Creates a new variable with all values the constant \( k \).

Remark
This function always uses the default device, and also uses the default graph when specifying Node as the template variable.
Parameters
  • shape: Shape of the new variable.
  • k: The constant \( k \) of values in the variable.

Tensor identity_tensor(std::uint32_t size, Device *dev)

Creates a new Tensor with an \( N \)-dimensional identity matrix.

Return
A new Tensor.
Parameters
  • size: Size \( N \) of the matrix in the new Tensor.
  • dev: Device to manage the new Tensor, or nullptr to use the default device.

Node identity_node(std::uint32_t size, Device *dev, Graph *g)

Creates a new Node with an \( N \)-dimensional identity matrix.

Return
A new Node.
Parameters
  • size: Size \( N \) of the matrix in the new Node.
  • dev: Device to manage the new Node, or nullptr to use the default device.
  • g: Graph to manage the instance of the Node, or nullptr to use the default graph.

template <typename Var>
type_traits::Identity<Var> identity(std::uint32_t size, Device *dev)

Creates a new variable with an \( N \)-dimensional identity matrix.

Return
A new variable.
Remark
This function uses the default graph when specifying Node as the template variable.
Parameters
  • size: Size \( N \) of the matrix in the new Node.
  • dev: Device to manage the new variable, or nullptr to use the default device.

template <typename Var>
type_traits::Identity<Var> identity(std::uint32_t size, Device &dev)

Creates a new variable with an \( N \)-dimensional identity matrix.

Return
A new variable.
Remark
This function uses the default graph when specifying Node as the template variable.
Parameters
  • size: Size \( N \) of the matrix in the new Node.
  • dev: Device to manage the new variable.

template <typename Var>
type_traits::Identity<Var> identity(std::uint32_t size)

Creates a new variable with an \( N \)-dimensional identity matrix.

Return
A new variable.
Remark
This function always uses the default device, and also uses the default graph when specifying Node as the template variable.
Parameters
  • size: Size \( N \) of the matrix in the new Node.

namespace batch

Functions

template <typename Var>
type_traits::Identity<Var> mean(const Var &x)

Calculates means along the minibatch.

Return
A new variable.
Remark
This function is implemented as a composite of some other functions.
Parameters
  • x: A variable representing values before reduction.

template <typename Var>
type_traits::Identity<Var> normalize(const Var &x)

Applies the batch normalization:

\[\begin{split} \begin{array}{rcl} m_x & := & \frac{1}{B} \sum_{i=1}^{B} x_i, \\ v_x & := & \frac{B}{B - 1} \left( \frac{1}{B} \sum_{i=0}^{B} x_i^2 - m_x^2 \right), \\ \mathrm{batch::normalize}(x) & := & \frac{x - m_x}{\sqrt{v_x + \epsilon}}, \end{array} \end{split}\]
where \( B \) is the minibatch size of \( x \).
Return
A new variable.
Remark
This function is implemented as a composite of some other functions.
Parameters
  • x: A variable representing values before normalization.

template <typename Var>
type_traits::Identity<Var> pick(const Var &x, const std::vector<std::uint32_t> &ids)

Selects items from the batch by IDs.

Return
A new variable.
Parameters
  • x: A variable representing an original data.
  • ids: List of batch IDs. Each value must be lower than x.shape().batch().

template <typename Var>
type_traits::Identity<Var> slice(const Var &x, std::uint32_t lower, std::uint32_t upper)

Extracts a specific range \( [L, U) \) of items along the batch axis.

Return
A new variable.
Parameters
  • x: A variable representing an original data.
  • lower: Lower bound \( L \) of the batch.
  • upper: Upper bound \( U \) of the batch.

template <typename Var>
std::vector<type_traits::Identity<Var>> split(const Var &x, std::uint32_t n)

Splits a given variable into specified number of partitions along the batch.

Return
A list of n variables. Each variable is identical with: batch::split(x, n)[i] == batch::slice(x, L(i), U(i)), where L(i) := i * x.shape().batch() / n and U(i) := (i + 1) * x.shape().batch() / n.
Parameters
  • x: A variable representing an original data.
  • n: The number of resulting partitions. n should be able to divide x.shape().batch() without residue.
Exceptions
  • primitiv::Error: n can not divide s.shape().batch() without residue.

template <typename Container>
type_traits::Reduce<Container> concat(const Container &xs)

Concatenates multiple variables along the batch axis.

Return
A new variable.
Parameters
  • xs: Iterable container of variables. xs must have both begin() and end() functions that return the begin/end iterators.

template <typename Container>
type_traits::ReducePtr<Container> concat(const Container &xs)

Same as above, but xs has pointers of variables.

Return
A new variable.
Parameters
  • xs: Iterable container of pointers of variables. xs must have both begin() and end() functions that return the begin/end iterators.

template <typename Var>
type_traits::Identity<Var> sum(const Var &x)

Applies summation along the minibatch. Following example shows how this function work:

\[\begin{split} \begin{array}{lcl} x & := & \left( \begin{array}{ccc} 1 & 4 & 7 \\ 2 & 5 & 8 \\ 3 & 6 & 9 \end{array} \right), \left( \begin{array}{ccc} 11 & 14 & 17 \\ 12 & 15 & 18 \\ 13 & 16 & 19 \end{array} \right), \\ \mathrm{batch::sum}(x) & = & \left( \begin{array}{ccc} 12 & 18 & 24 \\ 14 & 20 & 26 \\ 16 & 22 & 28 \end{array} \right). \end{array} \end{split}\]
Return
A new variable.
Parameters
  • x: A variable representing values before reduction.

namespace random

Functions

Tensor bernoulli_tensor(const Shape &shape, float p, Device *dev)

Creates a new Tensor with values sampled from the Bernoulli distribution.

Return
A new Tensor.
Parameters
  • shape: Shape of the new Tensor.
  • p: The parameter \( p \) of the Bernoulli distribution.
  • dev: Device to manage the new Tensor, or nullptr to use the default device.

Node bernoulli_node(const Shape &shape, float p, Device *dev, Graph *g)

Creates a new Node with values sampled from the Bernoulli distribution.

Return
A new Node.
Parameters
  • shape: Shape of the new Node.
  • p: The parameter \( p \) of the Bernoulli distribution.
  • dev: Device to manage the new Node, or nullptr to use the default device.
  • g: Graph to manage the instance of the Node, or nullptr to use the default graph.

template <typename Var>
type_traits::Identity<Var> bernoulli(const Shape &shape, float p, Device *dev)

Creates a new variable with values sampled from the Bernoulli distribution.

Return
A new variable.
Remark
This function uses the default graph when specifying Node as the template variable.
Parameters
  • shape: Shape of the new variable.
  • p: The parameter \( p \) of the Bernoulli distribution.
  • dev: Device to manage the new variable, or nullptr to use the default device.

template <typename Var>
type_traits::Identity<Var> bernoulli(const Shape &shape, float p, Device &dev)

Creates a new variable with values sampled from the Bernoulli distribution.

Return
A new variable.
Remark
This function uses the default graph when specifying Node as the template variable.
Parameters
  • shape: Shape of the new variable.
  • p: The parameter \( p \) of the Bernoulli distribution.
  • dev: Device to manage the new variable.

template <typename Var>
type_traits::Identity<Var> bernoulli(const Shape &shape, float p)

Creates a new variable with values sampled from the Bernoulli distribution.

Return
A new variable.
Remark
This function always uses the default device, and also uses the default graph when specifying Node as the template variable.
Parameters
  • shape: Shape of the new variable.
  • p: The parameter \( p \) of the Bernoulli distribution.

Tensor uniform_tensor(const Shape &shape, float lower, float upper, Device *dev)

Creates a new Tensor with values sampled from the uniform distribution.

Return
A new Tensor.
Parameters
  • shape: Shape of the new Tensor.
  • lower: The lower bound \( L \) of the uniform distribution.
  • upper: The upper bound \( U \) of the uniform distribution.
  • dev: Device to manage the new Tensor, or nullptr to use the default device.

Node uniform_node(const Shape &shape, float lower, float upper, Device *dev, Graph *g)

Creates a new Node with values sampled from the uniform distribution.

Return
A new Node.
Parameters
  • shape: Shape of the new Node.
  • lower: The lower bound \( L \) of the uniform distribution.
  • upper: The upper bound \( U \) of the uniform distribution.
  • dev: Device to manage the new Node, or nullptr to use the default device.
  • g: Graph to manage the instance of the Node, or nullptr to use the default graph.

template <typename Var>
type_traits::Identity<Var> uniform(const Shape &shape, float lower, float upper, Device *dev)

Creates a new variable with values sampled from the uniform distribution.

Return
A new variable.
Remark
This function uses the default graph when specifying Node as the template variable.
Parameters
  • shape: Shape of the new variable.
  • lower: The lower bound \( L \) of the uniform distribution.
  • upper: The upper bound \( U \) of the uniform distribution.
  • dev: Device to manage the new variable, or nullptr to use the default device.

template <typename Var>
type_traits::Identity<Var> uniform(const Shape &shape, float lower, float upper, Device &dev)

Creates a new variable with values sampled from the uniform distribution.

Return
A new variable.
Remark
This function uses the default graph when specifying Node as the template variable.
Parameters
  • shape: Shape of the new variable.
  • lower: The lower bound \( L \) of the uniform distribution.
  • upper: The upper bound \( U \) of the uniform distribution.
  • dev: Device to manage the new variable.

template <typename Var>
type_traits::Identity<Var> uniform(const Shape &shape, float lower, float upper)

Creates a new variable with values sampled from the uniform distribution.

Return
A new variable.
Remark
This function always uses the default device, and also uses the default graph when specifying Node as the template variable.
Parameters
  • shape: Shape of the new variable.
  • lower: The lower bound \( L \) of the uniform distribution.
  • upper: The upper bound \( U \) of the uniform distribution.

Tensor normal_tensor(const Shape &shape, float mean, float sd, Device *dev)

Creates a new Tensor with values sampled from the normal distribution.

Return
A new Tensor.
Parameters
  • shape: Shape of the new Tensor.
  • mean: The mean \( \mu \) of the normal distribution.
  • sd: The standard deviation \( \sigma \) of the normal distribution.
  • dev: Device to manage the new Tensor, or nullptr to use the default device.

Node normal_node(const Shape &shape, float mean, float sd, Device *dev, Graph *g)

Creates a new Node with values sampled from the normal distribution.

Return
A new Node.
Parameters
  • shape: Shape of the new Node.
  • mean: The mean \( \mu \) of the normal distribution.
  • sd: The standard deviation \( \sigma \) of the normal distribution.
  • dev: Device to manage the new Node, or nullptr to use the default device.
  • g: Graph to manage the instance of the Node, or nullptr to use the default graph.

template <typename Var>
type_traits::Identity<Var> normal(const Shape &shape, float mean, float sd, Device *dev)

Creates a new variable with values sampled from the normal distribution.

Return
A new variable.
Remark
This function uses the default graph when specifying Node as the template variable.
Parameters
  • shape: Shape of the new variable.
  • mean: The mean \( \mu \) of the normal distribution.
  • sd: The standard deviation \( \sigma \) of the normal distribution.
  • dev: Device to manage the new variable, or nullptr to use the default device.

template <typename Var>
type_traits::Identity<Var> normal(const Shape &shape, float mean, float sd, Device &dev)

Creates a new variable with values sampled from the normal distribution.

Return
A new variable.
Remark
This function uses the default graph when specifying Node as the template variable.
Parameters
  • shape: Shape of the new variable.
  • mean: The mean \( \mu \) of the normal distribution.
  • sd: The standard deviation \( \sigma \) of the normal distribution.
  • dev: Device to manage the new variable.

template <typename Var>
type_traits::Identity<Var> normal(const Shape &shape, float mean, float sd)

Creates a new variable with values sampled from the normal distribution.

Return
A new variable.
Remark
This function always uses the default device, and also uses the default graph when specifying Node as the template variable.
Parameters
  • shape: Shape of the new variable.
  • mean: The mean \( \mu \) of the normal distribution.
  • sd: The standard deviation \( \sigma \) of the normal distribution.

Tensor log_normal_tensor(const Shape &shape, float mean, float sd, Device *dev)

Creates a new Tensor with values sampled from the log-normal distribution.

Return
A new Tensor.
Parameters
  • shape: Shape of the new Tensor.
  • mean: The parameter \( \mu \) of the log-normal distribution.
  • sd: The parameter \( \sigma \) of the log-normal distribution.
  • dev: Device to manage the new Tensor, or nullptr to use the default device.

Node log_normal_node(const Shape &shape, float mean, float sd, Device *dev, Graph *g)

Creates a new Node with values sampled from the log-normal distribution.

Return
A new Node.
Parameters
  • shape: Shape of the new Node.
  • mean: The parameter \( \mu \) of the log-normal distribution.
  • sd: The parameter \( \sigma \) of the log-normal distribution.
  • dev: Device to manage the new Node, or nullptr to use the default device.
  • g: Graph to manage the instance of the Node, or nullptr to use the default graph.

template <typename Var>
type_traits::Identity<Var> log_normal(const Shape &shape, float mean, float sd, Device &dev)

Creates a new variable with values sampled from the log-normal distribution.

Creates a new variable with values sampled from the log-normal distribution.

Return
A new variable.
Remark
This function uses the default graph when specifying Node as the template variable.
Return
A new variable.
Remark
This function uses the default graph when specifying Node as the template variable.
Parameters
  • shape: Shape of the new variable.
  • mean: The parameter \( \mu \) of the log-normal distribution.
  • sd: The parameter \( \sigma \) of the log-normal distribution.
  • dev: Device to manage the new variable, or nullptr to use the default device.
Parameters
  • shape: Shape of the new variable.
  • mean: The parameter \( \mu \) of the log-normal distribution.
  • sd: The parameter \( \sigma \) of the log-normal distribution.
  • dev: Device to manage the new variable.

template <typename Var>
type_traits::Identity<Var> log_normal(const Shape &shape, float mean, float sd)

Creates a new variable with values sampled from the log-normal distribution.

Return
A new variable.
Remark
This function always uses the default device, and also uses the default graph when specifying Node as the template variable.
Parameters
  • shape: Shape of the new variable.
  • mean: The parameter \( \mu \) of the log-normal distribution.
  • sd: The parameter \( \sigma \) of the log-normal distribution.

Tensor gumbel_tensor(const Shape &shape, float mu, float beta, Device *dev)

Creates a new Tensor with values sampled from the Gumbel distribution.

Return
A new Tensor.
Parameters
  • shape: Shape of the new Tensor.
  • mu: The location parameter \( \mu \) of the Gumbel distribution.
  • beta: The scale parameter \( \beta \) of the Gumbel distribution.
  • dev: Device to manage the new Tensor, or nullptr to use the default device.

Node gumbel_node(const Shape &shape, float mu, float beta, Device *dev, Graph *g)

Creates a new Node with values sampled from the Gumbel distribution.

Return
A new Node.
Parameters
  • shape: Shape of the new Node.
  • mu: The location parameter \( \mu \) of the Gumbel distribution.
  • beta: The scale parameter \( \beta \) of the Gumbel distribution.
  • dev: Device to manage the new Node, or nullptr to use the default device.
  • g: Graph to manage the instance of the Node, or nullptr to use the default graph.

template <typename Var>
type_traits::Identity<Var> gumbel(const Shape &shape, float mu, float beta, Device *dev)

Creates a new variable with values sampled from the Gumbel distribution.

Return
A new variable.
Remark
This function uses the default graph when specifying Node as the template variable.
Parameters
  • shape: Shape of the new variable.
  • mu: The location parameter \( \mu \) of the Gumbel distribution.
  • beta: The scale parameter \( \beta \) of the Gumbel distribution.
  • dev: Device to manage the new variable, or nullptr to use the default device.

template <typename Var>
type_traits::Identity<Var> gumbel(const Shape &shape, float mu, float beta, Device &dev)

Creates a new variable with values sampled from the Gumbel distribution.

Return
A new variable.
Remark
This function uses the default graph when specifying Node as the template variable.
Parameters
  • shape: Shape of the new variable.
  • mu: The location parameter \( \mu \) of the Gumbel distribution.
  • beta: The scale parameter \( \beta \) of the Gumbel distribution.
  • dev: Device to manage the new variable.

template <typename Var>
type_traits::Identity<Var> gumbel(const Shape &shape, float mu, float beta)

Creates a new variable with values sampled from the Gumbel distribution.

Return
A new variable.
Remark
This function always uses the default device, and also uses the default graph when specifying Node as the template variable.
Parameters
  • shape: Shape of the new variable.
  • mu: The location parameter \( \mu \) of the Gumbel distribution.
  • beta: The scale parameter \( \beta \) of the Gumbel distribution.