kaldi.matrix

PyKaldi defines the following CPU vector/matrix types:

Type 32-bit floating point 64-bit floating point Other
Dense Matrix Matrix DoubleMatrix CompressedMatrix
Dense Vector Vector DoubleVector  
Symmetric Matrix SpMatrix DoubleSpMatrix  
Triangular Matrix TpMatrix DoubleTpMatrix  
Sparse Matrix SparseMatrix DoubleSparseMatrix  
Sparse Vector SparseVector DoubleSparseMatrix  

In addition, there is a GeneralMatrix type which is a wrapper around Matrix, SparseMatrix and CompressedMatrix types.

The dense Vector/Matrix types come in two flavors.

Vector/Matrix instances own the memory buffers backing them. Instantiating a new Vector/Matrix object allocates new memory for storing the elements. They support destructive operations that reallocate memory.

SubVector/SubMatrix instances, on the other hand, share the memory buffers owned by other objects. Instantiating a new SubVector/SubMatrix object does not allocate new memory. Since they provide views into other existing objects, they do not support destructive operations that reallocate memory. Other than this caveat, they are equivalent to Vector/Matrix instances for all practical purposes. Almost any function or method accepting a Vector/Matrix instance can instead be passed a SubVector/SubMatrix instance.

Note

All mutating vector/matrix methods are marked with an underscore suffix. These methods overwrite the contents and return the resulting object, unless they have other return values, to support method chaining.

Functions

set_printoptions Set options for printing.

Classes

DoubleMatrix Double precision matrix.
DoubleSubMatrix Double precision matrix view.
DoubleSubVector Double precision vector view.
DoubleVector Double precision vector.
Matrix Single precision matrix.
SubMatrix Single precision matrix view.
SubVector Single precision vector view.
Vector Single precision vector.
class kaldi.matrix.DoubleMatrix(*args)[source]

Double precision matrix.

DoubleMatrix():
Creates an empty matrix.
DoubleMatrix(num_rows: int, num_cols: int):
Creates a new matrix of given size and fills it with zeros.
Parameters:
  • num_rows (int) – Number of rows of the new matrix.
  • num_cols (int) – Number of cols of the new matrix.
DoubleMatrix(obj: matrix_like):
Creates a new matrix with the elements in obj.
Parameters:obj (matrix_like) – A matrix, a 2-D numpy array, any object exposing a 2-D array interface, an object with an __array__ method returning a 2-D numpy array, or any (nested) sequence that can be interpreted as a matrix.
add_(alpha:float)

Adds a scalar to each element of the matrix.

Parameters:alpha (float) – The scalar to add.
add_cols_(src, indices)

Adds columns from another matrix.

Adds column indices[r] of src to column r. As a special case, if indexes[i] == -1, skips column i. All elements of indices must be in [-1, src.num_cols-1], and src.num_rows must equal self.num_rows.

Parameters:
  • src (DoubleMatrix) – The input matrix.
  • indices (List[int]) – The list of column indices.
add_diag_vec_mat_(alpha:float, v:DoubleVectorBase, M:DoubleMatrixBase, transM:MatrixTransposeType, beta:float=default)

Adds given matrix to this one after scaling its rows.

Perform the operation \(S = \alpha\ diag(v)\ M + \beta\ S\).

Parameters:
  • alpha (float) – The scalar multiplier for \(diag(v) M\).
  • v (DoubleVector) – The scaling vector.
  • M (DoubleMatrix) – The input matrix.
  • transM (MatrixTransposeType) – Whether to use M or its transpose. Defaults to MatrixTransposeType.NO_TRANS.
  • beta (float) – The scalar multiplier for the destination matrix. Defaults to 1.0.
Raises:

RuntimeError – In case of size mismatch.

add_mat_(alpha, M, trans=<MatrixTransposeType.NO_TRANS: 111>)

Adds another matrix to this one.

Performs the operation \(S = \alpha\ M + S\).

Parameters:
Raises:

RuntimeError – In case of size mismatch.

add_mat_diag_vec_(alpha:float, M:DoubleMatrixBase, transM:MatrixTransposeType, v:DoubleVectorBase, beta:float=default)

Adds given matrix to this one after scaling its columns.

Perform the operation \(S = \alpha\ M\ diag(v) + \beta\ S\).

Parameters:
  • alpha (float) – The scalar multiplier for \(M\ diag(v)\).
  • M (DoubleMatrix) – The input matrix.
  • transM (MatrixTransposeType) – Whether to use M or its transpose. Defaults to MatrixTransposeType.NO_TRANS.
  • v (DoubleVector) – The scaling vector.
  • beta (float) – The scalar multiplier for the destination matrix. Defaults to 1.0.
Raises:

RuntimeError – In case of size mismatch.

add_mat_mat_(A, B, transA=<MatrixTransposeType.NO_TRANS: 111>, transB=<MatrixTransposeType.NO_TRANS: 111>, alpha=1.0, beta=1.0, sparseA=False, sparseB=False)

Adds the product of given matrices.

Performs the operation \(M = \alpha\ A\ B + \beta\ M\).

Parameters:
  • A (DoubleMatrix or DoubleTpMatrix or DoubleSpMatrix) – The first input matrix.
  • B (DoubleMatrix or DoubleTpMatrix or DoubleSpMatrix) – The second input matrix.
  • transA (MatrixTransposeType) – Whether to use A or its transpose. Defaults to MatrixTransposeType.NO_TRANS.
  • transB (MatrixTransposeType) – Whether to use B or its transpose. Defaults to MatrixTransposeType.NO_TRANS.
  • alpha (float) – The scalar multiplier for the product. Defaults to 1.0.
  • beta (float) – The scalar multiplier for the destination vector. Defaults to 1.0.
  • sparseA (bool) – Whether to use the algorithm that is faster when A is sparse. Defaults to False.
  • sparseA – Whether to use the algorithm that is faster when B is sparse. Defaults to False.
Raises:
  • RuntimeError – In case of size mismatch.
  • TypeError – If matrices of given types can not be multiplied.
add_mat_mat_elements_(alpha:float, A:DoubleMatrixBase, B:DoubleMatrixBase, beta:float)

Adds the element-wise multiplication of given matrices to this one.

Performs the operation \(S = \alpha\ A\odot B + \beta\ S\).

Parameters:
  • alpha (float) – The scalar multiplier for \(A \odot B\).
  • A (DoubleMatrix) – The first input matrix.
  • B (DoubleMatrix) – The second input matrix.
  • beta (float) – The scalar multiplier for the destination matrix.
Raises:

RuntimeError – In case of size mismatch.

add_mat_mat_mat_(alpha:float, A:DoubleMatrixBase, transA:MatrixTransposeType, B:DoubleMatrixBase, transB:MatrixTransposeType, C:DoubleMatrixBase, transC:MatrixTransposeType, beta:float)

Adds the product of three matrices to this one.

Performs the operation \(S = \alpha\ A\ B\ C + \beta\ S\).

Parameters:
Raises:

RuntimeError – In case of size mismatch.

add_rows_(src, indices, alpha=1.0)

Adds rows from another matrix.

Scales row indices[r] of src with alpha and adds it to row r. As a special case, if indexes[i] == -1, skips row i. All elements of indices must be in [-1, src.num_rows-1], and src.num_cols must equal self.num_cols.

Parameters:
  • src (DoubleMatrix) – The input matrix.
  • indices (List[int]) – The list of row indices.
  • alpha (float) – The scalar multiplier. Defaults to 1.0.
add_to_diag_(alpha:float)

Adds a scalar to the diagonal elements of the matrix.

Parameters:alpha (float) – The scalar to add.
add_vec_to_cols_(alpha:float, v:DoubleVectorBase)

Adds input vector to each column of this matrix.

Performs the operation \(M_{ij} = M_{ij} + \alpha\ v_{i}\).

Parameters:
Raises:

RuntimeError – In case of size mismatch.

add_vec_to_rows_(alpha:float, v:DoubleVectorBase)

Adds input vector to each row of this matrix.

Performs the operation \(M_{ij} = M_{ij} + \alpha\ v_{j}\).

Parameters:
Raises:

RuntimeError – In case of size mismatch.

add_vec_vec_(alpha:float, a:DoubleVectorBase, b:DoubleVectorBase)

Adds outer product of input vectors to this matrix.

Performs the operation \(M = M + \alpha\ a \ b^T\).

Parameters:
apply_ceiling_(ceiling:float)

Applies ceiling operation to each element.

Performs the operation M[i,j] = min(M[i,j], ceiling).

Parameters:ceiling (float) – The ceiling value.
Returns:The number of elements changed.
apply_exp_()

Applies exponential operation to each element.

apply_floor_(floor:float)

Applies floor operation to each element.

Performs the operation M[i,j] = max(M[i,j], floor).

Parameters:floor (float) – The floor value.
Returns:The number of elements changed.
apply_heaviside_()

Applies the Heaviside step function to each element.

apply_log_()

Applies natural log operation to each element.

apply_pow_(power:float)

Takes each element to the given power.

Parameters:power (float) – The exponent value.
Raises:RuntimeError – If an element cannot be raised to the given power.
apply_pow_abs_(power:float, include_sign:bool=default)

Takes the absolute value of each element raised to the given power.

If the power is negative and the input is zero, the output is zero.

Parameters:
  • power (float) – The power value.
  • include_sign (bool) – Whether to include the sign of the input value. Defaults to False.
Raises:

RuntimeError – If an element cannot be raised to the given power.

apply_softmax_() → float

Applies the softmax operation to each element.

Performs the operation \(M_{i,j} = \frac{\exp(M_{i,j})}{\sum_{k,j} \exp(M_{k,j})}\).

Returns:\(\log(\sum_{k,j} \exp(M_{k,j}))\).
approx_equal(other, tol=0.01)

Checks if matrices are approximately equal.

Parameters:
  • other (DoubleMatrix) – The matrix to compare against.
  • tol (float) – The tolerance for the equality check. Defaults to 0.01.
Returns:

True if self.size() == other.size() and ||self-other|| <= tol*||self||. False otherwise.

clone()

Clones the matrix.

The clone allocates new memory for its contents and supports matrix operations that reallocate memory, i.e. it is not a view.

Returns:A copy of the matrix.
Return type:DoubleMatrix
col_range(col_start, num_cols)

Returns the given range of columns as a new matrix view.

Parameters:
  • col_start (int) – The start column index.
  • num_cols (int) – The number of columns.
Returns:

A matrix view representing the given column range.

Return type:

DoubleSubMatrix

cond() → float

Returns the condition number of the SVD computation.

copy_(src, trans=<MatrixTransposeType.NO_TRANS: 111>)

Copies the elements from another matrix.

Parameters:
Raises:

ValueError – In case of size mismatch.

copy_col_from_vec_(v:DoubleVectorBase, col:int)

Copies a vector into the specified column.

Parameters:
Raises:

RuntimeError – In case of size mismatch.

copy_cols_(src, indices)

Copies columns from another matrix.

Copies column r from column indices[r] of src. As a special case, if indexes[i] == -1, sets column i to zero. All elements of indices must be in [-1, src.num_cols-1], and src.num_rows must equal self.num_rows.

Parameters:
  • src (DoubleMatrix) – The input matrix.
  • indices (List[int]) – The list of column indices.
copy_cols_from_vec_(v:DoubleVectorBase)

Copies column elements from a vector.

This method has two modes of operation. If the number of elements in self and v are the same, then elements of v are copied into self column by column. If the number of elements in v is equal to self.num_rows, then the elements of v are copied into each column of self.

Parameters:v (DoubleVector) – The input vector.
Raises:RuntimeError – In case of size mismatch.
copy_diag_from_vec_(v:DoubleVectorBase)

Copies a vector into the diagonal.

Parameters:v (DoubleVector) – The input vector.
Raises:RuntimeError – In case of size mismatch.
copy_lower_to_upper_()

Copies lower triangle to upper triangle.

Raises:RuntimeError – If matrix is not square.
copy_row_from_vec_(v:DoubleVectorBase, row:int)

Copies a vector into the specified row.

Parameters:
Raises:

RuntimeError – In case of size mismatch.

copy_rows_(src, indices)

Copies rows from another matrix.

Copies row r from row indices[r] of src. As a special case, if indexes[i] == -1, sets row i to zero. All elements of indices must be in [-1, src.num_rows-1], and src.num_cols must equal self.num_cols.

Parameters:
  • src (DoubleMatrix) – The input matrix.
  • indices (List[int]) – The list of row indices.
copy_rows_from_vec_(v:DoubleVectorBase)

Copies row elements from a vector.

This method has two modes of operation. If the number of elements in self and v are the same, then elements of v are copied into self row by row. If the number of elements in v is equal to self.num_cols, then the elements of v are copied into each row of self.

Parameters:v (Vector) – The input vector.
Raises:RuntimeError – In case of size mismatch.
copy_upper_to_lower_()

Copies upper triangle to lower triangle.

Raises:RuntimeError – If matrix is not square.
data

Matrix data as a memoryview.

diff_sigmoid_(value:DoubleMatrixBase, diff:DoubleMatrixBase)

Backpropagates derivatives through the sigmoid function.

Performs the operation M[i,j] = diff[i,j] * value[i,j] * (1 - value[i,j]).

Parameters:
diff_tanh_(value:DoubleMatrixBase, diff:DoubleMatrixBase)

Backpropagates derivatives through the tanh function.

Performs the operation M[i,j] = diff[i,j] * (1 - value[i,j]^2).

Parameters:
div_elements_(A:DoubleMatrixBase)

Divides element-wise with another matrix.

Performs the operation M = M oslash A.

Parameters:A (DoubleMatrix) – The denominator.
Raises:RuntimeError – In case of size mismatch.
eig()

Computes eigendecomposition.

Factorizes a square matrix into \(P\ D\ P^{-1}\).

The relationship of \(D\) to the eigenvalues is slightly complicated, due to the need for \(P\) to be real. In the symmetric case, \(D\) is diagonal and real, but in the non-symmetric case there may be complex-conjugate pairs of eigenvalues. In this case, for the equation \(y = P\ D\ P^{-1}\) to hold, \(D\) must actually be block diagonal, with 2x2 blocks corresponding to any such pairs. If a pair is \(\lambda +- i\mu\), \(D\) will have a corresponding 2x2 block \([\lambda, \mu; -\mu, \lambda]\). Note that if the matrix is not invertible, \(P\) may not be invertible so in this case instead of the equation \(y = P\ D\ P^{-1}\) holding, we have \(y\ P = P\ D\).

Returns:3-element tuple containing
  • P (DoubleMatrix): The eigenvector matrix, where ith column corresponds to the ith eigenvector.
  • r (DoubleVector): The vector with real components of the eigenvalues.
  • i (DoubleVector): The vector with imaginary components of the eigenvalues.
Raises:ValueError – If the matrix is not square.
equal(other:DoubleMatrixBase) → bool

Checks if matrices are exactly equal.

Parameters:other (DoubleMatrix) – The matrix to check against
Returns:True if self[i,j] == other[i,j]` for all i,j. False otherwise.
frobenius_norm() → float

Returns the Frobenius norm of the matrix

from_matrix(M:DoubleMatrixBase, trans:MatrixTransposeType=default) → DoubleMatrix

Creates a new matrix from a given matrix.

Parameters:
from_size(r:int, c:int, resize_type:MatrixResizeType=default, stride_type:MatrixStrideType=default) → DoubleMatrix

Creates a new matrix of given size.

Parameters:
  • r (int) – The number or rows.
  • c (int) – The number or columns.
  • resize_type (MatrixResizeType) – How to initialize the elements. If MatrixResizeType.SET_ZERO or MatrixResizeType.COPY_DATA, they are set to zero. If MatrixResizeType.UNDEFINED, they are left uninitialized. Defaults to MatrixResizeType.SET_ZERO.
  • stride_type (MatrixStrideType) – Determines how the elements are laid out in memory. If MatrixStrideType.STRIDE_EQUAL_NUM_COLS, the stride is equal to the number of columns. If MatrixStrideType.DEFAULT_STRIDE, the stride is equal to the smallest multiple of 16 that is larger than the number of columns. Defaults to MatrixStrideType.DEFAULT_STRIDE.
group_max_(src:DoubleMatrixBase)

Computes group max of elements in another matrix.

Performs the operation self[i,j] = max(src[i,j*gs:(j+1)*gs]) where gs = src.num_cols / self.num_cols.

Parameters:src (DoubleMatrix) – The source matrix.
Raises:RuntimeError – If src.num_rows != self.num_rows or src.num_rows % self.num_rows != 0.
group_max_deriv_(input:DoubleMatrixBase, output:DoubleMatrixBase)

Computes derivatives for the group max operation.

Parameters:
group_pnorm_(src:DoubleMatrixBase, power:float)

Computes group p-norm of elements in another matrix.

Performs the operation self[i,j] = norm(src[i,j*gs:(j+1)*gs], power) where gs = src.num_cols / self.num_cols

Parameters:
  • src (DoubleMatrix) – The source matrix.
  • power (float) – The p value for p-norm. It must be non-negative.
Raises:

RuntimeError – If src.num_rows != self.num_rows or src.num_rows % self.num_rows != 0.

group_pnorm_deriv_(input:DoubleMatrixBase, output:DoubleMatrixBase, power:float)

Computes derivatives for the group p-norm operation.

Parameters:
  • input (DoubleMatrix) – The input to group p-norm operation (src in group_pnorm_()).
  • output (DoubleMatrix) – The output of group p-norm operation.
  • power (float) – The p value for p-norm. It must be non-negative.
heaviside_(src:DoubleMatrixBase)

Applies Heaviside step function to elements of another matrix.

Parameters:src (DoubleMatrix) – The source matrix.
Raises:RuntimeError – In case of size mismatch.
invert_()

Inverts the matrix.

Returns:2-element tuple containing
  • log_det (float): The log determinant.
  • det_sign (float): The sign of the determinant, 1 or -1.
Raises:RuntimeError – If matrix is not square.
invert_elements_()

Inverts the elements.

is_diagonal(cutoff:float=default) → bool

Checks if the matrix is approximately diagonal.

Parameters:cutoff (float) – The cutoff value. Defaults to 1.0e-05.
Returns:True if sum(digonal_elements)*cutoff > sum(nondiagonal_elements). False otherwise.
is_symmetric(cutoff:float=default) → bool

Checks if the matrix is approximately symmetric.

Parameters:cutoff (float) – The cutoff value. Defaults to 1.0e-05.
Returns:True if the matrix is approximately symmetric. False otherwise.
is_unit(cutoff:float=default) → bool

Checks if the matrix is identity-like.

Checks if max(M - I) <= cutoff where I is a matrix with the same size as M, ones on the diagonal and zeros elsewhere.

Parameters:cutoff (float) – The cutoff value. Defaults to 1.0e-05.
Returns:True if max(M - I) <= cutoff.

Note

The matrix does not have to be square.

is_zero(cutoff:float=default) → bool

Checks if the elements are all zeros.

Parameters:cutoff (float) – The cutoff value. Defaults to 1.0e-05.
Returns:True if max(abs(M)) <= cutoff.
largest_abs_elem() → float

Returns the largest of the absolute values of the elements.

log_det() -> (log_det:float, det_sign:float)

Computes log determinant of the matrix.

Returns:2-element tuple containing
  • log_det (float): The log determinant.
  • det_sign (float): The sign of the determinant, 1 or -1.
log_sum_exp(prune:float=default) → float

Computes \(f(M)=\log(\sum_{i,j} \exp(M_{i,j}))\) without exp overflow.

If prune > 0.0, ignores terms less than max(M) - prune.

Parameters:prune (float) – The pruning beam. Defaults to -1.0.
Returns:\(\log(\sum_{i,j} \exp(M_{i,j}))\).
max() → float

Returns the maximum value in the matrix.

max_with_mat_(A:DoubleMatrixBase)

Applies an element-wise max operation.

Performs the operation M[i,j] = max(M[i,j], A[i,j]).

Parameters:A (DoubleMatrix) – The input matrix.
Raises:RuntimeError – In case of size mismatch.
min() → float

Returns the minimum value in the matrix.

min_singular_value() → float

Returns the smallest singular value.

min_with_mat_(A:DoubleMatrixBase)

Applies an element-wise min operation.

Performs the operation M[i,j] = min(M[i,j], A[i,j]).

Parameters:A (DoubleMatrix) – The input matrix.
Raises:RuntimeError – In case of size mismatch.
mul_cols_vec_(scale:DoubleVectorBase)

Scales columns with the elements in a vector.

Performs the operation M[i,j] = scale[j] * M[i,j].

Parameters:scale (DoubleVector) – The scaling vector.
Raises:RuntimeError – In case of size mismatch.
mul_elements_(A:DoubleMatrixBase)

Multiplies element-wise with another matrix.

Performs the operation M = M odot A.

Parameters:A (DoubleMatrix) – The multiplier.
Raises:RuntimeError – In case of size mismatch.
mul_rows_group_mat_(src:DoubleMatrixBase)

Scales matrix with another matrix.

Divides each row of self into src.num_cols equal groups, and then scales i`th row's `j`th group of elements with `src[i,j].

Parameters:src (DoubleMatrix) – The scaling matrix.
Raises:RuntimeError – If self.num_rows != src.num_rows or self.num_cols % src.num_cols != 0.
mul_rows_vec_(scale:DoubleVectorBase)

Scales rows with the elements in a vector.

Performs the operation M[i,j] = scale[i] * M[i,j].

Parameters:scale (DoubleVector) – The scaling vector.
Raises:RuntimeError – In case of size mismatch.
num_cols

Number of columns (zero for empty matrix).

num_rows

Number of rows (zero for empty matrix).

numpy()

Converts the matrix to a 2-D NumPy array.

The NumPy array is a view into the matrix, i.e. no data is copied.

Returns:A NumPy array sharing data with this matrix.
Return type:numpy.ndarray
orthogonalize_rows_()

Orthogonalizes rows using the Gram-Schmidt process.

Uses random number generation to fill in rows with something non-zero, in cases where the original matrix was of deficient row rank.

