nnet-precondition-test.cc
Go to the documentation of this file.
1 // nnet2/nnet-precondition-test.cc
2 
3 // Copyright 2012 Johns Hopkins University (author: Daniel Povey)
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
21 #include "util/common-utils.h"
22 
23 namespace kaldi {
24 namespace nnet2 {
25 
27  MatrixIndexT N = 2 + Rand() % 30,
28  D = 1 + Rand() % 20;
29  BaseFloat lambda = 0.1;
30  CuMatrix<BaseFloat> R(N, D), P(N, D);
31  R.SetRandn();
32  P.SetRandn(); // contents should be overwritten.
33 
34  PreconditionDirections(R, lambda, &P);
35  // The rest of this function will do the computation the function is doing in
36  // a different, less efficient way and compare with the function call.
37 
39  G.SetUnit();
40  G.ScaleDiag(lambda);
41  // G += R^T R.
42  G.AddMat2(1.0/(N-1), R, kTrans, 1.0);
43 
44  for (int32 n = 0; n < N; n++) {
47  Gn.AddVec2(-1.0/(N-1), rn); // subtract the
48  // outer product of "this" vector.
49  Gn.Invert();
51  CuVector<BaseFloat> pn_compare(D);
52  pn_compare.AddSpVec(1.0, Gn, rn, 0.0);
53  KALDI_ASSERT(pn.ApproxEqual(pn_compare, 0.1));
54  }
55 }
56 
57 
58 } // namespace nnet2
59 } // namespace kaldi
60 
61 
62 int main() {
63  using namespace kaldi;
64  using namespace kaldi::nnet2;
65  for (int32 i = 0; i < 10; i++)
67 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
int main()
kaldi::int32 int32
void PreconditionDirections(const CuMatrixBase< BaseFloat > &R, double lambda, CuMatrixBase< BaseFloat > *P)
See below for comment.
This class represents a matrix that&#39;s stored on the GPU if we have one, and in memory if not...
Definition: matrix-common.h:71
void Invert()
Note: the CuMatrix version of the Invert() function will only work for positive definite matrices; it...
Definition: cu-sp-matrix.cc:93
int32 MatrixIndexT
Definition: matrix-common.h:98
struct rnnlm::@11::@12 n
void AddSpVec(const Real alpha, const CuSpMatrix< Real > &S, const CuVectorBase< Real > &v, const Real beta)
Definition: cu-vector.cc:535
void SetUnit()
< Set to zero
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
void ScaleDiag(Real alpha)
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
bool ApproxEqual(const CuVectorBase< Real > &other, float tol=0.01) const
Definition: cu-vector.cc:583
void UnitTestPreconditionDirections()
void AddMat2(const Real alpha, const CuMatrixBase< Real > &M, MatrixTransposeType transM, const Real beta)
void AddVec2(const Real alpha, const CuVectorBase< Real > &v)