Legend:
Library
Module
Module type
Parameter
Class
Class type
N-dimensional array module This module is built atop of Genarray module in OCaml Bigarray. The module also heavily relies on Lacaml to call native BLAS/LAPACK to improve the performance. The documentation of some math functions is copied directly from Lacaml.
type('a, 'b) t = ('a, 'b, Bigarray.c_layout)Bigarray.Genarray.t
N-dimensional array abstract type
type('a, 'b) kind = ('a, 'b)Bigarray.kind
Type of the ndarray, e.g., Bigarray.Float32, Bigarray.Complex64, and etc.
empty Bigarray.Float64 [|3;4;5|] creates a three diemensional array of type Bigarray.Float64. Each dimension has the following size: 3, 4, and 5. The elements in the array are not initialised, they can be any value. empty is faster than zeros to create a ndarray.
The module only support the following four types of ndarray: Bigarray.Float32, Bigarray.Float64, Bigarray.Complex32, and Bigarray.Complex64.
val create : ('a, 'b)kind->int array->'a->('a, 'b)t
create Bigarray.Float64 [|3;4;5|] 2. creates a three-diemensional array of type Bigarray.Float64. Each dimension has the following size: 3, 4, and 5. The elements in the array are initialised to 2.
val init : ('a, 'b)kind->int array->(int ->'a)->('a, 'b)t
init Bigarray.Float64 d f creates a ndarray x of shape d, then using f to initialise the elements in x. The input of f is 1-dimensional index of the ndarray. You need to explicitly convert it if you need N-dimensional index. The function Owl_utils._index_1d_nd can help you.
val init_nd : ('a, 'b)kind->int array->(int array->'a)->('a, 'b)t
init_nd is almost the same as init but f receives n-dimensional index as input. It is more convenient since you don't have to convert the index by yourself, but this also means init_nd is slower than init.
zeros Bigarray.Complex32 [|3;4;5|] creates a three-diemensional array of type Bigarray.Complex32. Each dimension has the following size: 3, 4, and 5. The elements in the array are initialised to "zero". Depending on the kind, zero can be 0. or Complex.zero.
ones Bigarray.Complex32 [|3;4;5|] creates a three-diemensional array of type Bigarray.Complex32. Each dimension has the following size: 3, 4, and 5. The elements in the array are initialised to "one". Depending on the kind, one can be 1. or Complex.one.
val uniform : ?scale:float ->('a, 'b)kind->int array->('a, 'b)t
uniform Bigarray.Float64 [|3;4;5|] creates a three-diemensional array of type Bigarray.Float64. Each dimension has the following size: 3, 4, and 5. The elements in the array follow a uniform distribution 0,1.
val gaussian : ?sigma:float ->('a, 'b)kind->int array->('a, 'b)t
gaussian Float64 [|3;4;5|] ...
val sequential : ('a, 'b)kind->?a:'a->?step:'a->int array->('a, 'b)t
sequential Bigarray.Float64 [|3;4;5|] 2. creates a three-diemensional array of type Bigarray.Float64. Each dimension has the following size: 3, 4, and 5. The elements in the array are assigned sequential values.
?a specifies the starting value and the default value is zero; whilst ?step specifies the step size with default value one.
val linspace : ('a, 'b)kind->'a->'a->int ->('a, 'b)t
linspace k 0. 9. 10 ...
val logspace : ('a, 'b)kind->?base:float ->'a->'a->int ->('a, 'b)t
logspace k 0. 9. 10 ...
val bernoulli :
('a, 'b)kind->?p:float ->?seed:int ->int array->('a, 'b)t
kind x returns the type of ndarray x. It is one of the four possible values: Bigarray.Float32, Bigarray.Float64, Bigarray.Complex32, and Bigarray.Complex64.
slice s x returns a copy of the slice in x. The slice is defined by a which is an int option array. E.g., for a ndarray x of dimension [|2; 2; 3|], slice [0] x takes the following slices of index \(0,*,*\), i.e., [|0;0;0|], [|0;0;1|], [|0;0;2|] ... Also note that if the length of s is less than the number of dimensions of x, slice function will append slice definition to higher diemensions by assuming all the elements in missing dimensions will be taken.
Basically, slice function offers very much the same semantic as that in numpy, i.e., start:stop:step grammar, so if you how to index and slice ndarray in numpy, you should not find it difficult to use this function. Please just refer to numpy documentation or my tutorial.
There are two differences between slice_left and slice: slice_left does not make a copy but simply moving the pointer; slice_left can only make a slice from left-most axis whereas slice is much more flexible and can work on arbitrary axis which need not start from left-most side.
reshape x d transforms x into a new shape definted by d. Note the reshape function will not make a copy of x, the returned ndarray shares the same memory with the original x.
reverse x reverse the order of all elements in the flattened x and returns the results in a new ndarray. The original x remains intact.
val transpose : ?axis:int array->('a, 'b)t->('a, 'b)t
transpose ~axis x makes a copy of x, then transpose it according to ~axis. ~axis must be a valid permutation of x dimension indices. E.g., for a three-dimensional ndarray, it can be 2;1;0, 0;2;1, 1;2;0, and etc.
tile x a tiles the data in x according the repitition specified by a. This function provides the exact behaviour as numpy.tile, please refer to the numpy's online documentation for details.
val repeat : ?axis:int ->('a, 'b)t->int ->('a, 'b)t
repeat ~axis x a repeats the elements along axis for a times. The default value of ?axis is the highest dimension of x. This function is similar to numpy.repeat except that a is an integer instead of an array.
val concatenate : ?axis:int ->('a, 'b)t array->('a, 'b)t
concatenate ~axis:2 x concatenates an array of ndarrays along the third dimension. For the ndarrays in x, they must have the same shape except the dimension specified by axis. The default value of axis is 0, i.e., the lowest dimension on an ndarray.
val squeeze : ?axis:int array->('a, 'b)t->('a, 'b)t
squeeze ~axis x removes single-dimensional entries from the shape of x.
expand x d reshapes x by increasing its rank from num_dims x to d. The opposite operation is squeeze x.
val pad : ?v:'a->int list list->('a, 'b)t->('a, 'b)t
pad ~v:0. [[1;1]] x
val dropout : ?rate:float ->?seed:int ->('a, 'b)t->('a, 'b)t
dropout ~rate:0.3 x drops out 30% of the elements in x, in other words, by setting their values to zeros.
val mmap :
Unix.file_descr ->?pos:int64 ->('a, 'b)kind->bool ->int array->('a, 'b)t
mmap fd kind layout shared dims ...
Iterate array elements
val iteri :
?axis:int option array->(int array->'a-> unit)->('a, 'b)t->
unit
iteri ~axis f x applies function f to each element in a slice defined by ~axis. If ~axis is not passed in, then iteri simply iterates all the elements in x.
val iter : ?axis:int option array->('a-> unit)->('a, 'b)t-> unit
iter ~axis f x is similar to iteri ~axis f x, excpet the index i of an element is not passed in f. Note that iter is much faster than iteri.
val mapi :
?axis:int option array->(int array->'a->'a)->('a, 'b)t->('a, 'b)t
mapi ~axis f x makes a copy of x, then applies f to each element in a slice defined by ~axis. If ~axis is not passed in, then mapi simply iterates all the elements in x.
val map : ?axis:int option array->('a->'a)->('a, 'b)t->('a, 'b)t
map ~axis f x is similar to mapi ~axis f x except the index of the current element is not passed to the function f. Note that map is much faster than mapi.
val map2i :
?axis:int option array->(int array->'a->'a->'a)->('a, 'b)t->('a, 'b)t->('a, 'b)t
map2i ~axis f x y applies f to two elements of the same position in a slice defined by ~axis in both x and y. If ~axis is not passed in, then map2i simply iterates all the elements in x and y. The two matrices mush have the same shape.
val map2 :
?axis:int option array->('a->'a->'a)->('a, 'b)t->('a, 'b)t->('a, 'b)t
map2 ~axis f x y is similar to map2i ~axis f x y except the index of the index of the current element is not passed to the function f.
filteri ~axis f x uses f to filter out certain elements in a slice defined by ~axis. An element will be included if f returns true. The returned result is a list of indices of the selected elements.
val filter :
?axis:int option array->('a-> bool)->('a, 'b)t->int array array
Similar to filteri, but the indices of the elements are not passed to f.
val foldi :
?axis:int option array->(int array->'c->'a->'c)->'c->('a, 'b)t->'c
foldi ~axis f a x folds all the elements in a slice defined by ~axis with the function f. If ~axis is not passed in, then foldi simply folds all the elements in x.
val fold : ?axis:int option array->('c->'a->'c)->'c->('a, 'b)t->'c
Similar to foldi, except that the index of an element is not passed to f.
val iteri_slice :
int array->(int array array->('a, 'b)t-> unit)->('a, 'b)t->
unit
iteri_slice s f x iterates the slices along the passed in axis indices s, and applies the function f to each of them. The order of iterating slices is based on the order of axis in s.
E.g., for a three-dimensional ndarray of shape [|2;2;2|], iteri_slice [|1;0|] f x will access the slices in the following order: [ [0]; [0]; [] ], [ [1]; [0]; [] ], [ [1]; [1]; [] ]. Also note the slice passed in f is a copy of the original data.
val iter_slice : int array->(('a, 'b)t-> unit)->('a, 'b)t-> unit
Similar to iteri_slice, except that the index of a slice is not passed to f.
val iter2i :
(int array->'a->'b-> unit)->('a, 'c)t->('b, 'd)t->
unit
Similar to iteri but applies to two N-dimensional arrays x and y. Both x and y must have the same shape.
val iter2 : ('a->'b-> unit)->('a, 'c)t->('b, 'd)t-> unit
Similar to iter2i, except that the index of a slice is not passed to f.
max_i x returns the maximum of all elements in x along with its index.
val minmax_i : (float, 'a)t->(float * int array) * (float * int array)
minmax_i x returns ((min_v,min_i), (max_v,max_i)) where (min_v,min_i) is the minimum value in x along with its index while (max_v,max_i) is the maximum value along its index.
relu x computes the rectified linear unit function max(x, 0) of the elements in x and returns the result in a new ndarray.
val elu : ?alpha:float ->(float, 'a)t->(float, 'a)t
elu alpha x computes the exponential linear unit function x >= 0. ? x : (alpha * (exp(x) - 1)) of the elements in x and returns the result in a new ndarray.
val leaky_relu : ?alpha:float ->(float, 'a)t->(float, 'a)t
leaky_relu alpha x computes the leaky rectified linear unit function x >= 0. ? x : (alpha * x) of the elements in x and returns the result in a new ndarray.
l2norm_sqr x calculates the sum of 2-norm (or l2norm, Euclidean norm) of all elements in x. The function uses conjugate transpose in the product, hence it always returns a float number.
ssqr x a computes the sum of squared differences of all the elements in x from constant a. This function only computes the square of each element rather than the conjugate transpose as l2norm_sqr does.
val conv2d_backward_input :
(float, 'a)t->(float, 'a)t->int array->(float, 'a)t->(float, 'a)t
val conv2d_backward_kernel :
(float, 'a)t->(float, 'a)t->int array->(float, 'a)t->(float, 'a)t
val conv3d_backward_input :
(float, 'a)t->(float, 'a)t->int array->(float, 'a)t->(float, 'a)t
val conv3d_backward_kernel :
(float, 'a)t->(float, 'a)t->int array->(float, 'a)t->(float, 'a)t
val max_pool2d_backward :
padding->(float, 'a)t->int array->int array->(float, 'a)t->(float, 'a)t
val avg_pool2d_backward :
padding->(float, 'a)t->int array->int array->(float, 'a)t->(float, 'a)t
Some helper and experimental functions
The following functions are helper functions for some other functions in both Ndarray and Ndview modules. In general, you are not supposed to use these functions directly.