Raises:RuntimeError – If self.num_rows > self.num_cols`.
power_(pow:float) → bool

Takes the matrix to the specified power.

This method uses an algorithm that works in general for fractional and negative powers. The input matrix must be invertible and have a reasonable condition number. The algorithm is based on eigenvalue decomposition. It will return False and leave the matrix unchanged, if the matrix has real negative eigenvalues (or if it has zero eigenvalues and the power is negative).

Parameters:pow (float) – The exponent.
Returns:True if the operation was successful.
range(row_start, num_rows, col_start, num_cols)

Returns the given range of elements as a new matrix view.

Parameters:
  • row_start (int) – The start row index.
  • num_rows (int) – The number of rows.
  • col_start (int) – The start column index.
  • num_cols (int) – The number of columns.
Returns:

A matrix view representing the given range.

Return type:

DoubleSubMatrix

read_(is:istream, binary:bool, add:bool=default)

Reads the matrix from the given C++ stream.

Resizes the matrix to match the size of the matrix read from stream.

Parameters:
  • is (istream) – The input C++ stream.
  • binary (bool) – Whether the stream is binary.
  • add (bool) – Whether to add existing contents to the read matrix. Defaults to False.
resize_(r:int, c:int, resize_type:MatrixResizeType=default, stride_type:MatrixStrideType=default)

Resizes the matrix.

Parameters:
  • r (int) – The new number of rows.
  • c (int) – The new number of columns.
  • resize_type (MatrixResizeType) – How to initialize the elements. If MatrixResizeType.SET_ZERO or MatrixResizeType.COPY_DATA, they are set to zero. If MatrixResizeType.UNDEFINED, they are left uninitialized. Defaults to MatrixResizeType.SET_ZERO.
  • stride_type (MatrixStrideType) – Determines how the elements are laid out in memory. If MatrixStrideType.STRIDE_EQUAL_NUM_COLS, the stride is equal to the number of columns. If MatrixStrideType.DEFAULT_STRIDE, the stride is equal to the smallest multiple of 16 that is larger than the number of columns. Defaults to MatrixStrideType.DEFAULT_STRIDE.
row(index)

Returns the given row as a new vector view.

Parameters:index (int) – The row index.
Returns:A vector view representing the given row.
Return type:DoubleSubVector
row_data(index)

Returns row data as a memoryview.

row_range(row_start, num_rows)

Returns the given range of rows as a new matrix view.

Parameters:
  • row_start (int) – The start row index.
  • num_rows (int) – The number of rows.
Returns:

A matrix view representing the given row range.

Return type:

DoubleSubMatrix

scale_(alpha:float)

Scales the matrix.

Parameters:alpha (float) – The scalar multiplier.
set_mat_mat_div_mat_(A:DoubleMatrixBase, B:DoubleMatrixBase, C:DoubleMatrixBase)

Computes an elementwise multiplication followed by division.

Performs the operation \(S = A \odot B \oslash C\) where \(\odot\) and \(\oslash\) are elementwise multiplication and division. If \(C[i,j] == 0\) then \(S[i,j]\) remains intact.

Parameters:
Raises:

RuntimeError – In case of size mismatch.

set_rand_uniform_()

Sets the elements to numbers uniformly distributed on (0,1).

set_randn_()

Sets the elements to numbers from standard normal distribution.

set_unit_()

Sets diagonal elements to one, off-diagonal elements to zero.

Note

Works for non-square matrices too.

set_zero_()

Sets the elements to zero.

shape

Two element tuple representing the size of the matrix.

sigmoid_(src:DoubleMatrixBase)

Applies sigmoid function to elements of another matrix.

Parameters:src (DoubleMatrix) – The source matrix.
Raises:RuntimeError – In case of size mismatch.
singular_values()

Computes singular values.

Returns:The vector of singular values.
Return type:DoubleVector
size()

Returns the size of the matrix.

Returns:A tuple (num_rows, num_cols) of integers.
size_in_bytes() → int

Returns the size (in bytes) of the data held by the matrix.

soft_hinge_(src:DoubleMatrixBase)

Applies soft hinge function to elements of another matrix.

Performs the operation M[i,j] = log(1 + exp(src[i,j])).

Parameters:src (DoubleMatrix) – The source matrix.
Raises:RuntimeError – In case of size mismatch.
stride

Row stride (distance in memory between each row, >= num_cols).

sum() → float

Returns the sum of the elements.

svd(destructive=False)

Computes singular-value decomposition.

Factorizes a matrix into \(U\ diag(s)\ V^T\).

For non-square matrices, requires self.num_rows >= self.num_cols.

Parameters:destructive (bool) – Whether to use the destructive operation which avoids a copy but mutates self. Defaults to False.
Returns:3-element tuple containing
Raises:ValueError – If self.num_rows < self.num_cols.

Note

Vt in the output is already transposed. The singular values in s are not sorted.

See also

singular_values() sort_svd()

swap_(other:DoubleMatrix)

Swaps the contents of matrices.

Shallow swap.

Parameters:other (DoubleMatrix) – The matrix to swap contents with.
sym_add_mat2_(alpha:float, M:DoubleMatrixBase, transA:MatrixTransposeType, beta:float)

Adds the square of given matrix to this one.

Performs the operation on symmetric matrices \(S = \alpha\ M\ M^T + \beta\ S\).

Note

It only updates the lower triangle of self. It will leave the matrix asymmetric. If you need it symmetric as a regular matrix, call copy_lower_to_upper_() after this operation.

Parameters:
  • alpha (float) – The scalar multiplier for \(M\ M^T\).
  • M (DoubleMatrix) – The input matrix.
  • transM (MatrixTransposeType) – Whether to use M or its transpose. Defaults to MatrixTransposeType.NO_TRANS.
  • beta (float) – The scalar multiplier for the destination matrix.
Raises:

RuntimeError – In case of size mismatch.

sym_pos_semi_def_eig(s:DoubleVectorBase, P:DoubleMatrixBase, check_thresh:float=default)

Computes eigendecomposition of a positive semi-definite matrix.

Uses SVD to compute the eigendecomposition of a symmetric positive semi-definite matrix: \(M = P\ diag(s) \ P^T\), where \(P\) is an orthogonal matrix, i.e. \(P^{-1} = P^T\).

Parameters:
  • s (DoubleVector) – The eigenvalue vector.
  • P (DoubleMatrix) – The eigenvector matrix.
  • check_thresh (float) – The threshold used for checking if input is positive semi-definite. Defaults to 0.001.

Note

Set check_thresh to 2 to ensure the positive semi-definite check won’t ever complain, however it will zero out negative dimensions in the matrix.

Raises:RuntimeError – If input is not positive semi-definite.
tanh_(src:DoubleMatrixBase)

Applies tanh function to elements of another matrix.

Parameters:src (DoubleMatrix) – The source matrix.
Raises:RuntimeError – In case of size mismatch.
trace(check_square:bool=default) → float

Returns the trace.

If matrix is not square it will return the trace of the square matrix formed from the minimum dimension, e.g. if the matrix is 3x5 it will return the trace of the 3x3 submatrix at the beginning.

Parameters:

check_square (bool) – Check if the matrix is square. Defaults to True.

Raises:
  • RuntimeError – If the matrix is not square and
  • **check_square** is True.
transpose_()

Transposes the matrix.

write(os:ostream, binary:bool)

Writes the matrix to given C++ stream.

Parameters:
  • os (ostream) – The output C++ straem.
  • binary (bool) – Whether the stream is binary.
class kaldi.matrix.DoubleSubMatrix(obj, row_start=0, num_rows=None, col_start=0, num_cols=None)[source]

Double precision matrix view.

Creates a new matrix view from a matrix like object.

If possible the new matrix view will share its data with the obj, i.e. no copy will be made. A copy will only be made if obj.__array__ returns a copy, if obj is a sequence, or if a copy is needed to satisfy any of the other requirements (data type, order, etc.). Regardless of whether a copy is made or not, the new matrix view will not own the memory buffer backing it, i.e. it will not support matrix operations that reallocate memory.

Parameters:
  • obj (matrix_like) – A matrix, a 2-D numpy array, any object exposing a 2-D array interface, an object with an __array__ method returning a 2-D numpy array, or any sequence that can be interpreted as a matrix.
  • row_start (int) – The start row index. Defaults to 0.
  • num_rows (int) – The number of rows. If None, it is set to self.num_rows - row_start. Defaults to None.
  • col_start (int) – The start column index. Defaults to 0.
  • num_cols (int) – The number of columns. If None, it is set to self.num_cols - col_start. Defaults to None.
add_(alpha:float)

Adds a scalar to each element of the matrix.

Parameters:alpha (float) – The scalar to add.
add_cols_(src, indices)

Adds columns from another matrix.

Adds column indices[r] of src to column r. As a special case, if indexes[i] == -1, skips column i. All elements of indices must be in [-1, src.num_cols-1], and src.num_rows must equal self.num_rows.

Parameters:
  • src (DoubleMatrix) – The input matrix.
  • indices (List[int]) – The list of column indices.
add_diag_vec_mat_(alpha:float, v:DoubleVectorBase, M:DoubleMatrixBase, transM:MatrixTransposeType, beta:float=default)

Adds given matrix to this one after scaling its rows.

Perform the operation \(S = \alpha\ diag(v)\ M + \beta\ S\).

Parameters:
  • alpha (float) – The scalar multiplier for \(diag(v) M\).
  • v (DoubleVector) – The scaling vector.
  • M (DoubleMatrix) – The input matrix.
  • transM (MatrixTransposeType) – Whether to use M or its transpose. Defaults to MatrixTransposeType.NO_TRANS.
  • beta (float) – The scalar multiplier for the destination matrix. Defaults to 1.0.
Raises:

RuntimeError – In case of size mismatch.

add_mat_(alpha, M, trans=<MatrixTransposeType.NO_TRANS: 111>)

Adds another matrix to this one.

Performs the operation \(S = \alpha\ M + S\).

Parameters:
Raises:

RuntimeError – In case of size mismatch.

add_mat_diag_vec_(alpha:float, M:DoubleMatrixBase, transM:MatrixTransposeType, v:DoubleVectorBase, beta:float=default)

Adds given matrix to this one after scaling its columns.

Perform the operation \(S = \alpha\ M\ diag(v) + \beta\ S\).

Parameters:
  • alpha (float) – The scalar multiplier for \(M\ diag(v)\).
  • M (DoubleMatrix) – The input matrix.
  • transM (MatrixTransposeType) – Whether to use M or its transpose. Defaults to MatrixTransposeType.NO_TRANS.
  • v (DoubleVector) – The scaling vector.
  • beta (float) – The scalar multiplier for the destination matrix. Defaults to 1.0.
Raises:

RuntimeError – In case of size mismatch.

add_mat_mat_(A, B, transA=<MatrixTransposeType.NO_TRANS: 111>, transB=<MatrixTransposeType.NO_TRANS: 111>, alpha=1.0, beta=1.0, sparseA=False, sparseB=False)

Adds the product of given matrices.

Performs the operation \(M = \alpha\ A\ B + \beta\ M\).

Parameters:
  • A (DoubleMatrix or DoubleTpMatrix or DoubleSpMatrix) – The first input matrix.
  • B (DoubleMatrix or DoubleTpMatrix or DoubleSpMatrix) – The second input matrix.
  • transA (MatrixTransposeType) – Whether to use A or its transpose. Defaults to MatrixTransposeType.NO_TRANS.
  • transB (MatrixTransposeType) – Whether to use B or its transpose. Defaults to MatrixTransposeType.NO_TRANS.
  • alpha (float) – The scalar multiplier for the product. Defaults to 1.0.
  • beta (float) – The scalar multiplier for the destination vector. Defaults to 1.0.
  • sparseA (bool) – Whether to use the algorithm that is faster when A is sparse. Defaults to False.
  • sparseA – Whether to use the algorithm that is faster when B is sparse. Defaults to False.
Raises:
  • RuntimeError – In case of size mismatch.
  • TypeError – If matrices of given types can not be multiplied.
add_mat_mat_elements_(alpha:float, A:DoubleMatrixBase, B:DoubleMatrixBase, beta:float)

Adds the element-wise multiplication of given matrices to this one.

Performs the operation \(S = \alpha\ A\odot B + \beta\ S\).

Parameters:
  • alpha (float) – The scalar multiplier for \(A \odot B\).
  • A (DoubleMatrix) – The first input matrix.
  • B (DoubleMatrix) – The second input matrix.
  • beta (float) – The scalar multiplier for the destination matrix.
Raises:

RuntimeError – In case of size mismatch.

add_mat_mat_mat_(alpha:float, A:DoubleMatrixBase, transA:MatrixTransposeType, B:DoubleMatrixBase, transB:MatrixTransposeType, C:DoubleMatrixBase, transC:MatrixTransposeType, beta:float)

Adds the product of three matrices to this one.

Performs the operation \(S = \alpha\ A\ B\ C + \beta\ S\).

Parameters:
Raises:

RuntimeError – In case of size mismatch.

add_rows_(src, indices, alpha=1.0)

Adds rows from another matrix.

Scales row indices[r] of src with alpha and adds it to row r. As a special case, if indexes[i] == -1, skips row i. All elements of indices must be in [-1, src.num_rows-1], and src.num_cols must equal self.num_cols.

Parameters:
  • src (DoubleMatrix) – The input matrix.
  • indices (List[int]) – The list of row indices.
  • alpha (float) – The scalar multiplier. Defaults to 1.0.
add_to_diag_(alpha:float)

Adds a scalar to the diagonal elements of the matrix.

Parameters:alpha (float) – The scalar to add.
add_vec_to_cols_(alpha:float, v:DoubleVectorBase)

Adds input vector to each column of this matrix.

Performs the operation \(M_{ij} = M_{ij} + \alpha\ v_{i}\).

Parameters:
Raises:

RuntimeError – In case of size mismatch.

add_vec_to_rows_(alpha:float, v:DoubleVectorBase)

Adds input vector to each row of this matrix.

Performs the operation \(M_{ij} = M_{ij} + \alpha\ v_{j}\).

Parameters:
Raises:

RuntimeError – In case of size mismatch.

add_vec_vec_(alpha:float, a:DoubleVectorBase, b:DoubleVectorBase)

Adds outer product of input vectors to this matrix.

Performs the operation \(M = M + \alpha\ a \ b^T\).

Parameters:
apply_ceiling_(ceiling:float)

Applies ceiling operation to each element.

Performs the operation M[i,j] = min(M[i,j], ceiling).

Parameters:ceiling (float) – The ceiling value.
Returns:The number of elements changed.
apply_exp_()

Applies exponential operation to each element.

apply_floor_(floor:float)

Applies floor operation to each element.

Performs the operation M[i,j] = max(M[i,j], floor).

Parameters:floor (float) – The floor value.
Returns:The number of elements changed.
apply_heaviside_()

Applies the Heaviside step function to each element.

apply_log_()

Applies natural log operation to each element.

apply_pow_(power:float)

Takes each element to the given power.

Parameters:power (float) – The exponent value.
Raises:RuntimeError – If an element cannot be raised to the given power.
apply_pow_abs_(power:float, include_sign:bool=default)

Takes the absolute value of each element raised to the given power.

If the power is negative and the input is zero, the output is zero.

Parameters:
  • power (float) – The power value.
  • include_sign (bool) – Whether to include the sign of the input value. Defaults to False.
Raises:

RuntimeError – If an element cannot be raised to the given power.

apply_softmax_() → float

Applies the softmax operation to each element.

Performs the operation \(M_{i,j} = \frac{\exp(M_{i,j})}{\sum_{k,j} \exp(M_{k,j})}\).

Returns:\(\log(\sum_{k,j} \exp(M_{k,j}))\).
approx_equal(other, tol=0.01)

Checks if matrices are approximately equal.

Parameters:
  • other (DoubleMatrix) – The matrix to compare against.
  • tol (float) – The tolerance for the equality check. Defaults to 0.01.
Returns:

True if self.size() == other.size() and ||self-other|| <= tol*||self||. False otherwise.

clone()

Clones the matrix.

The clone allocates new memory for its contents and supports matrix operations that reallocate memory, i.e. it is not a view.

Returns:A copy of the matrix.
Return type:DoubleMatrix
col_range(col_start, num_cols)

Returns the given range of columns as a new matrix view.

Parameters:
  • col_start (int) – The start column index.
  • num_cols (int) – The number of columns.
Returns:

A matrix view representing the given column range.

Return type:

DoubleSubMatrix

cond() → float

Returns the condition number of the SVD computation.

copy_(src, trans=<MatrixTransposeType.NO_TRANS: 111>)

Copies the elements from another matrix.

Parameters:
Raises:

ValueError – In case of size mismatch.

copy_col_from_vec_(v:DoubleVectorBase, col:int)

Copies a vector into the specified column.

Parameters:
Raises:

RuntimeError – In case of size mismatch.

copy_cols_(src, indices)

Copies columns from another matrix.

Copies column r from column indices[r] of src. As a special case, if indexes[i] == -1, sets column i to zero. All elements of indices must be in [-1, src.num_cols-1], and src.num_rows must equal self.num_rows.

Parameters:
  • src (DoubleMatrix) – The input matrix.
  • indices (List[int]) – The list of column indices.
copy_cols_from_vec_(v:DoubleVectorBase)

Copies column elements from a vector.

This method has two modes of operation. If the number of elements in self and v are the same, then elements of v are copied into self column by column. If the number of elements in v is equal to self.num_rows, then the elements of v are copied into each column of self.

Parameters:v (DoubleVector) – The input vector.
Raises:RuntimeError – In case of size mismatch.
copy_diag_from_vec_(v:DoubleVectorBase)

Copies a vector into the diagonal.

Parameters:v (DoubleVector) – The input vector.
Raises:RuntimeError – In case of size mismatch.
copy_lower_to_upper_()

Copies lower triangle to upper triangle.

Raises:RuntimeError – If matrix is not square.
copy_row_from_vec_(v:DoubleVectorBase, row:int)

Copies a vector into the specified row.

Parameters:
Raises:

RuntimeError – In case of size mismatch.

copy_rows_(src, indices)

Copies rows from another matrix.

Copies row r from row indices[r] of src. As a special case, if indexes[i] == -1, sets row i to zero. All elements of indices must be in [-1, src.num_rows-1], and src.num_cols must equal self.num_cols.

Parameters:
  • src (DoubleMatrix) – The input matrix.
  • indices (List[int]) – The list of row indices.
copy_rows_from_vec_(v:DoubleVectorBase)

Copies row elements from a vector.

This method has two modes of operation. If the number of elements in self and v are the same, then elements of v are copied into self row by row. If the number of elements in v is equal to self.num_cols, then the elements of v are copied into each row of self.

Parameters:v (Vector) – The input vector.
Raises:RuntimeError – In case of size mismatch.
copy_upper_to_lower_()

Copies upper triangle to lower triangle.

Raises:RuntimeError – If matrix is not square.
data

Matrix data as a memoryview.

diff_sigmoid_(value:DoubleMatrixBase, diff:DoubleMatrixBase)

Backpropagates derivatives through the sigmoid function.

Performs the operation M[i,j] = diff[i,j] * value[i,j] * (1 - value[i,j]).

Parameters:
diff_tanh_(value:DoubleMatrixBase, diff:DoubleMatrixBase)

Backpropagates derivatives through the tanh function.

Performs the operation M[i,j] = diff[i,j] * (1 - value[i,j]^2).

Parameters:
div_elements_(A:DoubleMatrixBase)

Divides element-wise with another matrix.

Performs the operation M = M oslash A.

Parameters:A (DoubleMatrix) – The denominator.
Raises:RuntimeError – In case of size mismatch.
eig()

Computes eigendecomposition.

Factorizes a square matrix into \(P\ D\ P^{-1}\).

The relationship of \(D\) to the eigenvalues is slightly complicated, due to the need for \(P\) to be real. In the symmetric case, \(D\) is diagonal and real, but in the non-symmetric case there may be complex-conjugate pairs of eigenvalues. In this case, for the equation \(y = P\ D\ P^{-1}\) to hold, \(D\) must actually be block diagonal, with 2x2 blocks corresponding to any such pairs. If a pair is \(\lambda +- i\mu\), \(D\) will have a corresponding 2x2 block \([\lambda, \mu; -\mu, \lambda]\). Note that if the matrix is not invertible, \(P\) may not be invertible so in this case instead of the equation \(y = P\ D\ P^{-1}\) holding, we have \(y\ P = P\ D\).

Returns:3-element tuple containing
  • P (DoubleMatrix): The eigenvector matrix, where ith column corresponds to the ith eigenvector.
  • r (DoubleVector): The vector with real components of the eigenvalues.
  • i (DoubleVector): The vector with imaginary components of the eigenvalues.
Raises:ValueError – If the matrix is not square.
equal(other:DoubleMatrixBase) → bool

Checks if matrices are exactly equal.

Parameters:other (DoubleMatrix) – The matrix to check against
Returns:True if self[i,j] == other[i,j]` for all i,j. False otherwise.
frobenius_norm() → float

Returns the Frobenius norm of the matrix

group_max_(src:DoubleMatrixBase)

Computes group max of elements in another matrix.

Performs the operation self[i,j] = max(src[i,j*gs:(j+1)*gs]) where gs = src.num_cols / self.num_cols.

Parameters:src (DoubleMatrix) – The source matrix.
Raises:RuntimeError – If src.num_rows != self.num_rows or src.num_rows % self.num_rows != 0.
group_max_deriv_(input:DoubleMatrixBase, output:DoubleMatrixBase)

Computes derivatives for the group max operation.

Parameters:
group_pnorm_(src:DoubleMatrixBase, power:float)

Computes group p-norm of elements in another matrix.

Performs the operation self[i,j] = norm(src[i,j*gs:(j+1)*gs], power) where gs = src.num_cols / self.num_cols

Parameters:
  • src (DoubleMatrix) – The source matrix.
  • power (float) – The p value for p-norm. It must be non-negative.
Raises:

RuntimeError – If src.num_rows != self.num_rows or src.num_rows % self.num_rows != 0.

group_pnorm_deriv_(input:DoubleMatrixBase, output:DoubleMatrixBase, power:float)

Computes derivatives for the group p-norm operation.

Parameters:
  • input (DoubleMatrix) – The input to group p-norm operation (src in group_pnorm_()).
  • output (DoubleMatrix) – The output of group p-norm operation.
  • power (float) – The p value for p-norm. It must be non-negative.
heaviside_(src:DoubleMatrixBase)

Applies Heaviside step function to elements of another matrix.

Parameters:src (DoubleMatrix) – The source matrix.
Raises:RuntimeError – In case of size mismatch.
invert_()

Inverts the matrix.

Returns:2-element tuple containing
  • log_det (float): The log determinant.
  • det_sign (float): The sign of the determinant, 1 or -1.
Raises:RuntimeError – If matrix is not square.
invert_elements_()

Inverts the elements.

is_diagonal(cutoff:float=default) → bool

Checks if the matrix is approximately diagonal.

Parameters:cutoff (float) – The cutoff value. Defaults to 1.0e-05.
Returns:True if sum(digonal_elements)*cutoff > sum(nondiagonal_elements). False otherwise.
is_symmetric(cutoff:float=default) → bool

Checks if the matrix is approximately symmetric.

Parameters:cutoff (float) – The cutoff value. Defaults to 1.0e-05.
Returns:True if the matrix is approximately symmetric. False otherwise.
is_unit(cutoff:float=default) → bool

Checks if the matrix is identity-like.

Checks if max(M - I) <= cutoff where I is a matrix with the same size as M, ones on the diagonal and zeros elsewhere.

Parameters:cutoff (float) – The cutoff value. Defaults to 1.0e-05.
Returns:True if max(M - I) <= cutoff.

Note

The matrix does not have to be square.

is_zero(cutoff:float=default) → bool

Checks if the elements are all zeros.

Parameters:cutoff (float) – The cutoff value. Defaults to 1.0e-05.
Returns:True if max(abs(M)) <= cutoff.
largest_abs_elem() → float

Returns the largest of the absolute values of the elements.

log_det() -> (log_det:float, det_sign:float)

Computes log determinant of the matrix.

Returns:2-element tuple containing
  • log_det (float): The log determinant.
  • det_sign (float): The sign of the determinant, 1 or -1.
log_sum_exp(prune:float=default) → float

Computes \(f(M)=\log(\sum_{i,j} \exp(M_{i,j}))\) without exp overflow.

If prune > 0.0, ignores terms less than max(M) - prune.

Parameters:prune (float) – The pruning beam. Defaults to -1.0.
Returns:\(\log(\sum_{i,j} \exp(M_{i,j}))\).
max() → float

Returns the maximum value in the matrix.

max_with_mat_(A:DoubleMatrixBase)

Applies an element-wise max operation.

Performs the operation M[i,j] = max(M[i,j], A[i,j]).

Parameters:A (DoubleMatrix) – The input matrix.
Raises:RuntimeError – In case of size mismatch.
min() → float

Returns the minimum value in the matrix.

min_singular_value() → float

Returns the smallest singular value.

min_with_mat_(A:DoubleMatrixBase)

Applies an element-wise min operation.

Performs the operation M[i,j] = min(M[i,j], A[i,j]).

Parameters:A (DoubleMatrix) – The input matrix.
Raises:RuntimeError – In case of size mismatch.
mul_cols_vec_(scale:DoubleVectorBase)

Scales columns with the elements in a vector.

Performs the operation M[i,j] = scale[j] * M[i,j].

Parameters:scale (DoubleVector) – The scaling vector.
Raises:RuntimeError – In case of size mismatch.
mul_elements_(A:DoubleMatrixBase)

Multiplies element-wise with another matrix.

Performs the operation M = M odot A.

Parameters:A (DoubleMatrix) – The multiplier.
Raises:RuntimeError – In case of size mismatch.
mul_rows_group_mat_(src:DoubleMatrixBase)

Scales matrix with another matrix.

Divides each row of self into src.num_cols equal groups, and then scales i`th row's `j`th group of elements with `src[i,j].

Parameters:src (DoubleMatrix) – The scaling matrix.
Raises:RuntimeError – If self.num_rows != src.num_rows or self.num_cols % src.num_cols != 0.
mul_rows_vec_(scale:DoubleVectorBase)

Scales rows with the elements in a vector.

Performs the operation M[i,j] = scale[i] * M[i,j].

Parameters:scale (DoubleVector) – The scaling vector.
Raises:RuntimeError – In case of size mismatch.
num_cols

Number of columns (zero for empty matrix).

num_rows

Number of rows (zero for empty matrix).

numpy()

Converts the matrix to a 2-D NumPy array.

The NumPy array is a view into the matrix, i.e. no data is copied.

Returns:A NumPy array sharing data with this matrix.
Return type:numpy.ndarray
orthogonalize_rows_()

Orthogonalizes rows using the Gram-Schmidt process.

Uses random number generation to fill in rows with something non-zero, in cases where the original matrix was of deficient row rank.

