28 const std::vector<int32> &ys,
40 int32 num_classes = *std::max_element(ys.begin(), ys.end()) + 1;
45 for (
int32 i = 0;
i < xs_num_rows;
i++) {
46 xs_with_prior(
i, xs_num_cols) = 1.0;
52 for (
int32 i = 0;
i < num_classes;
i++) {
58 KALDI_LOG <<
"Finished training parameters without mixture components.";
62 if (conf.
mix_up > num_classes) {
63 MixUp(ys, num_classes, conf);
66 KALDI_LOG <<
"Finished training mixture components.";
72 const int32 &num_classes,
76 for (
int32 i = 0;
i < ys.size();
i++) {
82 std::vector<int32> targets;
84 int32 new_dim = std::accumulate(targets.begin(), targets.end(),
85 static_cast<int32>(0));
88 <<
". Training " << new_dim <<
" mixture components.";
91 num_components = old_dim,
101 for (
int32 i = 0;
i < targets.size();
i++) {
106 int32 offset = num_components;
129 for (
int32 step = 0; step < max_steps; step++) {
130 DoStep(xs, xw, ys, &lbfgs, normalizer);
145 log_posteriors->
Resize(xs_num_rows, num_classes);
152 for (
int32 i = 0;
i < xs_num_rows;
i++) {
153 xs_with_prior(
i, xs_num_cols) = 1.0;
158 log_posteriors->
Set(-std::numeric_limits<BaseFloat>::infinity());
161 for (
int32 i = 0;
i < xs_num_rows;
i++) {
162 for (
int32 j = 0;
j < num_mixes;
j++) {
164 (*log_posteriors)(
i,k) =
LogAdd((*log_posteriors)(
i,k), xw(i,
j));
167 log_posteriors->
Row(
i).Add(-xw.
Row(
i).LogSumExp());
176 log_posteriors->
Resize(num_classes);
183 x_with_prior(x_dim) = 1.0;
187 log_posteriors->
Set(-std::numeric_limits<BaseFloat>::infinity());
189 for (
int32 i = 0;
i < num_mixes;
i++) {
191 (*log_posteriors)(
j) =
LogAdd((*log_posteriors)(
j), xw(
i));
213 grad_vec.CopyRowsFromMat(gradient);
216 lbfgs->
DoStep(objf, grad_vec);
221 KALDI_LOG <<
"Objective function is " << objf;
230 int32 num_classes = *std::max_element(ys.begin(), ys.end()) + 1;
231 std::vector< std::vector<int32> > class_to_cols(num_classes, std::vector<int32>());
233 class_to_cols[
class_[
i]].push_back(
i);
236 for (
int32 i = 0;
i < ys.size();
i++) {
242 const std::vector<int32> &cols = class_to_cols[ys[
i]];
245 for (
int32 j = 0;
j < cols.size();
j++) {
246 class_sum += row(cols[
j]);
248 if (class_sum < 1.0e-20) class_sum = 1.0e-20;
249 raw_objf +=
Log(class_sum);
258 grad->
Row(k).AddVec(p/class_sum - p, x);
260 grad->
Row(k).AddVec(-1.0 * p, x);
265 grad->
Scale(1.0/ys.size());
267 raw_objf /= ys.size();
268 BaseFloat regularizer = - 0.5 * normalizer
270 KALDI_VLOG(2) <<
"Objf is " << raw_objf <<
" + " << regularizer
271 <<
" = " << (raw_objf + regularizer);
272 return raw_objf + regularizer;
276 const std::vector<int32> classes) {
279 class_.resize(classes.size());
293 WriteToken(os, binary,
"<LogisticRegression>");
298 WriteToken(os, binary,
"</LogisticRegression>");
307 if (token ==
"<class>") {
311 for (
int32 i = 0;
i < num_classes;
i++) {
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
void DoStep(Real function_value, const VectorBase< Real > &gradient)
The user calls this function to provide the class with the function and gradient info at the point Ge...
void Write(std::ostream &out, bool binary) const
write to stream.
BaseFloat GetObjfAndGrad(const Matrix< BaseFloat > &xs, const std::vector< int32 > &ys, const Matrix< BaseFloat > &xw, Matrix< BaseFloat > *grad, BaseFloat normalizer)
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
void TrainParameters(const Matrix< BaseFloat > &xs, const std::vector< int32 > &ys, const LogisticRegressionConfig &conf, Matrix< BaseFloat > *xw)
void Write(std::ostream &os, bool binary) const
void AddMat(const Real alpha, const MatrixBase< Real > &M, MatrixTransposeType transA=kNoTrans)
*this += alpha * M [or M^T]
void ReadToken(std::istream &is, bool binary, std::string *str)
ReadToken gets the next token and puts it in str (exception on failure).
const VectorBase< Real > & GetValue(Real *objf_value=NULL) const
This returns the value of the variable x that has the best objective function so far, and the corresponding objective function value if requested.
void Train(const Matrix< BaseFloat > &xs, const std::vector< int32 > &ys, const LogisticRegressionConfig &conf)
void ApplyLog()
Apply natural log to all elements.
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 LogSumExp(Real prune=-1.0) const
Returns log(sum(exp())) without exp overflow If prune > 0.0, ignores terms less than the max - prune...
void GetSplitTargets(const Vector< BaseFloat > &state_occs, int32 target_components, BaseFloat power, BaseFloat min_count, std::vector< int32 > *targets)
Get Gaussian-mixture or substate-mixture splitting targets, according to a power rule (e...
void GetLogPosteriors(const Matrix< BaseFloat > &xs, Matrix< BaseFloat > *log_posteriors)
void CopyFromVec(const VectorBase< Real > &v)
Copy data from another vector (must match own size).
void Read(std::istream &in, bool binary, bool add=false)
read from stream.
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
void ReadIntegerVector(std::istream &is, bool binary, std::vector< T > *v)
Function for reading STL vector of integer types.
void Scale(Real alpha)
Multiply each element with a scalar value.
void ExpectToken(std::istream &is, bool binary, const char *token)
ExpectToken tries to read in the given token, and throws an exception on failure. ...
void AddMatMat(const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const Real beta)
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 WriteToken(std::ostream &os, bool binary, const char *token)
The WriteToken functions are for writing nonempty sequences of non-space characters.
MatrixIndexT Dim() const
Returns the dimension of the vector.
void SetZero()
Sets matrix to zero.
void AddMatVec(const Real alpha, const MatrixBase< Real > &M, const MatrixTransposeType trans, const VectorBase< Real > &v, const Real beta)
Add matrix times vector : this <– beta*this + alpha*M*v.
void SetRandn()
Set vector to random normally-distributed noise.
double LogAdd(double x, double y)
void ScalePriors(const Vector< BaseFloat > &prior_scales)
A class representing a vector.
void SetWeights(const Matrix< BaseFloat > &weights, const std::vector< int32 > classes)
#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 Read(std::istream &is, bool binary)
void WriteIntegerVector(std::ostream &os, bool binary, const std::vector< T > &v)
Function for writing STL vectors of integer types.
void CopyRowsFromMat(const MatrixBase< Real > &M)
Performs a row stack of the matrix M.
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).
std::vector< int32 > class_
This is an implementation of L-BFGS.
Matrix< BaseFloat > weights_
void Add(Real c)
Add a constant to each element of a vector.
void CopyRowsFromVec(const VectorBase< Real > &v)
This function has two modes of operation.
Sub-matrix representation.
Represents a non-allocating general vector which can be defined as a sub-vector of higher-level vecto...
const VectorBase< Real > & GetProposedValue() const
This returns the value at which the function wants us to compute the objective function and gradient...
void MixUp(const std::vector< int32 > &ys, const int32 &num_classes, const LogisticRegressionConfig &conf)
void Set(Real)
Sets all elements to a specific value.
BaseFloat DoStep(const Matrix< BaseFloat > &xs, Matrix< BaseFloat > *xw, const std::vector< int32 > &ys, OptimizeLbfgs< BaseFloat > *lbfgs, BaseFloat normalizer)