sparse-matrix-test.cc
Go to the documentation of this file.
1 // matrix/sparse-matrix-test.cc
2 
3 // Copyright 2015 Guoguo Chen
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 
20 #include "matrix/matrix-lib.h"
21 #include "util/stl-utils.h"
22 
23 namespace kaldi {
24 
25 template <typename Real>
27  for (int32 i = 0; i < 10; i++) {
28  MatrixIndexT dim = 10 + Rand() % 40;
29 
30  SparseVector<Real> svec(dim);
31  svec.SetRandn(0.8);
32 
33  Vector<Real> vec(dim);
34  vec.SetRandn();
35  svec.CopyElementsToVec(&vec);
36 
37  Real sum1 = svec.Sum();
38  Real sum2 = vec.Sum();
39  AssertEqual(sum1, sum2, 0.00001);
40  }
41 }
42 
43 template <typename Real>
45  for (int32 i = 0; i < 10; i++) {
46  MatrixIndexT dim = 10 + Rand() % 40;
47 
48  SparseVector<Real> svec(dim);
49  svec.SetRandn(0.8);
50 
51  Vector<Real> vec(dim);
52  vec.SetRandn();
53  svec.CopyElementsToVec(&vec);
54 
55  Vector<Real> other_vec1(dim);
56  other_vec1.SetRandn();
57  Vector<Real> other_vec2 = other_vec1;
58 
59  svec.AddToVec(0.7, &other_vec1);
60  other_vec2.AddVec(0.7, vec);
61  AssertEqual(other_vec1, other_vec2, 0.00001);
62  }
63 }
64 
65 template <typename Real>
67  for (int32 i = 0; i < 10; i++) {
68  MatrixIndexT dim = 10 + Rand() % 40;
69  if (RandInt(0, 3) == 0)
70  dim = RandInt(1, 5);
71 
72  SparseVector<Real> svec(dim);
73  if (RandInt(0, 3) != 0)
74  svec.SetRandn(0.8);
75 
76  Vector<Real> vec(dim);
77  vec.SetRandn();
78  svec.CopyElementsToVec(&vec);
79 
80  int32 index1, index2;
81  Real max1, max2;
82 
83  max1 = svec.Max(&index1);
84  max2 = vec.Max(&index2);
85 
86  AssertEqual(max1, max2, 0.00001);
87  AssertEqual(index1, index2, 0.00001);
88  }
89 }
90 
91 template <typename Real>
93  for (int32 i = 0; i < 10; i++) {
94  MatrixIndexT dim = 10 + Rand() % 40;
95 
96  SparseVector<Real> svec(dim);
97  svec.SetRandn(0.8);
98 
99  Vector<Real> vec(dim);
100  vec.SetRandn();
101  svec.CopyElementsToVec(&vec);
102 
103  Vector<Real> other_vec(dim);
104  other_vec.SetRandn();
105 
106  Real product1 = VecSvec(other_vec, svec);
107  Real product2 = VecVec(other_vec, vec);
108 
109  KALDI_ASSERT(fabs(product1 - product2) < 1.0e-04);
110  }
111 }
112 
113 template <typename Real>
115  for (int32 i = 0; i < 10; i++) {
116  MatrixIndexT row = 10 + Rand() % 40;
117  MatrixIndexT col = 10 + Rand() % 50;
118 
119  SparseMatrix<Real> smat(row, col);
120  smat.SetRandn(0.8);
121 
122  Matrix<Real> mat(row, col);
123  mat.SetRandn();
124  smat.CopyToMat(&mat);
125 
126  Real sum1 = smat.Sum();
127  Real sum2 = mat.Sum();
128  AssertEqual(sum1, sum2, 0.00001);
129  }
130 }
131 
132 template <typename Real>
134  for (int32 i = 0; i < 10; i++) {
135  MatrixIndexT row = 10 + Rand() % 40;
136  MatrixIndexT col = 10 + Rand() % 50;
137 
138  SparseMatrix<Real> smat(row, col);
139  smat.SetRandn(0.8);
140 
141  Matrix<Real> mat(row, col);
142  mat.SetRandn();
143  smat.CopyToMat(&mat);
144 
145  Real norm1 = smat.FrobeniusNorm();
146  Real norm2 = mat.FrobeniusNorm();
147  AssertEqual(norm1, norm2, 0.00001);
148  }
149 }
150 
151 template <typename Real>
153  for (int32 i = 0; i < 10; i++) {
154  MatrixIndexT row = 10 + Rand() % 40;
155  MatrixIndexT col = 10 + Rand() % 50;
156 
157  SparseMatrix<Real> smat(row, col);
158  smat.SetRandn(0.8);
159 
160  Matrix<Real> mat(row, col);
161  mat.SetRandn();
162  smat.CopyToMat(&mat);
163 
164  Matrix<Real> other_mat1(row, col);
165  other_mat1.SetRandn();
166  Matrix<Real> other_mat2 = other_mat1;
167 
168  smat.AddToMat(0.7, &other_mat1);
169  other_mat2.AddMat(0.7, mat);
170  AssertEqual(other_mat1, other_mat2, 0.00001);
171  }
172 }
173 
174 template <typename Real>
176  int32 num_rows = RandInt(1, 10),
177  num_cols = RandInt(0, 10);
178  if (num_cols == 0)
179  num_rows = 0;
180 
181  Matrix<Real> mat(num_rows, num_cols);
182 
183  for (int32 r = 0; r < num_rows; r++) {
184  for (int32 c = 0; c < num_cols; c++) {
185  if (RandInt(0, 5) == 0)
186  mat(r, c) = RandGauss();
187  }
188  }
189  SparseMatrix<Real> smat(mat);
190 
191  Matrix<Real> mat2(num_rows, num_cols);
192  mat2.SetRandn();
193  smat.CopyToMat(&mat2);
194  AssertEqual(mat, mat2);
195 }
196 
197 template <typename Real>
199  for (int32 i = 0; i < 10; i++) {
200  MatrixIndexT row = 10 + Rand() % 40;
201  MatrixIndexT col = 10 + Rand() % 50;
202 
203  Matrix<Real> mat1(row, col);
204  Matrix<Real> mat2(col, row);
205  Matrix<Real> mat3(row, col);
206  mat1.SetRandn();
207  mat2.SetRandn();
208  mat3.SetRandn();
209 
210  SparseMatrix<Real> smat1(row, col);
211  SparseMatrix<Real> smat2(col, row);
212  smat1.SetRandn(0.8);
213  smat2.SetRandn(0.8);
214 
215  smat1.CopyToMat(&mat1);
216  smat2.CopyToMat(&mat2);
217 
218  Real trace1 = TraceMatMat(mat3, mat1, kTrans);
219  Real trace2 = TraceMatSmat(mat3, smat1, kTrans);
220  AssertEqual(trace1, trace2, 0.00001);
221 
222  trace1 = TraceMatMat(mat3, mat2, kNoTrans);
223  trace2 = TraceMatSmat(mat3, smat2, kNoTrans);
224  AssertEqual(trace1, trace2, 0.00001);
225  }
226 }
227 
228 template <typename Real>
230 
231  for (int32 t = 0; t < 4; t++) {
232  MatrixIndexT m = RandInt(10, 20), n = RandInt(10, 20), o = RandInt(10, 20);
233  MatrixTransposeType Btrans = (RandInt(0, 1) == 0 ? kTrans : kNoTrans);
234 
235  // we are effectively comparing trace(A B C) computed as
236  // trace((A B) C) vs. trace ((C A) B)
237 
238  BaseFloat alpha = 0.333, beta = 1.764;
239 
240 
241  Matrix<Real> A(m, n);
242  A.SetRandn();
243  SparseMatrix<Real> B(Btrans == kNoTrans ? n : o,
244  Btrans == kNoTrans ? o : n);
245  B.SetRandn(0.5);
246 
247  Matrix<Real> AB(m, o);
248  AB.SetRandn(); // this random extra part is used in testing the 'beta'.
249 
250  Matrix<Real> C(o, m);
251  C.SetRandn();
252 
253  Matrix<Real> CA(o, n);
254  CA.AddMatMat(1.0, C, kNoTrans, A, kNoTrans, 0.0);
255  Real trace_abc_alpha = TraceMatSmat(CA, B, Btrans);
256 
257  Real trace_abc_beta = TraceMatMat(AB, C, kNoTrans);
258 
259  AB.AddMatSmat(alpha, A, B, Btrans, beta);
260 
261  // next line is in case I made certain mistakes like setting matrix to
262  // random.
263  KALDI_ASSERT(trace_abc_alpha != 0.0 && trace_abc_beta != 0.0);
264  Real result1 = TraceMatMat(AB, C, kNoTrans),
265  result2 = alpha * trace_abc_alpha + beta * trace_abc_beta;
266  AssertEqual(result1, result2, 0.01);
267  }
268 }
269 
270 
271 template <typename Real>
273 
274  for (int32 t = 0; t < 4; t++) {
275  MatrixIndexT m = RandInt(10, 20), n = RandInt(10, 20), o = RandInt(10, 20);
276  MatrixTransposeType Btrans = (RandInt(0, 1) == 0 ? kTrans : kNoTrans);
277 
278  // we are effectively comparing trace(A B C) computed as
279  // trace((A B) C) vs. trace ((C A) B)
280 
281  BaseFloat alpha = 0.333, beta = 1.764;
282 
283  Matrix<Real> A(m, n);
284  A.SetRandn();
285  SparseMatrix<Real> B(Btrans == kNoTrans ? n : o,
286  Btrans == kNoTrans ? o : n);
287  B.SetRandn(0.5);
288 
289  Matrix<Real> C(o, m);
290  C.SetRandn();
291 
292  Matrix<Real> BC(n, m);
293  BC.SetRandn(); // this random extra part is used in testing the 'beta'.
294 
295  Matrix<Real> CA(o, n);
296  CA.AddMatMat(1.0, C, kNoTrans, A, kNoTrans, 0.0);
297  Real trace_abc_alpha = TraceMatSmat(CA, B, Btrans);
298 
299  Real trace_abc_beta = TraceMatMat(A, BC, kNoTrans);
300 
301  BC.AddSmatMat(alpha, B, Btrans, C, beta);
302 
303  // next line is in case I made certain mistakes like setting matrix to
304  // random.
305  KALDI_ASSERT(trace_abc_alpha != 0.0 && trace_abc_beta != 0.0);
306  Real result1 = TraceMatMat(A, BC, kNoTrans),
307  result2 = alpha * trace_abc_alpha + beta * trace_abc_beta;
308  AssertEqual(result1, result2, 0.01);
309  }
310 }
311 
312 
313 template <typename Real>
315  // SparseVector
316  UnitTestSparseVectorSum<Real>();
317  UnitTestSparseVectorAddToVec<Real>();
318  UnitTestSparseVectorMax<Real>();
319  UnitTestSparseVectorVecSvec<Real>();
320 
321  // SparseMatrix
322  UnitTestSparseMatrixSum<Real>();
323  UnitTestSparseMatrixFrobeniusNorm<Real>();
324  UnitTestSparseMatrixAddToMat<Real>();
325  UnitTestSparseMatrixTraceMatSmat<Real>();
326  for (int32 i = 0; i < 30; i++)
327  UnitTestSparseMatrixConstructor<Real>();
328 
329 
330  // Matrix functions involving sparse matrices.
331  UnitTestMatrixAddMatSmat<Real>();
332  UnitTestMatrixAddSmatMat<Real>();
333 }
334 
335 } // namespace kaldi
336 
337 int main() {
339  kaldi::SparseMatrixUnitTest<float>();
340  kaldi::SparseMatrixUnitTest<double>();
341  KALDI_LOG << "Tests succeeded.";
342  return 0;
343 }
void CopyElementsToVec(VectorBase< OtherReal > *vec) const
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void UnitTestSparseVectorMax()
void UnitTestSparseMatrixTraceMatSmat()
void UnitTestMatrixAddSmatMat()
Real Max(int32 *index) const
void SetRandn(BaseFloat zero_prob)
Sets elements to zero with probability zero_prob, else normally distributed.
void AddToVec(Real alpha, VectorBase< OtherReal > *vec) const
void UnitTestSparseVectorAddToVec()
void AddMat(const Real alpha, const MatrixBase< Real > &M, MatrixTransposeType transA=kNoTrans)
*this += alpha * M [or M^T]
float RandGauss(struct RandomState *state=NULL)
Definition: kaldi-math.h:155
kaldi::int32 int32
A class for storing matrices.
Definition: kaldi-matrix.h:823
void SparseMatrixUnitTest()
void UnitTestSparseMatrixSum()
Real VecSvec(const VectorBase< Real > &vec, const SparseVector< Real > &svec)
void CopyToMat(MatrixBase< OtherReal > *other, MatrixTransposeType t=kNoTrans) const
Copy to matrix. It must already have the correct size.
void SetVerboseLevel(int32 i)
This should be rarely used, except by programs using Kaldi as library; command-line programs set the ...
Definition: kaldi-error.h:64
void AddToMat(BaseFloat alpha, MatrixBase< Real > *other, MatrixTransposeType t=kNoTrans) const
Does *other = *other + alpha * *this.
void UnitTestSparseVectorVecSvec()
int32 MatrixIndexT
Definition: matrix-common.h:98
struct rnnlm::@11::@12 n
void UnitTestSparseVectorSum()
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)
Real Max() const
Returns the maximum value of any element, or -infinity for the empty vector.
void UnitTestSparseMatrixConstructor()
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.
Real Sum() const
Returns sum of all elements in matrix.
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
Real Sum() const
Returns sum of the elements.
void SetRandn()
Set vector to random normally-distributed noise.
void UnitTestSparseMatrixFrobeniusNorm()
void AddSmatMat(Real alpha, const SparseMatrix< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, Real beta)
(*this) = alpha * op(A) * B + beta * (*this), where A is sparse.
A class representing a vector.
Definition: kaldi-vector.h:406
void SetRandn(BaseFloat zero_prob)
Sets up to a pseudo-randomly initialized matrix, with each element zero with probability zero_prob an...
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
Real FrobeniusNorm() const
Frobenius norm, which is the sqrt of sum of square elements.
MatrixTransposeType
Definition: matrix-common.h:32
static void AssertEqual(float a, float b, float relative_tolerance=0.001)
assert abs(a - b) <= relative_tolerance * (abs(a)+abs(b))
Definition: kaldi-math.h:276
void UnitTestSparseMatrixAddToMat()
void UnitTestMatrixAddMatSmat()
Real TraceMatSmat(const MatrixBase< Real > &A, const SparseMatrix< Real > &B, MatrixTransposeType trans)
Real FrobeniusNorm() const
void AddMatSmat(Real alpha, const MatrixBase< Real > &A, const SparseMatrix< Real > &B, MatrixTransposeType transB, Real beta)
(*this) = alpha * A * op(B) + beta * (*this), where B is sparse and op(B) is either B or trans(B) dep...
#define KALDI_LOG
Definition: kaldi-error.h:153
Real VecVec(const VectorBase< Real > &a, const VectorBase< Real > &b)
Returns dot product between v1 and v2.
Definition: kaldi-vector.cc:37
void AddVec(const Real alpha, const VectorBase< OtherReal > &v)
Add vector : *this = *this + alpha * rv (with casting between floats and doubles) ...
int main()
int32 RandInt(int32 min_val, int32 max_val, struct RandomState *state)
Definition: kaldi-math.cc:95