Raises:RuntimeError – If self.num_rows > self.num_cols`.
power_(pow:float) → bool

Takes the matrix to the specified power.

This method uses an algorithm that works in general for fractional and negative powers. The input matrix must be invertible and have a reasonable condition number. The algorithm is based on eigenvalue decomposition. It will return False and leave the matrix unchanged, if the matrix has real negative eigenvalues (or if it has zero eigenvalues and the power is negative).

Parameters:pow (float) – The exponent.
Returns:True if the operation was successful.
range(row_start, num_rows, col_start, num_cols)

Returns the given range of elements as a new matrix view.

Parameters:
  • row_start (int) – The start row index.
  • num_rows (int) – The number of rows.
  • col_start (int) – The start column index.
  • num_cols (int) – The number of columns.
Returns:

A matrix view representing the given range.

Return type:

DoubleSubMatrix

read_(is:istream, binary:bool, add:bool=default)

Reads the matrix from the given C++ stream.

Parameters:
  • is (istream) – The input C++ stream.
  • binary (bool) – Whether the stream is binary.
  • add (bool) – Whether to add existing contents to the read matrix. Defaults to False.
row(index)

Returns the given row as a new vector view.

Parameters:index (int) – The row index.
Returns:A vector view representing the given row.
Return type:DoubleSubVector
row_data(index)

Returns row data as a memoryview.

row_range(row_start, num_rows)

Returns the given range of rows as a new matrix view.

Parameters:
  • row_start (int) – The start row index.
  • num_rows (int) – The number of rows.
Returns:

A matrix view representing the given row range.

Return type:

DoubleSubMatrix

scale_(alpha:float)

Scales the matrix.

Parameters:alpha (float) – The scalar multiplier.
set_mat_mat_div_mat_(A:DoubleMatrixBase, B:DoubleMatrixBase, C:DoubleMatrixBase)

Computes an elementwise multiplication followed by division.

Performs the operation \(S = A \odot B \oslash C\) where \(\odot\) and \(\oslash\) are elementwise multiplication and division. If \(C[i,j] == 0\) then \(S[i,j]\) remains intact.

Parameters:
Raises:

RuntimeError – In case of size mismatch.

set_rand_uniform_()

Sets the elements to numbers uniformly distributed on (0,1).

set_randn_()

Sets the elements to numbers from standard normal distribution.

set_unit_()

Sets diagonal elements to one, off-diagonal elements to zero.

Note

Works for non-square matrices too.

set_zero_()

Sets the elements to zero.

shape

Two element tuple representing the size of the matrix.

sigmoid_(src:DoubleMatrixBase)

Applies sigmoid function to elements of another matrix.

Parameters:src (DoubleMatrix) – The source matrix.
Raises:RuntimeError – In case of size mismatch.
singular_values()

Computes singular values.

Returns:The vector of singular values.
Return type:DoubleVector
size()

Returns the size of the matrix.

Returns:A tuple (num_rows, num_cols) of integers.
size_in_bytes() → int

Returns the size (in bytes) of the data held by the matrix.

soft_hinge_(src:DoubleMatrixBase)

Applies soft hinge function to elements of another matrix.

Performs the operation M[i,j] = log(1 + exp(src[i,j])).

Parameters:src (DoubleMatrix) – The source matrix.
Raises:RuntimeError – In case of size mismatch.
stride

Row stride (distance in memory between each row, >= num_cols).

sum() → float

Returns the sum of the elements.

svd(destructive=False)

Computes singular-value decomposition.

Factorizes a matrix into \(U\ diag(s)\ V^T\).

For non-square matrices, requires self.num_rows >= self.num_cols.

Parameters:destructive (bool) – Whether to use the destructive operation which avoids a copy but mutates self. Defaults to False.
Returns:3-element tuple containing
Raises:ValueError – If self.num_rows < self.num_cols.

Note

Vt in the output is already transposed. The singular values in s are not sorted.

See also

singular_values() sort_svd()

sym_add_mat2_(alpha:float, M:DoubleMatrixBase, transA:MatrixTransposeType, beta:float)

Adds the square of given matrix to this one.

Performs the operation on symmetric matrices \(S = \alpha\ M\ M^T + \beta\ S\).

Note

It only updates the lower triangle of self. It will leave the matrix asymmetric. If you need it symmetric as a regular matrix, call copy_lower_to_upper_() after this operation.

Parameters:
  • alpha (float) – The scalar multiplier for \(M\ M^T\).
  • M (DoubleMatrix) – The input matrix.
  • transM (MatrixTransposeType) – Whether to use M or its transpose. Defaults to MatrixTransposeType.NO_TRANS.
  • beta (float) – The scalar multiplier for the destination matrix.
Raises:

RuntimeError – In case of size mismatch.

sym_pos_semi_def_eig(s:DoubleVectorBase, P:DoubleMatrixBase, check_thresh:float=default)

Computes eigendecomposition of a positive semi-definite matrix.

Uses SVD to compute the eigendecomposition of a symmetric positive semi-definite matrix: \(M = P\ diag(s) \ P^T\), where \(P\) is an orthogonal matrix, i.e. \(P^{-1} = P^T\).

Parameters:
  • s (DoubleVector) – The eigenvalue vector.
  • P (DoubleMatrix) – The eigenvector matrix.
  • check_thresh (float) – The threshold used for checking if input is positive semi-definite. Defaults to 0.001.

Note

Set check_thresh to 2 to ensure the positive semi-definite check won’t ever complain, however it will zero out negative dimensions in the matrix.

Raises:RuntimeError – If input is not positive semi-definite.
tanh_(src:DoubleMatrixBase)

Applies tanh function to elements of another matrix.

Parameters:src (DoubleMatrix) – The source matrix.
Raises:RuntimeError – In case of size mismatch.
trace(check_square:bool=default) → float

Returns the trace.

If matrix is not square it will return the trace of the square matrix formed from the minimum dimension, e.g. if the matrix is 3x5 it will return the trace of the 3x3 submatrix at the beginning.

Parameters:

check_square (bool) – Check if the matrix is square. Defaults to True.

Raises:
  • RuntimeError – If the matrix is not square and
  • **check_square** is True.
transpose_()

Transposes the matrix.

write(os:ostream, binary:bool)

Writes the matrix to given C++ stream.

Parameters:
  • os (ostream) – The output C++ straem.
  • binary (bool) – Whether the stream is binary.
class kaldi.matrix.DoubleSubVector(obj, start=0, length=None)[source]

Double precision vector view.

Creates a new vector view from a vector like object.

If possible the new vector view will share its data with the obj, i.e. no copy will be made. A copy will only be made if obj.__array__ returns a copy, if obj is a sequence, or if a copy is needed to satisfy any of the other requirements (data type, order, etc.). Regardless of whether a copy is made or not, the new vector view will not own the memory buffer backing it, i.e. it will not support vector operations that reallocate memory.

Parameters:
  • obj (vector_like) – A vector, a 1-D numpy array, any object exposing a 1-D array interface, an object whose __array__ method returns a 1-D numpy array, or any sequence that can be interpreted as a vector.
  • start (int) – The index of the view start. Defaults to 0.
  • length (int) – The length of the view. If None, it is set to len(obj) - start. Defaults to None.
add_(value:float)

Adds the given value to each element.

Parameters:value (float) – The value to add.
add_col_sum_mat_(alpha, M, beta=1.0)

Adds the sum of matrix columns.

Performs the operation \(y = \alpha\ \sum_i M[:,i] + \beta\ y\).

Parameters:
  • alpha (float) – The scalar multiplier for the column sum.
  • M (DoubleMatrix) – The input matrix.
  • beta (float) – The scalar multiplier for the destination vector. Defaults to 1.0.
Raises:

ValueError – If self.dim != M.num_rows.

add_diag_mat2_(alpha, M, trans=<MatrixTransposeType.NO_TRANS: 111>, beta=1.0)

Adds the diagonal of a matrix multiplied with its transpose.

Performs the operation \(y = \alpha\ diag(M M^T) + \beta\ y\).

Parameters:
  • alpha (float) – The scalar multiplier for the diagonal.
  • M (DoubleMatrix) – The input matrix.
  • trans (MatrixTransposeType) – Whether to use M or its transpose. Defaults to MatrixTransposeType.NO_TRANS.
  • beta (float) – The scalar multiplier for the destination vector. Defaults to 1.0.
Raises:

ValueError – In case of size mismatch.

add_diag_mat_mat_(alpha, M, transM, N, transN, beta=1.0)

Adds the diagonal of a matrix-matrix product.

Performs the operation \(y = \alpha\ diag(M N) + \beta\ y\).

Parameters:
  • alpha (float) – The scalar multiplier for the diagonal.
  • M (DoubleMatrix) – The first input matrix.
  • transM (MatrixTransposeType) – Whether to use M or its transpose.
  • N (DoubleMatrix) – The second input matrix.
  • transN (MatrixTransposeType) – Whether to use N or its transpose.
  • beta (float) – The scalar multiplier for the destination vector. Defaults to 1.0.
Raises:

ValueError – In case of size mismatch.

add_mat_vec_(alpha, M, trans, v, beta, sparse=False)

Computes a matrix-vector product.

Performs the operation \(y = \alpha\ M\ v + \beta\ y\).

Parameters:
Raises:

ValueError – In case of size mismatch.

add_row_sum_mat_(alpha, M, beta=1.0)

Adds the sum of matrix rows.

Performs the operation \(y = \alpha\ \sum_i M[i] + \beta\ y\).

Parameters:
  • alpha (float) – The scalar multiplier for the row sum.
  • M (DoubleMatrix) – The input matrix.
  • beta (float) – The scalar multiplier for the destination vector. Defaults to 1.0.
Raises:

ValueError – If self.dim != M.num_cols.

add_vec2_(alpha, v)

Adds the squares of elements from another vector.

Performs the operation \(y = y + \alpha\ v\odot v\).

Parameters:
Raises:

RuntimeError – In case of size mismatch.

add_vec_(alpha, v)

Adds another vector.

Performs the operation \(y = y + \alpha\ v\).

Parameters:
Raises:

RuntimeError – In case of size mismatch.

add_vec_div_vec_(alpha:float, v:DoubleVectorBase, r:DoubleVectorBase, beta:float)

Computes an element-wise vector-vector division.

Performs the operation \(y = \alpha\ v\oslash r + \beta\ y\).

Parameters:
  • alpha (float) – The scalar multiplier for the division.
  • v (DoubleVector) – The first input vector.
  • r (DoubleVector) – The second input vector.
  • beta (float) – The scalar multiplier for the destination vector.
Raises:

RuntimeError – In case of size mismatch.

add_vec_vec_(alpha:float, v:DoubleVectorBase, r:DoubleVectorBase, beta:float)

Computes an element-wise vector-vector product.

Performs the operation \(y = \alpha\ v\odot r + \beta\ y\).

Parameters:
  • alpha (float) – The scalar multiplier for the product.
  • v (DoubleVector) – The first input vector.
  • r (DoubleVector) – The second input vector.
  • beta (float) – The scalar multiplier for the destination vector.
Raises:

RuntimeError – In case of size mismatch.

apply_abs_()

Applies the absolute value operation to each element.

apply_ceiling_(ceiling:float) → int

Applies the ceiling operation to each element.

Performs the operation y[i] = min(y[i], ceiling).

Parameters:ceiling (float) – The ceiling value.
Returns:The number of elements changed.
apply_exp_()

Applies the exponential operation to each element.

apply_floor_(floor:float) → int

Applies the floor operation to each element.

Performs the operation y[i] = max(y[i], floor).

Parameters:floor (float) – The floor value.
Returns:The number of elements changed.
apply_floor_vector_(floor:DoubleVectorBase) → int

Applies a separate floor operation to each element.

Performs the operation y[i] = max(y[i], floor[i]).

Parameters:floor (DoubleVector) – The floor vector.
Returns:The number of elements changed.
apply_log_()

Applies the natural log operation to each element.

apply_log_and_copy_(v:DoubleVectorBase)

Applies the natural log operation to the given vector and copies it.

Parameters:v (DoubleVector) – The input vector.
apply_log_softmax_() → float

Applies the logsoftmax operation to each element.

Performs the operation \(y_i = y_i - \log(\sum_j \exp(y_j))\).

Returns:\(\log(\sum_j \exp(y_j))\).
apply_pow_(power:float)

Takes each element to the given power.

Parameters:power (float) – The exponent value.
Raises:RuntimeError – If an element cannot be raised to the given power.
apply_pow_abs_(power:float, include_sign:bool=default)

Takes the absolute value of each element raised to the given power.

If the power is negative and the input is zero, the output is zero.

Parameters:
  • power (float) – The power value.
  • include_sign (bool) – Whether to include the sign of the input value. Defaults to False.
Raises:

RuntimeError – If an element cannot be raised to the given power.

apply_softmax_() → float

Applies the softmax operation to each element.

Performs the operation \(y_i = \frac{\exp(y_i)}{\sum_j \exp(y_j)}\).

Returns:\(\log(\sum_j \exp(y_j))\).
approx_equal(other, tol=0.01)

Checks if vectors are approximately equal.

Parameters:
  • other (DoubleVector) – The vector to compare against.
  • tol (float) – The tolerance for the equality check. Defaults to 0.01.
Returns:

True if self.dim == other.dim and ||self-other|| <= tol*||self||. False otherwise.

clone()

Clones the vector.

The clone allocates new memory for its contents and supports vector operations that reallocate memory, i.e. it is not a view.

Returns:A copy of the vector.
Return type:DoubleVector
copy_(src)

Copies the elements from another vector.

Parameters:src (Vector or DoubleVector) – The input vector.
Raises:ValueError – In case of size mismatch.
copy_col_from_mat_(M, col)

Copies the elements from a matrix column.

Parameters:
Raises:
copy_cols_from_mat_(M)

Copies the elements from a matrix column-by-columm.

Parameters:M (DoubleMatrix) – The input matrix.
Raises:ValueError – In case of size mismatch.
copy_diag_from_mat_(M)

Copies the digonal elements from a matrix.

Parameters:M (Matrix or DoubleSpMatrix or DoubleTpMatrix) – The input matrix.
Raises:ValueError – In case of size mismatch.
copy_from_packed_(M)

Copies the elements from a packed matrix.

Parameters:M (SpMatrix or TpMatrix or DoubleSpMatrix or DoubleTpMatrix) – The input packed matrix.
Raises:ValueError – If self.dim !=  M.num_rows * (M.num_rows + 1) / 2.
copy_row_from_mat_(M, row)

Copies the elements from a matrix row.

Parameters:
Raises:
copy_rows_from_mat_(M)

Copies the elements from a matrix row-by-row.

Parameters:M (Matrix or DoubleMatrix) – The input matrix.
Raises:ValueError – In case of size mismatch.
data

Vector data as a memoryview.

dim

Dimension (zero for empty vector).

div_elements_(v)

Divides the elements with the elements of another vector.

Performs the operation y[i] /= v[i].

Parameters:v (Vector or DoubleVector) – The input vector.
Raises:RuntimeError – In case of size mismatch.
invert_elements_()

Inverts the elements.

Raises:RuntimeError – If any of the elements is zero.
is_zero(cutoff:float=default) → bool

Checks if the elements are all zeros.

Parameters:cutoff (float) – The cutoff value. Defaults to 1.0e-06.
Returns:True if max(abs(self)) <= cutoff.
log_sum_exp(prune:float=default) → float

Computes \(f(x)=\log(\sum_i \exp(x_i))\) without exp overflow.

If prune > 0.0, ignores terms less than max(x) - prune.

Parameters:prune (float) – The pruning beam. Defaults to -1.0.
Returns:\(\log(\sum_i \exp(x_i))\).
max() → float

Returns the maximum value in the vector.

max_index() -> (value:float, index:int)

Returns the maximum value in the vector, and the associated index.

min() → float

Returns the minimum value in the vector.

min_index() -> (value:float, index:int)

Returns the minimum value in the vector, and the associated index.

mul_elements_(v)

Multiplies the elements with the elements of another vector.

Performs the operation y[i] *= v[i].

Parameters:v (Vector or DoubleVector) – The input vector.
Raises:RuntimeError – In case of size mismatch.
mul_tp_(M, trans)

Multiplies the vector with a lower-triangular matrix.

Performs the operation \(y = M\ y\).

Parameters:
Raises:

ValueError – In case of size mismatch.

norm(p:float) → float

Computes the p-norm of the vector.

Parameters:p (float) – The p value. It must be non-negative.
Returns:The p-norm.
Raises:RuntimeError – If p < 0 or p-norm cannot be computed.
numpy()

Converts the vector to a 1-D NumPy array.

The NumPy array is a view into the vector, i.e. no data is copied.

Returns:A NumPy array sharing data with this vector.
Return type:numpy.ndarray
rand_categorical() → int

Chooses a random index into the vector.

Chooses an index with probability proportional to its element value. Requires min(self) >= 0 and sum(self) > 0.

Returns:A random index value.
Raises:RuntimeError – If min(self) < 0 || sum(self) <= 0.
range(start, length)

Returns the given range of elements as a new vector view.

Parameters:
  • start (int) – The start index.
  • length (int) – The length.
Returns:

A vector view representing the given range.

Return type:

DoubleSubVector

read_(is:istream, binary:bool, add:bool=default)

Reads the vector from the given C++ stream.

Parameters:
  • is (istream) – The input C++ stream.
  • binary (bool) – Whether the stream is binary.
  • add (bool) – Whether to add existing contents to the read vector. Defaults to False.
replace_value_(orig:float, changed:float)

Replaces the elements.

Performs the operation y[y == orig] = changed.

Parameters:
  • orig (float) – The original value to replace.
  • changed (float) – The value to replace with.
scale_(alpha:float)

Scales the vector.

Parameters:alpha (float) – The scalar multiplier.
set_(f:float)

Sets the elements to the given value.

Parameters:f (float) – The value to set.
set_randn_()

Sets the elements to numbers from standard normal distribution.

set_randn_uniform_()

Sets the elements to numbers uniformly distributed on (0,1).

set_zero_()

Sets the elements to zero.

shape

Single element tuple representing the size of the vector.

sigmoid_(src:DoubleVectorBase)

Sets the elements to the sigmoid of the elements in another vector.

Parameters:src (DoubleVector) – The input vector.
Raises:RuntimeError – In case of size mismatch.
size()

Returns the size of the vector as a single element tuple.

size_in_bytes() → int

Returns the size (in bytes) of the data held by the vector.

solve_(M, trans)

Solves a linear system.

The linear system is defined as \(M\ x = b\), where \(b\) and \(x\) are the initial and final values of the vector, respectively.

Warning

Does not test for \(M\) being singular or near-singular.

Parameters:
Raises:

ValueError – In case of size mismatch.

sum() → float

Returns the sum of the elements.

sum_log() → float

Returns the sum of the logs of the elements.

Returns NaN if any of the elements is negative.

tanh_(src:DoubleVectorBase)

Sets the elements to the tanh of the elements in another vector.

Parameters:src (DoubleVector) – The input vector.
Raises:RuntimeError – In case of size mismatch.
write(os:ostream, binary:bool)

Writes the vector to given C++ stream.

Parameters:
  • os (ostream) – The output C++ straem.
  • binary (bool) – Whether the stream is binary.
class kaldi.matrix.DoubleVector(*args)[source]

Double precision vector.

DoubleVector():
Creates an empty vector.
DoubleVector(size: int):
Creates a new vector of given size and fills it with zeros.
Parameters:size (int) – Size of the new vector.
DoubleVector(obj: vector_like):
Creates a new vector with the elements in obj.
Parameters:obj (vector_like) – A vector, a 1-D numpy array, any object exposing a 1-D array interface, an object with an __array__ method returning a 1-D numpy array, or any sequence that can be interpreted as a vector.
add_(value:float)

Adds the given value to each element.

Parameters:value (float) – The value to add.
add_col_sum_mat_(alpha, M, beta=1.0)

Adds the sum of matrix columns.

Performs the operation \(y = \alpha\ \sum_i M[:,i] + \beta\ y\).

Parameters:
  • alpha (float) – The scalar multiplier for the column sum.
  • M (DoubleMatrix) – The input matrix.
  • beta (float) – The scalar multiplier for the destination vector. Defaults to 1.0.
Raises:

ValueError – If self.dim != M.num_rows.

add_diag_mat2_(alpha, M, trans=<MatrixTransposeType.NO_TRANS: 111>, beta=1.0)

Adds the diagonal of a matrix multiplied with its transpose.

Performs the operation \(y = \alpha\ diag(M M^T) + \beta\ y\).

Parameters:
  • alpha (float) – The scalar multiplier for the diagonal.
  • M (DoubleMatrix) – The input matrix.
  • trans (MatrixTransposeType) – Whether to use M or its transpose. Defaults to MatrixTransposeType.NO_TRANS.
  • beta (float) – The scalar multiplier for the destination vector. Defaults to 1.0.
Raises:

ValueError – In case of size mismatch.

add_diag_mat_mat_(alpha, M, transM, N, transN, beta=1.0)

Adds the diagonal of a matrix-matrix product.

Performs the operation \(y = \alpha\ diag(M N) + \beta\ y\).

Parameters:
  • alpha (float) – The scalar multiplier for the diagonal.
  • M (DoubleMatrix) – The first input matrix.
  • transM (MatrixTransposeType) – Whether to use M or its transpose.
  • N (DoubleMatrix) – The second input matrix.
  • transN (MatrixTransposeType) – Whether to use N or its transpose.
  • beta (float) – The scalar multiplier for the destination vector. Defaults to 1.0.
Raises:

ValueError – In case of size mismatch.

add_mat_vec_(alpha, M, trans, v, beta, sparse=False)

Computes a matrix-vector product.

Performs the operation \(y = \alpha\ M\ v + \beta\ y\).

Parameters:
Raises:

ValueError – In case of size mismatch.

add_row_sum_mat_(alpha, M, beta=1.0)

Adds the sum of matrix rows.

Performs the operation \(y = \alpha\ \sum_i M[i] + \beta\ y\).

Parameters:
  • alpha (float) – The scalar multiplier for the row sum.
  • M (DoubleMatrix) – The input matrix.
  • beta (float) – The scalar multiplier for the destination vector. Defaults to 1.0.
Raises:

ValueError – If self.dim != M.num_cols.

add_vec2_(alpha, v)

Adds the squares of elements from another vector.

Performs the operation \(y = y + \alpha\ v\odot v\).

Parameters:
Raises:

RuntimeError – In case of size mismatch.

add_vec_(alpha, v)

Adds another vector.

Performs the operation \(y = y + \alpha\ v\).

Parameters:
Raises:

RuntimeError – In case of size mismatch.

add_vec_div_vec_(alpha:float, v:DoubleVectorBase, r:DoubleVectorBase, beta:float)

Computes an element-wise vector-vector division.

Performs the operation \(y = \alpha\ v\oslash r + \beta\ y\).

Parameters:
  • alpha (float) – The scalar multiplier for the division.
  • v (DoubleVector) – The first input vector.
  • r (DoubleVector) – The second input vector.
  • beta (float) – The scalar multiplier for the destination vector.
Raises:

RuntimeError – In case of size mismatch.

add_vec_vec_(alpha:float, v:DoubleVectorBase, r:DoubleVectorBase, beta:float)

Computes an element-wise vector-vector product.

Performs the operation \(y = \alpha\ v\odot r + \beta\ y\).

Parameters:
  • alpha (float) – The scalar multiplier for the product.
  • v (DoubleVector) – The first input vector.
  • r (DoubleVector) – The second input vector.
  • beta (float) – The scalar multiplier for the destination vector.
Raises:

RuntimeError – In case of size mismatch.

apply_abs_()

Applies the absolute value operation to each element.

apply_ceiling_(ceiling:float) → int

Applies the ceiling operation to each element.

Performs the operation y[i] = min(y[i], ceiling).

Parameters:ceiling (float) – The ceiling value.
Returns:The number of elements changed.
apply_exp_()

Applies the exponential operation to each element.

apply_floor_(floor:float) → int

Applies the floor operation to each element.

Performs the operation y[i] = max(y[i], floor).

Parameters:floor (float) – The floor value.
Returns:The number of elements changed.
apply_floor_vector_(floor:DoubleVectorBase) → int

Applies a separate floor operation to each element.

Performs the operation y[i] = max(y[i], floor[i]).

Parameters:floor (DoubleVector) – The floor vector.
Returns:The number of elements changed.
apply_log_()

Applies the natural log operation to each element.

apply_log_and_copy_(v:DoubleVectorBase)

Applies the natural log operation to the given vector and copies it.

Parameters:v (DoubleVector) – The input vector.
apply_log_softmax_() → float

Applies the logsoftmax operation to each element.

Performs the operation \(y_i = y_i - \log(\sum_j \exp(y_j))\).

Returns:\(\log(\sum_j \exp(y_j))\).
apply_pow_(power:float)

Takes each element to the given power.

Parameters:power (float) – The exponent value.
Raises:RuntimeError – If an element cannot be raised to the given power.
apply_pow_abs_(power:float, include_sign:bool=default)

Takes the absolute value of each element raised to the given power.

If the power is negative and the input is zero, the output is zero.

Parameters:
  • power (float) – The power value.
  • include_sign (bool) – Whether to include the sign of the input value. Defaults to False.
Raises:

RuntimeError – If an element cannot be raised to the given power.

apply_softmax_() → float

Applies the softmax operation to each element.

Performs the operation \(y_i = \frac{\exp(y_i)}{\sum_j \exp(y_j)}\).

Returns:\(\log(\sum_j \exp(y_j))\).
approx_equal(other, tol=0.01)

Checks if vectors are approximately equal.

Parameters:
  • other (DoubleVector) – The vector to compare against.
  • tol (float) – The tolerance for the equality check. Defaults to 0.01.
Returns:

True if self.dim == other.dim and ||self-other|| <= tol*||self||. False otherwise.

clone()

Clones the vector.

The clone allocates new memory for its contents and supports vector operations that reallocate memory, i.e. it is not a view.

Returns:A copy of the vector.
Return type:DoubleVector
copy_(src)

Copies the elements from another vector.

Parameters:src (Vector or DoubleVector) – The input vector.
Raises:ValueError – In case of size mismatch.
copy_col_from_mat_(M, col)

Copies the elements from a matrix column.

Parameters:
Raises:
copy_cols_from_mat_(M)

Copies the elements from a matrix column-by-columm.

Parameters:M (DoubleMatrix) – The input matrix.
Raises:ValueError – In case of size mismatch.
copy_diag_from_mat_(M)

Copies the digonal elements from a matrix.

Parameters:M (Matrix or DoubleSpMatrix or DoubleTpMatrix) – The input matrix.
Raises:ValueError – In case of size mismatch.
copy_from_packed_(M)

Copies the elements from a packed matrix.

Parameters:M (SpMatrix or TpMatrix or DoubleSpMatrix or DoubleTpMatrix) – The input packed matrix.
Raises:ValueError – If self.dim !=  M.num_rows * (M.num_rows + 1) / 2.
copy_row_from_mat_(M, row)

Copies the elements from a matrix row.

Parameters:
Raises:
copy_rows_from_mat_(M)

Copies the elements from a matrix row-by-row.

Parameters:M (Matrix or DoubleMatrix) – The input matrix.
Raises:ValueError – In case of size mismatch.
data

Vector data as a memoryview.

dim

Dimension (zero for empty vector).

div_elements_(v)

Divides the elements with the elements of another vector.

Performs the operation y[i] /= v[i].

Parameters:v (Vector or DoubleVector) – The input vector.
Raises:RuntimeError – In case of size mismatch.
from_size(size:int, resize_type:MatrixResizeType=default) → DoubleVector

Creates a new vector of given size.

Parameters:
  • size (int) – The size of the new vector.
  • resize_type (MatrixResizeType) – How to initialize the elements. If MatrixResizeType.SET_ZERO or MatrixResizeType.COPY_DATA, they are set to zero. If MatrixResizeType.UNDEFINED, they are left uninitialized. Defaults to MatrixResizeType.SET_ZERO.
from_vector(v:DoubleVectorBase) → DoubleVector

Creates a new vector from a given vector.

Parameters:v (DoubleVector) – The input vector.
invert_elements_()

Inverts the elements.

Raises:RuntimeError – If any of the elements is zero.
is_zero(cutoff:float=default) → bool

Checks if the elements are all zeros.

Parameters:cutoff (float) – The cutoff value. Defaults to 1.0e-06.
Returns:True if max(abs(self)) <= cutoff.
log_sum_exp(prune:float=default) → float

Computes \(f(x)=\log(\sum_i \exp(x_i))\) without exp overflow.

If prune > 0.0, ignores terms less than max(x) - prune.

Parameters:prune (float) – The pruning beam. Defaults to -1.0.
Returns:\(\log(\sum_i \exp(x_i))\).
max() → float

Returns the maximum value in the vector.

max_index() -> (value:float, index:int)

Returns the maximum value in the vector, and the associated index.

min() → float

Returns the minimum value in the vector.

min_index() -> (value:float, index:int)

Returns the minimum value in the vector, and the associated index.

mul_elements_(v)

Multiplies the elements with the elements of another vector.

Performs the operation y[i] *= v[i].

Parameters:v (Vector or DoubleVector) – The input vector.
Raises:RuntimeError – In case of size mismatch.
mul_tp_(M, trans)

Multiplies the vector with a lower-triangular matrix.

Performs the operation \(y = M\ y\).

Parameters:
Raises:

ValueError – In case of size mismatch.

norm(p:float) → float

Computes the p-norm of the vector.

Parameters:p (float) – The p value. It must be non-negative.
Returns:The p-norm.
Raises:RuntimeError – If p < 0 or p-norm cannot be computed.
numpy()

Converts the vector to a 1-D NumPy array.

The NumPy array is a view into the vector, i.e. no data is copied.

Returns:A NumPy array sharing data with this vector.
Return type:numpy.ndarray
rand_categorical() → int

Chooses a random index into the vector.

Chooses an index with probability proportional to its element value. Requires min(self) >= 0 and sum(self) > 0.

Returns:A random index value.
Raises:RuntimeError – If min(self) < 0 || sum(self) <= 0.
range(start, length)

Returns the given range of elements as a new vector view.

Parameters:
  • start (int) – The start index.
  • length (int) – The length.
Returns:

A vector view representing the given range.

Return type:

DoubleSubVector

read_(is:istream, binary:bool, add:bool=default)

Reads the vector from the given C++ stream.

Resizes the vector to match the size of the matrix read from stream.

Parameters:
  • is (istream) – The input C++ stream.
  • binary (bool) – Whether the stream is binary.
  • add (bool) – Whether to add existing contents to the read vector. Defaults to False.
replace_value_(orig:float, changed:float)

Replaces the elements.

Performs the operation y[y == orig] = changed.

Parameters:
  • orig (float) – The original value to replace.
  • changed (float) – The value to replace with.
resize_(length:int, resize_type:MatrixResizeType=default)

Resizes the vector.

Parameters:
  • length (int) – The new length.
  • resize_type (MatrixResizeType) – How to initialize the elements. If MatrixResizeType.SET_ZERO they are set to zero. If MatrixResizeType.COPY_DATA, existing elements are retained, new ones are set to zero. If MatrixResizeType.UNDEFINED, they are left uninitialized. Defaults to MatrixResizeType.SET_ZERO.
scale_(alpha:float)

Scales the vector.

Parameters:alpha (float) – The scalar multiplier.
set_(f:float)

Sets the elements to the given value.

Parameters:f (float) – The value to set.
set_randn_()

Sets the elements to numbers from standard normal distribution.

set_randn_uniform_()

Sets the elements to numbers uniformly distributed on (0,1).

set_zero_()

Sets the elements to zero.

shape

Single element tuple representing the size of the vector.

sigmoid_(src:DoubleVectorBase)

Sets the elements to the sigmoid of the elements in another vector.

Parameters:src (DoubleVector) – The input vector.
Raises:RuntimeError – In case of size mismatch.
size()

Returns the size of the vector as a single element tuple.

size_in_bytes() → int

Returns the size (in bytes) of the data held by the vector.

solve_(M, trans)

Solves a linear system.

The linear system is defined as \(M\ x = b\), where \(b\) and \(x\) are the initial and final values of the vector, respectively.

Warning

Does not test for \(M\) being singular or near-singular.

Parameters:
Raises:

ValueError – In case of size mismatch.

sum() → float

Returns the sum of the elements.

sum_log() → float

Returns the sum of the logs of the elements.

Returns NaN if any of the elements is negative.

swap_(other:DoubleVector)

Swaps the contents of vectors.

Shallow swap.

Parameters:other (DoubleVector) – The vector to swap contents with.
tanh_(src:DoubleVectorBase)

Sets the elements to the tanh of the elements in another vector.

Parameters:src (DoubleVector) – The input vector.
Raises:RuntimeError – In case of size mismatch.
write(os:ostream, binary:bool)

Writes the vector to given C++ stream.

Parameters:
  • os (ostream) – The output C++ straem.
  • binary (bool) – Whether the stream is binary.
class kaldi.matrix.Matrix(*args)[source]

Single precision matrix.

Matrix():
Creates an empty matrix.
Matrix(num_rows: int, num_cols: int):
Creates a new matrix of given size and fills it with zeros.
Parameters:
  • num_rows (int) – Number of rows of the new matrix.
  • num_cols (int) – Number of cols of the new matrix.
Matrix(obj: matrix_like):
Creates a new matrix with the elements in obj.
Parameters:obj (matrix_like) – A matrix, a 2-D numpy array, any object exposing a 2-D array interface, an object with an __array__ method returning a 2-D numpy array, or any (nested) sequence that can be interpreted as a matrix.
add_(alpha:float)

Adds a scalar to each element of the matrix.

Parameters:alpha (float) – The scalar to add.
add_cols_(src, indices)

Adds columns from another matrix.

Adds column indices[r] of src to column r. As a special case, if indexes[i] == -1, skips column i. All elements of indices must be in [-1, src.num_cols-1], and src.num_rows must equal self.num_rows.

Parameters:
  • src (Matrix) – The input matrix.
  • indices (List[int]) – The list of column indices.
add_diag_vec_mat_(alpha:float, v:VectorBase, M:MatrixBase, transM:MatrixTransposeType, beta:float=default)

Adds given matrix to this one after scaling its rows.

Perform the operation \(S = \alpha\ diag(v)\ M + \beta\ S\).

Parameters:
  • alpha (float) – The scalar multiplier for \(diag(v) M\).
  • v (Vector) – The scaling vector.
  • M (Matrix) – The input matrix.
  • transM (MatrixTransposeType) – Whether to use M or its transpose. Defaults to MatrixTransposeType.NO_TRANS.
  • beta (float) – The scalar multiplier for the destination matrix. Defaults to 1.0.
Raises:

RuntimeError – In case of size mismatch.

add_mat_(alpha, M, trans=<MatrixTransposeType.NO_TRANS: 111>)

Adds another matrix to this one.

Performs the operation \(S = \alpha\ M + S\).

Parameters:
Raises:

RuntimeError – In case of size mismatch.

add_mat_diag_vec_(alpha:float, M:MatrixBase, transM:MatrixTransposeType, v:VectorBase, beta:float=default)

Adds given matrix to this one after scaling its columns.

Perform the operation \(S = \alpha\ M\ diag(v) + \beta\ S\).

Parameters:
  • alpha (float) – The scalar multiplier for \(M\ diag(v)\).
  • M (Matrix) – The input matrix.
  • transM (MatrixTransposeType) – Whether to use M or its transpose. Defaults to MatrixTransposeType.NO_TRANS.
  • v (Vector) – The scaling vector.
  • beta (float) – The scalar multiplier for the destination matrix. Defaults to 1.0.
Raises:

RuntimeError – In case of size mismatch.

add_mat_mat_(A, B, transA=<MatrixTransposeType.NO_TRANS: 111>, transB=<MatrixTransposeType.NO_TRANS: 111>, alpha=1.0, beta=1.0, sparseA=False, sparseB=False)

Adds the product of given matrices.

Performs the operation \(M = \alpha\ A\ B + \beta\ M\).

Parameters:
  • A (Matrix or TpMatrix or SpMatrix) – The first input matrix.
  • B (Matrix or TpMatrix or SpMatrix) – The second input matrix.
  • transA (MatrixTransposeType) – Whether to use A or its transpose. Defaults to MatrixTransposeType.NO_TRANS.
  • transB (MatrixTransposeType) – Whether to use B or its transpose. Defaults to MatrixTransposeType.NO_TRANS.
  • alpha (float) – The scalar multiplier for the product. Defaults to 1.0.
  • beta (float) – The scalar multiplier for the destination vector. Defaults to 1.0.
  • sparseA (bool) – Whether to use the algorithm that is faster when A is sparse. Defaults to False.
  • sparseA – Whether to use the algorithm that is faster when B is sparse. Defaults to False.
Raises:
  • RuntimeError – In case of size mismatch.
  • TypeError – If matrices of given types can not be multiplied.
add_mat_mat_elements_(alpha:float, A:MatrixBase, B:MatrixBase, beta:float)

Adds the element-wise multiplication of given matrices to this one.

Performs the operation \(S = \alpha\ A\odot B + \beta\ S\).

Parameters:
  • alpha (float) – The scalar multiplier for \(A \odot B\).
  • A (Matrix) – The first input matrix.
  • B (Matrix) – The second input matrix.
  • beta (float) – The scalar multiplier for the destination matrix.
Raises:

RuntimeError – In case of size mismatch.

add_mat_mat_mat_(alpha:float, A:MatrixBase, transA:MatrixTransposeType, B:MatrixBase, transB:MatrixTransposeType, C:MatrixBase, transC:MatrixTransposeType, beta:float)

Adds the product of three matrices to this one.

Performs the operation \(S = \alpha\ A\ B\ C + \beta\ S\).

Parameters:
  • alpha (float) – The scalar multiplier for \(A\ B\ C\).
  • A (Matrix) – The first input matrix.
  • transA (MatrixTransposeType) – Whether to use A or its transpose.
  • B (Matrix) – The second input matrix.
  • transB (MatrixTransposeType) – Whether to use B or its transpose.
  • C (Matrix) – The third input matrix.
  • transC (MatrixTransposeType) – Whether to use C or its transpose.
  • beta (float) – The scalar multiplier for the destination matrix.
Raises:

RuntimeError – In case of size mismatch.

add_rows_(src, indices, alpha=1.0)

Adds rows from another matrix.

Scales row indices[r] of src with alpha and adds it to row r. As a special case, if indexes[i] == -1, skips row i. All elements of indices must be in [-1, src.num_rows-1], and src.num_cols must equal self.num_cols.

Parameters:
  • src (Matrix) – The input matrix.
  • indices (List[int]) – The list of row indices.
  • alpha (float) – The scalar multiplier. Defaults to 1.0.
add_to_diag_(alpha:float)

Adds a scalar to the diagonal elements of the matrix.

Parameters:alpha (float) – The scalar to add.
add_vec_to_cols_(alpha:float, v:VectorBase)

Adds input vector to each column of this matrix.

Performs the operation \(M_{ij} = M_{ij} + \alpha\ v_{i}\).

Parameters:
  • alpha (float) – The scalar multiplier.
  • v (Vector) – The input vector.
Raises:

RuntimeError – In case of size mismatch.

add_vec_to_rows_(alpha:float, v:VectorBase)

Adds input vector to each row of this matrix.

Performs the operation \(M_{ij} = M_{ij} + \alpha\ v_{j}\).

Parameters:
  • alpha (float) – The scalar multiplier.
  • v (Vector) – The input vector.
Raises:

RuntimeError – In case of size mismatch.

add_vec_vec_(alpha:float, a:VectorBase, b:VectorBase)

Adds outer product of input vectors to this matrix.

Performs the operation \(M = M + \alpha\ a \ b^T\).

Parameters:
  • alpha (float) – The scalar multiplier.
  • a (Vector) – The first input vector.
  • b (Vector) – The second input vector.
apply_ceiling_(ceiling:float)

Applies ceiling operation to each element.

Performs the operation M[i,j] = min(M[i,j], ceiling).

Parameters:ceiling (float) – The ceiling value.
Returns:The number of elements changed.
apply_exp_()

Applies exponential operation to each element.

apply_floor_(floor:float)

Applies floor operation to each element.

Performs the operation M[i,j] = max(M[i,j], floor).

Parameters:floor (float) – The floor value.
Returns:The number of elements changed.
apply_heaviside_()

Applies the Heaviside step function to each element.

apply_log_()

Applies natural log operation to each element.

apply_pow_(power:float)

Takes each element to the given power.

Parameters:power (float) – The exponent value.
Raises:RuntimeError – If an element cannot be raised to the given power.
apply_pow_abs_(power:float, include_sign:bool=default)

Takes the absolute value of each element raised to the given power.

If the power is negative and the input is zero, the output is zero.

Parameters:
  • power (float) – The power value.
  • include_sign (bool) – Whether to include the sign of the input value. Defaults to False.
Raises:

RuntimeError – If an element cannot be raised to the given power.

apply_softmax_() → float

Applies the softmax operation to each element.

Performs the operation \(M_{i,j} = \frac{\exp(M_{i,j})}{\sum_{k,j} \exp(M_{k,j})}\).

Returns:\(\log(\sum_{k,j} \exp(M_{k,j}))\).
approx_equal(other, tol=0.01)

Checks if matrices are approximately equal.

Parameters:
  • other (Matrix) – The matrix to compare against.
  • tol (float) – The tolerance for the equality check. Defaults to 0.01.
Returns:

True if self.size() == other.size() and ||self-other|| <= tol*||self||. False otherwise.

clone()

Clones the matrix.

The clone allocates new memory for its contents and supports matrix operations that reallocate memory, i.e. it is not a view.

Returns:A copy of the matrix.
Return type:Matrix
col_range(col_start, num_cols)

Returns the given range of columns as a new matrix view.

Parameters:
  • col_start (int) – The start column index.
  • num_cols (int) – The number of columns.
Returns:

A matrix view representing the given column range.

Return type:

SubMatrix

cond() → float

Returns the condition number of the SVD computation.

copy_(src, trans=<MatrixTransposeType.NO_TRANS: 111>)

Copies the elements from another matrix.

Parameters:
Raises:

ValueError – In case of size mismatch.

copy_col_from_vec_(v:VectorBase, col:int)

Copies a vector into the specified column.

Parameters:
  • v (Vector) – The input vector.
  • col (int) – The column index.
Raises:

RuntimeError – In case of size mismatch.

copy_cols_(src, indices)

Copies columns from another matrix.

Copies column r from column indices[r] of src. As a special case, if indexes[i] == -1, sets column i to zero. All elements of indices must be in [-1, src.num_cols-1], and src.num_rows must equal self.num_rows.

Parameters:
  • src (Matrix) – The input matrix.
  • indices (List[int]) – The list of column indices.
copy_cols_from_vec_(v:VectorBase)

Copies column elements from a vector.

This method has two modes of operation. If the number of elements in self and v are the same, then elements of v are copied into self column by column. If the number of elements in v is equal to self.num_rows, then the elements of v are copied into each column of self.

Parameters:v (Vector) – The input vector.
Raises:RuntimeError – In case of size mismatch.
copy_diag_from_vec_(v:VectorBase)

Copies a vector into the diagonal.

Parameters:v (Vector) – The input vector.
Raises:RuntimeError – In case of size mismatch.
copy_lower_to_upper_()

Copies lower triangle to upper triangle.

Raises:RuntimeError – If matrix is not square.
copy_row_from_vec_(v:VectorBase, row:int)

Copies a vector into the specified row.

Parameters:
  • v (Vector) – The input vector.
  • row (int) – The row index.
Raises:

RuntimeError – In case of size mismatch.

copy_rows_(src, indices)

Copies rows from another matrix.

Copies row r from row indices[r] of src. As a special case, if indexes[i] == -1, sets row i to zero. All elements of indices must be in [-1, src.num_rows-1], and src.num_cols must equal self.num_cols.

Parameters:
  • src (Matrix) – The input matrix.
  • indices (List[int]) – The list of row indices.
copy_rows_from_vec_(v:VectorBase)

Copies row elements from a vector.

This method has two modes of operation. If the number of elements in self and v are the same, then elements of v are copied into self row by row. If the number of elements in v is equal to self.num_cols, then the elements of v are copied into each row of self.

Parameters:v (Vector) – The input vector.
Raises:RuntimeError – In case of size mismatch.
copy_upper_to_lower_()

Copies upper triangle to lower triangle.

Raises:RuntimeError – If matrix is not square.
data

Matrix data as a memoryview.

diff_sigmoid_(value:MatrixBase, diff:MatrixBase)

Backpropagates derivatives through the sigmoid function.

Performs the operation M[i,j] = diff[i,j] * value[i,j] * (1 - value[i,j]).

Parameters:
  • value (Matrix) – The activations.
  • diff (Matrix) – The derivatives.
diff_tanh_(value:MatrixBase, diff:MatrixBase)

Backpropagates derivatives through the tanh function.

Performs the operation M[i,j] = diff[i,j] * (1 - value[i,j]^2).

Parameters:
  • value (Matrix) – The activations.
  • diff (Matrix) – The derivatives.
div_elements_(A:MatrixBase)

Divides element-wise with another matrix.

Performs the operation M = M oslash A.

Parameters:A (Matrix) – The denominator.
Raises:RuntimeError – In case of size mismatch.
eig()

Computes eigendecomposition.

Factorizes a square matrix into \(P\ D\ P^{-1}\).

The relationship of \(D\) to the eigenvalues is slightly complicated, due to the need for \(P\) to be real. In the symmetric case, \(D\) is diagonal and real, but in the non-symmetric case there may be complex-conjugate pairs of eigenvalues. In this case, for the equation \(y = P\ D\ P^{-1}\) to hold, \(D\) must actually be block diagonal, with 2x2 blocks corresponding to any such pairs. If a pair is \(\lambda +- i\mu\), \(D\) will have a corresponding 2x2 block \([\lambda, \mu; -\mu, \lambda]\). Note that if the matrix is not invertible, \(P\) may not be invertible so in this case instead of the equation \(y = P\ D\ P^{-1}\) holding, we have \(y\ P = P\ D\).

Returns:3-element tuple containing
  • P (Matrix): The eigenvector matrix, where ith column corresponds to the ith eigenvector.
  • r (Vector): The vector with real components of the eigenvalues.
  • i (Vector): The vector with imaginary components of the eigenvalues.
Raises:ValueError – If the matrix is not square.
equal(other:MatrixBase) → bool

Checks if matrices are exactly equal.

Parameters:other (Matrix) – The matrix to check against
Returns:True if self[i,j] == other[i,j]` for all i,j. False otherwise.
frobenius_norm() → float

