Tensorex.Operator (tensorex v0.5.1) View Source

Functions for basic arithmetic operations with tensors.

Link to this section Summary

Functions

Adds two tensors.

Divides all elements of the tensor by the scalar.

Makes a product of tensors.

Makes a dot product of tensors.

Negates a tensor.

Subtracts a tensor from another.

Returns a transposed tensor.

Link to this section Functions

Specs

Adds two tensors.

iex> Tensorex.Operator.add(
...>   Tensorex.from_list([[0,  1  ,  2  ],
...>                       [3, -4  , -5.5]]),
...>   Tensorex.from_list([[3, -2  , -2  ],
...>                       [6, -8.1, 12  ]]))
%Tensorex{data: %{[0, 0] => 3, [0, 1] =>  -1,
                  [1, 0] => 9, [1, 1] => -12.1, [1, 2] => 6.5}, shape: [2, 3]}

iex> Tensorex.Operator.add(
...>   Tensorex.from_list([[0  ,  1  ,  2  ],
...>                       [3  , -4  , -5.5]]),
...>   Tensorex.from_list([[0.0, -1  , -2  ],
...>                       [6  , -8.1, 12  ]]))
%Tensorex{data: %{[1, 0] => 9, [1, 1] => -12.1, [1, 2] => 6.5}, shape: [2, 3]}

iex> Tensorex.Operator.add(
...>   Tensorex.from_list([[ 0,  6],
...>                       [-3,  0]]),
...>   Tensorex.from_list([[ 8,  0],
...>                       [ 0,  9]]))
%Tensorex{data: %{[0, 0] =>  8, [0, 1] => 6,
                  [1, 0] => -3, [1, 1] => 9}, shape: [2, 2]}

Specs

divide(Tensorex.t(), number()) :: Tensorex.t()

Divides all elements of the tensor by the scalar.

iex> Tensorex.Operator.divide(
...>   Tensorex.from_list([[2  , 3.5, -1.6,   8.2],
...>                       [1.1, 3.0,  0  , -12.1]]), 4)
%Tensorex{data: %{[0, 0] => 0.5  , [0, 1] => 0.875, [0, 2] => -0.4, [0, 3] =>  2.05 ,
                  [1, 0] => 0.275, [1, 1] => 0.75 ,                 [1, 3] => -3.025}, shape: [2, 4]}
Link to this function

multiply(scalar, tensor)

View Source

Specs

multiply(Tensorex.t() | number(), Tensorex.t() | number()) :: Tensorex.t()

Makes a product of tensors.

If both arguments are tensors, it returns a tensor product of them. When one of arguments is a number/0, then all elements of the tensor will be amplified by the scalar.

iex> Tensorex.Operator.multiply(
...>   Tensorex.from_list([2, 5.2, -4  , 0  ]),
...>   Tensorex.from_list([2, 3.5, -1.6, 8.2]))
%Tensorex{data: %{[0, 0] => 4   , [0, 1] =>   7.0, [0, 2] => -3.2 , [0, 3] => 16.4 ,
                  [1, 0] => 10.4, [1, 1] =>  18.2, [1, 2] => -8.32, [1, 3] => 42.64,
                  [2, 0] => -8  , [2, 1] => -14.0, [2, 2] =>  6.4 , [2, 3] => -32.8}, shape: [4, 4]}

iex> Tensorex.Operator.multiply(3.5,
...>   Tensorex.from_list([[2   ,  3.5, -1.5, 8.0],
...>                       [4.12, -2  ,  1  , 0  ]]))
%Tensorex{data: %{[0, 0] =>  7.0 , [0, 1] => 12.25, [0, 2] => -5.25, [0, 3] => 28.0,
                  [1, 0] => 14.42, [1, 1] => -7.0 , [1, 2] =>  3.5                 }, shape: [2, 4]}
Link to this function

multiply(tensorex1, tensorex2, axes)

View Source

Specs

Makes a dot product of tensors.

Components specified by the axes will be sumed up (or contracted).

iex> Tensorex.Operator.multiply(
...>   Tensorex.from_list([0, 0.0,  0.0, 0  ]),
...>   Tensorex.from_list([2, 3.5, -1.6, 8.2]), [{0, 0}])
0.0

