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 bothbegin()
andend()
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 bothbegin()
endend()
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 bothbegin()
andend()
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 bothbegin()
endend()
functions that return the begin/end iterators.
-
Node
zeros_node
(const Shape &shape, Device *dev, Graph *g)¶ Creates a new Node with all values \( 0 \).
-
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
-
template <typename Var>
type_traits::Identity<Var>zeros
(const Shape &shape, Device &dev)¶ Creates a new variable with all values \( 0 \).
-
template <typename Var>
type_traits::Identity<Var>zeros
(const Shape &shape)¶ Creates a new variable with all values \( 0 \).
-
Node
ones_node
(const Shape &shape, Device *dev, Graph *g)¶ Creates a new Node with all values \( 1 \).
-
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
-
template <typename Var>
type_traits::Identity<Var>ones
(const Shape &shape, Device &dev)¶ Creates a new variable with all values \( 1 \).
-
template <typename Var>
type_traits::Identity<Var>ones
(const Shape &shape)¶ Creates a new variable with all values \( 1 \).
-
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 and1
discards all values.enabled
: Iftrue
, 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.
-
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 toshape.size()
and each data is ordered by the column-major order.dev
: Device to manage inner data of the Node, ornullptr
to use the default device.g
: Graph to manage the instance of the Node, ornullptr
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
-
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
-
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 toshape.size()
and each data is ordered by the column-major order.
-
template <typename Var>
type_traits::Identity<Var>parameter
(Parameter ¶m)¶ Creates a new variable from a specific Parameter.
-
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, ornullptr
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 ofx
and the number of values inids
:\[\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 axisdim
. Each value must be lower thanx.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 \) ofdim
.upper
: Upper bound \( U \) ofdim
.
-
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))
, whereL(i) := i * x.shape()[dim] / n
andU(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 dividex.shape()[dim]
without residue.
- Exceptions
primitiv::Error
:n
can not divides.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 bothbegin()
andend()
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 bothbegin()
andend()
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.
-
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 ofx
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 ofx
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 ofa
must be either a scalar, a column vector or a matrix.b
: A variable representing an argument \( B \). The shape ofb
must be either a scalar, a column vector or a matrix, andb.shape()[0]
must be equal toa.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 axisdim
.
-
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 axisdim
.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 axisdim
. Each value must be lower thanx.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 \).
-
Node
constant_node
(const Shape &shape, float k, Device *dev, Graph *g)¶ Creates a new Node with all values the constant \( k \).
-
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 \).
-
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 \).
-
template <typename Var>
type_traits::Identity<Var>constant
(const Shape &shape, float k)¶ Creates a new variable with all values the constant \( k \).
-
Tensor
identity_tensor
(std::uint32_t size, Device *dev)¶ Creates a new Tensor with an \( N \)-dimensional identity matrix.
-
Node
identity_node
(std::uint32_t size, Device *dev, Graph *g)¶ Creates a new Node with an \( N \)-dimensional identity matrix.
-
template <typename Var>
type_traits::Identity<Var>identity
(std::uint32_t size, Device *dev)¶ Creates a new variable with an \( N \)-dimensional identity matrix.
-
template <typename Var>
type_traits::Identity<Var>identity
(std::uint32_t size, Device &dev)¶ Creates a new variable with an \( N \)-dimensional identity matrix.
-
template <typename Var>
type_traits::Identity<Var>identity
(std::uint32_t size)¶ Creates a new variable with an \( N \)-dimensional identity matrix.
-
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 thanx.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))
, whereL(i) := i * x.shape().batch() / n
andU(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 dividex.shape().batch()
without residue.
- Exceptions
primitiv::Error
:n
can not divides.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 bothbegin()
andend()
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 bothbegin()
andend()
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.
-
template <typename Var>
-
namespace
random
¶ Functions
-
Tensor
bernoulli_tensor
(const Shape &shape, float p, Device *dev)¶ Creates a new Tensor with values sampled from the Bernoulli distribution.
-
Node
bernoulli_node
(const Shape &shape, float p, Device *dev, Graph *g)¶ Creates a new Node with values sampled from the Bernoulli distribution.
-
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.
-
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.
-
template <typename Var>
type_traits::Identity<Var>bernoulli
(const Shape &shape, float p)¶ Creates a new variable with values sampled from 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.
-
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, ornullptr
to use the default device.g
: Graph to manage the instance of the Node, ornullptr
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
-
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
-
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.
-
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, ornullptr
to use the default device.g
: Graph to manage the instance of the Node, ornullptr
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
-
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
-
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.
-
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, ornullptr
to use the default device.g
: Graph to manage the instance of the Node, ornullptr
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.
-
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.
-
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, ornullptr
to use the default device.g
: Graph to manage the instance of the Node, ornullptr
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
-
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
-
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.
-
Tensor
-
template <typename Var>