Returns the Frobenius norm of the matrix

from_matrix(M:MatrixBase, trans:MatrixTransposeType=default) → Matrix

Creates a new matrix from a given matrix.

Parameters:
  • M (Matrix) – The input matrix.
  • trans (MatrixTransposeType) – Whether to use M or its transpose. Defaults to MatrixTransposeType.NO_TRANS.
from_size(r:int, c:int, resize_type:MatrixResizeType=default, stride_type:MatrixStrideType=default) → Matrix

Creates a new matrix of given size.

Parameters:
  • r (int) – The number or rows.
  • c (int) – The number or columns.
  • resize_type (MatrixResizeType) – How to initialize the elements. If MatrixResizeType.SET_ZERO or MatrixResizeType.COPY_DATA, they are set to zero. If MatrixResizeType.UNDEFINED, they are left uninitialized. Defaults to MatrixResizeType.SET_ZERO.
  • stride_type (MatrixStrideType) – Determines how the elements are laid out in memory. If MatrixStrideType.STRIDE_EQUAL_NUM_COLS, the stride is equal to the number of columns. If MatrixStrideType.DEFAULT_STRIDE, the stride is equal to the smallest multiple of 16 that is larger than the number of columns. Defaults to MatrixStrideType.DEFAULT_STRIDE.
group_max_(src:MatrixBase)

Computes group max of elements in another matrix.

Performs the operation self[i,j] = max(src[i,j*gs:(j+1)*gs]) where gs = src.num_cols / self.num_cols.

Parameters:src (MatrixBase) – The source matrix.
Raises:RuntimeError – If src.num_rows != self.num_rows or src.num_rows % self.num_rows != 0.
group_max_deriv_(input:MatrixBase, output:MatrixBase)

Computes derivatives for the group max operation.

Parameters:
  • input (Matrix) – The input to group max operation (src in group_max_()).
  • output (Matrix) – The output of group max operation.
group_pnorm_(src:MatrixBase, power:float)

Computes group p-norm of elements in another matrix.

Performs the operation self[i,j] = norm(src[i,j*gs:(j+1)*gs], power) where gs = src.num_cols / self.num_cols.

Parameters:
  • src (Matrix) – The source matrix.
  • power (float) – The p value for p-norm. It must be non-negative.
Raises:

RuntimeError – If src.num_rows != self.num_rows or src.num_rows % self.num_rows != 0.

group_pnorm_deriv_(input:MatrixBase, output:MatrixBase, power:float)

Computes derivatives for the group p-norm operation.

Parameters:
  • input (Matrix) – The input to group p-norm operation (src in group_pnorm_()).
  • output (Matrix) – The output of group p-norm operation.
  • power (float) – The p value for p-norm. It must be non-negative.
heaviside_(src:MatrixBase)

Applies Heaviside step function to elements of another matrix.

Parameters:src (Matrix) – The source matrix.
Raises:RuntimeError – In case of size mismatch.
invert_(in_double_precision=False)

Inverts the matrix.

Parameters:in_double_precision (bool) – Whether to do the inversion in double precision. Defaults to False.
Returns:2-element tuple containing
  • log_det (float): The log determinant.
  • det_sign (float): The sign of the determinant, 1 or -1.
Raises:RuntimeError – If matrix is not square.
invert_elements_()

Inverts the elements.

is_diagonal(cutoff:float=default) → bool

Checks if the matrix is approximately diagonal.

Parameters:cutoff (float) – The cutoff value. Defaults to 1.0e-05.
Returns:True if sum(digonal_elements)*cutoff > sum(nondiagonal_elements). False otherwise.
is_symmetric(cutoff:float=default) → bool

Checks if the matrix is approximately symmetric.

Parameters:cutoff (float) – The cutoff value. Defaults to 1.0e-05.
Returns:True if the matrix is approximately symmetric. False otherwise.
is_unit(cutoff:float=default) → bool

Checks if the matrix is identity-like.

Checks if max(M - I) <= cutoff where I is a matrix with the same size as M, ones on the diagonal and zeros elsewhere.

Parameters:cutoff (float) – The cutoff value. Defaults to 1.0e-05.
Returns:True if max(M - I) <= cutoff.

Note

The matrix does not have to be square.

is_zero(cutoff:float=default) → bool

Checks if the elements are all zeros.

Parameters:cutoff (float) – The cutoff value. Defaults to 1.0e-05.
Returns:True if max(abs(M)) <= cutoff.
largest_abs_elem() → float

Returns the largest of the absolute values of the elements.

log_det() -> (log_det:float, det_sign:float)

Computes log determinant of the matrix.

Returns:2-element tuple containing
  • log_det (float): The log determinant.
  • det_sign (float): The sign of the determinant, 1 or -1.
log_sum_exp(prune:float=default) → float

Computes \(f(M)=\log(\sum_{i,j} \exp(M_{i,j}))\) without exp overflow.

If prune > 0.0, ignores terms less than max(M) - prune.

Parameters:prune (float) – The pruning beam. Defaults to -1.0.
Returns:\(\log(\sum_{i,j} \exp(M_{i,j}))\).
max() → float

Returns the maximum value in the matrix.

max_with_mat_(A:MatrixBase)

Applies an element-wise max operation.

Performs the operation M[i,j] = max(M[i,j], A[i,j]).

Parameters:A (Matrix) – The input matrix.
Raises:RuntimeError – In case of size mismatch.
min() → float

Returns the minimum value in the matrix.

min_singular_value() → float

Returns the smallest singular value.

min_with_mat_(A:MatrixBase)

Applies an element-wise min operation.

Performs the operation M[i,j] = min(M[i,j], A[i,j]).

Parameters:A (Matrix) – The input matrix.
Raises:RuntimeError – In case of size mismatch.
mul_cols_vec_(scale:VectorBase)

Scales columns with the elements in a vector.

Performs the operation M[i,j] = scale[j] * M[i,j].

Parameters:scale (Vector) – The scaling vector.
Raises:RuntimeError – In case of size mismatch.
mul_elements_(A:MatrixBase)

Multiplies element-wise with another matrix.

Performs the operation M = M odot A.

Parameters:A (Matrix) – The multiplier.
Raises:RuntimeError – In case of size mismatch.
mul_rows_group_mat_(src:MatrixBase)

Scales matrix with another matrix.

Divides each row of self into src.num_cols equal groups, and then scales i`th row's `j`th group of elements with `src[i,j].

Parameters:src (Matrix) – The scaling matrix.
Raises:RuntimeError – If self.num_rows != src.num_rows or self.num_cols % src.num_cols != 0.
mul_rows_vec_(scale:VectorBase)

Scales rows with the elements in a vector.

Performs the operation M[i,j] = scale[i] * M[i,j].

Parameters:scale (Vector) – The scaling vector.
Raises:RuntimeError – In case of size mismatch.
num_cols

Number of columns (zero for empty matrix).

num_rows

Number of rows (zero for empty matrix).

numpy()

Converts the matrix to a 2-D NumPy array.

The NumPy array is a view into the matrix, i.e. no data is copied.

Returns:A NumPy array sharing data with this matrix.
Return type:numpy.ndarray
orthogonalize_rows_()

Orthogonalizes rows using the Gram-Schmidt process.

Uses random number generation to fill in rows with something non-zero, in cases where the original matrix was of deficient row rank.

Raises:RuntimeError – If self.num_rows > self.num_cols.
power_(pow:float) → bool

Takes the matrix to the specified power.

This method uses an algorithm that works in general for fractional and negative powers. The input matrix must be invertible and have a reasonable condition number. The algorithm is based on eigenvalue decomposition. It will return False and leave the matrix unchanged, if the matrix has real negative eigenvalues (or if it has zero eigenvalues and the power is negative).

Parameters:pow (float) – The exponent.
Returns:True if the operation was successful.
range(row_start, num_rows, col_start, num_cols)

Returns the given range of elements as a new matrix view.

Parameters:
  • row_start (int) – The start row index.
  • num_rows (int) – The number of rows.
  • col_start (int) – The start column index.
  • num_cols (int) – The number of columns.
Returns:

A matrix view representing the given range.

Return type:

SubMatrix

read_(is:istream, binary:bool, add:bool=default)

Reads the matrix from the given C++ stream.

Resizes the matrix to match the size of the matrix read from stream.

Parameters:
  • is (istream) – The input C++ stream.
  • binary (bool) – Whether the stream is binary.
  • add (bool) – Whether to add existing contents to the read matrix. Defaults to False.
resize_(r:int, c:int, resize_type:MatrixResizeType=default, stride_type:MatrixStrideType=default)

Resizes the matrix.

Parameters:
  • r (int) – The new number of rows.
  • c (int) – The new number of columns.
  • resize_type (MatrixResizeType) – How to initialize the elements. If MatrixResizeType.SET_ZERO or MatrixResizeType.COPY_DATA, they are set to zero. If MatrixResizeType.UNDEFINED, they are left uninitialized. Defaults to MatrixResizeType.SET_ZERO.
  • stride_type (MatrixStrideType) – Determines how the elements are laid out in memory. If MatrixStrideType.STRIDE_EQUAL_NUM_COLS, the stride is equal to the number of columns. If MatrixStrideType.DEFAULT_STRIDE, the stride is equal to the smallest multiple of 16 that is larger than the number of columns. Defaults to MatrixStrideType.DEFAULT_STRIDE.
row(index)

Returns the given row as a new vector view.

Parameters:index (int) – The row index.
Returns:A vector view representing the given row.
Return type:SubVector
row_data(index)

Returns row data as a memoryview.

row_range(row_start, num_rows)

Returns the given range of rows as a new matrix view.

Parameters:
  • row_start (int) – The start row index.
  • num_rows (int) – The number of rows.
Returns:

A matrix view representing the given row range.

Return type:

SubMatrix

scale_(alpha:float)

Scales the matrix.

Parameters:alpha (float) – The scalar multiplier.
set_mat_mat_div_mat_(A:MatrixBase, B:MatrixBase, C:MatrixBase)

Computes an elementwise multiplication followed by division.

Performs the operation \(S = A \odot B \oslash C\) where \(\odot\) and \(\oslash\) are elementwise multiplication and division. If \(C[i,j] == 0\) then \(S[i,j]\) remains intact.

Parameters:
  • A (Matrix) – The first input matrix.
  • B (Matrix) – The second input matrix.
  • C (Matrix) – The third input matrix.
Raises:

RuntimeError – In case of size mismatch.

set_rand_uniform_()

Sets the elements to numbers uniformly distributed on (0,1).

set_randn_()

Sets the elements to numbers from standard normal distribution.

set_unit_()

Sets diagonal elements to one, off-diagonal elements to zero.

Note

Works for non-square matrices too.

set_zero_()

Sets the elements to zero.

shape

Two element tuple representing the size of the matrix.

sigmoid_(src:MatrixBase)

Applies sigmoid function to elements of another matrix.

Parameters:src (Matrix) – The source matrix.
Raises:RuntimeError – In case of size mismatch.
singular_values()

Computes singular values.

Returns:The vector of singular values.
Return type:Vector
size()

Returns the size of the matrix.

Returns:A tuple (num_rows, num_cols) of integers.
size_in_bytes() → int

Returns the size (in bytes) of the data held by the matrix.

soft_hinge_(src:MatrixBase)

Applies soft hinge function to elements of another matrix.

Performs the operation M[i,j] = log(1 + exp(src[i,j])).

Parameters:src (Matrix) – The source matrix.
Raises:RuntimeError – In case of size mismatch.
stride

Row stride (distance in memory between each row, >= num_cols).

sum() → float

Returns the sum of the elements.

svd(destructive=False)

Computes singular-value decomposition.

Factorizes a matrix into \(U\ diag(s)\ V^T\).

For non-square matrices, requires self.num_rows >= self.num_cols.

Parameters:destructive (bool) – Whether to use the destructive operation which avoids a copy but mutates self. Defaults to False.
Returns:3-element tuple containing
  • s (Vector): The vector of singular values.
  • U (Matrix): The left orthonormal matrix.
  • Vt (Matrix): The right orthonormal matrix.
Raises:ValueError – If self.num_rows < self.num_cols.

Note

Vt in the output is already transposed. The singular values in s are not sorted.

See also

singular_values() sort_svd()

swap_(other:Matrix)

Swaps the contents of matrices.

Shallow swap.

Parameters:other (Matrix) – The matrix to swap contents with.
sym_add_mat2_(alpha:float, M:MatrixBase, transA:MatrixTransposeType, beta:float)

Adds the square of given matrix to this one.

Performs the operation on symmetric matrices \(S = \alpha\ M\ M^T + \beta\ S\).

Note

It only updates the lower triangle of self. It will leave the matrix asymmetric. If you need it symmetric as a regular matrix, call copy_lower_to_upper_() after this operation.

Parameters:
  • alpha (float) – The scalar multiplier for \(M\ M^T\).
  • M (Matrix) – The input matrix.
  • transM (MatrixTransposeType) – Whether to use M or its transpose. Defaults to MatrixTransposeType.NO_TRANS.
  • beta (float) – The scalar multiplier for the destination matrix.
Raises:

RuntimeError – In case of size mismatch.

sym_pos_semi_def_eig(s:VectorBase, P:MatrixBase, check_thresh:float=default)

Computes eigendecomposition of a positive semi-definite matrix.

Uses SVD to compute the eigendecomposition of a symmetric positive semi-definite matrix: \(M = P\ diag(s) \ P^T\), where \(P\) is an orthogonal matrix, i.e. \(P^{-1} = P^T\).

Parameters:
  • s (Vector) – The eigenvalue vector.
  • P (Matrix) – The eigenvector matrix.
  • check_thresh (float) – The threshold used for checking if input is positive semi-definite. Defaults to 0.001.

Note

Set check_thresh to 2 to ensure the positive semi-definite check won’t ever complain, however it will zero out negative dimensions in the matrix.

Raises:RuntimeError – If input is not positive semi-definite.
tanh_(src:MatrixBase)

Applies tanh function to elements of another matrix.

Parameters:src (Matrix) – The source matrix.
Raises:RuntimeError – In case of size mismatch.
trace(check_square:bool=default) → float

Returns the trace.

If matrix is not square it will return the trace of the square matrix formed from the minimum dimension, e.g. if the matrix is 3x5 it will return the trace of the 3x3 submatrix at the beginning.

Parameters:

check_square (bool) – Check if the matrix is square. Defaults to True.

Raises:
  • RuntimeError – If the matrix is not square and
  • **check_square** is True.
transpose_()

Transposes the matrix.

write(os:ostream, binary:bool)

Writes the matrix to given C++ stream.

Parameters:
  • os (ostream) – The output C++ straem.
  • binary (bool) – Whether the stream is binary.
class kaldi.matrix.SubMatrix(obj, row_start=0, num_rows=None, col_start=0, num_cols=None)[source]

Single precision matrix view.

Creates a new matrix view from a matrix like object.

If possible the new matrix view will share its data with the obj, i.e. no copy will be made. A copy will only be made if obj.__array__ returns a copy, if obj is a sequence, or if a copy is needed to satisfy any of the other requirements (data type, order, etc.). Regardless of whether a copy is made or not, the new matrix view will not own the memory buffer backing it, i.e. it will not support matrix operations that reallocate memory.

Parameters:
  • obj (matrix_like) – A matrix, a 2-D numpy array, any object exposing a 2-D array interface, an object with an __array__ method returning a 2-D numpy array, or any sequence that can be interpreted as a matrix.
  • row_start (int) – The start row index. Defaults to 0.
  • num_rows (int) – The number of rows. If None, it is set to self.num_rows - row_start. Defaults to None.
  • col_start (int) – The start column index. Defaults to 0.
  • num_cols (int) – The number of columns. If None, it is set to self.num_cols - col_start. Defaults to None.
add_(alpha:float)

Adds a scalar to each element of the matrix.

Parameters:alpha (float) – The scalar to add.
add_cols_(src, indices)

Adds columns from another matrix.

Adds column indices[r] of src to column r. As a special case, if indexes[i] == -1, skips column i. All elements of indices must be in [-1, src.num_cols-1], and src.num_rows must equal self.num_rows.

Parameters:
  • src (Matrix) – The input matrix.
  • indices (List[int]) – The list of column indices.
add_diag_vec_mat_(alpha:float, v:VectorBase, M:MatrixBase, transM:MatrixTransposeType, beta:float=default)

Adds given matrix to this one after scaling its rows.

Perform the operation \(S = \alpha\ diag(v)\ M + \beta\ S\).

Parameters:
  • alpha (float) – The scalar multiplier for \(diag(v) M\).
  • v (Vector) – The scaling vector.
  • M (Matrix) – The input matrix.
  • transM (MatrixTransposeType) – Whether to use M or its transpose. Defaults to MatrixTransposeType.NO_TRANS.
  • beta (float) – The scalar multiplier for the destination matrix. Defaults to 1.0.
Raises:

RuntimeError – In case of size mismatch.

add_mat_(alpha, M, trans=<MatrixTransposeType.NO_TRANS: 111>)

Adds another matrix to this one.

Performs the operation \(S = \alpha\ M + S\).

Parameters:
Raises:

RuntimeError – In case of size mismatch.

add_mat_diag_vec_(alpha:float, M:MatrixBase, transM:MatrixTransposeType, v:VectorBase, beta:float=default)

Adds given matrix to this one after scaling its columns.

Perform the operation \(S = \alpha\ M\ diag(v) + \beta\ S\).

Parameters:
  • alpha (float) – The scalar multiplier for \(M\ diag(v)\).
  • M (Matrix) – The input matrix.
  • transM (MatrixTransposeType) – Whether to use M or its transpose. Defaults to MatrixTransposeType.NO_TRANS.
  • v (Vector) – The scaling vector.
  • beta (float) – The scalar multiplier for the destination matrix. Defaults to 1.0.
Raises:

RuntimeError – In case of size mismatch.

add_mat_mat_(A, B, transA=<MatrixTransposeType.NO_TRANS: 111>, transB=<MatrixTransposeType.NO_TRANS: 111>, alpha=1.0, beta=1.0, sparseA=False, sparseB=False)

Adds the product of given matrices.

Performs the operation \(M = \alpha\ A\ B + \beta\ M\).

Parameters:
  • A (Matrix or TpMatrix or SpMatrix) – The first input matrix.
  • B (Matrix or TpMatrix or SpMatrix) – The second input matrix.
  • transA (MatrixTransposeType) – Whether to use A or its transpose. Defaults to MatrixTransposeType.NO_TRANS.
  • transB (MatrixTransposeType) – Whether to use B or its transpose. Defaults to MatrixTransposeType.NO_TRANS.
  • alpha (float) – The scalar multiplier for the product. Defaults to 1.0.
  • beta (float) – The scalar multiplier for the destination vector. Defaults to 1.0.
  • sparseA (bool) – Whether to use the algorithm that is faster when A is sparse. Defaults to False.
  • sparseA – Whether to use the algorithm that is faster when B is sparse. Defaults to False.
Raises:
  • RuntimeError – In case of size mismatch.
  • TypeError – If matrices of given types can not be multiplied.
add_mat_mat_elements_(alpha:float, A:MatrixBase, B:MatrixBase, beta:float)

Adds the element-wise multiplication of given matrices to this one.

Performs the operation \(S = \alpha\ A\odot B + \beta\ S\).

Parameters:
  • alpha (float) – The scalar multiplier for \(A \odot B\).
  • A (Matrix) – The first input matrix.
  • B (Matrix) – The second input matrix.
  • beta (float) – The scalar multiplier for the destination matrix.
Raises:

RuntimeError – In case of size mismatch.

add_mat_mat_mat_(alpha:float, A:MatrixBase, transA:MatrixTransposeType, B:MatrixBase, transB:MatrixTransposeType, C:MatrixBase, transC:MatrixTransposeType, beta:float)

Adds the product of three matrices to this one.

Performs the operation \(S = \alpha\ A\ B\ C + \beta\ S\).

Parameters:
  • alpha (float) – The scalar multiplier for \(A\ B\ C\).
  • A (Matrix) – The first input matrix.
  • transA (MatrixTransposeType) – Whether to use A or its transpose.
  • B (Matrix) – The second input matrix.
  • transB (MatrixTransposeType) – Whether to use B or its transpose.
  • C (Matrix) – The third input matrix.
  • transC (MatrixTransposeType) – Whether to use C or its transpose.
  • beta (float) – The scalar multiplier for the destination matrix.
Raises:

RuntimeError – In case of size mismatch.

add_rows_(src, indices, alpha=1.0)

Adds rows from another matrix.

Scales row indices[r] of src with alpha and adds it to row r. As a special case, if indexes[i] == -1, skips row i. All elements of indices must be in [-1, src.num_rows-1], and src.num_cols must equal self.num_cols.

Parameters:
  • src (Matrix) – The input matrix.
  • indices (List[int]) – The list of row indices.
  • alpha (float) – The scalar multiplier. Defaults to 1.0.
add_to_diag_(alpha:float)

Adds a scalar to the diagonal elements of the matrix.

Parameters:alpha (float) – The scalar to add.
add_vec_to_cols_(alpha:float, v:VectorBase)

Adds input vector to each column of this matrix.

Performs the operation \(M_{ij} = M_{ij} + \alpha\ v_{i}\).

Parameters:
  • alpha (float) – The scalar multiplier.
  • v (Vector) – The input vector.
Raises:

RuntimeError – In case of size mismatch.

add_vec_to_rows_(alpha:float, v:VectorBase)

Adds input vector to each row of this matrix.

Performs the operation \(M_{ij} = M_{ij} + \alpha\ v_{j}\).

Parameters:
  • alpha (float) – The scalar multiplier.
  • v (Vector) – The input vector.
Raises:

RuntimeError – In case of size mismatch.

add_vec_vec_(alpha:float, a:VectorBase, b:VectorBase)

Adds outer product of input vectors to this matrix.

Performs the operation \(M = M + \alpha\ a \ b^T\).

Parameters:
  • alpha (float) – The scalar multiplier.
  • a (Vector) – The first input vector.
  • b (Vector) – The second input vector.
apply_ceiling_(ceiling:float)

Applies ceiling operation to each element.

Performs the operation M[i,j] = min(M[i,j], ceiling).

Parameters:ceiling (float) – The ceiling value.
Returns:The number of elements changed.
apply_exp_()

Applies exponential operation to each element.

apply_floor_(floor:float)

Applies floor operation to each element.

Performs the operation M[i,j] = max(M[i,j], floor).

Parameters:floor (float) – The floor value.
Returns:The number of elements changed.
apply_heaviside_()

Applies the Heaviside step function to each element.

apply_log_()

Applies natural log operation to each element.

apply_pow_(power:float)

Takes each element to the given power.

Parameters:power (float) – The exponent value.
Raises:RuntimeError – If an element cannot be raised to the given power.
apply_pow_abs_(power:float, include_sign:bool=default)

Takes the absolute value of each element raised to the given power.

If the power is negative and the input is zero, the output is zero.

Parameters:
  • power (float) – The power value.
  • include_sign (bool) – Whether to include the sign of the input value. Defaults to False.
Raises:

RuntimeError – If an element cannot be raised to the given power.

apply_softmax_() → float

Applies the softmax operation to each element.

Performs the operation \(M_{i,j} = \frac{\exp(M_{i,j})}{\sum_{k,j} \exp(M_{k,j})}\).

Returns:\(\log(\sum_{k,j} \exp(M_{k,j}))\).
approx_equal(other, tol=0.01)

Checks if matrices are approximately equal.

Parameters:
  • other (Matrix) – The matrix to compare against.
  • tol (float) – The tolerance for the equality check. Defaults to 0.01.
Returns:

True if self.size() == other.size() and ||self-other|| <= tol*||self||. False otherwise.

clone()

Clones the matrix.

The clone allocates new memory for its contents and supports matrix operations that reallocate memory, i.e. it is not a view.

Returns:A copy of the matrix.
Return type:Matrix
col_range(col_start, num_cols)

Returns the given range of columns as a new matrix view.

Parameters:
  • col_start (int) – The start column index.
  • num_cols (int) – The number of columns.
Returns:

A matrix view representing the given column range.

Return type:

SubMatrix

cond() → float

Returns the condition number of the SVD computation.

copy_(src, trans=<MatrixTransposeType.NO_TRANS: 111>)

Copies the elements from another matrix.

Parameters:
Raises:

ValueError – In case of size mismatch.

copy_col_from_vec_(v:VectorBase, col:int)

Copies a vector into the specified column.

Parameters:
  • v (Vector) – The input vector.
  • col (int) – The column index.
Raises:

RuntimeError – In case of size mismatch.

copy_cols_(src, indices)

Copies columns from another matrix.

Copies column r from column indices[r] of src. As a special case, if indexes[i] == -1, sets column i to zero. All elements of indices must be in [-1, src.num_cols-1], and src.num_rows must equal self.num_rows.

Parameters:
  • src (Matrix) – The input matrix.
  • indices (List[int]) – The list of column indices.
copy_cols_from_vec_(v:VectorBase)

Copies column elements from a vector.

This method has two modes of operation. If the number of elements in self and v are the same, then elements of v are copied into self column by column. If the number of elements in v is equal to self.num_rows, then the elements of v are copied into each column of self.

Parameters:v (Vector) – The input vector.
Raises:RuntimeError – In case of size mismatch.
copy_diag_from_vec_(v:VectorBase)

Copies a vector into the diagonal.

Parameters:v (Vector) – The input vector.
Raises:RuntimeError – In case of size mismatch.
copy_lower_to_upper_()

Copies lower triangle to upper triangle.

Raises:RuntimeError – If matrix is not square.
copy_row_from_vec_(v:VectorBase, row:int)

Copies a vector into the specified row.

Parameters:
  • v (Vector) – The input vector.
  • row (int) – The row index.
Raises:

RuntimeError – In case of size mismatch.

copy_rows_(src, indices)

Copies rows from another matrix.

Copies row r from row indices[r] of src. As a special case, if indexes[i] == -1, sets row i to zero. All elements of indices must be in [-1, src.num_rows-1], and src.num_cols must equal self.num_cols.

Parameters:
  • src (Matrix) – The input matrix.
  • indices (List[int]) – The list of row indices.
