29 const std::vector<Nnet> &nnets,
31 int32 num_nnets = nnets.size();
33 int32 num_uc = nnets[0].NumUpdatableComponents();
40 for (
int32 n = 1;
n < num_nnets;
n++) {
42 dest->
AddNnet(scale_params_n, nnets[
n]);
50 const std::vector<NnetExample> &validation_set,
51 const std::vector<Nnet> &nnets) {
52 int32 minibatch_size = 1024;
53 int32 num_nnets =
static_cast<int32>(nnets.size());
55 BaseFloat tot_frames = validation_set.size();
57 BaseFloat best_objf = -std::numeric_limits<BaseFloat>::infinity();
59 for (
int32 n = 0;
n < num_nnets;
n++) {
61 minibatch_size) / tot_frames;
63 if (n == 0 || objf > best_objf) {
69 KALDI_LOG <<
"Objective functions for the source neural nets are " << objfs;
71 int32 num_uc = nnets[0].NumUpdatableComponents();
75 scale_params.
Set(1.0 / num_nnets);
79 minibatch_size) / tot_frames;
80 KALDI_LOG <<
"Objf with all neural nets averaged is " << objf;
81 if (objf > best_objf) {
93 const std::vector<NnetExample> &validation_set,
94 const std::vector<Nnet> &nnets,
98 num_nnets =
static_cast<int32>(nnets.size());
99 if (initial_model < 0 || initial_model > num_nnets)
102 KALDI_ASSERT(initial_model >= 0 && initial_model <= num_nnets);
103 int32 num_uc = nnets[0].NumUpdatableComponents();
105 scale_params->
Resize(num_uc * num_nnets);
106 if (initial_model < num_nnets) {
107 KALDI_LOG <<
"Initializing with neural net with index " << initial_model;
109 scale_params->
Set(0.0);
116 KALDI_LOG <<
"Initializing with all neural nets averaged.";
117 scale_params->
Set(1.0 / num_nnets);
125 const std::vector<NnetExample> &validation_set,
127 const std::vector<Nnet> &nnets,
134 CombineNnets(scale_params_float, nnets, &nnet_combined);
136 Nnet nnet_gradient(nnet_combined);
137 bool is_gradient =
true;
138 nnet_gradient.
SetZero(is_gradient);
141 int32 batch_size = 1024;
147 double tot_frames = validation_set.size();
148 if (gradient != NULL) {
150 for (
int32 n = 0; n < static_cast<int32>(nnets.size());
n++) {
157 double dotprod = uc->
DotProduct(*uc_gradient) / tot_frames;
158 (*gradient)(
i) = dotprod;
167 KALDI_LOG <<
"Double-checking gradient computation";
171 double delta = 1.0e-04, fg = fabs((*gradient)(
i));
172 if (fg < 1.0e-07) fg = 1.0e-07;
173 if (fg * delta < 1.0e-05)
174 delta = 1.0e-05 / fg;
177 scale_params_temp(
i) += delta;
183 manual_gradient(
i) = (new_ans - ans) / delta;
185 KALDI_LOG <<
"Manually computed gradient is " << manual_gradient;
186 KALDI_LOG <<
"Gradient we computed is " << *gradient;
194 const std::vector<NnetExample> &validation_set,
195 const std::vector<Nnet> &nnets,
209 double objf, initial_objf;
213 lbfgs_options.
m = dim;
228 KALDI_VLOG(2) <<
"Iteration " <<
i <<
" scale-params = " << scale_params
229 <<
", objf = " << objf <<
", gradient = " << gradient;
231 if (
i == 0) initial_objf = objf;
233 lbfgs.
DoStep(objf, gradient);
240 KALDI_LOG <<
"Combining nnets, validation objf per frame changed from " 241 << initial_objf <<
" to " << objf;
244 nnets[0].NumUpdatableComponents());
246 KALDI_LOG <<
"Final scale factors are " << scale_params_mat;
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
const Component & GetComponent(int32 c) const
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 AddNnet(const VectorBase< BaseFloat > &scales, const Nnet &other)
For each updatatable component, adds to it the corresponding element of "other" times the appropriate...
double ComputeNnetGradient(const Nnet &nnet, const std::vector< NnetExample > &validation_set, int32 batch_size, Nnet *gradient)
ComputeNnetGradient is mostly used to compute gradients on validation sets; it divides the example in...
static void GetInitialScaleParams(const NnetCombineConfig &combine_config, const std::vector< NnetExample > &validation_set, const std::vector< Nnet > &nnets, Vector< double > *scale_params)
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.
static int32 GetInitialModel(const std::vector< NnetExample > &validation_set, const std::vector< Nnet > &nnets)
Returns an integer saying which model to use: either 0 ...
void Resize(MatrixIndexT length, MatrixResizeType resize_type=kSetZero)
Set vector to a specified size (can be zero).
double ComputeNnetObjf(const Nnet &nnet, const std::vector< NnetExample > &examples, double *tot_accuracy)
Computes objective function over a minibatch.
int32 NumComponents() const
Returns number of components– think of this as similar to # of layers, but e.g.
void CopyFromVec(const VectorBase< Real > &v)
Copy data from another vector (must match own size).
void SetZero(bool treat_as_gradient)
virtual BaseFloat DotProduct(const UpdatableComponent &other) const =0
Here, "other" is a component of the same specific type.
MatrixIndexT Dim() const
Returns the dimension of the vector.
Configuration class that controls neural net combination, where we combine a number of neural nets...
void ScaleComponents(const VectorBase< BaseFloat > &scales)
Scales the parameters of each of the updatable components.
A class representing a vector.
#define KALDI_ASSERT(cond)
void Set(Real f)
Set all members of a vector to a specified value.
static void CombineNnets(const Vector< BaseFloat > &scale_params, const std::vector< Nnet > &nnets, Nnet *dest)
This is an implementation of L-BFGS.
void CopyRowsFromVec(const VectorBase< Real > &v)
This function has two modes of operation.
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...
int32 NumUpdatableComponents(const Nnet &dest)
Returns the number of updatable components in the nnet.
Class UpdatableComponent is a Component which has trainable parameters and contains some global param...
static BaseFloat ComputeObjfAndGradient(const std::vector< NnetExample > &validation_set, const Vector< double > &scale_params, const Nnet &orig_nnet, const Nnet &direction, Vector< double > *gradient)