iex> Tensorex.Operator.multiply(
...>   Tensorex.from_list([[2  , 3.5, -1.6,   8.2],
...>                       [1.1, 3.0,  8  , -12.1]]),
...>   Tensorex.from_list([[0  , 0.0],
...>                       [0.0, 0  ],
...>                       [0.0, 0  ],
...>                       [0  , 0  ]]), [{0, 1}, {1, 0}])
0.0

iex> Tensorex.Operator.multiply(
...>   Tensorex.from_list([2, 5.2, -4  , 0  ]),
...>   Tensorex.from_list([2, 3.5, -1.6, 8.2]), [{0, 0}])
28.6

iex> Tensorex.Operator.multiply(
...>   Tensorex.from_list([[ 2   ,  5.5, -4  , 0  ],
...>                       [ 4.12, -2  ,  1  , 0  ]]),
...>   Tensorex.from_list([[ 2   ,  3.5],
...>                       [-1.6 ,  8.2],
...>                       [ 2   , -3.5],
...>                       [-1.5 ,  8.0]]), [{0, 1}])
%Tensorex{data: %{[0, 0] => 18.42, [0, 1] =>  30.584, [0, 2] => -10.42, [0, 3] =>  29.96,
                  [1, 0] =>  4.0 , [1, 1] => -25.2  , [1, 2] =>  18.0 , [1, 3] => -24.25,
                  [2, 0] => -4.5 , [2, 1] =>  14.6  , [2, 2] => -11.5 , [2, 3] =>  14.0 }, shape: [4, 4]}

Specs

negate(Tensorex.t()) :: Tensorex.t()

Negates a tensor.

iex> Tensorex.Operator.negate(
...>   Tensorex.from_list([[ 2  , 3.5, -4  , 0  ],
...>                       [-2.2, 6  ,  0.0, 5.5]]))
%Tensorex{data: %{[0, 0] => -2  , [0, 1] => -3.5, [0, 2] => 4,
                  [1, 0] =>  2.2, [1, 1] => -6  ,              [1, 3] => -5.5}, shape: [2, 4]}
Link to this function

subtract(tensor, tensorex)

View Source

Specs

subtract(Tensorex.t(), Tensorex.t()) :: Tensorex.t()

Subtracts a tensor from another.

iex> Tensorex.Operator.subtract(
...>   Tensorex.from_list([[0,  1,  2], [3, -4,   -5.5]]),
...>   Tensorex.from_list([[3, -2, -2], [6, -8.1, 12  ]]))
%Tensorex{data: %{[0, 0] => -3, [0, 1] => 3  , [0, 2] =>   4  ,
                  [1, 0] => -3, [1, 1] => 4.1, [1, 2] => -17.5}, shape: [2, 3]}

iex> Tensorex.Operator.subtract(
...>   Tensorex.from_list([[0,   1, 2], [3, -4,   -5.5]]),
...>   Tensorex.from_list([[0.0, 1, 2], [6, -8.1, 12  ]]))
%Tensorex{data: %{[1, 0] => -3, [1, 1] => 4.1, [1, 2] => -17.5}, shape: [2, 3]}
Link to this function

transpose(tensorex, axes)

View Source

Specs

transpose(Tensorex.t(), [{non_neg_integer(), non_neg_integer()}, ...]) ::
  Tensorex.t()

Returns a transposed tensor.

iex> Tensorex.Operator.transpose(
...>   Tensorex.from_list([[[ 2   ,  5.5, -4, 0  ],
...>                        [ 4.12, -2  ,  1, 0  ]],
...>                       [[ 3   ,  1.2,  5, 8.9],
...>                        [ 1   ,  6  ,  7, 1.3]]]), [{0, 2}])
%Tensorex{data: %{[0, 0, 0] =>  2   , [0, 0, 1] => 3  ,
                  [0, 1, 0] =>  4.12, [0, 1, 1] => 1  ,
                  [1, 0, 0] =>  5.5 , [1, 0, 1] => 1.2,
                  [1, 1, 0] => -2   , [1, 1, 1] => 6  ,
                  [2, 0, 0] => -4   , [2, 0, 1] => 5  ,
                  [2, 1, 0] =>  1   , [2, 1, 1] => 7  ,
                                      [3, 0, 1] => 8.9,
                                      [3, 1, 1] => 1.3}, shape: [4, 2, 2]}