copy_rows_from_vec_(v:VectorBase)

Copies row elements from a vector.

This method has two modes of operation. If the number of elements in self and v are the same, then elements of v are copied into self row by row. If the number of elements in v is equal to self.num_cols, then the elements of v are copied into each row of self.

Parameters:v (Vector) – The input vector.
Raises:RuntimeError – In case of size mismatch.
copy_upper_to_lower_()

Copies upper triangle to lower triangle.

Raises:RuntimeError – If matrix is not square.
data

Matrix data as a memoryview.

diff_sigmoid_(value:MatrixBase, diff:MatrixBase)

Backpropagates derivatives through the sigmoid function.

Performs the operation M[i,j] = diff[i,j] * value[i,j] * (1 - value[i,j]).

Parameters:
  • value (Matrix) – The activations.
  • diff (Matrix) – The derivatives.
diff_tanh_(value:MatrixBase, diff:MatrixBase)

Backpropagates derivatives through the tanh function.

Performs the operation M[i,j] = diff[i,j] * (1 - value[i,j]^2).

Parameters:
  • value (Matrix) – The activations.
  • diff (Matrix) – The derivatives.
div_elements_(A:MatrixBase)

Divides element-wise with another matrix.

Performs the operation M = M oslash A.

Parameters:A (Matrix) – The denominator.
Raises:RuntimeError – In case of size mismatch.
eig()

Computes eigendecomposition.

Factorizes a square matrix into \(P\ D\ P^{-1}\).

The relationship of \(D\) to the eigenvalues is slightly complicated, due to the need for \(P\) to be real. In the symmetric case, \(D\) is diagonal and real, but in the non-symmetric case there may be complex-conjugate pairs of eigenvalues. In this case, for the equation \(y = P\ D\ P^{-1}\) to hold, \(D\) must actually be block diagonal, with 2x2 blocks corresponding to any such pairs. If a pair is \(\lambda +- i\mu\), \(D\) will have a corresponding 2x2 block \([\lambda, \mu; -\mu, \lambda]\). Note that if the matrix is not invertible, \(P\) may not be invertible so in this case instead of the equation \(y = P\ D\ P^{-1}\) holding, we have \(y\ P = P\ D\).

Returns:3-element tuple containing
  • P (Matrix): The eigenvector matrix, where ith column corresponds to the ith eigenvector.
  • r (Vector): The vector with real components of the eigenvalues.
  • i (Vector): The vector with imaginary components of the eigenvalues.
Raises:ValueError – If the matrix is not square.
equal(other:MatrixBase) → bool

Checks if matrices are exactly equal.

Parameters:other (Matrix) – The matrix to check against
Returns:True if self[i,j] == other[i,j]` for all i,j. False otherwise.
frobenius_norm() → float

Returns the Frobenius norm of the matrix

group_max_(src:MatrixBase)

Computes group max of elements in another matrix.

Performs the operation self[i,j] = max(src[i,j*gs:(j+1)*gs]) where gs = src.num_cols / self.num_cols.

Parameters:src (MatrixBase) – The source matrix.
Raises:RuntimeError – If src.num_rows != self.num_rows or src.num_rows % self.num_rows != 0.
group_max_deriv_(input:MatrixBase, output:MatrixBase)

Computes derivatives for the group max operation.

Parameters:
  • input (Matrix) – The input to group max operation (src in group_max_()).
  • output (Matrix) – The output of group max operation.
group_pnorm_(src:MatrixBase, power:float)

Computes group p-norm of elements in another matrix.

Performs the operation self[i,j] = norm(src[i,j*gs:(j+1)*gs], power) where gs = src.num_cols / self.num_cols.

Parameters:
  • src (Matrix) – The source matrix.
  • power (float) – The p value for p-norm. It must be non-negative.
Raises:

RuntimeError – If src.num_rows != self.num_rows or src.num_rows % self.num_rows != 0.

group_pnorm_deriv_(input:MatrixBase, output:MatrixBase, power:float)

Computes derivatives for the group p-norm operation.

Parameters:
  • input (Matrix) – The input to group p-norm operation (src in group_pnorm_()).
  • output (Matrix) – The output of group p-norm operation.
  • power (float) – The p value for p-norm. It must be non-negative.
heaviside_(src:MatrixBase)

Applies Heaviside step function to elements of another matrix.

Parameters:src (Matrix) – The source matrix.
Raises:RuntimeError – In case of size mismatch.
invert_(in_double_precision=False)

Inverts the matrix.

Parameters:in_double_precision (bool) – Whether to do the inversion in double precision. Defaults to False.
Returns:2-element tuple containing
  • log_det (float): The log determinant.
  • det_sign (float): The sign of the determinant, 1 or -1.
Raises:RuntimeError – If matrix is not square.
invert_elements_()

Inverts the elements.

is_diagonal(cutoff:float=default) → bool

Checks if the matrix is approximately diagonal.

Parameters:cutoff (float) – The cutoff value. Defaults to 1.0e-05.
Returns:True if sum(digonal_elements)*cutoff > sum(nondiagonal_elements). False otherwise.
is_symmetric(cutoff:float=default) → bool

Checks if the matrix is approximately symmetric.

Parameters:cutoff (float) – The cutoff value. Defaults to 1.0e-05.
Returns:True if the matrix is approximately symmetric. False otherwise.
is_unit(cutoff:float=default) → bool

Checks if the matrix is identity-like.

Checks if max(M - I) <= cutoff where I is a matrix with the same size as M, ones on the diagonal and zeros elsewhere.

Parameters:cutoff (float) – The cutoff value. Defaults to 1.0e-05.
Returns:True if max(M - I) <= cutoff.

Note

The matrix does not have to be square.

is_zero(cutoff:float=default) → bool

Checks if the elements are all zeros.

Parameters:cutoff (float) – The cutoff value. Defaults to 1.0e-05.
Returns:True if max(abs(M)) <= cutoff.
largest_abs_elem() → float

Returns the largest of the absolute values of the elements.

log_det() -> (log_det:float, det_sign:float)

Computes log determinant of the matrix.

Returns:2-element tuple containing
  • log_det (float): The log determinant.
  • det_sign (float): The sign of the determinant, 1 or -1.
log_sum_exp(prune:float=default) → float

Computes \(f(M)=\log(\sum_{i,j} \exp(M_{i,j}))\) without exp overflow.

If prune > 0.0, ignores terms less than max(M) - prune.

Parameters:prune (float) – The pruning beam. Defaults to -1.0.
Returns:\(\log(\sum_{i,j} \exp(M_{i,j}))\).
max() → float

Returns the maximum value in the matrix.

max_with_mat_(A:MatrixBase)

Applies an element-wise max operation.

Performs the operation M[i,j] = max(M[i,j], A[i,j]).

Parameters:A (Matrix) – The input matrix.
Raises:RuntimeError – In case of size mismatch.
min() → float

Returns the minimum value in the matrix.

min_singular_value() → float

Returns the smallest singular value.

min_with_mat_(A:MatrixBase)

Applies an element-wise min operation.

Performs the operation M[i,j] = min(M[i,j], A[i,j]).

Parameters:A (Matrix) – The input matrix.
Raises:RuntimeError – In case of size mismatch.
mul_cols_vec_(scale:VectorBase)

Scales columns with the elements in a vector.

Performs the operation M[i,j] = scale[j] * M[i,j].

Parameters:scale (Vector) – The scaling vector.
Raises:RuntimeError – In case of size mismatch.
mul_elements_(A:MatrixBase)

Multiplies element-wise with another matrix.

Performs the operation M = M odot A.

Parameters:A (Matrix) – The multiplier.
Raises:RuntimeError – In case of size mismatch.
mul_rows_group_mat_(src:MatrixBase)

Scales matrix with another matrix.

Divides each row of self into src.num_cols equal groups, and then scales i`th row's `j`th group of elements with `src[i,j].

Parameters:src (Matrix) – The scaling matrix.
Raises:RuntimeError – If self.num_rows != src.num_rows or self.num_cols % src.num_cols != 0.
mul_rows_vec_(scale:VectorBase)

Scales rows with the elements in a vector.

Performs the operation M[i,j] = scale[i] * M[i,j].

Parameters:scale (Vector) – The scaling vector.
Raises:RuntimeError – In case of size mismatch.
num_cols

Number of columns (zero for empty matrix).

num_rows

Number of rows (zero for empty matrix).

numpy()

Converts the matrix to a 2-D NumPy array.

The NumPy array is a view into the matrix, i.e. no data is copied.

Returns:A NumPy array sharing data with this matrix.
Return type:numpy.ndarray
orthogonalize_rows_()

Orthogonalizes rows using the Gram-Schmidt process.

Uses random number generation to fill in rows with something non-zero, in cases where the original matrix was of deficient row rank.

Raises:RuntimeError – If self.num_rows > self.num_cols.
power_(pow:float) → bool

Takes the matrix to the specified power.

This method uses an algorithm that works in general for fractional and negative powers. The input matrix must be invertible and have a reasonable condition number. The algorithm is based on eigenvalue decomposition. It will return False and leave the matrix unchanged, if the matrix has real negative eigenvalues (or if it has zero eigenvalues and the power is negative).

Parameters:pow (float) – The exponent.
Returns:True if the operation was successful.
range(row_start, num_rows, col_start, num_cols)

Returns the given range of elements as a new matrix view.

Parameters:
  • row_start (int) – The start row index.
  • num_rows (int) – The number of rows.
  • col_start (int) – The start column index.
  • num_cols (int) – The number of columns.
Returns:

A matrix view representing the given range.

Return type:

SubMatrix

read_(is:istream, binary:bool, add:bool=default)

Reads the matrix from the given C++ stream.

Parameters:
  • is (istream) – The input C++ stream.
  • binary (bool) – Whether the stream is binary.
  • add (bool) – Whether to add existing contents to the read matrix. Defaults to False.
row(index)

Returns the given row as a new vector view.

Parameters:index (int) – The row index.
Returns:A vector view representing the given row.
Return type:SubVector
row_data(index)

Returns row data as a memoryview.

row_range(row_start, num_rows)

Returns the given range of rows as a new matrix view.

Parameters:
  • row_start (int) – The start row index.
  • num_rows (int) – The number of rows.
Returns:

A matrix view representing the given row range.

Return type:

SubMatrix

scale_(alpha:float)

Scales the matrix.

Parameters:alpha (float) – The scalar multiplier.
set_mat_mat_div_mat_(A:MatrixBase, B:MatrixBase, C:MatrixBase)

Computes an elementwise multiplication followed by division.

Performs the operation \(S = A \odot B \oslash C\) where \(\odot\) and \(\oslash\) are elementwise multiplication and division. If \(C[i,j] == 0\) then \(S[i,j]\) remains intact.

Parameters:
  • A (Matrix) – The first input matrix.
  • B (Matrix) – The second input matrix.
  • C (Matrix) – The third input matrix.
Raises:

RuntimeError – In case of size mismatch.

set_rand_uniform_()

Sets the elements to numbers uniformly distributed on (0,1).

set_randn_()

Sets the elements to numbers from standard normal distribution.

set_unit_()

Sets diagonal elements to one, off-diagonal elements to zero.

Note

Works for non-square matrices too.

set_zero_()

Sets the elements to zero.

shape

Two element tuple representing the size of the matrix.

sigmoid_(src:MatrixBase)

Applies sigmoid function to elements of another matrix.

Parameters:src (Matrix) – The source matrix.
Raises:RuntimeError – In case of size mismatch.
singular_values()

Computes singular values.

Returns:The vector of singular values.
Return type:Vector
size()

Returns the size of the matrix.

Returns:A tuple (num_rows, num_cols) of integers.
size_in_bytes() → int

Returns the size (in bytes) of the data held by the matrix.

soft_hinge_(src:MatrixBase)

Applies soft hinge function to elements of another matrix.

Performs the operation M[i,j] = log(1 + exp(src[i,j])).

Parameters:src (Matrix) – The source matrix.
Raises:RuntimeError – In case of size mismatch.
stride

Row stride (distance in memory between each row, >= num_cols).

sum() → float

Returns the sum of the elements.

svd(destructive=False)

Computes singular-value decomposition.

Factorizes a matrix into \(U\ diag(s)\ V^T\).

For non-square matrices, requires self.num_rows >= self.num_cols.

Parameters:destructive (bool) – Whether to use the destructive operation which avoids a copy but mutates self. Defaults to False.
Returns:3-element tuple containing
  • s (Vector): The vector of singular values.
  • U (Matrix): The left orthonormal matrix.
  • Vt (Matrix): The right orthonormal matrix.
Raises:ValueError – If self.num_rows < self.num_cols.

Note

Vt in the output is already transposed. The singular values in s are not sorted.

See also

singular_values() sort_svd()

sym_add_mat2_(alpha:float, M:MatrixBase, transA:MatrixTransposeType, beta:float)

Adds the square of given matrix to this one.

Performs the operation on symmetric matrices \(S = \alpha\ M\ M^T + \beta\ S\).

Note

It only updates the lower triangle of self. It will leave the matrix asymmetric. If you need it symmetric as a regular matrix, call copy_lower_to_upper_() after this operation.

Parameters:
  • alpha (float) – The scalar multiplier for \(M\ M^T\).
  • M (Matrix) – The input matrix.
  • transM (MatrixTransposeType) – Whether to use M or its transpose. Defaults to MatrixTransposeType.NO_TRANS.
  • beta (float) – The scalar multiplier for the destination matrix.
Raises:

RuntimeError – In case of size mismatch.

sym_pos_semi_def_eig(s:VectorBase, P:MatrixBase, check_thresh:float=default)

Computes eigendecomposition of a positive semi-definite matrix.

Uses SVD to compute the eigendecomposition of a symmetric positive semi-definite matrix: \(M = P\ diag(s) \ P^T\), where \(P\) is an orthogonal matrix, i.e. \(P^{-1} = P^T\).

Parameters:
  • s (Vector) – The eigenvalue vector.
  • P (Matrix) – The eigenvector matrix.
  • check_thresh (float) – The threshold used for checking if input is positive semi-definite. Defaults to 0.001.

Note

Set check_thresh to 2 to ensure the positive semi-definite check won’t ever complain, however it will zero out negative dimensions in the matrix.

Raises:RuntimeError – If input is not positive semi-definite.
tanh_(src:MatrixBase)

Applies tanh function to elements of another matrix.

Parameters:src (Matrix) – The source matrix.
Raises:RuntimeError – In case of size mismatch.
trace(check_square:bool=default) → float

Returns the trace.

If matrix is not square it will return the trace of the square matrix formed from the minimum dimension, e.g. if the matrix is 3x5 it will return the trace of the 3x3 submatrix at the beginning.

Parameters:

check_square (bool) – Check if the matrix is square. Defaults to True.

Raises:
  • RuntimeError – If the matrix is not square and
  • **check_square** is True.
transpose_()

Transposes the matrix.

write(os:ostream, binary:bool)

Writes the matrix to given C++ stream.

Parameters:
  • os (ostream) – The output C++ straem.
  • binary (bool) – Whether the stream is binary.
class kaldi.matrix.SubVector(obj, start=0, length=None)[source]

Single precision vector view.

Creates a new vector view from a vector like object.

If possible the new vector view will share its data with the obj, i.e. no copy will be made. A copy will only be made if obj.__array__ returns a copy, if obj is a sequence, or if a copy is needed to satisfy any of the other requirements (data type, order, etc.). Regardless of whether a copy is made or not, the new vector view will not own the memory buffer backing it, i.e. it will not support vector operations that reallocate memory.

Parameters:
  • obj (vector_like) – A vector, a 1-D numpy array, any object exposing a 1-D array interface, an object whose __array__ method returns a 1-D numpy array, or any sequence that can be interpreted as a vector.
  • start (int) – The index of the view start. Defaults to 0.
  • length (int) – The length of the view. If None, it is set to len(obj) - start. Defaults to None.
add_(value:float)

Adds the given value to each element.

Parameters:value (float) – The value to add.
add_col_sum_mat_(alpha, M, beta=1.0)

Adds the sum of matrix columns.

Performs the operation \(y = \alpha\ \sum_i M[:,i] + \beta\ y\).

Parameters:
  • alpha (float) – The scalar multiplier for the column sum.
  • M (Matrix) – The input matrix.
  • beta (float) – The scalar multiplier for the destination vector. Defaults to 1.0.
Raises:

ValueError – If self.dim != M.num_rows.

add_diag_mat2_(alpha, M, trans=<MatrixTransposeType.NO_TRANS: 111>, beta=1.0)

Adds the diagonal of a matrix multiplied with its transpose.

Performs the operation \(y = \alpha\ diag(M M^T) + \beta\ y\).

Parameters:
  • alpha (float) – The scalar multiplier for the diagonal.
  • M (Matrix) – The input matrix.
  • trans (MatrixTransposeType) – Whether to use M or its transpose. Defaults to MatrixTransposeType.NO_TRANS.
  • beta (float) – The scalar multiplier for the destination vector. Defaults to 1.0.
Raises:

ValueError – In case of size mismatch.

add_diag_mat_mat_(alpha, M, transM, N, transN, beta=1.0)

Adds the diagonal of a matrix-matrix product.

Performs the operation \(y = \alpha\ diag(M N) + \beta\ y\).

Parameters:
  • alpha (float) – The scalar multiplier for the diagonal.
  • M (Matrix) – The first input matrix.
  • transM (MatrixTransposeType) – Whether to use M or its transpose.
  • N (Matrix) – The second input matrix.
  • transN (MatrixTransposeType) – Whether to use N or its transpose.
  • beta (float) – The scalar multiplier for the destination vector. Defaults to 1.0.
Raises:

ValueError – In case of size mismatch.

add_mat_vec_(alpha, M, trans, v, beta, sparse=False)

Computes a matrix-vector product.

Performs the operation \(y = \alpha\ M\ v + \beta\ y\).

Parameters:
  • alpha (float) – The scalar multiplier for the matrix-vector product.
  • M (Matrix or SpMatrix or TpMatrix) – The input matrix.
  • trans (MatrixTransposeType) – Whether to use M or its transpose.
  • v (Vector) – The input vector.
  • beta (int) – The scalar multiplier for the destination vector.
  • sparse (bool) – Whether to use the algorithm that is faster when v is sparse. Defaults to False.
Raises:

ValueError – In case of size mismatch.

add_row_sum_mat_(alpha, M, beta=1.0)

Adds the sum of matrix rows.

Performs the operation \(y = \alpha\ \sum_i M[i] + \beta\ y\).

Parameters:
  • alpha (float) – The scalar multiplier for the row sum.
  • M (Matrix) – The input matrix.
  • beta (float) – The scalar multiplier for the destination vector. Defaults to 1.0.
Raises:

ValueError – If self.dim != M.num_cols.

add_vec2_(alpha, v)

Adds the squares of elements from another vector.

Performs the operation \(y = y + \alpha\ v\odot v\).

Parameters:
Raises:

RuntimeError – In case of size mismatch.

add_vec_(alpha, v)

Adds another vector.

Performs the operation \(y = y + \alpha\ v\).

Parameters:
Raises:

RuntimeError – In case of size mismatch.

add_vec_div_vec_(alpha:float, v:VectorBase, r:VectorBase, beta:float)

Computes an element-wise vector-vector division.

Performs the operation \(y = \alpha\ v\oslash r + \beta\ y\).

Parameters:
  • alpha (float) – The scalar multiplier for the division.
  • v (Vector) – The first input vector.
  • r (Vector) – The second input vector.
  • beta (float) – The scalar multiplier for the destination vector.
Raises:

RuntimeError – In case of size mismatch.

add_vec_vec_(alpha:float, v:VectorBase, r:VectorBase, beta:float)

Computes an element-wise vector-vector product.

Performs the operation \(y = \alpha\ v\odot r + \beta\ y\).

Parameters:
  • alpha (float) – The scalar multiplier for the product.
  • v (Vector) – The first input vector.
  • r (Vector) – The second input vector.
  • beta (float) – The scalar multiplier for the destination vector.
Raises:

RuntimeError – In case of size mismatch.

apply_abs_()

Applies the absolute value operation to each element.

apply_ceiling_(ceiling:float) → int

Applies the ceiling operation to each element.

Performs the operation y[i] = min(y[i], ceiling).

Parameters:ceiling (float) – The ceiling value.
Returns:The number of elements changed.
apply_ceiling_no_count_(ceiling:float)

Applies the ceiling operation to each element.

Performs the operation y[i] = min(y[i], ceiling).

Parameters:ceiling (float) – The ceiling value.
apply_exp_()

Applies the exponential operation to each element.

apply_floor_(floor:float) → int

Applies the floor operation to each element.

Performs the operation y[i] = max(y[i], floor).

Parameters:floor (float) – The floor value.
Returns:The number of elements changed.
apply_floor_no_count_(floor:float)

Applies the floor operation to each element.

Performs the operation y[i] = max(y[i], floor).

Parameters:floor (float) – The floor value.
apply_floor_vector_(floor:VectorBase) → int

Applies a separate floor operation to each element.

Performs the operation y[i] = max(y[i], floor[i]).

Parameters:floor (Vector) – The floor vector.
Returns:The number of elements changed.
apply_log_()

Applies the natural log operation to each element.

apply_log_and_copy_(v:VectorBase)

Applies the natural log operation to the given vector and copies it.

Parameters:v (Vector) – The input vector.
apply_log_softmax_() → float

Applies the logsoftmax operation to each element.

Performs the operation \(y_i = y_i - \log(\sum_j \exp(y_j))\).

Returns:\(\log(\sum_j \exp(y_j))\).
apply_pow_(power:float)

Takes each element to the given power.

Parameters:power (float) – The exponent value.
Raises:RuntimeError – If an element cannot be raised to the given power.
apply_pow_abs_(power:float, include_sign:bool=default)

Takes the absolute value of each element raised to the given power.

If the power is negative and the input is zero, the output is zero.

Parameters:
  • power (float) – The power value.
  • include_sign (bool) – Whether to include the sign of the input value. Defaults to False.
Raises:

RuntimeError – If an element cannot be raised to the given power.

apply_softmax_() → float

Applies the softmax operation to each element.

Performs the operation \(y_i = \frac{\exp(y_i)}{\sum_j \exp(y_j)}\).

Returns:\(\log(\sum_j \exp(y_j))\).
approx_equal(other, tol=0.01)

Checks if vectors are approximately equal.

Parameters:
  • other (Vector) – The vector to compare against.
  • tol (float) – The tolerance for the equality check. Defaults to 0.01.
Returns:

True if self.dim == other.dim and ||self-other|| <= tol*||self||. False otherwise.

clone()

Clones the vector.

The clone allocates new memory for its contents and supports vector operations that reallocate memory, i.e. it is not a view.

Returns:A copy of the vector.
Return type:Vector
copy_(src)

Copies the elements from another vector.

Parameters:src (Vector or DoubleVector) – The input vector.
Raises:ValueError – In case of size mismatch.
copy_col_from_mat_(M, col)

Copies the elements from a matrix column.

Parameters:
Raises:
copy_cols_from_mat_(M)

Copies the elements from a matrix column-by-columm.

Parameters:M (Matrix) – The input matrix.
Raises:ValueError – In case of size mismatch.
copy_diag_from_mat_(M)

Copies the digonal elements from a matrix.

Parameters:M (Matrix or SpMatrix or TpMatrix) – The input matrix.
Raises:ValueError – In case of size mismatch.
copy_from_packed_(M)

Copies the elements from a packed matrix.

Parameters:M (SpMatrix or TpMatrix or DoubleSpMatrix or DoubleTpMatrix) – The input packed matrix.
Raises:ValueError – If self.dim !=  M.num_rows * (M.num_rows + 1) / 2.
copy_row_from_mat_(M, row)

Copies the elements from a matrix row.

Parameters:
Raises:
copy_rows_from_mat_(M)

Copies the elements from a matrix row-by-row.

Parameters:M (Matrix or DoubleMatrix) – The input matrix.
Raises:ValueError – In case of size mismatch.
data

Vector data as a memoryview.

dim

Dimension (zero for empty vector).

div_elements_(v)

Divides the elements with the elements of another vector.

Performs the operation y[i] /= v[i].

Parameters:v (Vector or DoubleVector) – The input vector.
Raises:RuntimeError – In case of size mismatch.
invert_elements_()

Inverts the elements.

Raises:RuntimeError – If any of the elements is zero.
is_zero(cutoff:float=default) → bool

Checks if the elements are all zeros.

Parameters:cutoff (float) – The cutoff value. Defaults to 1.0e-06.
Returns:True if max(abs(self)) <= cutoff.
log_sum_exp(prune:float=default) → float

Computes \(f(x)=\log(\sum_i \exp(x_i))\) without exp overflow.

If prune > 0.0, ignores terms less than max(x) - prune.

Parameters:prune (float) – The pruning beam. Defaults to -1.0.
Returns:\(\log(\sum_i \exp(x_i))\).
max() → float

Returns the maximum value in the vector.

max_index() -> (value:float, index:int)

Returns the maximum value in the vector, and the associated index.

min() → float

Returns the minimum value in the vector.

min_index() -> (value:float, index:int)

Returns the minimum value in the vector, and the associated index.

mul_elements_(v)

Multiplies the elements with the elements of another vector.

Performs the operation y[i] *= v[i].

Parameters:v (Vector or DoubleVector) – The input vector.
Raises:RuntimeError – In case of size mismatch.
mul_tp_(M, trans)

Multiplies the vector with a lower-triangular matrix.

Performs the operation \(y = M\ y\).

Parameters:
Raises:

ValueError – In case of size mismatch.

norm(p:float) → float

Computes the p-norm of the vector.

Parameters:p (float) – The p value. It must be non-negative.
Returns:The p-norm.
Raises:RuntimeError – If p < 0 or p-norm cannot be computed.
numpy()

Converts the vector to a 1-D NumPy array.

The NumPy array is a view into the vector, i.e. no data is copied.

Returns:A NumPy array sharing data with this vector.
Return type:numpy.ndarray
rand_categorical() → int

Chooses a random index into the vector.

Chooses an index with probability proportional to its element value. Requires min(self) >= 0 and sum(self) > 0.

Returns:A random index value.
Raises:RuntimeError – If min(self) < 0 || sum(self) <= 0.
range(start, length)

Returns the given range of elements as a new vector view.

Parameters:
  • start (int) – The start index.
  • length (int) – The length.
Returns:

A vector view representing the given range.

Return type:

SubVector

read_(is:istream, binary:bool, add:bool=default)

Reads the vector from the given C++ stream.

Parameters:
  • is (istream) – The input C++ stream.
  • binary (bool) – Whether the stream is binary.
  • add (bool) – Whether to add existing contents to the read vector. Defaults to False.
replace_value_(orig:float, changed:float)

Replaces the elements.

Performs the operation y[y == orig] = changed.

Parameters:
  • orig (float) – The original value to replace.
  • changed (float) – The value to replace with.
scale_(alpha:float)

Scales the vector.

Parameters:alpha (float) – The scalar multiplier.
set_(f:float)

Sets the elements to the given value.

Parameters:f (float) – The value to set.
set_rand_uniform_()

Sets the elements to numbers uniformly distributed on (0,1).

set_randn_()

Sets the elements to numbers from standard normal distribution.

set_zero_()

Sets the elements to zero.

shape

Single element tuple representing the size of the vector.

sigmoid_(src:VectorBase)

Sets the elements to the sigmoid of the elements in another vector.

Parameters:src (Vector) – The input vector.
Raises:RuntimeError – In case of size mismatch.
size()

Returns the size of the vector as a single element tuple.

size_in_bytes() → int

Returns the size (in bytes) of the data held by the vector.

solve_(M, trans)

Solves a linear system.

The linear system is defined as \(M\ x = b\), where \(b\) and \(x\) are the initial and final values of the vector, respectively.

Warning

Does not test for \(M\) being singular or near-singular.

Parameters:
Raises:

ValueError – In case of size mismatch.

sum() → float

Returns the sum of the elements.

sum_log() → float

Returns the sum of the logs of the elements.

Returns NaN if any of the elements is negative.

tanh_(src:VectorBase)

Sets the elements to the tanh of the elements in another vector.

Parameters:src (Vector) – The input vector.
Raises:RuntimeError – In case of size mismatch.
write(os:ostream, binary:bool)

Writes the vector to given C++ stream.

Parameters:
  • os (ostream) – The output C++ straem.
  • binary (bool) – Whether the stream is binary.
class kaldi.matrix.Vector(*args)[source]

Single precision vector.

Vector():
Creates an empty vector.
Vector(size: int):
Creates a new vector of given size and fills it with zeros.
Parameters:size (int) – Size of the new vector.
Vector(obj: vector_like):
Creates a new vector with the elements in obj.
Parameters:obj (vector_like) – A vector, a 1-D numpy array, any object exposing a 1-D array interface, an object with an __array__ method returning a 1-D numpy array, or any sequence that can be interpreted as a vector.
add_(value:float)

Adds the given value to each element.

Parameters:value (float) – The value to add.
add_col_sum_mat_(alpha, M, beta=1.0)

Adds the sum of matrix columns.

Performs the operation \(y = \alpha\ \sum_i M[:,i] + \beta\ y\).

Parameters:
  • alpha (float) – The scalar multiplier for the column sum.
  • M (Matrix) – The input matrix.
  • beta (float) – The scalar multiplier for the destination vector. Defaults to 1.0.
Raises:

ValueError – If self.dim != M.num_rows.

add_diag_mat2_(alpha, M, trans=<MatrixTransposeType.NO_TRANS: 111>, beta=1.0)

Adds the diagonal of a matrix multiplied with its transpose.

Performs the operation \(y = \alpha\ diag(M M^T) + \beta\ y\).

Parameters:
  • alpha (float) – The scalar multiplier for the diagonal.
  • M (Matrix) – The input matrix.
  • trans (MatrixTransposeType) – Whether to use M or its transpose. Defaults to MatrixTransposeType.NO_TRANS.
  • beta (float) – The scalar multiplier for the destination vector. Defaults to 1.0.
Raises:

ValueError – In case of size mismatch.

add_diag_mat_mat_(alpha, M, transM, N, transN, beta=1.0)

Adds the diagonal of a matrix-matrix product.

Performs the operation \(y = \alpha\ diag(M N) + \beta\ y\).

