86 KALDI_WARN <<
"Rank " <<
rank_ <<
" of online preconditioner is >= dim " << D
88 << (D - 1) <<
" (but this is probably still too high)";
93 for (
int32 r = 0; r < R; r++) {
94 std::vector<int32> cols;
95 for (
int32 c = r; c < D; c += R)
97 for (
int32 i = 0;
i < cols.size();
i++) {
99 R_t_(r, c) = (
i == 0 ? 1.1 : 1.0) /
100 sqrt(1.1 * 1.1 + cols.size() - 1);
111 int32 num_init_iters = 3;
112 for (
int32 i = 0;
i < num_init_iters;
i++) {
123 if (ans > 0.9) ans = 0.9;
146 F_t.
Eig(&eigs, NULL);
152 S_t.AddMat2(1.0 / N, *X_t,
kTrans, 0.0);
157 T_t.
AddSp(1.0 - eta, F_t);
172 double c_t_floor = pow(
rho_t_ * (1.0 - eta), 2);
176 KALDI_WARN <<
"Floored " << nf <<
" elements of c_t.";
191 double rho_t1 = (1.0 / (D - R)) *
192 (eta * S_t.Trace() + (1.0 - eta) * (D *
rho_t_ +
d_t_.
Sum()) - sqrt_c_t.
Sum());
198 if (rho_t1 < floor_val) {
199 KALDI_WARN <<
"flooring rho_{t+1} to " << floor_val <<
", was " << rho_t1;
205 KALDI_WARN <<
"Floored " << nf <<
" elements of d_{t+1}.";
208 if (nf == 0 && rho_t1 > floor_val) {
209 double tr_F_t1 = D * rho_t1 + d_t1.
Sum(), tr_T_t = T_t.
Trace();
226 double gamma = (tr_Xhat_Xhat == 0 ? 1.0 : sqrt(tr_x_x / tr_Xhat_Xhat));
245 if (!unit.
IsUnit(1.0e-03)) {
246 KALDI_WARN <<
"R is not orthogonal, reorthogonalizing.";
264 N = (2 * R) +
Rand() % 30,
265 D = R + 1 +
Rand() % 20;
271 if (
Rand() % 3 == 0) zero =
true;
277 big_eig_factor = big_eig_factor * big_eig_factor;
280 big_eig_vector.
Scale(big_eig_factor);
288 int32 num_iters = 100;
289 for (
int32 iter = 0; iter < num_iters; iter++) {
291 if (one) M_cpu.
Set(1.0);
296 M_cpu.
AddVecVec(1.0, rand_vec, big_eig_vector);
308 AssertEqual(trace1, trace2 * gamma2 * gamma2, 1.0e-02);
327 using namespace kaldi;
329 for (
int32 loop = 0; loop < 2; loop++) {
331 CuDevice::Instantiate().SetDebugStrideMode(
true);
333 CuDevice::Instantiate().SelectGpuId(
"no");
335 CuDevice::Instantiate().SelectGpuId(
"optional");
void CopyFromMat(const MatrixBase< OtherReal > &src, MatrixTransposeType trans=kNoTrans)
void AddMat2(const Real alpha, const MatrixBase< Real > &M, MatrixTransposeType transM, const Real beta)
rank-N update: if (transM == kNoTrans) (*this) = beta*(*this) + alpha * M * M^T, or (if transM == kTr...
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
bool IsUnit(Real cutoff=1.0e-05) const
OnlineNaturalGradientSimple()
void AddMat2Vec(const Real alpha, const MatrixBase< Real > &M, MatrixTransposeType transM, const VectorBase< Real > &v, const Real beta=0.0)
Extension of rank-N update: this <– beta*this + alpha * M * diag(v) * M^T.
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Base class which provides matrix operations not involving resizing or allocation. ...
void AddDiagMat2(Real alpha, const MatrixBase< Real > &M, MatrixTransposeType trans=kNoTrans, Real beta=1.0)
Add the diagonal of a matrix times itself: *this = diag(M M^T) + beta * *this (if trans == kNoTrans)...
BaseFloat Eta(int32 N) const
void Eig(VectorBase< Real > *s, MatrixBase< Real > *P=NULL) const
Solves the symmetric eigenvalue problem: at end we should have (*this) = P * diag(s) * P^T...
Keywords for search: natural gradient, naturalgradient, NG-SGD.
This class represents a matrix that's stored on the GPU if we have one, and in memory if not...
void Resize(MatrixIndexT length, MatrixResizeType resize_type=kSetZero)
Set vector to a specified size (can be zero).
void CopyFromMat(const MatrixBase< OtherReal > &M, MatrixTransposeType trans=kNoTrans)
Copy given matrix. (no resize is done).
Real Min() const
Returns the minimum value of any element, or +infinity for the empty vector.
Real Norm(Real p) const
Compute the p-th norm of the vector.
void AddDiagMatMat(Real alpha, const CuMatrixBase< Real > &M, MatrixTransposeType transM, const CuMatrixBase< Real > &N, MatrixTransposeType transN, Real beta=1.0)
Add the diagonal of a matrix product: *this = diag(M N), assuming the "trans" arguments are both kNoT...
void ApplyFloor(Real floor_val, MatrixIndexT *floored_count=nullptr)
Applies floor to all elements.
void CopyFromVec(const VectorBase< Real > &v)
Copy data from another vector (must match own size).
void AddToDiag(const Real r)
void CopyFromVec(const CuVectorBase< Real > &src)
Copy functions; these will crash if the dimension do not match.
void AddSp(const Real alpha, const SpMatrix< Real > &Ma)
double num_samples_history_
void AddMatSp(const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const SpMatrix< Real > &B, const Real beta)
this <– beta*this + alpha*A*B.
void SetRandn()
Sets to random values of a normal distribution.
void AddMatMat(const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const Real beta)
void PreconditionDirections(CuMatrixBase< BaseFloat > *R, CuVectorBase< BaseFloat > *row_prod, BaseFloat *scale)
Real Max() const
Returns the maximum value of any element, or -infinity for the empty vector.
void PreconditionDirections(CuMatrixBase< BaseFloat > *X, BaseFloat *scale)
This call implements the main functionality of this class.
void InitDefault(int32 D)
Real TraceMatMat(const MatrixBase< Real > &A, const MatrixBase< Real > &B, MatrixTransposeType trans)
We need to declare this here as it will be a friend function.
void Scale(Real alpha)
Multiplies all elements by this constant.
int Rand(struct RandomState *state)
Real Sum() const
Returns sum of the elements.
void SetRandn()
Set vector to random normally-distributed noise.
void MulRowsVec(const VectorBase< Real > &scale)
Equivalent to (*this) = diag(scale) * (*this).
void Init(const MatrixBase< double > &R0)
Matrix for CUDA computing.
void InvertElements()
Invert all elements.
#define KALDI_ASSERT(cond)
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
void Set(Real f)
Set all members of a vector to a specified value.
void ApplyPow(Real power)
Take all elements of vector to a power.
void AddVecVec(const Real alpha, const VectorBase< OtherReal > &a, const VectorBase< OtherReal > &b)
*this += alpha * a * b^T
static void AssertEqual(float a, float b, float relative_tolerance=0.001)
assert abs(a - b) <= relative_tolerance * (abs(a)+abs(b))
void Resize(const MatrixIndexT r, const MatrixIndexT c, MatrixResizeType resize_type=kSetZero, MatrixStrideType stride_type=kDefaultStride)
Sets matrix to a specified size (zero is OK as long as both r and c are zero).
MatrixIndexT NumRows() const
Dimensions.
Provides a vector abstraction class.
void Add(Real c)
Add a constant to each element of a vector.
void Invert(Real *logdet=NULL, Real *det_sign=NULL, bool inverse_needed=true)
matrix inverse.
Real VecVec(const VectorBase< Real > &a, const VectorBase< Real > &b)
Returns dot product between v1 and v2.
void AddVec(const Real alpha, const VectorBase< OtherReal > &v)
Add vector : *this = *this + alpha * rv (with casting between floats and doubles) ...
Represents a non-allocating general vector which can be defined as a sub-vector of higher-level vecto...
void UnitTestPreconditionDirectionsOnline()
void PreconditionDirectionsCpu(MatrixBase< double > *R, VectorBase< double > *row_prod, BaseFloat *scale)
void SortSvd(VectorBase< Real > *s, MatrixBase< Real > *U, MatrixBase< Real > *Vt, bool sort_on_absolute_value)
Function to ensure that SVD is sorted.
void Set(Real)
Sets all elements to a specific value.
int32 RandInt(int32 min_val, int32 max_val, struct RandomState *state)
Vector for CUDA computing.