Feature transforms (LDA)
Linear/affine feature transforms — the equivalent of kaldi's
transform-feats (kaldi-native-fbank/csrc/feature-transform.h,
transform-feats). This is how an
LDA matrix is applied after the splice stage.
The transform is a matrix \(M\) of shape (output_dim, n):
\(n\) = input dim → linear: \(y = x\, M^{T}\)
\(n\) = input dim + 1 → affine: the last column of \(M\) is the offset, \(y = x\, M_{[:,\,0:n-1]}^{T} + M_{[:,\,n-1]}\)
import kaldi_native_fbank as knf
lda = knf.FeatureTransform.read("lda.mat")
out = lda.apply(feats) # (T, lda.output_dim)
out = knf.transform_features(feats, lda) # same thing
API reference
- class kaldi_native_fbank.FeatureTransform(self: kaldi_native_fbank.FeatureTransform, mat: Annotated[numpy.typing.ArrayLike, numpy.float32, '[m, n]'])
A linear (or affine) feature transform, e.g. an LDA matrix.
This is kaldi's transform-feats (same semantics). The transform is a matrix \(M\) of shape \((D_{out} \times n)\):
if \(n\) equals the input feature dimension, the transform is linear: \(y = x\, M^{T}\);
if \(n\) is one larger, the transform is affine and the last column of \(M\) is the offset: \(y = x\, M_{[:,\,0:n-1]}^{T} + M_{[:,\,n-1]}\);
otherwise
apply()raises an exception.
Example
lda = knf.FeatureTransform.read("lda.mat") out = lda.apply(feats) # (T, lda.output_dim) out = knf.transform_features(feats, lda)
See also
Construct a transform from a matrix directly.
- Parameters:
mat -- The transform matrix of shape
(output_dim, input_dim), or one column wider for an affine transform (the last column is the offset).
- __init__(self: kaldi_native_fbank.FeatureTransform, mat: Annotated[numpy.typing.ArrayLike, numpy.float32, '[m, n]']) None
Construct a transform from a matrix directly.
- Parameters:
mat -- The transform matrix of shape
(output_dim, input_dim), or one column wider for an affine transform (the last column is the offset).
- apply(self: kaldi_native_fbank.FeatureTransform, feats: Annotated[numpy.typing.ArrayLike, numpy.float32, '[m, n]']) Annotated[numpy.typing.NDArray[numpy.float32], '[m, n]']
Apply the transform to a feature matrix.
kaldi
transform-featssemantics (rows = frames): if the transform hasfeats.shape[1]columns the output is \(Y = X M^{T}\) (linear); if it has one column more, the transform is affine and the last column is the offset. Otherwise an exception is raised.- Parameters:
feats (numpy.ndarray) -- Feature matrix of shape
(num_frames, dim), float32.- Returns:
The transformed matrix of shape
(num_frames, output_dim), float32.- Return type:
numpy.ndarray
- property input_dim
Number of columns of the stored matrix.
This is the feature dimension in the linear form; an affine transform consumes
input_dim - 1features plus the implicit 1.0.
- is_affine(self: kaldi_native_fbank.FeatureTransform, input_dim: SupportsInt) bool
Whether the transform is affine for inputs of dimension
input_dim.The matrix alone cannot record the intended input dimension, so the rule is the one kaldi's
transform-featsapplies: the transform is affine iff it has one column more than the feature dimension (the extra column is the offset).- Parameters:
input_dim -- The input feature dimension.
- Returns:
True for an affine transform.
- property output_dim
Number of rows of the stored matrix (the output dimension).
- static read(filename: str) kaldi_native_fbank.FeatureTransform
Read a transform matrix from a kaldi matrix file.
Supports the kaldi binary format (
"FM"/"DM", as written by kaldi'sMatrix::Write) and the kaldi text format ([ ... ]).- Parameters:
filename -- Path of the matrix file, e.g.
lda.mat.- Returns:
The transform.
- kaldi_native_fbank.transform_features(feats: Annotated[numpy.typing.ArrayLike, numpy.float32, '[m, n]'], transform: kaldi_native_fbank.FeatureTransform) Annotated[numpy.typing.NDArray[numpy.float32], '[m, n]']
Apply a transform to features and return the result.
Shorthand for
transform.apply(feats); matches kaldi's transform-feats.- Parameters:
feats (numpy.ndarray) -- Feature matrix of shape
(num_frames, dim), float32.transform -- The transform; see
FeatureTransform.
- Returns:
The transformed matrix of shape
(num_frames, transform.output_dim).- Return type:
numpy.ndarray
See also