Parameters:
  • alpha (float) – The scalar multiplier for the diagonal.
  • M (Matrix) – The first input matrix.
  • transM (MatrixTransposeType) – Whether to use M or its transpose.
  • N (Matrix) – The second input matrix.
  • transN (MatrixTransposeType) – Whether to use N or its transpose.
  • beta (float) – The scalar multiplier for the destination vector. Defaults to 1.0.
Raises:

ValueError – In case of size mismatch.

add_mat_vec_(alpha, M, trans, v, beta, sparse=False)

Computes a matrix-vector product.

Performs the operation \(y = \alpha\ M\ v + \beta\ y\).

Parameters:
  • alpha (float) – The scalar multiplier for the matrix-vector product.
  • M (Matrix or SpMatrix or TpMatrix) – The input matrix.
  • trans (MatrixTransposeType) – Whether to use M or its transpose.
  • v (Vector) – The input vector.
  • beta (int) – The scalar multiplier for the destination vector.
  • sparse (bool) – Whether to use the algorithm that is faster when v is sparse. Defaults to False.
Raises:

ValueError – In case of size mismatch.

add_row_sum_mat_(alpha, M, beta=1.0)

Adds the sum of matrix rows.

Performs the operation \(y = \alpha\ \sum_i M[i] + \beta\ y\).

Parameters:
  • alpha (float) – The scalar multiplier for the row sum.
  • M (Matrix) – The input matrix.
  • beta (float) – The scalar multiplier for the destination vector. Defaults to 1.0.
Raises:

ValueError – If self.dim != M.num_cols.

add_vec2_(alpha, v)

Adds the squares of elements from another vector.

Performs the operation \(y = y + \alpha\ v\odot v\).

Parameters:
Raises:

RuntimeError – In case of size mismatch.

add_vec_(alpha, v)

Adds another vector.

Performs the operation \(y = y + \alpha\ v\).

Parameters:
Raises:

RuntimeError – In case of size mismatch.

add_vec_div_vec_(alpha:float, v:VectorBase, r:VectorBase, beta:float)

Computes an element-wise vector-vector division.

Performs the operation \(y = \alpha\ v\oslash r + \beta\ y\).

Parameters:
  • alpha (float) – The scalar multiplier for the division.
  • v (Vector) – The first input vector.
  • r (Vector) – The second input vector.
  • beta (float) – The scalar multiplier for the destination vector.
Raises:

RuntimeError – In case of size mismatch.

add_vec_vec_(alpha:float, v:VectorBase, r:VectorBase, beta:float)

Computes an element-wise vector-vector product.

Performs the operation \(y = \alpha\ v\odot r + \beta\ y\).

Parameters:
  • alpha (float) – The scalar multiplier for the product.
  • v (Vector) – The first input vector.
  • r (Vector) – The second input vector.
  • beta (float) – The scalar multiplier for the destination vector.
Raises:

RuntimeError – In case of size mismatch.

apply_abs_()

Applies the absolute value operation to each element.

apply_ceiling_(ceiling:float) → int

Applies the ceiling operation to each element.

Performs the operation y[i] = min(y[i], ceiling).

Parameters:ceiling (float) – The ceiling value.
Returns:The number of elements changed.
apply_ceiling_no_count_(ceiling:float)

Applies the ceiling operation to each element.

Performs the operation y[i] = min(y[i], ceiling).

Parameters:ceiling (float) – The ceiling value.
apply_exp_()

Applies the exponential operation to each element.

apply_floor_(floor:float) → int

Applies the floor operation to each element.

Performs the operation y[i] = max(y[i], floor).

Parameters:floor (float) – The floor value.
Returns:The number of elements changed.
apply_floor_no_count_(floor:float)

Applies the floor operation to each element.

Performs the operation y[i] = max(y[i], floor).

Parameters:floor (float) – The floor value.
apply_floor_vector_(floor:VectorBase) → int

Applies a separate floor operation to each element.

Performs the operation y[i] = max(y[i], floor[i]).

Parameters:floor (Vector) – The floor vector.
Returns:The number of elements changed.
apply_log_()

Applies the natural log operation to each element.

apply_log_and_copy_(v:VectorBase)

Applies the natural log operation to the given vector and copies it.

Parameters:v (Vector) – The input vector.
apply_log_softmax_() → float

Applies the logsoftmax operation to each element.

Performs the operation \(y_i = y_i - \log(\sum_j \exp(y_j))\).

Returns:\(\log(\sum_j \exp(y_j))\).
apply_pow_(power:float)

Takes each element to the given power.

Parameters:power (float) – The exponent value.
Raises:RuntimeError – If an element cannot be raised to the given power.
apply_pow_abs_(power:float, include_sign:bool=default)

Takes the absolute value of each element raised to the given power.

If the power is negative and the input is zero, the output is zero.

Parameters:
  • power (float) – The power value.
  • include_sign (bool) – Whether to include the sign of the input value. Defaults to False.
Raises:

RuntimeError – If an element cannot be raised to the given power.

apply_softmax_() → float

Applies the softmax operation to each element.

Performs the operation \(y_i = \frac{\exp(y_i)}{\sum_j \exp(y_j)}\).

Returns:\(\log(\sum_j \exp(y_j))\).
approx_equal(other, tol=0.01)

Checks if vectors are approximately equal.

Parameters:
  • other (Vector) – The vector to compare against.
  • tol (float) – The tolerance for the equality check. Defaults to 0.01.
Returns:

True if self.dim == other.dim and ||self-other|| <= tol*||self||. False otherwise.

clone()

Clones the vector.

The clone allocates new memory for its contents and supports vector operations that reallocate memory, i.e. it is not a view.

Returns:A copy of the vector.
Return type:Vector
copy_(src)

Copies the elements from another vector.

Parameters:src (Vector or DoubleVector) – The input vector.
Raises:ValueError – In case of size mismatch.
copy_col_from_mat_(M, col)

Copies the elements from a matrix column.

Parameters:
Raises:
copy_cols_from_mat_(M)

Copies the elements from a matrix column-by-columm.

Parameters:M (Matrix) – The input matrix.
Raises:ValueError – In case of size mismatch.
copy_diag_from_mat_(M)

Copies the digonal elements from a matrix.

Parameters:M (Matrix or SpMatrix or TpMatrix) – The input matrix.
Raises:ValueError – In case of size mismatch.
copy_from_packed_(M)

Copies the elements from a packed matrix.

Parameters:M (SpMatrix or TpMatrix or DoubleSpMatrix or DoubleTpMatrix) – The input packed matrix.
Raises:ValueError – If self.dim !=  M.num_rows * (M.num_rows + 1) / 2.
copy_row_from_mat_(M, row)

Copies the elements from a matrix row.

Parameters:
Raises:
copy_rows_from_mat_(M)

Copies the elements from a matrix row-by-row.

Parameters:M (Matrix or DoubleMatrix) – The input matrix.
Raises:ValueError – In case of size mismatch.
data

Vector data as a memoryview.

dim

Dimension (zero for empty vector).

div_elements_(v)

Divides the elements with the elements of another vector.

Performs the operation y[i] /= v[i].

Parameters:v (Vector or DoubleVector) – The input vector.
Raises:RuntimeError – In case of size mismatch.
from_size(size:int, resize_type:MatrixResizeType=default) → Vector

Creates a new vector of given size.

Parameters:
  • size (int) – The size of the new vector.
  • resize_type (MatrixResizeType) – How to initialize the elements. If MatrixResizeType.SET_ZERO or MatrixResizeType.COPY_DATA, they are set to zero. If MatrixResizeType.UNDEFINED, they are left uninitialized. Defaults to MatrixResizeType.SET_ZERO.
from_vector(v:VectorBase) → Vector

Creates a new vector from a given vector.

Parameters:v (Vector) – The input vector.
invert_elements_()

Inverts the elements.

Raises:RuntimeError – If any of the elements is zero.
is_zero(cutoff:float=default) → bool

Checks if the elements are all zeros.

Parameters:cutoff (float) – The cutoff value. Defaults to 1.0e-06.
Returns:True if max(abs(self)) <= cutoff.
log_sum_exp(prune:float=default) → float

Computes \(f(x)=\log(\sum_i \exp(x_i))\) without exp overflow.

If prune > 0.0, ignores terms less than max(x) - prune.

Parameters:prune (float) – The pruning beam. Defaults to -1.0.
Returns:\(\log(\sum_i \exp(x_i))\).
max() → float

Returns the maximum value in the vector.

max_index() -> (value:float, index:int)

Returns the maximum value in the vector, and the associated index.

min() → float

Returns the minimum value in the vector.

min_index() -> (value:float, index:int)

Returns the minimum value in the vector, and the associated index.

mul_elements_(v)

Multiplies the elements with the elements of another vector.

Performs the operation y[i] *= v[i].

Parameters:v (Vector or DoubleVector) – The input vector.
Raises:RuntimeError – In case of size mismatch.
mul_tp_(M, trans)

Multiplies the vector with a lower-triangular matrix.

Performs the operation \(y = M\ y\).

Parameters:
Raises:

ValueError – In case of size mismatch.

norm(p:float) → float

Computes the p-norm of the vector.

Parameters:p (float) – The p value. It must be non-negative.
Returns:The p-norm.
Raises:RuntimeError – If p < 0 or p-norm cannot be computed.
numpy()

Converts the vector to a 1-D NumPy array.

The NumPy array is a view into the vector, i.e. no data is copied.

Returns:A NumPy array sharing data with this vector.
Return type:numpy.ndarray
rand_categorical() → int

Chooses a random index into the vector.

Chooses an index with probability proportional to its element value. Requires min(self) >= 0 and sum(self) > 0.

Returns:A random index value.
Raises:RuntimeError – If min(self) < 0 || sum(self) <= 0.
range(start, length)

Returns the given range of elements as a new vector view.

Parameters:
  • start (int) – The start index.
  • length (int) – The length.
Returns:

A vector view representing the given range.

Return type:

SubVector

read_(is:istream, binary:bool, add:bool=default)

Reads the vector from the given C++ stream.

Resizes the vector to match the size of the matrix read from stream.

Parameters:
  • is (istream) – The input C++ stream.
  • binary (bool) – Whether the stream is binary.
  • add (bool) – Whether to add existing contents to the read vector. Defaults to False.
replace_value_(orig:float, changed:float)

Replaces the elements.

Performs the operation y[y == orig] = changed.

Parameters:
  • orig (float) – The original value to replace.
  • changed (float) – The value to replace with.
resize_(length:int, resize_type:MatrixResizeType=default)

Resizes the vector.

Parameters:
  • length (int) – The new length.
  • resize_type (MatrixResizeType) – How to initialize the elements. If MatrixResizeType.SET_ZERO they are set to zero. If MatrixResizeType.COPY_DATA, existing elements are retained, new ones are set to zero. If MatrixResizeType.UNDEFINED, they are left uninitialized. Defaults to MatrixResizeType.SET_ZERO.
scale_(alpha:float)

Scales the vector.

Parameters:alpha (float) – The scalar multiplier.
set_(f:float)

Sets the elements to the given value.

Parameters:f (float) – The value to set.
set_rand_uniform_()

Sets the elements to numbers uniformly distributed on (0,1).

set_randn_()

Sets the elements to numbers from standard normal distribution.

set_zero_()

Sets the elements to zero.

shape

Single element tuple representing the size of the vector.

sigmoid_(src:VectorBase)

Sets the elements to the sigmoid of the elements in another vector.

Parameters:src (Vector) – The input vector.
Raises:RuntimeError – In case of size mismatch.
size()

Returns the size of the vector as a single element tuple.

size_in_bytes() → int

Returns the size (in bytes) of the data held by the vector.

solve_(M, trans)

Solves a linear system.

The linear system is defined as \(M\ x = b\), where \(b\) and \(x\) are the initial and final values of the vector, respectively.

Warning

Does not test for \(M\) being singular or near-singular.

Parameters:
Raises:

ValueError – In case of size mismatch.

sum() → float

Returns the sum of the elements.

sum_log() → float

Returns the sum of the logs of the elements.

Returns NaN if any of the elements is negative.

swap_(other:Vector)

Swaps the contents of vectors.

Shallow swap.

Parameters:other (Vector) – The vector to swap contents with.
tanh_(src:VectorBase)

Sets the elements to the tanh of the elements in another vector.

Parameters:src (Vector) – The input vector.
Raises:RuntimeError – In case of size mismatch.
write(os:ostream, binary:bool)

Writes the vector to given C++ stream.

Parameters:
  • os (ostream) – The output C++ straem.
  • binary (bool) – Whether the stream is binary.
kaldi.matrix.set_printoptions(precision=None, threshold=None, edgeitems=None, linewidth=None, profile=None)[source]

Set options for printing. Items shamelessly taken from Numpy

Parameters:
  • precision – Number of digits of precision for floating point output (default 8).
  • threshold – Total number of array elements which trigger summarization rather than full repr (default 1000).
  • edgeitems – Number of array items in summary at beginning and end of each dimension (default 3).
  • linewidth – The number of characters per line for the purpose of inserting line breaks (default 80). Thresholded matricies will ignore this parameter.
  • profile – Sane defaults for pretty printing. Can override with any of the above options. (default, short, full)

kaldi.matrix.common

Classes

MatrixResizeType An enumeration.
MatrixStrideType An enumeration.
MatrixTransposeType An enumeration.
SpCopyType An enumeration.
class kaldi.matrix.common.MatrixResizeType

An enumeration.

COPY_DATA = 2
SET_ZERO = 0
UNDEFINED = 1
class kaldi.matrix.common.MatrixStrideType

An enumeration.

DEFAULT = 0
NUM_COLS = 1
class kaldi.matrix.common.MatrixTransposeType

An enumeration.

NO_TRANS = 111
TRANS = 112
class kaldi.matrix.common.SpCopyType

An enumeration.

TAKE_LOWER = 0
TAKE_MEAN = 2
TAKE_MEAN_AND_CHECK = 3
TAKE_UPPER = 1

kaldi.matrix.compressed

Classes

CompressedMatrix A lossily compressed Matrix class
CompressionMethod An enumeration.
class kaldi.matrix.compressed.CompressedMatrix

A lossily compressed Matrix class

clear()

Deletes the data of the matrix

copy_col_to_vec(col:int, v:VectorBase)

Uncompresess and copies column of the matrix into vector v.

Parameters:
  • col (int) – Indexes the row. Indexing starts from 0
  • v (VectorBase) – The vector that will store the row.
Raises:

Error in case of dimesnionality mismatch

copy_from_mat(mat:MatrixBase, method:CompressionMethod=default)

Copy data from another matrix to self.

Parameters:
  • mat (MatrixBase) – The matrix from which we will copy the data
  • method (CompresssionMethod) – Which compression method to use
copy_row_to_vec(row:int, v:VectorBase)

Uncompresess and copies row of the matrix into vector v.

Parameters:
  • row (int) – Indexes the row. Indexing starts from 0
  • v (VectorBase) – The vector that will store the row.
Raises:

Error in case of dimesnionality mismatch

copy_to_mat(mat:MatrixBase, method:MatrixTransposeType=default)
Perfomrs mat=self if MatrixTransposeType is kNoTrans,
or mat^T=self if MatrixTransposeType is kTrans.
Parameters:
  • mat (MatrixBase) – The destination matrix
  • method (MatrixTransposeType) – Determines transposition of mat. Default value is kNoTrans
Raises:

Error in case of dimension mismatch

copy_to_mat_offset(row_offset:int, col_offset:int, dest:MatrixBase)
Uncompresses and copies submatrix of compressed matrix
into matrix dest. Submatrix starts at row row_offset and column column_offset and its size is defined by size of provided matrix dest.
Parameters:
  • row_offset (int) – The row offset. Indexing starts from 0
  • col_offset (int) – The col offset. Indexing starts from 0
  • dest (MatrixBase) – The matrix that will store the submatrix of self
Raises:

Error in case of dimesnionality violations.

new(mat:MatrixBase, method:CompressionMethod=default) → CompressedMatrix

Copy the data from a MatrixBase and compress it

Parameters:
  • mat (MatrixBase) – A matrix
  • method (CompressionMethod) – The way we will compress the matrix. Default is kAutomaticMethod
new_with_other(mat:CompressedMatrix) → CompressedMatrix

Set this matrix (self) as the other (mat)

Parameters:mat (CompressedMatrix) – A compressed Matrix
new_with_range(mat:CompressedMatrix, row_offset:int, num_rows:int, col_offset:int, num_cols:int, allow_padding:bool=default) → CompressedMatrix

Create a compressed matrix from another compressed matrix. If allow padding is true you can create an “expanded” matrix by either having a negative row_offset or by setting num_rows>mat.num_rows.

Parameters:
  • mat (CompressedMatrix) – A compressed matrix
  • row_offset (int) – From which row of mat to start copying. Indexing starts at 0
  • num_rows (int) – How many rows of mat to copy
  • col_offset (int) – From which column of mat to start copying. Indexing starts at 0
  • num_cols (int) – How many columns of mat to copy
  • allow_padding – Allows you to violate ``row’’ dimentionality conditions
num_cols

Number of columns (zero for empty matrix).

num_rows

Number of rows (zero for empty matrix).

scale(alpha:float)

scales all elements of matrix by alpha.

swap(other:CompressedMatrix)

Swap elements with another compressed matrix. Shallow swap.

Parameters:other (CompressedMatrix) – A compressed matrix
Raises:Error in case od dimension mismatch.
class kaldi.matrix.compressed.CompressionMethod

An enumeration.

AUTO = 1
ONE_BYTE_AUTO = 5
ONE_BYTE_UNSIGNED_INTEGER = 6
SPEECH_FEATURE = 2
TWO_BYTE_AUTO = 3
TWO_BYTE_SIGNED_INTEGER = 4
TWO_BYTE_ZERO_ONE = 7

kaldi.matrix.functions

Functions

add_outer_product_plus_minus Calls C++ function
approx_equal Checks if given vectors (or matrices) are approximately equal.
assert_equal Asserts given vectors (or matrices) are approximately equal.
assert_same_dim_matrix Checks if mat1 and mat2 have the same dimensions.
complex_fft Calls C++ function
compute_dct_matrix Calls C++ function
compute_pca Calls C++ function
create_eigenvalue_matrix Creates the eigenvalue matrix.
filter_matrix_rows Filters matrix rows.
real_fft Calls C++ function
solve_double_quadratic_matrix_problem Maximizes the auxiliary function Q(M) = tr(M^T*G)-0.5*tr(P1*M*Q1 M^T)-0.5*tr(P2*M*Q2*M^T) Assumes Q1, Q2, P1, and P2 are positive semidefinite.
solve_quadratic_matrix_problem Maximizes the auxiliary function Q(x) = tr(M^T*P*Y)-0.5*tr(P*M*Q*M^T) Assumes Q and P positive semidefinite.
solve_quadratic_problem Maximizes the auxiliary function Q(x) = x.*g - 0.5 x^T*H*x using a numerically stable method.
sort_svd Sorts singular-value decomposition in-place.
trace_mat Returns the trace of \(A\).
trace_mat_mat Returns the trace of \(A\ B\).
trace_mat_mat_mat Returns the trace of \(A\ B\ C\).
trace_mat_mat_mat_mat Returns the trace of \(A\ B\ C\ D\).
vec_mat_vec Computes a vector-matrix-vector product.
vec_vec Returns the dot product of vectors.

Classes

SolverOptions This class describes the options for maximizing various quadratic objective functions.
class kaldi.matrix.functions.SolverOptions

This class describes the options for maximizing various quadratic objective functions.

K

C++ ::kaldi::BaseFloat SolverOptions.K

check()

Raises error if K<10 or eps>1.0e-10

diagonal_precondition

C++ bool SolverOptions.diagonal_precondition

eps

C++ ::kaldi::BaseFloat SolverOptions.eps

name

C++ ::std::string SolverOptions.name

optimize_delta

C++ bool SolverOptions.optimize_delta

print_debug_output

C++ bool SolverOptions.print_debug_output

solver_options_with_name(name:str) → SolverOptions
Initialize the class. Default values are:
K = 1.0e+4, eps = 1.0e-40 optimize_delta = true diagonal_precondition = false

print_debug_output = true

kaldi.matrix.functions.add_outer_product_plus_minus(alpha:float, a:VectorBase, b:VectorBase, plus:MatrixBase, minus:MatrixBase)

Calls C++ function void ::kaldi::AddOuterProductPlusMinus(float, ::kaldi::VectorBase<float>, ::kaldi::VectorBase<float>, ::kaldi::MatrixBase<float> *, ::kaldi::MatrixBase<float> *)

kaldi.matrix.functions.approx_equal(a, b, tol=0.01)[source]

Checks if given vectors (or matrices) are approximately equal.

Parameters:
Returns:

True if input objects have the same type and size, and \(\Vert a-b \Vert \leq \mathrm{tol} \times \Vert a \Vert\).

Raises:

TypeError – If the first object is not a vector or matrix instance.

kaldi.matrix.functions.assert_equal(a, b, tol=0.01)[source]

Asserts given vectors (or matrices) are approximately equal.

Parameters:
Raises:
  • TypeError – If the first object is not a vector or matrix instance.
  • AssertionError – If input objects do not have the same type or size, or
  • \(\Vert a-b \Vert > \mathrm{tol} \times \Vert a \Vert\).
kaldi.matrix.functions.assert_same_dim_matrix(mat1:MatrixBase, mat2:MatrixBase)

Checks if mat1 and mat2 have the same dimensions.

Raises:
  • Error if mat1.num_rows!=mat2.num_rows
  • or mat1.num_cols!=mat2.num_cols
kaldi.matrix.functions.complex_fft(v:VectorBase, forward:bool, tmp_work:Vector=default)

Calls C++ function void ::kaldi::ComplexFft(::kaldi::VectorBase<float> *, bool, ::kaldi::Vector<float> *)

kaldi.matrix.functions.compute_dct_matrix(M:Matrix)

Calls C++ function void ::kaldi::ComputeDctMatrix(::kaldi::Matrix<float> *)

kaldi.matrix.functions.compute_pca(X:MatrixBase, U:MatrixBase, A:MatrixBase, print_eigs:bool=default, exact:bool=default)

Calls C++ function void ::kaldi::ComputePca(::kaldi::MatrixBase<float>, ::kaldi::MatrixBase<float> *, ::kaldi::MatrixBase<float> *, bool, bool)

kaldi.matrix.functions.create_eigenvalue_matrix(real, imag, D=None)[source]

Creates the eigenvalue matrix.

Eigenvalue matrix \(D\) is part of the decomposition used in eig. \(D\) will be block-diagonal with blocks of size 1 (for real eigenvalues) or 2x2 for complex pairs. If a complex pair is \(\lambda +- i\mu\), \(D\) will have a corresponding 2x2 block \([\lambda, \mu; -\mu, \lambda]\). This function will throw if any complex eigenvalues are not in complex conjugate pairs (or the members of such pairs are not consecutively numbered). The D you supply must has correct dimensions.

Parameters:
  • real (Vector or DoubleVector) – The real part of the eigenvalues.
  • imag (Vector or DoubleVector) – The imaginary part of the eigenvalues.
  • D (Matrix or DoubleMatrix or None) – The output matrix. If provided, the eigenvalue matrix is written into this matrix. If None, the eigenvalue matrix is returned. Defaults to None.
Returns:

The eigenvalue matrix if D is None.

Return type:

Matrix or DoubleMatrix

Raises:
kaldi.matrix.functions.filter_matrix_rows(matrix, keep_rows)[source]

Filters matrix rows.

The output is a matrix containing only the rows r of in such that keep_rows[r] == True.

Parameters:
Returns:

A new matrix constructed with the rows to keep.

Raises:
  • RuntimeError – If matrix.num_rows != keep_rows.length.
  • TypeError – If input matrix type is not supported.
kaldi.matrix.functions.real_fft(v:VectorBase, forward:bool)

Calls C++ function void ::kaldi::RealFft(::kaldi::VectorBase<float> *, bool)

kaldi.matrix.functions.solve_double_quadratic_matrix_problem(G:MatrixBase, P1:SpMatrix, P2:SpMatrix, Q1:SpMatrix, Q2:SpMatrix, opts:SolverOptions, M:MatrixBase) → float
Maximizes the auxiliary function
Q(M) = tr(M^T*G)-0.5*tr(P1*M*Q1 M^T)-0.5*tr(P2*M*Q2*M^T) Assumes Q1, Q2, P1, and P2 are positive semidefinite.
Parameters:
  • Q (SpMatrix) – A positive semidefinite matrix
  • Y (MatrixBase) – A matrix
  • P (SpMatrix) – A positive semidefinite matrix
  • opts (SolverOptions) – Options for the solver
  • M (MatrixBase) – A matrix
Returns:

The objective-function change

Raises:

Error in case of dimension mismatch

kaldi.matrix.functions.solve_quadratic_matrix_problem(Q:SpMatrix, Y:MatrixBase, P:SpMatrix, opts:SolverOptions, M:MatrixBase) → float
Maximizes the auxiliary function
Q(x) = tr(M^T*P*Y)-0.5*tr(P*M*Q*M^T) Assumes Q and P positive semidefinite.
Parameters:
  • Q (SpMatrix) – A positive semidefinite matrix
  • Y (MatrixBase) – A matrix
  • P (SpMatrix) – A positive semidefinite matrix
  • opts (SolverOptions) – Options for the solver
  • M (MatrixBase) – A matrix
Returns:

The objective-function change

Raises:

Error in case of dimension mismatch

kaldi.matrix.functions.solve_quadratic_problem(H:SpMatrix, g:VectorBase, opts:SolverOptions, x:VectorBase) → float
Maximizes the auxiliary function
Q(x) = x.*g - 0.5 x^T*H*x using a numerically stable method. Assumes H positive semidefinite.
Parameters:
  • H (SpMatrix) – A positive semidefinite number
  • g (VectorBase) – A vector
  • opts (SolverOptions) – Options for the solver
  • x (VectorBase) – A vector
Returns:

The objective-function change

Raises:

Error in case of dimension mismatch

kaldi.matrix.functions.sort_svd(s, U, Vt=None, sort_on_absolute_value=True)[source]

Sorts singular-value decomposition in-place.

SVD is \(U\ diag(s)\ V^T\).

This function is as generic as possible, to be applicable to other types of problems. Requires s.dim == U.num_cols, and sorts from greatest to least absolute value, moving the columns of U, and the rows of Vt, if provided, around in the same way.

Note

The ``absolute value’’ part won’t matter if this is an actual SVD, since singular values are non-negative.

Parameters:
  • s (Vector) – The singular values.
  • U (Matrix) – The \(U\) part of SVD.
  • Vt (Matrix) – The \(V^T\) part of SVD. Defaults to None.
  • sort_on_absolute_value (bool) – How to sort s. If True, sort from greatest to least absolute value. Otherwise, sort from greatest to least value. Defaults to True.
Raises:
kaldi.matrix.functions.trace_mat(A)[source]

Returns the trace of \(A\).

Parameters:A (Matrix or DoubleMatrix) – The input matrix.
kaldi.matrix.functions.trace_mat_mat(A, B, transA=<MatrixTransposeType.NO_TRANS: 111>)[source]

Returns the trace of \(A\ B\).

Precision of input matrices should match.

Parameters:
kaldi.matrix.functions.trace_mat_mat_mat(A, B, C, transA=<MatrixTransposeType.NO_TRANS: 111>, transB=<MatrixTransposeType.NO_TRANS: 111>, transC=<MatrixTransposeType.NO_TRANS: 111>)[source]

Returns the trace of \(A\ B\ C\).

Precision of input matrices should match.

Parameters:
  • A (Matrix or DoubleMatrix) – The first input matrix.
  • B (Matrix or DoubleMatrix or SpMatrix or DoubleSpMatrix) – The second input matrix.
  • C (Matrix or DoubleMatrix) – The third input matrix.
  • transA (_matrix_common.MatrixTransposeType) – Whether to use A or its transpose. Defaults to MatrixTransposeType.NO_TRANS.
  • transB (_matrix_common.MatrixTransposeType) – Whether to use B or its transpose. Defaults to MatrixTransposeType.NO_TRANS.
  • transC (_matrix_common.MatrixTransposeType) – Whether to use C or its transpose. Defaults to MatrixTransposeType.NO_TRANS.
kaldi.matrix.functions.trace_mat_mat_mat_mat(A, B, C, D, transA=<MatrixTransposeType.NO_TRANS: 111>, transB=<MatrixTransposeType.NO_TRANS: 111>, transC=<MatrixTransposeType.NO_TRANS: 111>, transD=<MatrixTransposeType.NO_TRANS: 111>)[source]

Returns the trace of \(A\ B\ C\ D\).

Precision of input matrices should match.

Parameters:
  • A (Matrix or DoubleMatrix) – The first input matrix.
  • B (Matrix or DoubleMatrix or SpMatrix or DoubleSpMatrix) – The second input matrix.
  • C (Matrix or DoubleMatrix) – The third input matrix.
  • D (Matrix or DoubleMatrix or SpMatrix or DoubleSpMatrix) – The fourth input matrix.
  • transA (_matrix_common.MatrixTransposeType) – Whether to use A or its transpose. Defaults to MatrixTransposeType.NO_TRANS.
  • transB (_matrix_common.MatrixTransposeType) – Whether to use B or its transpose. Defaults to MatrixTransposeType.NO_TRANS.
  • transC (_matrix_common.MatrixTransposeType) – Whether to use C or its transpose. Defaults to MatrixTransposeType.NO_TRANS.
  • transD (_matrix_common.MatrixTransposeType) – Whether to use D or its transpose. Defaults to MatrixTransposeType.NO_TRANS.
kaldi.matrix.functions.vec_mat_vec(v1, M, v2)[source]

Computes a vector-matrix-vector product.

Performs the operation \(v_1\ M\ v_2\).

Precision of input matrices should match.

Parameters:
Returns:

The vector-matrix-vector product.

Raises:

RuntimeError – In case of size mismatch.

kaldi.matrix.functions.vec_vec(v1, v2)[source]

Returns the dot product of vectors.

Parameters:
Returns:

The dot product of v1 and v2.

Raises:

kaldi.matrix.htk

Functions

read_htk Reads features in HTK format.
write_htk Writes features in HTK format.
write_sphinx Writes features in Sphinx format.

Classes

HtkHeader HTK header for features.
class kaldi.matrix.htk.HtkHeader

HTK header for features.

mNSamples

C++ ::int32 HtkHeader.mNSamples

mSampleKind

C++ ::uint16 HtkHeader.mSampleKind

mSamplePeriod

C++ ::int32 HtkHeader.mSamplePeriod

mSampleSize

C++ ::int16 HtkHeader.mSampleSize

kaldi.matrix.htk.read_htk(is:istream, M:Matrix, header:HtkHeader) → bool

Reads features in HTK format.

kaldi.matrix.htk.write_htk(os:ostream, M:MatrixBase, header:HtkHeader) → bool

Writes features in HTK format.

kaldi.matrix.htk.write_sphinx(os:ostream, M:MatrixBase) → bool

Writes features in Sphinx format.

kaldi.matrix.optimization

Functions

linear_cgd This function uses linear conjugate gradient descent to approximately solve the system A*x=b.

Classes

LbfgsOptions This class holds the options for the L-BFGS algorithm.
LinearCgdOptions Options for Linear CGD optimization
OptimizeLbfgs This class handles the L-BFGS optimization
class kaldi.matrix.optimization.LbfgsOptions
This class holds the options for the L-BFGS algorithm.
I pushes responsibility for determining when to stop, onto the user. There is no callback here. The optimization happens through calls of OptimizeLbfgs class itself. This does not implement constrained L-BFGS, but it will handle constrained problems correctly as long as the function approaches +infinity (or -infinity for maximization problems) when it gets close to the bound of the constraint. In these types of problems, you just let the function value be +infinity for minimization problems, or -infinity for maximization problems, outside these bounds).
Parameters:
  • minimize (bool) – if true, we’re minimizing, else maximizing
  • m (int) – m is the number of stored vectors L-BFGS keeps
  • first_step_learning_rate (float) – The very first step of L-BFGS is like gradient descent. If you want to configure the size of that step, you can do it using this variable
  • first_step_length (float) – If this variable is >0.0, it overrides first_step_learning_rate; on the first step we choose an approximate Hessian that is the multiple of the identity that would generate this step-length, or 1.0 if the gradient is zero
  • first_step_impr (float) – If this variable is >0.0, it overrides first_step_learning_rate; on the first step we choose an approximate Hessian that is the multiple of the identity that would generate this amount of objective function improvement (assuming the real objf was linear)
  • c1 (float) – A constant in Armijo rule = Wolfe condition i)
  • c2 (float) – A constant in Wolfe condition ii)
  • d (float) – An amount > 1.0 (default 2.0) that we initially multiply or divide the step length by, in the line search
  • max_line_search_iters (int) – after this many iters we restart L-BFGS
  • avg_step_length (int) –
avg_step_length

C++ int LbfgsOptions.avg_step_length

c1

C++ float LbfgsOptions.c1

c2

C++ float LbfgsOptions.c2

d

C++ float LbfgsOptions.d

first_step_impr

C++ float LbfgsOptions.first_step_impr

first_step_learning_rate

C++ float LbfgsOptions.first_step_learning_rate

first_step_length

C++ float LbfgsOptions.first_step_length

m

C++ int LbfgsOptions.m

max_line_search_iters

C++ int LbfgsOptions.max_line_search_iters

minimize

C++ bool LbfgsOptions.minimize

class kaldi.matrix.optimization.LinearCgdOptions

Options for Linear CGD optimization

Parameters:
  • max_iters (int) – The maximum number of iterations
  • max_error (float) – Maximum 2 norm of the residual A*x-b
  • recompute_residual_factor (float) – Every time the residual 2-norm decreases
  • this recompute_residual_factor since the last time it was computed from (by) –
  • recompute it from. This helps to keep the computed residual (scratch,) –
  • even in the presence of roundof (accurate) –
max_error

C++ ::kaldi::BaseFloat LinearCgdOptions.max_error

max_iters

C++ ::int32 LinearCgdOptions.max_iters

recompute_residual_factor

C++ ::kaldi::BaseFloat LinearCgdOptions.recompute_residual_factor

class kaldi.matrix.optimization.OptimizeLbfgs

This class handles the L-BFGS optimization

do_step(function_value:float, gradient:VectorBase)
The user calls this function to provide the class with the
function and gradient info at the point GetProposedValue(). If this point is outside the constraints you can set function_value to {+infinity,-infinity} for {minimization,maximization} problems. In this case the gradient, and also the second derivative (if you call the second overloaded version of this function) will be ignored.
Parameters:
  • function_value (float) – The current function value
  • gradient (VectorBase) – This will store the gradient
do_step_approx_hessian(function_value:float, gradient:VectorBase, diag_approx_2nd_deriv:VectorBase)
The user can call this version of DoStep() if it is desired
to set some kind of approximate Hessian on this iteration.
Parameters:
  • function_value (float) – The current function value
  • gradient (VectorBase) – This will store the gradient
  • diag_approx_2nd_deriv (VectorBase) – A vector whose values represent the diagonal of the Hessian
Raises:
  • Error if diag_approx_2nd_deriv is not strictly positive when minimizing
  • or strictly negative when maximizing.
recent_step_length() → float

Returns the average magnitude of the last n steps (but not more than the number we have stored). Before we have taken any steps, returns +infinity. Note: if the most recent step length was 0, it returns 0, regardless of the other step lengths. This makes it suitable as a convergence test (else we’d generate NaN’s).

kaldi.matrix.optimization.linear_cgd(opts:LinearCgdOptions, A:SpMatrix, b:VectorBase, x:VectorBase) → int

This function uses linear conjugate gradient descent to approximately solve the system A*x=b. The value of x at entry corresponds to the initial guess of x. The algorithm continues until the number of iterations equals b.size, or until ||A*x - b||_2<= max_error, or until the number of iterations equals max_iter, whichever happens sooner. It is a requirement that A be positive definite. It returns the number of iterations that were actually executed.

Parameters:
  • opts (LinearCgdOptions) –
  • A (Sp.Matrix) – A symmetric matrix. It must be positive definite
  • b (VectorBase) – A vector
Returns:

The number of iterations

kaldi.matrix.packed

Classes

DoubleSpMatrix Double precision symmetric matrix.
DoubleTpMatrix Double precision triangular matrix.
SpMatrix Single precision symmetric matrix.
TpMatrix Single precision triangular matrix.
class kaldi.matrix.packed.DoubleSpMatrix(num_rows=None, resize_type=<MatrixResizeType.SET_ZERO: 0>)[source]

Double precision symmetric matrix.

Creates a new symmetric matrix.

If num_rows is not None, initializes the symmetric matrix to the specified size. Otherwise, initializes an empty symmetric matrix.

Parameters:
  • num_rows (int) – Number of rows. Defaults to None.
  • resize_type (MatrixResizeType) – How to initialize the elements. If MatrixResizeType.SET_ZERO or MatrixResizeType.COPY_DATA, they are set to zero. If MatrixResizeType.UNDEFINED, they are left uninitialized. Defaults to MatrixResizeType.SET_ZERO.
add_diag_vec_(alpha:float, v:DoubleVectorBase)

Performs self = self+diag(v)

Parameters:
  • alpha (float) – A scaling constant
  • v (DoubleVectorBase) – A vector
Raises:

Error in case of dimension mismatch

add_mat2_(alpha:float, M:DoubleMatrixBase, transM:MatrixTransposeType, beta:float)
If transM == kNoTrans performs self = beta*self+alpha*M*M^T.
If transM == kTrans self = beta*self+alpha*M^T*M
Parameters:
  • alpha (float) – A constant
  • M (DoubleMatrixBase) – A matrix
  • transM (MatrixTransposeType) – Determines if M will be transposed
  • beta (float) – A constant
Raises:

Error in case of dimension mismatch

add_mat_2_sp_(alpha:float, M:DoubleMatrixBase, transM:MatrixTransposeType, A:DoubleSpMatrix, beta:float=default)
If transM == kNoTrans perform self = beta*self+alpha*M*A*M^T.
If transM == kTrans perform self = beta*self+alpha*M^T*A*M.
Parameters:
Raises:

Error in case of dimension mismatch

add_mat_2_vec_(alpha:float, M:DoubleMatrixBase, transM:MatrixTransposeType, v:DoubleVectorBase, beta:float=default)
If transM == kNoTrans perform self = beta*self+alpha*M*diag(v)*M^T.
If transM == kTrans perform self = beta*self+alpha*M^T*diag(v)*M.
Parameters:
  • alpha (float) – A constant
  • M (DoubleMatrixBase) – A matrix
  • transM (MatrixTransposeType) – Determines if M will be transposed
  • v (DoubleVectorBase) – A vector
  • beta (float) – A constant
Raises:

Error in case of dimension mismatch

add_smat_2_sp_(alpha:float, M:DoubleMatrixBase, transM:MatrixTransposeType, A:DoubleSpMatrix, beta:float=default)

Same as AddMat2Sp, optimized for sparse M.

add_sp_(alpha:float, Ma:DoubleSpMatrix)

Performs the operation self = self+alpha*Ma.

Parameters:
add_to_diag_(r:float)

Adds a constant r to the diagonal of the matrix.

Parameters:r (float) – A constant
add_vec2_(alpha:float, v:DoubleVectorBase)

Performs the operation self = self + alpha*(v*v^T)

Parameters:
  • alpha (float) – A scaling constant
  • v (DoubleVectorBase) – A vector
Raises:

Error in case of dimension mismatch

add_vec_2_sp_(alpha:float, v:DoubleVectorBase, S:DoubleSpMatrix, beta:float)

Perform self = beta*self + alpha*diag(v)*S*diag(v)

Parameters:
  • alpha (float) – A scaling constant
  • v (DoubleVectorBase) – A vector
  • S (DoubleSpMatrix) – A symmetric matrix
  • beta (float) – A scaling constant
Raises:

Error in case of dimension mismatch

add_vec_vec_(alpha:float, v:DoubleVectorBase, w:DoubleVectorBase)

Performs the operation self = self + alpha*(v*w^T+w*v^T)

Parameters:
  • alpha (float) – A scaling constant
  • v (DoubleVectorBase) – A vector
  • w (DoubleVectorBase) – A vector
Raises:

Error in case of dimension mismatch

apply_floor_(floor:float) → int

Applies floor to all elements. If self(i,j)<floor then self(i,j)=floor.

Parameters:floor (float) – The floor value to check against
Returns:The number of elements affected by the flooring operation
apply_floor_matrix_(floor:DoubleSpMatrix, alpha:float=default, verbose:bool=default) → int
Floors this symmetric matrix to the matrix alpha*floor,
where the matrix floor is positive definite. It is floored in the sense that after flooring it must be that x^T*self*x >= x^T*(alpha*floor)*x. floor must be positive definite.
Parameters:
  • floor (DoubleSpMatrix) – A positive definite matrix
  • alpha (float) – a contant
  • verbose (bool) – Default is False. If true, extra messages can be printed
Returns:

the number of elements that were floored.

Raises:

Error if floor is not positive definite.

apply_pow_(exponent:float)

Applies power to all elements of matrix.

Parameters:expoenent (float) – The exponent.
approx_equal(other:DoubleSpMatrix, tol:float=default) → bool
Checks if self is equal with other by calculating the Frobenius
norm of their difference.
Parameters:
Returns:

True if ||(self-other)||_F <= tol * ||self||_F

clone()[source]

Clones the symmetric matrix.

Returns:A copy of the symmetric matrix.
Return type:DoubleSpMatrix
cond() → float

Returns the condition number of the SVD computation.

copy_from_mat_(orig:DoubleMatrixBase, copy_type:SpCopyType=default)

Copies the elements from a square matrix.

Parameters:
  • orig (DoubleMatrixBase) – The input matrix.
  • copy_type (SpCopyType) – Determines how elements will be copied. If TAKE_LOWER, takes lower triangular elements in orig. If TAKE_UPPER, takes upper triangular elements in orig. If TAKE_MEAN, takes the average of symmetric elements in orig. If TAKE_MEAN_AND_CHECK, does an extra symmetry check before taking average of symmetric elements in orig.
Raises:

RuntimeError – If orig is not symmetric.

copy_from_packed_(orig:DoublePackedMatrix)

Copies the elements from a packed matrix.

Parameters:orig (DoublePackedMatrix) – The packed matrix to copy from.
Raises:RuntimeError – In case of dimension mismatch.
copy_from_sp_(other:DoubleSpMatrix)

Copies the elements from another symmetric matrix.

Parameters:other (DoubleSpMatrix) – The input symmetric matrix.
Raises:RuntimeError – In case of dimension mismatch.
copy_from_vec_(orig:DoubleSubVector)

Copies the elements from a vector.

Interprets the vector as having the same layout as the packed matrix. Input vector and the packed matrix must have the same size, i.e. orig.size == (self.num_rows * self.num_cols+1) / 2.

Parameters:orig (DoubleSubVector) – The vector to copy from.
frobenius_norm() → float

Returns the Frobenius norm of the matrix.

from_matrix(orig:DoubleMatrixBase, copy_type:SpCopyType=default) → DoubleSpMatrix

Creates a new symmetric matrix from the given square matrix.

Parameters:
  • orig (DoubleMatrixBase) – The input square matrix.
  • copy_type (SpCopyType) – Determines how elements will be copied. If TAKE_LOWER, takes lower triangular elements in orig. If TAKE_UPPER, takes upper triangular elements in orig. If TAKE_MEAN, takes the average of symmetric elements in orig. If TAKE_MEAN_AND_CHECK, does an extra symmetry check before taking average of symmetric elements in orig.
Raises:

RuntimeError – If orig is not a square matrix with matching size.

from_other(orig:DoubleSpMatrix) → DoubleSpMatrix

Creates a new symmetric matrix from the given symmetric matrix.

Parameters:orig (DoubleSpMatrix) – The input symmetric matrix.
from_size(r:int, resize_type:MatrixResizeType=default) → DoubleSpMatrix

Creates a new symmetric matrix of given size.

Parameters:
  • r (int) – The number or rows.
  • resize_type (MatrixResizeType) – Determines how the elements are initialized. If SET_ZERO (or COPY_DATA), they are set to zero. If UNDEFINED, they are left uninitialized. Defaults to SET_ZERO.
getitem(r:int, c:int) → float

Gets the element at the given index.

Parameters:
  • r (int) – The row index of the element to get.
  • c (int) – The column index of the element to get.
Returns:

The element at the given index.

Raises:

RuntimeError – If r > num_rows or c > num_cols.

invert_()

Inverts the matrix.

invert_double_()

Inverts the matrix in double precision.

is_diagonal(cutoff:float=default) → bool
Checks if the matrix is diagonal. The nondiagonal elements
must be less or equal to cutoff value.
Parameters:cutoff (float) – A cutoff value. Default is 1.0e-05
Returns:True if sum(digonal_elements)*cutoff > sum(nondiagonal_elements), otherwise false
is_pos_def() → bool

Returns True if the matrix is positive definite.

is_triagonal(cutoff:float=default) → bool

Returns true if the matrix is tridiagonal(based on the cutoff value).

is_unit(cutoff:float=default) → bool
Checks if the matrix is identity-like. It does not have to be square.
The nondiagonal elements must be less or equal to cutoff value, while the diagonal ones must be one.
Parameters:cutoff (float) – A cutoff value. Default is 1.0e-05
Returns:True if the diagonal elements are 1 and sum(nondiagonal_elements)<=cutoff
is_zero(cutoff:float=default) → bool

Checks if all elements are 0 (where 0 is the cutoff value).

Parameters:cutoff (float) – A cutoff value. Default is 1.0e-05
Returns:True if it is a zero matrix
limit_cond(maxCond:float=default, invert:bool=default) → int
Performs self = P*diag(floor(s))*P^T, where s is the eigenvalues
and P is the eigenvectors.
Parameters:MaxCond (float) – Determines the flooring threshold (i.e. floor=max_eigen/MaxCond)
Raises:Error if matrix is not positive definite
limit_cond_double(maxCond:float=default, invert:bool=default) → int

Same as LimitCond but the resulting operation has ``double’’ precision.

log_pos_def_det() → float

Computes the log determinant.

Returns:The log determinant
Raises:Error if matrix is not positive definite
max() → float

Returns the maximum value in the matrix.

max_abs_eig() → float

Returns the maximum of the absolute values of the eigenvalues.

min() → float

Returns the minimum value in the matrix.

num_cols

The number of columns.

num_rows

The number of rows.

qr_(Q:DoubleMatrixBase)
The symmetric QR algorithm. This is the symmetric QR algorithm,
from Golub and Van Loan 3rd ed., Algorithm 8.3.3.
Parameters:Q (MatrixBase) – A matrix
read_(is:istream, binary:bool, add:bool=default)

Reads the matrix from the given C++ stream.

Parameters:
  • is (istream) – The input C++ stream.
  • binary (bool) – Whether the stream is binary.
  • add (bool) – Whether to add existing contents to the read matrix. Defaults to False.
resize_(num_rows:int, resize_type:MatrixResizeType=default)

Resizes the matrix.

Parameters:
  • num_rows (int) – The new number of rows.
  • resize_type (MatrixResizeType) – Determines how the elements are initialized. If SET_ZERO, they are set to zero. If UNDEFINED, they are left uninitialized. If COPY_DATA, existing elements are retained, new ones are set to zero. Defaults to SET_ZERO.
scale_(c:float)

Scales each element with the given scalar.

Parameters:c (float) – The scaling coefficient.
scale_diag_(alpha:float)

Scales the diagonal of the matrix by alpha.

Parameters:alpha (float) – A scaling constant
set_diag_(alpha:float)

Sets all the diagonal elements to alpha.

Parameters:alpha (float) – A constant
set_randn_()

Sets the elements to numbers from standard normal distribution.

set_unit_()

Sets diagonal elements to one, off-diagonal elements to zero.

set_zero_()

Sets the elements to zero.

size()

Returns size as a tuple.

Returns:A tuple (num_rows, num_cols) of integers.
size_in_bytes() → int

Returns the size (in bytes) of the data held by the matrix.

swap_(other)

Swaps the contents with another matrix.

Shallow swap.

Parameters:other (DoubleMatrix or DoubleSpMatrix or DoubleTpMatrix) – The input matrix.
Raises:ValueError – If other is not a square matrix.
swap_with_matrix_(other:DoubleMatrix)

Swaps the contents with a matrix.

Shallow swap.

Parameters:other (Matrix) – The matrix to swap contents with.
swap_with_packed_(other:DoublePackedMatrix)

Swaps the contents with another packed matrix.

Shallow swap.

Parameters:other (PackedMatrix) – The matrix to swap contents with.
swap_with_symmetric_(other:DoubleSpMatrix)

Swaps the contents with another symmetric matrix.

Shallow swap.

Parameters:other (DoubleSpMatrix) – The matrix to swap contents with.
trace() → float

Returns the trace of the matrix.

tridiagonalize_(Q:DoubleMatrixBase)
Tridiagonalize the matrix with an orthogonal transformation.
Performs self = Q*self*Q^T for an orthogonal Q.
Parameters:Q (DoubleMatrixBase) – A matrix that must be orthogonal.
write(os:ostream, binary:bool)

Writes the matrix to given C++ stream.

Parameters:
  • os (ostream) – The output C++ straem.
  • binary (bool) – Whether the stream is binary.
class kaldi.matrix.packed.DoubleTpMatrix(num_rows=None, resize_type=<MatrixResizeType.SET_ZERO: 0>)[source]

Double precision triangular matrix.

Initializes a new triangular matrix.

If num_rows is not None, initializes the triangular matrix to the specified size. Otherwise, initializes an empty triangular matrix.

Parameters:
  • num_rows (int) – Number of rows. Defaults to None.
  • resize_type (MatrixResizeType) – How to initialize the elements. If MatrixResizeType.SET_ZERO or MatrixResizeType.COPY_DATA, they are set to zero. If MatrixResizeType.UNDEFINED, they are left uninitialized. Defaults to MatrixResizeType.SET_ZERO.
add_to_diag_(r:float)

Adds a constant r to the diagonal of the matrix.

Parameters:r (float) – A constant
add_tp(alpha:float, M:DoubleTpMatrix)

Performs self = self+alpha*M.

Parameters:
Raises:

RuntimeError – In case of dimension mismatch.

cholesky_(orig:DoubleSpMatrix)

Performs Cholesky decomposition.

Parameters:orig (DoubleSpMatrix) – The input symmetric matrix.
clone()[source]

Clones the triangular matrix.

Returns:A copy of the triangular matrix.
Return type:DoubleTpMatrix
copy_from_mat_(M:DoubleMatrixBase, trans:MatrixTransposeType=default)

Copies the elements from a square matrix.

Parameters:
  • M (DoubleMatrixBase) – The input matrix.
  • trans (MatrixTransposeType) – Determines which elements are copied. If NO_TRANS, copies the lower triangular elements. If TRANS, copies the upper triangular elements. Defaults to NO_TRANS
Raises:

RuntimeError – If M is not square or if dimensions of self and M are different.

copy_from_packed_(orig:DoublePackedMatrix)

Copies the elements from a packed matrix.

Parameters:orig (DoublePackedMatrix) – The packed matrix to copy from.
Raises:RuntimeError – In case of dimension mismatch.
copy_from_tp_(other:DoubleTpMatrix)

Copies the elements from another triangular matrix.

Parameters:other (DoubleTpMatrix) – The input triangular matrix.
Raises:RuntimeError – In case of dimension mismatch.
copy_from_vec_(orig:DoubleSubVector)

Copies the elements from a vector.

Interprets the vector as having the same layout as the packed matrix. Input vector and the packed matrix must have the same size, i.e. orig.size == (self.num_rows * self.num_cols+1) / 2.

Parameters:orig (DoubleSubVector) – The vector to copy from.
determinant() → float

Returns the determinant of the matrix.

from_other(orig:DoubleTpMatrix) → DoubleTpMatrix

Creates a new triangular matrix from the given triangular matrix.

Parameters:orig (DoubleTpMatrix) – The input triangular matrix.
from_size(r:int, resize_type:MatrixResizeType=default) → DoubleTpMatrix

Creates a new triangular matrix of given size.

Parameters:
  • r (int) – The number or rows.
  • resize_type (MatrixResizeType) – Determines how the elements are initialized. If SET_ZERO (or COPY_DATA), they are set to zero. If UNDEFINED, they are left uninitialized. Defaults to SET_ZERO.
getitem(r:int, c:int) → float

Gets the element at the given index.

Parameters:
  • r (int) – The row index of the element to get.
  • c (int) – The column index of the element to get.
Returns:

The element at the given index.

Raises:

RuntimeError – If r > num_rows or c > num_cols.

invert_()

Inverts the matrix

invert_double_()

Inverts the matrix in double precision.

max() → float

Returns the maximum value in the matrix.

min() → float

Returns the minimum value in the matrix.

num_cols

The number of columns.

num_rows

The number of rows.

read_(is:istream, binary:bool, add:bool=default)

Reads the matrix from the given C++ stream.

Parameters:
  • is (istream) – The input C++ stream.
  • binary (bool) – Whether the stream is binary.
  • add (bool) – Whether to add existing contents to the read matrix. Defaults to False.
resize_(num_rows:int, resize_type:MatrixResizeType=default)

Resizes the matrix.

Parameters:
  • num_rows (int) – The new number of rows.
  • resize_type (MatrixResizeType) – Determines how the elements are initialized. If SET_ZERO, they are set to zero. If UNDEFINED, they are left uninitialized. If COPY_DATA, existing elements are retained, new ones are set to zero. Defaults to SET_ZERO.
scale_(c:float)

Scales each element with the given scalar.

Parameters:c (float) – The scaling coefficient.
scale_diag_(alpha:float)

Scales the diagonal of the matrix by alpha.

Parameters:alpha (float) – A scaling constant
set_diag_(alpha:float)

Sets all the diagonal elements to alpha.

Parameters:alpha (float) – A constant
set_randn_()

Sets the elements to numbers from standard normal distribution.

set_unit_()

Sets diagonal elements to one, off-diagonal elements to zero.

set_zero_()

Sets the elements to zero.

size()

Returns size as a tuple.

Returns:A tuple (num_rows, num_cols) of integers.
size_in_bytes() → int

Returns the size (in bytes) of the data held by the matrix.

swap_(other)

Swaps the contents with another matrix.

Shallow swap.

Parameters:other (DoubleMatrix or DoubleSpMatrix or DoubleTpMatrix) – The input matrix.
Raises:ValueError – If other is not a square matrix.
swap_with_matrix_(other:DoubleMatrix)

Swaps the contents with a matrix.

Shallow swap.

Parameters:other (Matrix) – The matrix to swap contents with.
swap_with_packed_(other:DoublePackedMatrix)

Swaps the contents with another packed matrix.

Shallow swap.

Parameters:other (PackedMatrix) – The matrix to swap contents with.
swap_with_triangular_(other:DoubleTpMatrix)

Swaps the contents with another triangular matrix.

Shallow swap.

Parameters:other (DoubleTpMatrix) – The matrix to swap contents with.
trace() → float

Returns the trace of the matrix.

write(os:ostream, binary:bool)

Writes the matrix to given C++ stream.

Parameters:
  • os (ostream) – The output C++ straem.
  • binary (bool) – Whether the stream is binary.
class kaldi.matrix.packed.SpMatrix(num_rows=None, resize_type=<MatrixResizeType.SET_ZERO: 0>)[source]

Single precision symmetric matrix.

Creates a new symmetric matrix.

If num_rows is not None, initializes the symmetric matrix to the specified size. Otherwise, initializes an empty symmetric matrix.

Parameters:
  • num_rows (int) – The number of rows. Defaults to None.
  • resize_type (MatrixResizeType) – How to initialize the elements. If MatrixResizeType.SET_ZERO or MatrixResizeType.COPY_DATA, they are set to zero. If MatrixResizeType.UNDEFINED, they are left uninitialized. Defaults to MatrixResizeType.SET_ZERO.
add_diag_vec_(alpha:float, v:VectorBase)

Performs self = self+diag(v)

Parameters:
  • alpha (float) – A scaling constant
  • v (VectorBase) – A vector
Raises:

Error in case of dimension mismatch

add_mat2_(alpha:float, M:MatrixBase, transM:MatrixTransposeType, beta:float)
If transM == kNoTrans performs self = beta*self+alpha*M*M^T.
If transM == kTrans self = beta*self+alpha*M^T*M
Parameters:
  • alpha (float) – A constant
  • M (MatrixBase) – A matrix
  • transM (MatrixTransposeType) – Determines if M will be transposed
  • beta (float) – A constant
Raises:

Error in case of dimension mismatch

add_mat_2_sp_(alpha:float, M:MatrixBase, transM:MatrixTransposeType, A:SpMatrix, beta:float=default)
If transM == kNoTrans perform self = beta*self+alpha*M*A*M^T.
If transM == kTrans perform self = beta*self+alpha*M^T*A*M.
Parameters:
  • alpha (float) – A constant
  • M (MatrixBase) – A matrix
  • transM (MatrixTransposeType) – Determines if M will be transposed
  • A (SpMatrix) – A symmetric matrix
  • beta (float) – A constant
Raises:

Error in case of dimension mismatch

add_mat_2_vec_(alpha:float, M:MatrixBase, transM:MatrixTransposeType, v:VectorBase, beta:float=default)
If transM == kNoTrans perform self = beta*self+alpha*M*diag(v)*M^T.
If transM == kTrans perform self = beta*self+alpha*M^T*diag(v)*M.
Parameters:
  • alpha (float) – A constant
  • M (MatrixBase) – A matrix
  • transM (MatrixTransposeType) – Determines if M will be transposed
  • v (VectorBase) – A vector
  • beta (float) – A constant
Raises:

Error in case of dimension mismatch

add_smat_2_sp_(alpha:float, M:MatrixBase, transM:MatrixTransposeType, A:SpMatrix, beta:float=default)

Same as AddMat2Sp, optimized for sparse M.

add_sp_(alpha:float, Ma:SpMatrix)

Performs the operation self = self+alpha*Ma.

Parameters:
  • alpha (float) – A scaling constant
  • Ma (SpMatrix) – A symmetrix Matrix
add_to_diag_(r:float)

Adds a constant r to the diagonal of the matrix.

Parameters:r (float) – A constant
add_vec2_(alpha:float, v:VectorBase)

Performs the operation self = self + alpha*(v*v^T)

Parameters:
  • alpha (float) – A scaling constant
  • v (VectorBase) – A vector
Raises:

Error in case of dimension mismatch

add_vec_2_sp_(alpha:float, v:VectorBase, S:SpMatrix, beta:float)

Perform self = beta*self + alpha*diag(v)*S*diag(v)

Parameters:
  • alpha (float) – A scaling constant
  • v (VectorBase) – A vector
  • S (SpMatrix) – A symmetric matrix
  • beta (float) – A scaling constant
Raises:

Error in case of dimension mismatch

add_vec_vec_(alpha:float, v:VectorBase, w:VectorBase)

Performs the operation self = self + alpha*(v*w^T+w*v^T)

Parameters:
  • alpha (float) – A scaling constant
  • v (VectorBase) – A vector
  • w (VectorBase) – A vector
Raises:

Error in case of dimension mismatch

apply_floor_(floor:float) → int

Applies floor to all elements. If self(i,j)<floor then self(i,j)=floor.

Parameters:floor (float) – The floor value to check against
Returns:The number of elements affected by the flooring operation
apply_floor_matrix_(floor:SpMatrix, alpha:float=default, verbose:bool=default) → int
Floors this symmetric matrix to the matrix alpha*floor,
where the matrix floor is positive definite. It is floored in the sense that after flooring it must be that x^T*self*x >= x^T*(alpha*floor)*x. floor must be positive definite.
Parameters:
  • floor (SpMatrix) – A positive definite matrix
  • alpha (float) – a contant
  • verbose (bool) – Default is False. If true, extra messages can be printed
Returns:

the number of elements that were floored.

Raises:

Error if floor is not positive definite.

apply_pow_(exponent:float)

Applies power to all elements of matrix.

Parameters:expoenent (float) – The exponent.
approx_equal(other:SpMatrix, tol:float=default) → bool
Checks if self is equal with other by calculating the Frobenius
norm of their difference.
Parameters:
  • other (SpMatrix) – The matrix to check against
  • tol (float) – the tolerance
Returns:

True if ||(self-other)||_F <= tol * ||self||_F

clone()[source]

Clones the symmetric matrix.

Returns:A copy of the symmetric matrix.
Return type:SpMatrix
cond() → float

Returns the condition number of the SVD computation.

copy_from_mat_(orig:MatrixBase, copy_type:SpCopyType=default)

Copies the elements from a square matrix.

Parameters:
  • orig (MatrixBase) – The input matrix.
  • copy_type (SpCopyType) – Determines how elements will be copied. If TAKE_LOWER, takes lower triangular elements in orig. If TAKE_UPPER, takes upper triangular elements in orig. If TAKE_MEAN, takes the average of symmetric elements in orig. If TAKE_MEAN_AND_CHECK, does an extra symmetry check before taking average of symmetric elements in orig.
Raises:

RuntimeError – If orig is not symmetric.

copy_from_packed_(orig:PackedMatrix)

Copies the elements from a packed matrix.

Parameters:orig (PackedMatrix) – The packed matrix to copy from.
Raises:RuntimeError – In case of dimension mismatch.
copy_from_sp_(other:SpMatrix)

Copies the elements from another symmetric matrix.

Parameters:other (SpMatrix) – The input symmetric matrix.
Raises:RuntimeError – In case of dimension mismatch.
copy_from_vec_(orig:SubVector)

Copies the elements from a vector.

Interprets the vector as having the same layout as the packed matrix. Input vector and the packed matrix must have the same size, i.e. orig.size == (self.num_rows * self.num_cols+1) / 2.

Parameters:orig (SubVector) – The vector to copy from.
frobenius_norm() → float

Returns the Frobenius norm of the matrix.

from_matrix(orig:MatrixBase, copy_type:SpCopyType=default) → SpMatrix

Creates a new symmetric matrix from the given square matrix.

Parameters:
  • orig (MatrixBase) – The input square matrix.
  • copy_type (SpCopyType) – Determines how elements will be copied. If TAKE_LOWER, takes lower triangular elements in orig. If TAKE_UPPER, takes upper triangular elements in orig. If TAKE_MEAN, takes the average of symmetric elements in orig. If TAKE_MEAN_AND_CHECK, does an extra symmetry check before taking average of symmetric elements in orig.
Raises:

RuntimeError – If orig is not a square matrix with matching size.

from_other(orig:SpMatrix) → SpMatrix

Creates a new symmetric matrix from the given symmetric matrix.

Parameters:orig (SpMatrix) – The input symmetric matrix.
from_size(r:int, resize_type:MatrixResizeType=default) → SpMatrix

Creates a new symmetric matrix of given size.

Parameters:
  • r (int) – The number or rows.
  • resize_type (MatrixResizeType) – Determines how the elements are initialized. If SET_ZERO (or COPY_DATA), they are set to zero. If UNDEFINED, they are left uninitialized. Defaults to SET_ZERO.
getitem(r:int, c:int) → float

Gets the element at the given index.

Parameters:
  • r (int) – The row index of the element to get.
  • c (int) – The column index of the element to get.
Returns:

The element at the given index.

Raises:

RuntimeError – If r > num_rows or c > num_cols.

invert_()

Inverts the matrix.

invert_double_()

Inverts the matrix in double precision.

is_diagonal(cutoff:float=default) → bool
Checks if the matrix is diagonal. The nondiagonal elements
must be less or equal to cutoff value.
Parameters:cutoff (float) – A cutoff value. Default is 1.0e-05
Returns:True if sum(digonal_elements)*cutoff > sum(nondiagonal_elements), otherwise false
is_pos_def() → bool

Returns True if the matrix is positive definite.

is_triagonal(cutoff:float=default) → bool

Returns true if the matrix is tridiagonal(based on the cutoff value).

is_unit(cutoff:float=default) → bool
Checks if the matrix is identity-like. It does not have to be square.
The nondiagonal elements must be less or equal to cutoff value, while the diagonal ones must be one.
Parameters:cutoff (float) – A cutoff value. Default is 1.0e-05
Returns:True if the diagonal elements are 1 and sum(nondiagonal_elements)<=cutoff
is_zero(cutoff:float=default) → bool

Checks if all elements are 0 (where 0 is the cutoff value).

Parameters:cutoff (float) – A cutoff value. Default is 1.0e-05
Returns:True if it is a zero matrix
limit_cond(maxCond:float=default, invert:bool=default) → int
Performs self = P*diag(floor(s))*P^T, where s is the eigenvalues
and P is the eigenvectors.
Parameters:MaxCond (float) – Determines the flooring threshold (i.e. floor=max_eigen/MaxCond)
Raises:Error if matrix is not positive definite
limit_cond_double(maxCond:float=default, invert:bool=default) → int

Same as LimitCond but the resulting operation has ``double’’ precision.

log_pos_def_det() → float

Computes the log determinant.

Returns:The log determinant
Raises:RuntimeError – If matrix is not positive definite.
max() → float

Returns the maximum value in the matrix.

max_abs_eig() → float

Returns the maximum of the absolute values of the eigenvalues.

min() → float

Returns the minimum value in the matrix.

num_cols

The number of columns.

num_rows

The number of rows.

qr_(Q:MatrixBase)
The symmetric QR algorithm. This is the symmetric QR algorithm,
from Golub and Van Loan 3rd ed., Algorithm 8.3.3.
Parameters:Q (MatrixBase) – A matrix
read_(is:istream, binary:bool, add:bool=default)

Reads the matrix from the given C++ stream.

Parameters:
  • is (istream) – The input C++ stream.
  • binary (bool) – Whether the stream is binary.
  • add (bool) – Whether to add existing contents to the read matrix. Defaults to False.
resize_(num_rows:int, resize_type:MatrixResizeType=default)

Resizes the matrix.

Parameters:
  • num_rows (int) – The new number of rows.
  • resize_type (MatrixResizeType) – Determines how the elements are initialized. If SET_ZERO, they are set to zero. If UNDEFINED, they are left uninitialized. If COPY_DATA, existing elements are retained, new ones are set to zero. Defaults to SET_ZERO.
scale_(c:float)

Scales each element with the given scalar.

Parameters:c (float) – The scaling coefficient.
scale_diag_(alpha:float)

Scales the diagonal of the matrix by alpha.

Parameters:alpha (float) – A scaling constant
set_diag_(alpha:float)

Sets all the diagonal elements to alpha.

Parameters:alpha (float) – A constant
set_randn_()

Sets the elements to numbers from standard normal distribution.

set_unit_()

Sets diagonal elements to one, off-diagonal elements to zero.

set_zero_()

Sets the elements to zero.

size()

Returns size as a tuple.

Returns:A tuple (num_rows, num_cols) of integers.
size_in_bytes() → int

Returns the size (in bytes) of the data held by the matrix.

swap_(other)

Swaps the contents with another matrix.

Shallow swap.

Parameters:other (Matrix or SpMatrix or TpMatrix) – The input matrix.
Raises:ValueError – If other is not a square matrix.
swap_with_matrix_(other:Matrix)

Swaps the contents with a matrix.

Shallow swap.

Parameters:other (Matrix) – The matrix to swap contents with.
swap_with_packed_(other:PackedMatrix)

Swaps the contents with another packed matrix.

Shallow swap.

Parameters:other (PackedMatrix) – The matrix to swap contents with.
swap_with_symmetric_(other:SpMatrix)

Swaps the contents with another symmetric matrix.

Shallow swap.

Parameters:other (SpMatrix) – The matrix to swap contents with.
trace() → float

Returns the trace of the matrix.

tridiagonalize_(Q:MatrixBase)
Tridiagonalize the matrix with an orthogonal transformation.
Performs self = Q*self*Q^T for an orthogonal Q.
Parameters:Q (MatrixBase) – A matrix that must be orthogonal.
write(os:ostream, binary:bool)

Writes the matrix to given C++ stream.

Parameters:
  • os (ostream) – The output C++ straem.
  • binary (bool) – Whether the stream is binary.
class kaldi.matrix.packed.TpMatrix(num_rows=None, resize_type=<MatrixResizeType.SET_ZERO: 0>)[source]

Single precision triangular matrix.

Initializes a new triangular matrix.

If num_rows is not None, initializes the triangular matrix to the specified size. Otherwise, initializes an empty triangular matrix.

Parameters:
  • num_rows (int) – Number of rows. Defaults to None.
  • resize_type (MatrixResizeType) – How to initialize the elements. If MatrixResizeType.SET_ZERO or MatrixResizeType.COPY_DATA, they are set to zero. If MatrixResizeType.UNDEFINED, they are left uninitialized. Defaults to MatrixResizeType.SET_ZERO.
add_to_diag_(r:float)

Adds a constant r to the diagonal of the matrix.

Parameters:r (float) – A constant
add_tp(alpha:float, M:TpMatrix)

Performs self = self+alpha*M.

Parameters:
  • alpha (float) – A constant
  • M (TpMatrix) – A triangular matrix
Raises:

RuntimeError – In case of dimension mismatch.

cholesky_(orig:SpMatrix)

Performs Cholesky decomposition.

Parameters:orig (SpMatrix) – The input symmetric matrix.
clone()[source]

Clones the triangular matrix.

Returns:A copy of the triangular matrix.
Return type:TpMatrix
copy_from_mat_(M:MatrixBase, trans:MatrixTransposeType=default)

Copies the elements from a square matrix.

Parameters:
  • M (MatrixBase) – The input matrix.
  • trans (MatrixTransposeType) – Determines which elements are copied. If NO_TRANS, copies the lower triangular elements. If TRANS, copies the upper triangular elements. Defaults to NO_TRANS
Raises:

RuntimeError – If M is not square or if dimensions of self and M are different.

copy_from_packed_(orig:PackedMatrix)

Copies the elements from a packed matrix.

Parameters:orig (PackedMatrix) – The packed matrix to copy from.
Raises:RuntimeError – In case of dimension mismatch.
copy_from_tp_(other:TpMatrix)

Copies the elements from another triangular matrix.

Parameters:other (TpMatrix) – The input triangular matrix.
Raises:RuntimeError – In case of dimension mismatch.
copy_from_vec_(orig:SubVector)

Copies the elements from a vector.

Interprets the vector as having the same layout as the packed matrix. Input vector and the packed matrix must have the same size, i.e. orig.size == (self.num_rows * self.num_cols+1) / 2.

Parameters:orig (SubVector) – The vector to copy from.
determinant() → float

Returns the determinant of the matrix.

from_other(orig:TpMatrix) → TpMatrix

Creates a new triangular matrix from the given triangular matrix.

Parameters:orig (TpMatrix) – The input triangular matrix.
from_size(r:int, resize_type:MatrixResizeType=default) → TpMatrix

Creates a new triangular matrix of given size.

Parameters:
  • r (int) – The number or rows.
  • resize_type (MatrixResizeType) – Determines how the elements are initialized. If SET_ZERO (or COPY_DATA), they are set to zero. If UNDEFINED, they are left uninitialized. Defaults to SET_ZERO.
getitem(r:int, c:int) → float

Gets the element at the given index.

Parameters:
  • r (int) – The row index of the element to get.
  • c (int) – The column index of the element to get.
Returns:

The element at the given index.

Raises:

RuntimeError – If r > num_rows or c > num_cols.

invert_()

Inverts the matrix

invert_double_()

Inverts the matrix in double precision.

max() → float

Returns the maximum value in the matrix.

min() → float

Returns the minimum value in the matrix.

num_cols

The number of columns.

num_rows

The number of rows.

read_(is:istream, binary:bool, add:bool=default)

Reads the matrix from the given C++ stream.

Parameters:
  • is (istream) – The input C++ stream.
  • binary (bool) – Whether the stream is binary.
  • add (bool) – Whether to add existing contents to the read matrix. Defaults to False.
resize_(num_rows:int, resize_type:MatrixResizeType=default)

Resizes the matrix.

Parameters:
  • num_rows (int) – The new number of rows.
  • resize_type (MatrixResizeType) – Determines how the elements are initialized. If SET_ZERO, they are set to zero. If UNDEFINED, they are left uninitialized. If COPY_DATA, existing elements are retained, new ones are set to zero. Defaults to SET_ZERO.
scale_(c:float)

Scales each element with the given scalar.

Parameters:c (float) – The scaling coefficient.
scale_diag_(alpha:float)

Scales the diagonal of the matrix by alpha.

Parameters:alpha (float) – A scaling constant
set_diag_(alpha:float)

Sets all the diagonal elements to alpha.

Parameters:alpha (float) – A constant
set_randn_()

Sets the elements to numbers from standard normal distribution.

set_unit_()

Sets diagonal elements to one, off-diagonal elements to zero.

set_zero_()

Sets the elements to zero.

size()

Returns size as a tuple.

Returns:A tuple (num_rows, num_cols) of integers.
size_in_bytes() → int

Returns the size (in bytes) of the data held by the matrix.

swap_(other)

Swaps the contents with another matrix.

Shallow swap.

Parameters:other (Matrix or SpMatrix or TpMatrix) – The input matrix.
Raises:ValueError – If other is not a square matrix.
swap_with_matrix_(other:Matrix)

Swaps the contents with a matrix.

Shallow swap.

Parameters:other (Matrix) – The matrix to swap contents with.
swap_with_packed_(other:PackedMatrix)

Swaps the contents with another packed matrix.

Shallow swap.

Parameters:other (PackedMatrix) – The matrix to swap contents with.
swap_with_triangular_(other:TpMatrix)

Swaps the contents with another triangular matrix.

Shallow swap.

Parameters:other (TpMatrix) – The matrix to swap contents with.
trace() → float

Returns the trace of the matrix.

write(os:ostream, binary:bool)

Writes the matrix to given C++ stream.

Parameters:
  • os (ostream) – The output C++ straem.
  • binary (bool) – Whether the stream is binary.

kaldi.matrix.sparse

Functions

extract_row_range_with_padding Extracts a row-range from a general matrix.

Classes

DoubleSparseMatrix This class defines a Sparse Matrix.
DoubleSparseVector Defines a class for sparse vectors.
GeneralMatrix Defines a class for general matrices.
GeneralMatrixType An enumeration.
SparseMatrix This class defines a Sparse Matrix.
SparseVector Defines a class for sparse vectors.
class kaldi.matrix.sparse.DoubleSparseMatrix

This class defines a Sparse Matrix.

add_to_mat(alpha:float, other:DoubleMatrixBase, t:MatrixTransposeType=default)

Performs other = other+alpha*self if t==kNoTrans or other^T = other^T+alpha*self if t==kTrans

Parameters:
Raises:

Error in case of dimension mismatch

copy_elements_to_vec(other:DoubleVectorBase)

Copies the nonzero elements of the matrix to the vector other.

Parameters:

other (DoubleVector) – A vector to store the nonzero elements of self

Raises:
  • Error if the size of other is different than the number of nonzero
  • elements of self
copy_from_smat_(other:DoubleSparseMatrix)

Copy data from another sparse matrix. Will resize if necessary.

Parameters:other (DoubleSparseMatrix) – The matrix to copy from.
copy_to_mat(other:DoubleMatrixBase, t:MatrixTransposeType=default)

Calls C++ function void ::kaldi::SparseMatrix<double>::CopyToMat(::kaldi::MatrixBase<double> *, ::kaldi::MatrixTransposeType)

frobenius_norm() → float

Computes and returns the Frobenius norm.

from_dims(num_rows:int, num_cols:int) → DoubleSparseMatrix

Initialization by defining the dimensions.

Parameters:
  • num_rows (int) – The number of rows
  • num_cols (int) – The number of columns
from_other(other:DoubleSparseMatrix) → DoubleSparseMatrix

Initialization by copying the elements of another sparse matrix.

Parameters:other (DoubleSparseMatrix) – A sparse matrix
from_pairs(dim:int, pairs:list<list<tuple<int, float>>>) → DoubleSparseMatrix
Initialization by a list of list of tuples. The tuples hold
and index for the matrix and the corresponding value.
Parameters:
  • dim (int) – Determines the number of columns
  • pairs (list<list<tuple<python:int,float>>>) – Contains the element of the matrix. The length of the ``outer’’ list determines the number of rows
num_cols

Number of columns (zero for empty matrix).

num_elements() → int

Returns the number of nonzero elements.

num_rows

Number of rows (zero for empty matrix).

read_(is:istream, binary:bool)

Reads from C++ stream.

Parameters:
  • is (istream) – The input straem.
  • binary (bool) – If the stream is binary.
resize_(rows:int, cols:int, resize_type:MatrixResizeType=default)
Resizes a matrix. If the resize_type is kSetZero,
the new data will be zero. If the resize type is kUndefined, the new data will be undefined. Finally if the resize type is kCopyData, the new data will be the same as the old data in any shared positions, and zero elsewhere. stride_type defines how data will be read from memory, we advise to let it in its default value.
Parameters:
  • rows (int) – The new number of rows
  • cols (int) – The new number of columns
  • resize_type (MatrixResizeType) – Determines the initial values of the vector.
row(r:int) → DoubleSparseVector

Returns the row indexed by r as a sparse vector

scale_(alpha:float)

Scale the elements of the matrix by alpha.

set_randn_(zero_prob:float)

Sets up to a pseudo-randomly initialized matrix, with each element zero with probability zero_prob and else normally distributed.

set_row_(r:int, vec:DoubleSparseVector)

Sets row r to “vec”; makes sure it has the correct dimension.

sum() → float

Returns the sum of the elememts.

swap_(other:DoubleSparseMatrix)

Swaps the contents of self and other. Shallow swap.

write(os:ostream, binary:bool)

Writes to C++ stream.

Parameters:
  • os (ostream) – The output straem.
  • binary (bool) – If the stream is binary.
class kaldi.matrix.sparse.DoubleSparseVector

Defines a class for sparse vectors.

add_to_vec(alpha:float, vec:DoubleVectorBase)

Performs vec=vec+alpha*self

Parameters:
copy_elements_to_vec(vec:DoubleVectorBase)

Copy the elements of self to vec.

Parameters:vec (DoubleVector) – A vector
Raises:Error in case of dimension mismatch
copy_from_svec(other:DoubleSparseVector)

Copy elements of other to self

Parameters:other (DoubleSparseVector) – A sparse vector whose elements we will copy
Raises:Error in case of dimension mismatch
dim

Dimension (zero for empty vector).

from_dim(dim:int) → DoubleSparseVector

Initialization by defining the size of the vector.

Parameters:dim (int) – The size of the vector
from_other(other:DoubleSparseVector) → DoubleSparseVector

Initialization by copying the elements of other.

from_pairs(dim:int, pairs:list<tuple<int, float>>) → DoubleSparseVector

Initialization using a list of tuples(pairs).

Parameters:
  • dim (int) – The size of the vector
  • pairs (list<tuple<python:int,float>>) – A list of tuples. Each tuple holds an index in the vector and its corresponding value
get_elements(i:int) → tuple<int, float>

Get an indexed element.

Returns:A tuple holding the index and a float
max() -> (value:float, index:int)

Returns a tuple holding the maximum value(float) of the vector and its index(int).

num_elements() → int

Returns the number of nonzero elements.

read_(is:istream, binary:bool)

Reads from C++ stream.

Parameters:
  • is (istream) – The input straem.
  • binary (bool) – If the stream is binary.
resize_(dim:int, resize_type:MatrixResizeType=default)
Set vector size to length (length can be zero).
The default resize type is kSetZero, in which case the new data will be zero. If the resize type is kUndefined, the new data will be undefined. Finally if the resize type is kCopyData, the new data will be the same as the old data in any shared positions, and zero elsewhere.
Parameters:
  • dim (int) – The new size of the vector
  • resize_type (MatrixResizeType) – Determines the initial values of the vector.
scale(alpha:float)

Scale the elements of the vector by alpha

set_randn_(zero_prob:float)

Sets elements to zero with probability zero_prob, else normally distributed.

sum() → float

Returns the summation of the elements.

swap_(other:DoubleSparseVector)

Swaps the contents of self and other. Shallow swap.

Parameters:other (DoubleVector) – The vector whose elements we want to swap with self.
write(os:ostream, binary:bool)

Writes to C++ stream.

Parameters:
  • os (ostream) – The output straem.
  • binary (bool) – If the stream is binary.
class kaldi.matrix.sparse.GeneralMatrix

Defines a class for general matrices. A general matrix can be either full(kFullMatrix), compresssed(kCompressedMatrix), or sparse(kSparseMatrix).

add_to_mat(alpha:float, mat:MatrixBase, trans:MatrixTransposeType=default)
Performs mat=mat+alpha*self if MatrixTransposeType==kNoTrans.
Performs mat^T=mat^T+alpha*self if MatrixTransposeType==kNoTrans.
Parameters:
  • alpha (float) – A constant
  • mat (MatrixBase) – A matrix
  • trans (MatrixTransposeType) – Determines transposition of mat. Default is kNoTrans
Raises:

Error in case of dimension mismatch

clear()

Clear the contents of self.

compress()

Compresses the matrix to a sparse one.

copy_to_cu_mat(cu_mat:CuMatrixBase, trans:MatrixTransposeType=default)

Calls C++ function void ::kaldi::GeneralMatrix::CopyToMat(::kaldi::CuMatrixBase<float> *, ::kaldi::MatrixTransposeType)

copy_to_mat(mat:MatrixBase, trans:MatrixTransposeType=default)

Calls C++ function void ::kaldi::GeneralMatrix::CopyToMat(::kaldi::MatrixBase<float> *, ::kaldi::MatrixTransposeType)

from_compressed(other:CompressedMatrix) → GeneralMatrix

Initialization of a General Matrix from matrix of class CompressesMatrix.

from_matrix(other:MatrixBase) → GeneralMatrix

Initialization of a General Matrix from matrix of class MatrixBase.

from_other(other:GeneralMatrix) → GeneralMatrix

Initialization of a General Matrix from matrix of class GeneralMatrix.

from_sparse(other:SparseMatrix) → GeneralMatrix

Initialization of a General Matrix from matrix of class SparseMatrix.

get_compressed_matrix() → CompressedMatrix

Returns the contents as a compressed matrix.

Raises:
  • Error if type returns something other than kCompressedMatrix,
  • or NumRows() != 0
get_full_matrix() → Matrix

Returns the contents as an instance of class Matrix.

Raises:
  • Error if the type is anything other than kFullMatrix
  • or NumRows() != 0
get_matrix() → Matrix

Outputs the contents as an instance of class Matrix.

get_sparse_matrix() → SparseMatrix

Returns the contents as a SparseMatrix.

Raises:
  • Error if type returns something other than kSparseMatrix,
  • or NumRows() != 0
num_cols() → int

Return the number of columns.

num_rows() → int

Return the number of rows.

read(is:istream, binary:bool)

Reads from C++ stream.

Parameters:
  • is (istream) – The input straem.
  • binary (bool) – If the stream is binary.
  • add (bool) – True if you want to add the stream to self.
scale(alpha:float)

Scale all elements of the matrix by alpha.

swap(other:GeneralMatrix)

Swaps the contents of self and other. Shallow swap.

swap_compressed_matrix(cmat:CompressedMatrix)

Swaps the current compressed matrix with another(cmat).

swap_full_matrix(cmat:Matrix)

Swap the contents of self with cmat.

Raises:
  • Error if type is anything other than kFullMatrix,
  • or if NumRows() != 0
swap_sparse_matrix(smat:SparseMatrix)

Swaps the contents of self and smat.

Raises:
  • Error if type returns something other than kSparseMatrix,
  • or NumRows() != 0
type() → GeneralMatrixType

Returns the type of the matrix: kSparseMatrix, kCompressedMatrix or kFullMatrix. If this matrix is empty, returns kFullMatrix.

uncompress()

Uncompresses the matrix to a one of class Matrix.

write(os:ostream, binary:bool)

Writes to C++ stream.

Parameters:
  • os (ostream) – The output straem.
  • binary (bool) – If the stream is binary.
class kaldi.matrix.sparse.GeneralMatrixType

An enumeration.

COMPRESSED_MATRIX = 1
FULL_MATRIX = 0
SPARSE_MATRIX = 2
class kaldi.matrix.sparse.SparseMatrix

This class defines a Sparse Matrix.

add_to_mat(alpha:float, other:MatrixBase, t:MatrixTransposeType=default)

Performs other = other+alpha*self if t==kNoTrans or other^T = other^T+alpha*self if t==kTrans

Parameters:
Raises:

Error in case of dimension mismatch

copy_elements_to_vec(other:VectorBase)

Copies the nonzero elements of the matrix to the vector other.

Parameters:

other (Vector) – A vector to store the nonzero elements of self

Raises:
  • Error if the size of other is different than the number of nonzero
  • elements of self
copy_from_smat_(other:SparseMatrix)

Copy data from another sparse matrix. Will resize if necessary.

Parameters:other (SparseMatrix) – The matrix to copy from.
copy_to_mat(other:MatrixBase, t:MatrixTransposeType=default)

Calls C++ function void ::kaldi::SparseMatrix<float>::CopyToMat(::kaldi::MatrixBase<float> *, ::kaldi::MatrixTransposeType)

frobenius_norm() → float

Computes and returns the Frobenius norm.

from_dims(num_rows:int, num_cols:int) → SparseMatrix

Initialization by defining the dimensions.

Parameters:
  • num_rows (int) – The number of rows
  • num_cols (int) – The number of columns
from_matrix(mat:MatrixBase) → SparseMatrix

Initialization by copying the elements of a matrix.

Parameters:mat (Matrix) – A matrix
from_other(other:SparseMatrix) → SparseMatrix

Initialization by copying the elements of another sparse matrix.

Parameters:other (SparseMatrix) – A sparse matrix
from_pairs(dim:int, pairs:list<list<tuple<int, float>>>) → SparseMatrix
Initialization by a list of list of tuples. The tuples hold
and index for the matrix and the corresponding value.
Parameters:
  • dim (int) – Determines the number of columns
  • pairs (list<list<tuple<python:int,float>>>) – Contains the element of the matrix. The length of the ``outer’’ list determines the number of rows
num_cols

Number of columns (zero for empty matrix).

num_elements() → int

Returns the number of nonzero elements.

num_rows

Number of rows (zero for empty matrix).

read_(is:istream, binary:bool)

Reads from C++ stream.

Parameters:
  • is (istream) – The input straem.
  • binary (bool) – If the stream is binary.
resize_(rows:int, cols:int, resize_type:MatrixResizeType=default)
Resizes a matrix. If the resize_type is kSetZero,
the new data will be zero. If the resize type is kUndefined, the new data will be undefined. Finally if the resize type is kCopyData, the new data will be the same as the old data in any shared positions, and zero elsewhere. stride_type defines how data will be read from memory, we advise to let it in its default value.
Parameters:
  • rows (int) – The new number of rows
  • cols (int) – The new number of columns
  • resize_type (MatrixResizeType) – Determines the initial values of the vector.
row(r:int) → SparseVector

Returns the row indexed by r as a sparse vector

scale_(alpha:float)

Scale the elements of the matrix by alpha.

set_randn_(zero_prob:float)

Sets up to a pseudo-randomly initialized matrix, with each element zero with probability zero_prob and else normally distributed.

set_row_(r:int, vec:SparseVector)

Sets row r to “vec”; makes sure it has the correct dimension.

sum() → float

Returns the sum of the elememts.

swap_(other:SparseMatrix)

Swaps the contents of self and other. Shallow swap.

write(os:ostream, binary:bool)

Writes to C++ stream.

Parameters:
  • os (ostream) – The output straem.
  • binary (bool) – If the stream is binary.
class kaldi.matrix.sparse.SparseVector

Defines a class for sparse vectors.

add_to_vec(alpha:float, vec:VectorBase)

Performs vec=vec+alpha*self

Parameters:
  • alpha (float) – A scaling constant
  • vec (Vector) – A vector
copy_elements_to_vec(vec:VectorBase)

Copy the elements of self to vec.

Parameters:vec (Vector) – A vector
Raises:Error in case of dimension mismatch
copy_from_svec_(other:SparseVector)

Copy elements of other to self

Parameters:other (SparseVector) – A sparse vector whose elements we will copy
Raises:Error in case of dimension mismatch
dim

Dimension (zero for empty vector).

from_dim(dim:int) → SparseVector

Initialization by defining the size of the vector.

Parameters:dim (int) – The size of the vector
from_other(other:SparseVector) → SparseVector

Initialization by copying the elements of other.

from_pairs(dim:int, pairs:list<tuple<int, float>>) → SparseVector

Initialization using a list of tuples(pairs).

Parameters:
  • dim (int) – The size of the vector
  • pairs (list<tuple<python:int,float>>) – A list of tuples. Each tuple holds an index in the vector and its corresponding value
from_vector(vec:VectorBase) → SparseVector

Initialization by copying the elements of vector.

get_elements(i:int) → tuple<int, float>

Get an indexed element.

Returns:A tuple holding the index and a float
max() -> (value:float, index:int)

Returns a tuple holding the maximum value(float) of the vector and its index(int).

num_elements() → int

Returns the number of nonzero elements.

read_(is:istream, binary:bool)

Reads from C++ stream.

Parameters:
  • is (istream) – The input straem.
  • binary (bool) – If the stream is binary.
resize_(dim:int, resize_type:MatrixResizeType=default)
Set vector size to length (length can be zero).
The default resize type is kSetZero, in which case the new data will be zero. If the resize type is kUndefined, the new data will be undefined. Finally if the resize type is kCopyData, the new data will be the same as the old data in any shared positions, and zero elsewhere.
Parameters:
  • dim (int) – The new size of the vector
  • resize_type (MatrixResizeType) – Determines the initial values of the vector.
scale_(alpha:float)

Scale the elements of the vector by alpha

set_randn_(zero_prob:float)

Sets elements to zero with probability zero_prob, else normally distributed.

sum() → float

Returns the summation of the elements.

swap_(other:SparseVector)

Swaps the contents of self and other. Shallow swap.

Parameters:other (Vector) – The vector whose elements we want to swap with self.
write(os:ostream, binary:bool)

Writes to C++ stream.

Parameters:
  • os (ostream) – The output straem.
  • binary (bool) – If the stream is binary.
kaldi.matrix.sparse.extract_row_range_with_padding(matrix:GeneralMatrix, row_offset:int, num_rows:int) → GeneralMatrix

Extracts a row-range from a general matrix.

This function extracts a row-range from a general matrix and writes a GeneralMatrix containing the same type of underlying matrix. If the row-range is partly outside the row-range of ‘in’ (i.e. if row_offset < 0 or row_offset + num_rows > in.NumRows()) then it will pad with copies of the first and last row as needed. This is more efficient than un-compressing and re-compressing the underlying CompressedMatrix, and causes less accuracy loss due to re-compression (no loss in most cases).
Parameters:
  • matrix (GeneralMatrix) – The general matrix.
  • row_offset (int) – The start row index.
  • num_rows (int) – The number of rows.
Returns:

A general matrix.