41 token.erase(token.length()-1);
44 KALDI_ERR <<
"Unknown component type " << token;
45 ans->
Read(is, binary);
53 if (component_type ==
"SigmoidComponent") {
55 }
else if (component_type ==
"TanhComponent") {
57 }
else if (component_type ==
"PowerComponent") {
59 }
else if (component_type ==
"SoftmaxComponent") {
61 }
else if (component_type ==
"LogSoftmaxComponent") {
63 }
else if (component_type ==
"RectifiedLinearComponent") {
65 }
else if (component_type ==
"NormalizeComponent") {
67 }
else if (component_type ==
"SoftHingeComponent") {
69 }
else if (component_type ==
"PnormComponent") {
71 }
else if (component_type ==
"MaxoutComponent") {
73 }
else if (component_type ==
"ScaleComponent") {
75 }
else if (component_type ==
"AffineComponent") {
77 }
else if (component_type ==
"AffineComponentPreconditioned") {
79 }
else if (component_type ==
"AffineComponentPreconditionedOnline") {
81 }
else if (component_type ==
"SumGroupComponent") {
83 }
else if (component_type ==
"BlockAffineComponent") {
85 }
else if (component_type ==
"BlockAffineComponentPreconditioned") {
87 }
else if (component_type ==
"PermuteComponent") {
89 }
else if (component_type ==
"DctComponent") {
91 }
else if (component_type ==
"FixedLinearComponent") {
93 }
else if (component_type ==
"FixedAffineComponent") {
95 }
else if (component_type ==
"FixedScaleComponent") {
97 }
else if (component_type ==
"FixedBiasComponent") {
99 }
else if (component_type ==
"SpliceComponent") {
101 }
else if (component_type ==
"SpliceMaxComponent") {
103 }
else if (component_type ==
"DropoutComponent") {
105 }
else if (component_type ==
"AdditiveNoiseComponent") {
107 }
else if (component_type ==
"Convolutional1dComponent") {
109 }
else if (component_type ==
"MaxpoolingComponent") {
117 std::istringstream istr(initializer_line);
118 std::string component_type;
119 istr >> component_type >> std::ws;
120 std::string rest_of_line;
121 getline(istr, rest_of_line);
124 KALDI_ERR <<
"Bad initializer line (no such type of Component): " 136 const std::string &token1,
137 const std::string &token2) {
141 if (temp == token1) {
144 if (temp != token2) {
145 KALDI_ERR <<
"Expecting token " << token1 <<
" or " << token2
146 <<
" but got " << temp;
155 std::vector<std::string> split_string;
158 std::string name_equals = name +
"=";
159 size_t len = name_equals.length();
161 for (
size_t i = 0;
i < split_string.size();
i++) {
162 if (split_string[
i].compare(0, len, name_equals) == 0) {
164 KALDI_ERR <<
"Bad option " << split_string[
i];
167 for (
size_t j = 0;
j < split_string.size();
j++) {
169 if (!string->empty()) *
string +=
" ";
170 *
string += split_string[
j];
181 std::vector<std::string> split_string;
184 std::string name_equals = name +
"=";
185 size_t len = name_equals.length();
187 for (
size_t i = 0;
i < split_string.size();
i++) {
188 if (split_string[
i].compare(0, len, name_equals) == 0) {
189 std::string b = split_string[
i].substr(len);
191 KALDI_ERR <<
"Bad option " << split_string[
i];
192 if (b[0] ==
'f' || b[0] ==
'F') *param =
false;
193 else if (b[0] ==
't' || b[0] ==
'T') *param =
true;
195 KALDI_ERR <<
"Bad option " << split_string[
i];
198 for (
size_t j = 0;
j < split_string.size();
j++) {
200 if (!string->empty()) *
string +=
" ";
201 *
string += split_string[
j];
212 std::vector<std::string> split_string;
215 std::string name_equals = name +
"=";
216 size_t len = name_equals.length();
218 for (
size_t i = 0;
i < split_string.size();
i++) {
219 if (split_string[
i].compare(0, len, name_equals) == 0) {
221 KALDI_ERR <<
"Bad option " << split_string[
i];
224 for (
size_t j = 0;
j < split_string.size();
j++) {
226 if (!string->empty()) *
string +=
" ";
227 *
string += split_string[
j];
237 std::string *param) {
238 std::vector<std::string> split_string;
241 std::string name_equals = name +
"=";
242 size_t len = name_equals.length();
244 for (
size_t i = 0;
i < split_string.size();
i++) {
245 if (split_string[
i].compare(0, len, name_equals) == 0) {
246 *param = split_string[
i].substr(len);
250 for (
size_t j = 0;
j < split_string.size();
j++) {
252 if (!string->empty()) *
string +=
" ";
253 *
string += split_string[
j];
263 std::vector<int32> *param) {
264 std::vector<std::string> split_string;
267 std::string name_equals = name +
"=";
268 size_t len = name_equals.length();
270 for (
size_t i = 0;
i < split_string.size();
i++) {
271 if (split_string[
i].compare(0, len, name_equals) == 0) {
274 KALDI_ERR <<
"Bad option " << split_string[
i];
277 for (
size_t j = 0;
j < split_string.size();
j++) {
279 if (!string->empty()) *
string +=
" ";
280 *
string += split_string[
j];
298 std::vector<int32> indexes(reorder);
299 std::sort(indexes.begin(), indexes.end());
300 for (
int32 i = 0; i < static_cast<int32>(indexes.size());
i++)
306 std::stringstream stream;
313 std::stringstream stream;
315 <<
", output-dim=" <<
OutputDim() <<
", learning-rate=" 324 value_sum_.Resize(dim);
325 deriv_sum_.Resize(dim);
333 if (value_sum_.Dim() !=
InputDim() ||
334 (deriv != NULL && deriv_sum_.
Dim() !=
InputDim())) {
335 std::lock_guard<std::mutex> lock(mutex_);
336 if (value_sum_.Dim() !=
InputDim()) {
340 if (deriv != NULL && deriv_sum_.
Dim() !=
InputDim()) {
343 value_sum_.SetZero();
349 value_sum_.AddVec(1.0, temp);
352 deriv_sum_.AddVec(1.0, temp);
357 value_sum_.Scale(scale);
358 deriv_sum_.Scale(scale);
371 count_ += alpha * other.
count_;
375 std::ostringstream ostr_beg, ostr_end;
376 ostr_beg <<
"<" <<
Type() <<
">";
377 ostr_end <<
"</" <<
Type() <<
">";
381 value_sum_.Read(is, binary);
383 deriv_sum_.Read(is, binary);
390 std::ostringstream ostr_beg, ostr_end;
391 ostr_beg <<
"<" <<
Type() <<
">";
392 ostr_end <<
"</" <<
Type() <<
">";
397 value_sum_.Write(os, binary);
399 deriv_sum_.Write(os, binary);
406 dim_(other.dim_), value_sum_(other.value_sum_), deriv_sum_(other.deriv_sum_),
407 count_(other.count_) { }
410 std::string orig_args(args);
413 if (!ok || !args.empty() || dim <= 0)
414 KALDI_ERR <<
"Invalid initializer for layer of type " 415 <<
Type() <<
": \"" << orig_args <<
"\"";
420 input_dim_ = input_dim;
421 output_dim_ = output_dim;
423 input_dim_ = 10 * output_dim_;
429 std::string orig_args(args);
431 int32 output_dim = 0;
434 KALDI_LOG << output_dim <<
" " << input_dim <<
" " << ok;
435 if (!ok || !args.empty() || output_dim <= 0)
436 KALDI_ERR <<
"Invalid initializer for layer of type " 437 <<
Type() <<
": \"" << orig_args <<
"\"";
438 Init(input_dim, output_dim);
482 std::stringstream stream;
483 stream <<
Type() <<
", input-dim = " << input_dim_
484 <<
", output-dim = " << output_dim_;
489 input_dim_ = input_dim;
490 output_dim_ = output_dim;
492 input_dim_ = 10 * output_dim_;
494 KALDI_ASSERT(input_dim_ > 0 && output_dim_ >= 0 && p_ >= 0);
499 std::string orig_args(args);
501 int32 output_dim = 0;
506 if (!ok || !args.empty() || output_dim <= 0)
507 KALDI_ERR <<
"Invalid initializer for layer of type " 508 <<
Type() <<
": \"" << orig_args <<
"\"";
509 Init(input_dim, output_dim, p);
558 std::stringstream stream;
559 stream <<
Type() <<
", input-dim = " << input_dim_
560 <<
", output-dim = " << output_dim_
637 in_deriv->
AddMat(-1.0, out_value);
642 if (to_update != NULL)
682 in_deriv->
Scale(-1.0);
686 if (to_update != NULL)
699 std::string orig_args(args);
707 if (!ok || !args.empty() || dim <= 0)
708 KALDI_ERR <<
"Invalid initializer for layer of type " 709 <<
Type() <<
": \"" << orig_args <<
"\"";
737 in_deriv->
Scale(power_);
763 std::stringstream stream;
764 stream <<
Type() <<
", dim = " <<
dim_ 765 <<
", power = " << power_;
793 if (to_update != NULL)
830 if (to_update != NULL)
856 in_deriv->
Scale(scale_);
867 std::string orig_args(args);
871 KALDI_ERR <<
"Dimension not specified for ScaleComponent in config file";
873 KALDI_ERR <<
"Scale not specified for ScaleComponent in config file";
895 std::stringstream stream;
896 stream <<
Type() <<
", dim=" <<
dim_ <<
", scale=" << scale_;
942 if (to_update != NULL) {
989 if (to_update != NULL) {
998 linear_params_.Scale(scale);
999 bias_params_.Scale(scale);
1005 bias_params_.Resize(output_dim);
1006 linear_params_.Resize(output_dim, input_dim);
1019 linear_params_(component.linear_params_),
1020 bias_params_(component.bias_params_),
1021 is_gradient_(component.is_gradient_) { }
1030 bias_params.
Dim() != 0);
1037 if (treat_as_gradient) {
1042 if (treat_as_gradient)
1064 std::stringstream stream;
1069 linear_params_size),
1074 <<
", linear-params-stddev=" << linear_stddev
1075 <<
", bias-params-stddev=" << bias_stddev
1077 return stream.str();
1102 KALDI_ASSERT(output_dim > 0 && input_dim > 0 && param_stddev >= 0.0);
1110 std::string matrix_filename) {
1123 std::string orig_args(args);
1126 std::string matrix_filename;
1127 int32 input_dim = -1, output_dim = -1;
1130 Init(learning_rate, matrix_filename);
1133 "input-dim mismatch vs. matrix.");
1136 "output-dim mismatch vs. matrix.");
1140 BaseFloat param_stddev = 1.0 / std::sqrt(input_dim),
1144 Init(learning_rate, input_dim, output_dim,
1145 param_stddev, bias_stddev);
1148 KALDI_ERR <<
"Could not process these elements in initializer: " 1151 KALDI_ERR <<
"Bad initializer " << orig_args;
1189 if (to_update != NULL) {
1195 to_update->
Update(in_value, out_deriv);
1200 std::ostringstream ostr_beg, ostr_end;
1201 ostr_beg <<
"<" <<
Type() <<
">";
1202 ostr_end <<
"</" <<
Type() <<
">";
1214 if (tok ==
"<AvgInput>") {
1216 avg_input.
Read(is, binary);
1222 if (tok ==
"<IsGradient>") {
1232 std::ostringstream ostr_beg, ostr_end;
1233 ostr_beg <<
"<" <<
Type() <<
">";
1234 ostr_end <<
"</" <<
Type() <<
">";
1279 << rc_min <<
" to " << d <<
", SVD sum reduced from " 1280 << old_svd_sum <<
" to " << new_svd_sum;
1289 (*a)->linear_params_ = Vt;
1292 (*b)->linear_params_ = U;
1307 this->linear_params_,
kNoTrans, 0.0);
1309 this->bias_params_, 1.0);
1323 this->linear_params_,
kNoTrans, 0.0);
1325 this->bias_params_, 1.0);
1335 ans->linear_params_.MulRowsVec(next_component.
scales_);
1336 ans->bias_params_.MulElements(next_component.
scales_);
1361 std::ostringstream ostr_beg, ostr_end;
1362 ostr_beg <<
"<" <<
Type() <<
">";
1363 ostr_end <<
"</" <<
Type() <<
">";
1381 if (tok ==
"<MaxChange>") {
1391 std::string orig_args(args);
1392 std::string matrix_filename;
1394 BaseFloat alpha = 0.1, max_change = 0.0;
1395 int32 input_dim = -1, output_dim = -1;
1401 Init(learning_rate, alpha, max_change, matrix_filename);
1404 "input-dim mismatch vs. matrix.");
1407 "output-dim mismatch vs. matrix.");
1412 BaseFloat param_stddev = 1.0 / std::sqrt(input_dim),
1417 KALDI_ERR <<
"Bad initializer " << orig_args;
1418 Init(learning_rate, input_dim, output_dim, param_stddev,
1419 bias_stddev, alpha, max_change);
1422 KALDI_ERR <<
"Could not process these elements in initializer: " 1428 std::string matrix_filename) {
1431 max_change_ = max_change;
1451 KALDI_ASSERT(output_dim > 0 && input_dim > 0 && param_stddev >= 0.0);
1458 max_change_ = max_change;
1464 std::ostringstream ostr_beg, ostr_end;
1465 ostr_beg <<
"<" <<
Type() <<
">";
1466 ostr_end <<
"</" <<
Type() <<
">";
1482 std::stringstream stream;
1487 linear_params_size),
1492 <<
", linear-params-stddev=" << linear_stddev
1493 <<
", bias-params-stddev=" << bias_stddev
1495 <<
", alpha=" << alpha_
1496 <<
", max-change=" << max_change_;
1497 return stream.str();
1515 static int scaling_factor_printed = 0;
1519 out_deriv_norm(in_value_precon.
NumRows());
1521 out_deriv_norm.AddDiagMat2(1.0, out_deriv_precon,
kNoTrans, 0.0);
1523 in_norm.ApplyPow(0.5);
1524 out_deriv_norm.ApplyPow(0.5);
1531 if (sum <= max_change_)
return 1.0;
1534 if (scaling_factor_printed < 10) {
1535 KALDI_LOG <<
"Limiting step size to " << max_change_
1536 <<
" using scaling factor " << ans <<
", for component index " 1538 scaling_factor_printed++;
1552 0, in_value.
NumCols()).CopyFromMat(in_value);
1556 in_value.
NumCols(), 1).Set(1.0);
1560 out_deriv_precon(out_deriv.
NumRows(),
1572 if (max_change_ > 0.0)
1573 minibatch_scale = GetScalingFactor(in_value_precon, out_deriv_precon);
1577 0, in_value_precon.NumRows(),
1578 0, in_value_precon.NumCols() - 1);
1583 precon_ones.
CopyColFromMat(in_value_precon, in_value_precon.NumCols() - 1);
1589 in_value_precon_part,
kNoTrans, 1.0);
1597 if (rank_in_ >= input_dim) rank_in_ = input_dim - 1;
1598 if (rank_out_ >= output_dim) rank_out_ = output_dim - 1;
1602 preconditioner_in_ = temp;
1603 preconditioner_out_ = temp;
1604 SetPreconditionerConfigs();
1609 std::ostringstream ostr_beg, ostr_end;
1610 ostr_beg <<
"<" <<
Type() <<
">";
1611 ostr_end <<
"</" <<
Type() <<
">";
1622 if (tok ==
"<Rank>") {
1624 rank_out_ = rank_in_;
1632 if (tok ==
"<UpdatePeriod>") {
1645 SetPreconditionerConfigs();
1649 std::string orig_args(args);
1651 std::string matrix_filename;
1653 BaseFloat num_samples_history = 2000.0, alpha = 4.0,
1654 max_change_per_sample = 0.1;
1655 int32 input_dim = -1, output_dim = -1, rank_in = 30, rank_out = 80,
1660 ParseFromString(
"max-change-per-sample", &args, &max_change_per_sample);
1666 Init(learning_rate, rank_in, rank_out, update_period,
1667 num_samples_history, alpha, max_change_per_sample,
1671 "input-dim mismatch vs. matrix.");
1674 "output-dim mismatch vs. matrix.");
1678 BaseFloat param_stddev = 1.0 / std::sqrt(input_dim),
1682 Init(learning_rate, input_dim, output_dim, param_stddev,
1683 bias_stddev, rank_in, rank_out, update_period,
1684 num_samples_history, alpha, max_change_per_sample);
1687 KALDI_ERR <<
"Could not process these elements in initializer: " 1690 KALDI_ERR <<
"Bad initializer " << orig_args;
1694 preconditioner_in_.SetRank(rank_in_);
1695 preconditioner_in_.SetNumSamplesHistory(num_samples_history_);
1696 preconditioner_in_.SetAlpha(alpha_);
1697 preconditioner_in_.SetUpdatePeriod(update_period_);
1698 preconditioner_out_.SetRank(rank_out_);
1699 preconditioner_out_.SetNumSamplesHistory(num_samples_history_);
1700 preconditioner_out_.SetAlpha(alpha_);
1701 preconditioner_out_.SetUpdatePeriod(update_period_);
1708 std::string matrix_filename) {
1711 rank_out_ = rank_out;
1712 update_period_ = update_period;
1713 num_samples_history_ = num_samples_history;
1715 SetPreconditionerConfigs();
1717 max_change_per_sample_ = max_change_per_sample;
1732 max_change_per_sample_(0.1) {
1755 KALDI_ASSERT(output_dim > 0 && input_dim > 0 && param_stddev >= 0.0 &&
1756 bias_stddev >= 0.0);
1773 std::ostringstream ostr_beg, ostr_end;
1774 ostr_beg <<
"<" <<
Type() <<
">";
1775 ostr_end <<
"</" <<
Type() <<
">";
1789 WriteToken(os, binary,
"<NumSamplesHistory>");
1793 WriteToken(os, binary,
"<MaxChangePerSample>");
1799 std::stringstream stream;
1804 linear_params_size),
1809 <<
", linear-params-stddev=" << linear_stddev
1810 <<
", bias-params-stddev=" << bias_stddev
1818 return stream.str();
1845 static int scaling_factor_printed = 0;
1846 int32 minibatch_size = in_products.
Dim();
1855 KALDI_ASSERT(tot_change_norm - tot_change_norm == 0.0 &&
"NaN in backprop");
1857 if (tot_change_norm <= max_change_norm)
return 1.0;
1859 BaseFloat factor = max_change_norm / tot_change_norm;
1860 if (scaling_factor_printed < 10) {
1861 KALDI_LOG <<
"Limiting step size using scaling factor " 1862 << factor <<
", for component index " <<
Index();
1863 scaling_factor_printed++;
1877 0, in_value.
NumCols()).CopyFromMat(in_value);
1881 in_value.
NumCols(), 1).Set(1.0);
1888 out_row_products(row_products, 1);
1911 0, in_value_temp.
NumCols() - 1);
1922 in_value_precon_part,
kNoTrans, 1.0);
1926 if (treat_as_gradient) {
2000 for (
int32 b = 0; b < num_blocks_; b++) {
2002 b * input_block_dim, input_block_dim),
2003 out_block(*out, 0, num_frames,
2004 b * output_block_dim, output_block_dim),
2006 b * output_block_dim, output_block_dim,
2007 0, input_block_dim);
2008 out_block.AddMatMat(1.0, in_block,
kNoTrans, param_block,
kTrans, 1.0);
2017 num_frames = in_value.
NumRows();
2020 for (
int32 b = 0; b < num_blocks_; b++) {
2022 b * input_block_dim,
2024 out_deriv_block(out_deriv, 0, num_frames,
2025 b * output_block_dim, output_block_dim),
2027 b * output_block_dim, output_block_dim,
2028 0, input_block_dim);
2053 for (
int32 b = 0; b < num_blocks_; b++) {
2055 b * input_block_dim,
2057 in_deriv_block(*in_deriv, 0, num_frames,
2058 b * input_block_dim, input_block_dim),
2059 out_deriv_block(out_deriv, 0, num_frames,
2060 b * output_block_dim, output_block_dim),
2062 b * output_block_dim, output_block_dim,
2063 0, input_block_dim);
2066 in_deriv_block.AddMatMat(1.0, out_deriv_block,
kNoTrans,
2069 if (to_update != NULL)
2070 to_update->
Update(in_value, out_deriv);
2080 KALDI_ASSERT(output_dim > 0 && input_dim > 0 && param_stddev >= 0.0);
2081 KALDI_ASSERT(input_dim % num_blocks == 0 && output_dim % num_blocks == 0);
2090 num_blocks_ = num_blocks;
2094 std::string orig_args(args);
2097 int32 input_dim = -1, output_dim = -1, num_blocks = 1;
2102 BaseFloat param_stddev = 1.0 / std::sqrt(input_dim),
2107 KALDI_ERR <<
"Could not process these elements in initializer: " 2110 KALDI_ERR <<
"Bad initializer " << orig_args;
2111 Init(learning_rate, input_dim, output_dim,
2112 param_stddev, bias_stddev, num_blocks);
2125 ExpectToken(is, binary,
"</BlockAffineComponent>");
2129 WriteToken(os, binary,
"<BlockAffineComponent>");
2138 WriteToken(os, binary,
"</BlockAffineComponent>");
2168 param_stddev, bias_stddev, num_blocks);
2175 std::string orig_args(args);
2179 int32 input_dim = -1, output_dim = -1, num_blocks = 1;
2186 BaseFloat param_stddev = 1.0 / std::sqrt(input_dim),
2191 KALDI_ERR <<
"Could not process these elements in initializer: " 2194 KALDI_ERR <<
"Bad initializer " << orig_args;
2195 Init(learning_rate, input_dim, output_dim,
2196 param_stddev, bias_stddev, num_blocks,
2201 if (treat_as_gradient)
2220 ExpectToken(is, binary,
"</BlockAffineComponentPreconditioned>");
2224 bool binary)
const {
2225 WriteToken(os, binary,
"<BlockAffineComponentPreconditioned>");
2238 WriteToken(os, binary,
"</BlockAffineComponentPreconditioned>");
2263 num_frames = in_value.
NumRows();
2266 in_value_precon(num_frames, input_block_dim + 1,
kUndefined);
2267 in_value_temp.Set(1.0);
2269 0, input_block_dim);
2271 0, input_block_dim);
2275 for (
int32 b = 0; b < num_blocks_; b++) {
2277 b * input_block_dim,
2279 out_deriv_block(out_deriv, 0, num_frames,
2280 b * output_block_dim, output_block_dim),
2282 b * output_block_dim, output_block_dim,
2283 0, input_block_dim);
2294 in_value_precon_part,
kNoTrans, 1.0);
2296 bias_params_.Range(b * output_block_dim, output_block_dim).
2310 WriteToken(os, binary,
"<PermuteComponent>");
2313 WriteToken(os, binary,
"</PermuteComponent>");
2318 reorder_.resize(dim);
2319 for (
int32 i = 0;
i < dim;
i++) reorder_[
i] =
i;
2320 std::random_shuffle(reorder_.begin(), reorder_.end());
2324 std::string orig_args(args);
2327 if (!ok || !args.empty() || dim <= 0)
2328 KALDI_ERR <<
"Invalid initializer for layer of type " 2329 <<
Type() <<
": \"" << orig_args <<
"\"";
2341 std::vector<int32> reverse_reorder(reorder_.size());
2342 for (
size_t i = 0;
i < reorder_.size();
i++)
2343 reverse_reorder[reorder_[
i]] =
i;
2347 out->
CopyCols(in, cu_reverse_reorder);
2362 in_deriv->
CopyCols(out_deriv, cu_reorder);
2367 std::vector<Int32Pair> cpu_vec(sizes.size());
2368 std::vector<int32> reverse_cpu_vec;
2369 int32 cur_index = 0;
2370 for (
size_t i = 0;
i < sizes.size();
i++) {
2372 cpu_vec[
i].first = cur_index;
2373 cpu_vec[
i].second = cur_index + sizes[
i];
2374 cur_index += sizes[
i];
2375 for (
int32 j = cpu_vec[
i].first;
j < cpu_vec[
i].second;
j++)
2376 reverse_cpu_vec.push_back(
i);
2378 this->indexes_ = cpu_vec;
2379 this->reverse_indexes_ = reverse_cpu_vec;
2380 this->input_dim_ = cur_index;
2381 this->output_dim_ = sizes.size();
2385 std::string orig_args(args);
2386 std::vector<int32> sizes;
2389 if (!ok || !args.empty() || sizes.empty())
2390 KALDI_ERR <<
"Invalid initializer for layer of type " 2391 <<
Type() <<
": \"" << orig_args <<
"\"";
2406 std::vector<int32> sizes;
2411 if (!(token ==
"<SumGroupComponent>" ||
2412 token ==
"</SumGroupComponent>")) {
2413 KALDI_ERR <<
"Expected </SumGroupComponent>, got " << token;
2419 std::vector<Int32Pair> indexes;
2420 indexes_.CopyToVec(&indexes);
2421 sizes->resize(indexes.size());
2422 for (
size_t i = 0;
i < indexes.size();
i++) {
2423 (*sizes)[
i] = indexes[
i].second - indexes[
i].first;
2427 (*sizes)[
i] = indexes[
i].second - indexes[
i].first;
2432 WriteToken(os, binary,
"<SumGroupComponent>");
2434 std::vector<int32> sizes;
2435 this->GetSizes(&sizes);
2437 WriteToken(os, binary,
"</SumGroupComponent>");
2459 in_deriv->
CopyCols(out_deriv, reverse_indexes_);
2464 std::stringstream stream;
2465 std::ostringstream os;
2466 std::copy(context_.begin(), context_.end(),
2467 std::ostream_iterator<int32>(os,
" "));
2469 if (const_component_dim_ != 0)
2470 stream <<
", const_component_dim=" << const_component_dim_;
2472 return stream.str();
2476 int32 const_component_dim) {
2477 input_dim_ = input_dim;
2478 const_component_dim_ = const_component_dim;
2481 KALDI_ASSERT(input_dim_ > 0 && context_.front() <= 0 && context_.back() >= 0);
2483 KALDI_ASSERT(const_component_dim_ >= 0 && const_component_dim_ < input_dim_);
2489 std::string orig_args(args);
2490 int32 input_dim, left_context, right_context;
2491 std::vector <int32> context;
2498 int32 const_component_dim = 0;
2501 if (!(in_dim_ok && (context_ok || left_right_context_ok)) ||
2502 !args.empty() || input_dim <= 0)
2503 KALDI_ERR <<
"Invalid initializer for layer of type " 2504 <<
Type() <<
": \"" << orig_args <<
"\"";
2505 if (left_right_context_ok) {
2507 for (
int32 i = -left_context;
i <= right_context;
i++)
2508 context.push_back(
i);
2510 Init(input_dim, context, const_component_dim);
2514 return (input_dim_ - const_component_dim_)
2516 + const_component_dim_;
2520 if (offsets_.empty()) {
2521 KALDI_ASSERT((offset <= last_offset_) && (offset >= first_offset_));
2522 return offset - first_offset_;
2524 std::vector<int32>::const_iterator iter =
2525 std::lower_bound(offsets_.begin(), offsets_.end(), offset);
2527 KALDI_ASSERT(iter != offsets_.end() && *iter == offset);
2528 return static_cast<int32>(iter - offsets_.begin());
2533 if (offsets_.empty()) {
2534 int32 offset = index + first_offset_;
2535 KALDI_ASSERT((offset <= last_offset_) && (offset >= first_offset_));
2538 KALDI_ASSERT((index >= 0) && (index < offsets_.size()));
2539 return offsets_[index];
2547 if (! offsets_.empty()) {
2549 (last_offset_ == offsets_.back()));
2551 KALDI_ASSERT((first_offset_ >= 0) && (last_offset_ >= first_offset_));
2553 KALDI_ASSERT ( last_offset_ - first_offset_ + 1 > offsets_.size() );
2591 input_dim = in_info.
NumCols();
2593 if (out_chunk_size <= 0)
2594 KALDI_ERR <<
"Splicing features: output will have zero dimension. " 2595 <<
"Probably a code error.";
2599 int32 num_splice = context_.size();
2600 std::vector<std::vector<int32> > indexes(num_splice);
2601 for (
int32 c = 0; c < num_splice; c++)
2602 indexes[c].resize(out->
NumRows());
2606 int32 const_dim = const_component_dim_;
2607 std::vector<int32> const_indexes(const_dim == 0 ? 0 : out->
NumRows());
2613 for (
int32 c = 0; c < num_splice; c++) {
2614 for (
int32 out_index = 0; out_index < out_chunk_size; out_index++) {
2616 int32 in_index = in_info.
GetIndex(out_offset + context_[c]);
2617 indexes[c][chunk * out_chunk_size + out_index] =
2618 chunk * in_chunk_size + in_index;
2623 for (
int32 c = 0; c < num_splice; c++) {
2624 for (
int32 out_index = 0; out_index < out_chunk_size; out_index++) {
2625 int32 last_value = indexes[c][(chunk-1) * out_chunk_size + out_index];
2626 indexes[c][chunk * out_chunk_size + out_index] =
2627 (last_value == -1 ? -1 : last_value + in_chunk_size);
2631 if (const_dim != 0) {
2632 for (
int32 out_index = 0; out_index < out_chunk_size; out_index++)
2633 const_indexes[chunk * out_chunk_size + out_index] =
2634 chunk * in_chunk_size + out_index;
2641 for (
int32 c = 0; c < num_splice; c++) {
2642 int32 dim = input_dim - const_dim;
2646 out_part(*out, 0, out->
NumRows(),
2649 out_part.CopyRows(in_part, cu_indexes);
2651 if (const_dim != 0) {
2653 in.
NumCols() - const_dim, const_dim),
2654 out_part(*out, 0, out->
NumRows(),
2655 out->
NumCols() - const_dim, const_dim);
2658 out_part.CopyRows(in_part, cu_const_indexes);
2679 output_dim = out_deriv.
NumCols(),
2684 int32 num_splice = context_.size(),
2685 const_dim = const_component_dim_;
2690 std::vector<std::vector<int32> > indexes(num_splice);
2694 std::vector<int32> const_indexes(const_dim == 0 ? 0 : in_deriv->
NumRows(), -1);
2696 for (
int32 c = 0; c < indexes.size(); c++)
2697 indexes[c].resize(in_deriv->
NumRows(), -1);
2701 int32 dim = input_dim - const_dim;
2702 for (
int32 chunk = 0; chunk < num_chunks; chunk++) {
2705 for (
int32 c = 0; c < num_splice; c++) {
2706 for (
int32 out_index = 0; out_index < out_chunk_size; out_index++) {
2708 int32 in_index = in_info.
GetIndex(out_offset + context_[c]);
2709 indexes[c][chunk * in_chunk_size + in_index] =
2710 chunk * out_chunk_size + out_index;
2714 for (
int32 c = 0; c < num_splice; c++) {
2715 for (
int32 in_index = 0; in_index < in_chunk_size; in_index++) {
2716 int32 last_value = indexes[c][(chunk-1) * in_chunk_size + in_index];
2717 indexes[c][chunk * in_chunk_size + in_index] =
2718 (last_value == -1 ? -1 : last_value + out_chunk_size);
2724 if (const_dim != 0) {
2725 for (
int32 out_index = 0; out_index < out_chunk_size; out_index++) {
2726 const_indexes[chunk * in_chunk_size + out_index] =
2727 chunk * out_chunk_size + out_index;
2734 for (
int32 c = 0; c < num_splice; c++) {
2736 int32 dim = input_dim - const_dim;
2740 in_deriv_part(*in_deriv, 0, in_deriv->
NumRows(),
2743 in_deriv_part.
CopyRows(out_deriv_part, cu_indexes);
2745 temp_mat.CopyRows(out_deriv_part, cu_indexes);
2746 in_deriv_part.AddMat(1.0, temp_mat);
2749 if (const_dim != 0) {
2751 out_deriv.
NumCols() - const_dim,
2753 in_deriv_part(*in_deriv, 0, in_deriv->
NumRows(),
2754 in_deriv->
NumCols() - const_dim, const_dim);
2756 in_deriv_part.CopyRows(out_deriv_part, cu_const_indexes);
2773 if (token ==
"<LeftContext>") {
2774 int32 left_context=0, right_context=0;
2775 std::vector<int32> context;
2779 for (
int32 i = -1 * left_context;
i <= right_context;
i++)
2780 context.push_back(
i);
2782 }
else if (token ==
"<Context>") {
2786 <<
", the model might be corrupted";
2799 WriteToken(os, binary,
"<ConstComponentDim>");
2801 WriteToken(os, binary,
"</SpliceComponent>");
2806 std::stringstream stream;
2807 std::ostringstream os;
2808 std::copy(context_.begin(), context_.end(),
2809 std::ostream_iterator<int32>(os,
" "));
2811 return stream.str();
2815 std::vector<int32> context) {
2818 KALDI_ASSERT(dim_ > 0 && context_.front() <= 0 && context_.back() >= 0);
2824 std::string orig_args(args);
2825 int32 dim, left_context, right_context;
2826 std::vector <int32> context;
2830 &args, &left_context) &&
2834 if (!(dim_ok && (context_ok || left_right_context_ok)) ||
2835 !args.empty() || dim <= 0)
2836 KALDI_ERR <<
"Invalid initializer for layer of type " 2837 <<
Type() <<
": \"" << orig_args <<
"\"";
2838 if (left_right_context_ok) {
2840 for (
int32 i = -1 * left_context;
i <= right_context;
i++)
2841 context.push_back(
i);
2863 chunk * in_chunk_size, in_chunk_size,
2866 chunk * out_chunk_size,
2867 out_chunk_size, 0, dim);
2868 for (
int32 offset = 0; offset < context_.size(); offset++) {
2872 std::vector<int32> input_chunk_inds(out_chunk_size);
2873 for (
int32 i = 0;
i < out_chunk_size;
i++) {
2875 int32 out_chunk_offset =
2877 input_chunk_inds[
i] =
2878 in_info.
GetIndex(out_chunk_offset + context_[offset]);
2881 input_chunk_part.
CopyRows(input_chunk, cu_chunk_inds);
2885 output_chunk.
Max(input_chunk_part);
2913 chunk * in_chunk_size,
2916 in_value_chunk(in_value,
2917 chunk * in_chunk_size,
2920 out_deriv_chunk(out_deriv,
2921 chunk * out_chunk_size,
2924 for (
int32 r = 0; r < out_deriv_chunk.
NumRows(); r++) {
2925 int32 out_chunk_ind = r;
2926 int32 out_chunk_offset =
2929 for (
int32 c = 0; c < dim; c++) {
2930 int32 in_r_max = -1;
2931 BaseFloat max_input = -std::numeric_limits<BaseFloat>::infinity();
2932 for (
int32 context_ind = 0;
2933 context_ind < context_.size(); context_ind++) {
2935 in_info.
GetIndex(out_chunk_offset + context_[context_ind]);
2936 BaseFloat input = in_value_chunk(in_r, c);
2937 if (input > max_input) {
2943 (*in_deriv)(in_r_max, c) += out_deriv_chunk(r, c);
2951 ans->
Init(dim_, context_);
2960 if (token ==
"<LeftContext>") {
2961 int32 left_context = 0, right_context = 0;
2962 std::vector<int32> context;
2966 for (
int32 i = -1 * left_context;
i <= right_context;
i++)
2967 context.push_back(
i);
2969 }
else if (token ==
"<Context>") {
2972 KALDI_ERR <<
"Unknown token" << token <<
", the model might be corrupted";
2978 WriteToken(os, binary,
"<SpliceMaxComponent>");
2983 WriteToken(os, binary,
"</SpliceMaxComponent>");
2987 std::stringstream stream;
2989 if (dct_mat_.NumCols() != dct_mat_.NumRows())
2990 stream <<
", dct_keep_dim=" << dct_mat_.NumRows();
2992 return stream.str();
2996 int dct_keep_dim_ = (dct_keep_dim > 0) ? dct_keep_dim : dct_dim;
3002 dct_mat_.Resize(dct_keep_dim_, dct_dim);
3012 std::string orig_args(args);
3013 int32 dim, dct_dim, dct_keep_dim = 0;
3014 bool reorder =
false;
3021 if (!ok || !args.empty() || dim <= 0 || dct_dim <= 0 || dct_keep_dim < 0)
3022 KALDI_ERR <<
"Invalid initializer for layer of type " 3023 <<
Type() <<
": \"" << orig_args <<
"\"";
3024 Init(dim, dct_dim, reorder, dct_keep_dim);
3031 int32 dct_dim = dct_mat_.NumCols(),
3032 dct_keep_dim = dct_mat_.NumRows(),
3033 block_size_in = dim_ / dct_dim,
3034 block_size_out = dct_keep_dim;
3038 if (reverse)
std::swap(block_size_in, block_size_out);
3043 int32 num_blocks_in = block_size_out;
3044 for (
int32 b = 0; b < num_blocks_in; b++) {
3045 for (
int32 j = 0;
j < block_size_in;
j++) {
3046 temp(
j * block_size_out + b) = row(b * block_size_in +
j);
3058 int32 dct_dim = dct_mat_.NumCols(),
3059 dct_keep_dim = dct_mat_.NumRows(),
3061 num_chunks = dim_ / dct_dim;
3071 Reorder(&in_tmp,
false);
3074 for (
int32 chunk = 0; chunk < num_chunks; chunk++) {
3076 0, num_rows, dct_dim * chunk, dct_dim),
3078 0, num_rows, dct_keep_dim * chunk, dct_keep_dim);
3095 int32 dct_dim = dct_mat_.NumCols(),
3096 dct_keep_dim = dct_mat_.NumRows(),
3097 num_chunks = dim_ / dct_dim,
3098 num_rows = out_deriv.
NumRows();
3100 in_deriv->
Resize(num_rows, dim_);
3104 out_deriv_tmp = out_deriv;
3105 Reorder(&out_deriv_tmp,
false);
3107 for (
int32 chunk = 0; chunk < num_chunks; chunk++) {
3109 0, num_rows, dct_dim * chunk, dct_dim),
3110 out_deriv_mat(reorder_ ? out_deriv_tmp : out_deriv,
3111 0, num_rows, dct_keep_dim * chunk, dct_keep_dim);
3117 in_deriv_mat.AddMatMat(1.0, out_deriv_mat,
kNoTrans,
3121 Reorder(in_deriv,
true);
3137 int32 dct_dim = dct_mat_.NumCols();
3142 int32 dct_keep_dim = dct_mat_.NumRows();
3158 int32 dct_keep_dim = dct_dim;
3161 if (token ==
"<DctKeepDim>") {
3164 }
else if (token !=
"</DctComponent>") {
3165 KALDI_ERR <<
"Expected token \"</DctComponent>\", got instead \"" 3169 KALDI_ASSERT(dct_dim > 0 && dim_ > 0 && dim_ % dct_dim == 0);
3170 Init(dim_, dct_dim, reorder_, dct_keep_dim);
3176 std::string orig_args = args;
3177 std::string filename;
3180 if (!ok || !args.empty())
3181 KALDI_ERR <<
"Invalid initializer for layer of type " 3182 <<
Type() <<
": \"" << orig_args <<
"\"";
3185 Input ki(filename, &binary);
3194 std::stringstream stream;
3196 * static_cast<BaseFloat>(mat_.NumCols()),
3200 return stream.str();
3233 WriteToken(os, binary,
"<FixedLinearComponent>");
3235 mat_.Write(os, binary);
3236 WriteToken(os, binary,
"</FixedLinearComponent>");
3241 mat_.Read(is, binary);
3242 ExpectToken(is, binary,
"</FixedLinearComponent>");
3255 std::string orig_args = args;
3256 std::string filename;
3259 if (!ok || !args.empty())
3260 KALDI_ERR <<
"Invalid initializer for layer of type " 3261 <<
Type() <<
": \"" << orig_args <<
"\"";
3264 Input ki(filename, &binary);
3273 std::stringstream stream;
3276 linear_params_stddev =
3279 linear_params_size),
3283 stream <<
Component::Info() <<
", linear-params-stddev=" << linear_params_stddev
3284 <<
", bias-params-stddev=" << bias_params_stddev;
3285 return stream.str();
3320 WriteToken(os, binary,
"<FixedAffineComponent>");
3325 WriteToken(os, binary,
"</FixedAffineComponent>");
3333 ExpectToken(is, binary,
"</FixedAffineComponent>");
3343 std::string orig_args = args;
3344 std::string filename;
3347 if (!ok || !args.empty())
3348 KALDI_ERR <<
"Invalid initializer for layer of type " 3349 <<
Type() <<
": \"" << orig_args <<
"\"";
3358 std::stringstream stream;
3360 scales_mean = scales_.Sum() / scales_size,
3361 scales_stddev = std::sqrt(
VecVec(scales_, scales_) / scales_size
3362 - (scales_mean * scales_mean));
3364 <<
", scales-stddev=" << scales_stddev;
3365 return stream.str();
3383 *in_deriv = out_deriv;
3395 WriteToken(os, binary,
"<FixedScaleComponent>");
3397 scales_.Write(os, binary);
3398 WriteToken(os, binary,
"</FixedScaleComponent>");
3403 scales_.Read(is, binary);
3404 ExpectToken(is, binary,
"</FixedScaleComponent>");
3413 std::string orig_args = args;
3414 std::string filename;
3417 if (!ok || !args.empty())
3418 KALDI_ERR <<
"Invalid initializer for layer of type " 3419 <<
Type() <<
": \"" << orig_args <<
"\"";
3428 std::stringstream stream;
3430 bias_mean = bias_.Sum() / bias_size,
3431 bias_stddev = std::sqrt(
VecVec(bias_, bias_) / bias_size)
3432 - (bias_mean * bias_mean);
3434 <<
", bias-stddev=" << bias_stddev;
3435 return stream.str();
3453 *in_deriv = out_deriv;
3464 WriteToken(os, binary,
"<FixedBiasComponent>");
3466 bias_.Write(os, binary);
3467 WriteToken(os, binary,
"</FixedBiasComponent>");
3472 bias_.Read(is, binary);
3480 std::stringstream stream;
3482 << dropout_proportion_ <<
", dropout_scale = " 3484 return stream.str();
3488 std::string orig_args(args);
3490 BaseFloat dropout_proportion = 0.5, dropout_scale = 0.0;
3495 if (!ok || !args.empty() || dim <= 0)
3496 KALDI_ERR <<
"Invalid initializer for layer of type DropoutComponent: \"" 3497 << orig_args <<
"\"";
3498 Init(dim, dropout_proportion, dropout_scale);
3512 WriteToken(os, binary,
"<DropoutComponent>");
3517 WriteToken(os, binary,
"<DropoutProportion>");
3519 WriteToken(os, binary,
"</DropoutComponent>");
3527 dropout_proportion_ = dropout_proportion;
3528 dropout_scale_ = dropout_scale;
3542 KALDI_ASSERT(dropout_scale_ <= 1.0 && dropout_scale_ >= 0.0);
3545 high_scale = (1.0 - (dp * low_scale)) / (1.0 - dp),
3546 average = (low_scale * dp) +
3547 (high_scale * (1.0 - dp));
3558 if ((high_scale - low_scale) != 1.0)
3559 out->
Scale(high_scale - low_scale);
3560 if (low_scale != 0.0)
3561 out->
Add(low_scale);
3580 dropout_proportion_,
3585 std::string orig_args(args);
3591 if (!ok || !args.empty() || dim <= 0)
3592 KALDI_ERR <<
"Invalid initializer for layer of type AdditiveNoiseComponent: \"" 3593 << orig_args <<
"\"";
3602 ExpectToken(is, binary,
"</AdditiveNoiseComponent>");
3606 WriteToken(os, binary,
"<AdditiveNoiseComponent>");
3611 WriteToken(os, binary,
"</AdditiveNoiseComponent>");
3627 out->
AddMat(stddev_, rand);
3632 patch_dim_(0), patch_step_(0), patch_stride_(0),
3649 bias_params.
Dim() != 0);
3665 return num_patches * num_filters;
3673 BaseFloat bias_stddev,
bool appended_conv) {
3679 int32 num_splice = input_dim / patch_stride;
3680 int32 filter_dim = num_splice * patch_dim;
3681 int32 num_patches = 1 + (patch_stride - patch_dim) / patch_step;
3682 int32 num_filters = output_dim / num_patches;
3684 KALDI_ASSERT((patch_stride - patch_dim) % patch_step == 0);
3689 KALDI_ASSERT(param_stddev >= 0.0 && bias_stddev >= 0.0);
3699 std::string matrix_filename,
3700 bool appended_conv) {
3723 int32 num_filters = output_dim / num_patches;
3733 std::stringstream stream;
3738 filter_params_size),
3749 <<
", num-splice=" << num_splice
3750 <<
", num-patches=" << num_patches
3751 <<
", num-filters=" << num_filters
3752 <<
", filter-dim=" << filter_dim
3753 <<
", filter-params-stddev=" << filter_stddev
3754 <<
", bias-params-stddev=" << bias_stddev
3757 return stream.str();
3762 std::string orig_args(args);
3763 bool ok =
true, appended_conv =
false;
3765 std::string matrix_filename;
3766 int32 input_dim = -1, output_dim = -1;
3767 int32 patch_dim = -1, patch_step = -1, patch_stride = -1;
3775 Init(learning_rate, patch_dim, patch_step, patch_stride,
3776 matrix_filename, appended_conv);
3779 "input-dim mismatch vs. matrix.");
3782 "output-dim mismatch vs. matrix.");
3787 BaseFloat param_stddev = 1.0 / std::sqrt(input_dim), bias_stddev = 1.0;
3790 Init(learning_rate, input_dim, output_dim, patch_dim,
3791 patch_step, patch_stride, param_stddev, bias_stddev, appended_conv);
3794 KALDI_ERR <<
"Could not process these elements in initializer: " << args;
3796 KALDI_ERR <<
"Bad initializer " << orig_args;
3841 std::vector<int32> column_map(filter_dim * num_patches);
3844 for (
int32 patch = 0, index = 0; patch < num_patches; patch++) {
3846 for (
int32 splice = 0; splice < num_splice; splice++) {
3850 column_map[index] = (fstride +
d) * num_splice + splice;
3852 column_map[index] = fstride + cstride +
d;
3863 std::vector<CuSubMatrix<BaseFloat>* > tgt_batch, patch_batch, filter_params_batch;
3869 for (
int32 p = 0; p < num_patches; p++) {
3875 patches.
ColRange(p * filter_dim, filter_dim)));
3876 filter_params_batch.push_back(filter_params_elem);
3882 AddMatMatBatched<BaseFloat>(1.0, tgt_batch, patch_batch,
kNoTrans,
3883 filter_params_batch,
kTrans, 1.0);
3886 delete filter_params_elem;
3887 for (
int32 p = 0; p < num_patches; p++) {
3888 delete tgt_batch[p];
3889 delete patch_batch[p];
3922 std::vector<std::vector<int32> > *backward_indexes) {
3923 int32 i, size = forward_indexes.size();
3924 int32 reserve_size = 2 + size / input_dim;
3925 backward_indexes->resize(input_dim);
3926 std::vector<std::vector<int32> >::iterator iter = backward_indexes->begin(),
3927 end = backward_indexes->end();
3928 for (; iter != end; ++iter)
3929 iter->reserve(reserve_size);
3930 for (
int32 j = 0;
j < forward_indexes.size();
j++) {
3931 i = forward_indexes[
j];
3933 (*backward_indexes)[
i].push_back(
j);
3949 std::vector<std::vector<int32> > *out) {
3950 int32 D = in.size();
3953 if (in[
i].size() > L)
3957 (*out)[
i].resize(D, -1);
3959 for (
int32 j = 0;
j < in[
i].size();
j++) {
3960 (*out)[
j][
i] = in[
i][
j];
3992 std::vector<CuSubMatrix<BaseFloat>* > patch_deriv_batch, out_deriv_batch,
3993 filter_params_batch;
3999 for (
int32 p = 0; p < num_patches; p++) {
4003 p * filter_dim, filter_dim)));
4005 p * num_filters, num_filters)));
4006 filter_params_batch.push_back(filter_params_elem);
4008 AddMatMatBatched<BaseFloat>(1.0, patch_deriv_batch, out_deriv_batch,
kNoTrans,
4009 filter_params_batch,
kNoTrans, 0.0);
4012 delete filter_params_elem;
4013 for (
int32 p = 0; p < num_patches; p++) {
4014 delete patch_deriv_batch[p];
4015 delete out_deriv_batch[p];
4019 std::vector<int32> column_map(filter_dim * num_patches);
4020 for (
int32 patch = 0, index = 0; patch < num_patches; patch++) {
4022 for (
int32 splice = 0; splice < num_splice; splice++) {
4026 column_map[index] = (fstride +
d) * num_splice + splice;
4028 column_map[index] = fstride + cstride +
d;
4032 std::vector<std::vector<int32> > reversed_column_map;
4034 std::vector<std::vector<int32> > rearranged_column_map;
4036 for (
int32 p = 0; p < rearranged_column_map.size(); p++) {
4038 in_deriv->
AddCols(patches_deriv, cu_cols);
4041 if (to_update != NULL) {
4044 to_update->
Update(in_value, out_deriv);
4049 if (treat_as_gradient) {
4054 if (treat_as_gradient) {
4060 std::ostringstream ostr_beg, ostr_end;
4061 ostr_beg <<
"<" <<
Type() <<
">";
4062 ostr_end <<
"</" <<
Type() <<
">";
4076 if (tok ==
"<AppendedConv>") {
4087 if (tok ==
"<IsGradient>") {
4097 std::ostringstream ostr_beg, ostr_end;
4098 ostr_beg <<
"<" <<
Type() <<
">";
4099 ostr_end <<
"</" <<
Type() <<
">";
4178 std::vector<int32> column_map(filter_dim * num_patches);
4179 for (
int32 patch = 0, index = 0; patch < num_patches; patch++) {
4181 for (
int32 splice = 0; splice < num_splice; splice++) {
4185 column_map[index] = (fstride +
d) * num_splice + splice;
4187 column_map[index] = fstride + cstride +
d;
4192 patches.
CopyCols(in_value, cu_cols);
4209 std::vector<CuSubMatrix<BaseFloat>* > filters_grad_batch, diff_patch_batch,
4211 for (
int32 p = 0; p < num_patches; p++) {
4214 filters_grad_blocks_batch.RowRange(
4218 p * num_filters, num_filters)));
4220 p * filter_dim, filter_dim)));
4223 AddMatMatBatched<BaseFloat>(1.0, filters_grad_batch, diff_patch_batch,
4227 filters_grad.
AddMatBlocks(1.0, filters_grad_blocks_batch);
4235 bias_grad.
AddRowSumMat(1.0, out_deriv_col_blocks_sum, 1.0);
4238 for (
int32 p = 0; p < num_patches; p++) {
4239 delete filters_grad_batch[p];
4240 delete diff_patch_batch[p];
4241 delete patch_batch[p];
4253 input_dim_ = input_dim;
4254 output_dim_ = output_dim;
4255 pool_size_ = pool_size;
4256 pool_stride_ = pool_stride;
4261 int32 num_patches = input_dim_ / pool_stride_;
4264 int32 num_pools = num_patches / pool_size_;
4270 std::string orig_args(args);
4271 int32 input_dim = 0;
4272 int32 output_dim = 0;
4273 int32 pool_size = -1, pool_stride = -1;
4281 KALDI_LOG << output_dim <<
" " << input_dim <<
" " << ok;
4282 KALDI_LOG <<
"Pool: " << pool_size <<
" " 4283 << pool_stride <<
" " << ok;
4284 if (!ok || !args.empty() || output_dim <= 0)
4285 KALDI_ERR <<
"Invalid initializer for layer of type " 4286 <<
Type() <<
": \"" << orig_args <<
"\"";
4287 Init(input_dim, output_dim, pool_size, pool_stride);
4302 int32 num_patches = input_dim_ / pool_stride_;
4303 int32 num_pools = num_patches / pool_size_;
4306 for (
int32 q = 0; q < num_pools; q++) {
4310 for (
int32 r = 0; r < pool_size_; r++) {
4312 int32 p = r + q * pool_size_;
4313 pool.Max(in.
ColRange(p * pool_stride_, pool_stride_));
4325 int32 num_patches = input_dim_ / pool_stride_;
4326 int32 num_pools = num_patches / pool_size_;
4327 std::vector<int32> patch_summands(num_patches, 0);
4330 for(
int32 q = 0; q < num_pools; q++) {
4331 for(
int32 r = 0; r < pool_size_; r++) {
4332 int32 p = r + q * pool_size_;
4340 src.MulElements(mask);
4341 tgt.AddMat(1.0, src);
4343 patch_summands[p] += 1;
4348 for(
int32 p = 0; p < num_patches; p++) {
4351 tgt.Scale(1.0 / patch_summands[p]);
4364 ExpectToken(is, binary,
"</MaxpoolingComponent>");
4368 WriteToken(os, binary,
"<MaxpoolingComponent>");
4377 WriteToken(os, binary,
"</MaxpoolingComponent>");
4381 std::stringstream stream;
4382 stream <<
Type() <<
", input-dim = " << input_dim_
4383 <<
", output-dim = " << output_dim_
4384 <<
", pool-size = " << pool_size_
4385 <<
", pool-stride = " << pool_stride_;
4386 return stream.str();
virtual void Vectorize(VectorBase< BaseFloat > *params) const
Turns the parameters into vector form.
virtual void Propagate(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out) const
Perform forward pass propagation Input->Output.
int32 OutputDim() const
Get size of output vectors.
virtual void Write(std::ostream &os, bool binary) const
Write component to stream.
void MulElements(const CuVectorBase< Real > &v)
void CopyFromMat(const MatrixBase< OtherReal > &src, MatrixTransposeType trans=kNoTrans)
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
virtual void Read(std::istream &is, bool binary)
We implement Read at this level as it just needs the Type().
virtual std::string Info() const
virtual void SetParams(const VectorBase< BaseFloat > &bias, const MatrixBase< BaseFloat > &linear)
This kind of Component is a base-class for things like sigmoid and softmax.
virtual void Read(std::istream &is, bool binary)
bool ConvertStringToInteger(const std::string &str, Int *out)
Converts a string into an integer via strtoll and returns false if there was any kind of problem (i...
virtual void InitFromString(std::string args)
Initialize, typically from a line of a config file.
virtual void Read(std::istream &is, bool binary)
virtual int32 InputDim() const
Get size of input vectors.
virtual void UnVectorize(const VectorBase< BaseFloat > ¶ms)
Converts the parameters from vector form.
virtual int32 InputDim() const =0
Get size of input vectors.
CuVector< BaseFloat > bias_params_
void SoftHinge(const CuMatrixBase< Real > &src)
Apply the function y = log(1 + exp(x)), to each element.
void ApplyPow(Real power)
FixedAffineComponent is an affine transform that is supplied at network initialization time and is no...
virtual Component * Copy() const
Copy component (deep copy).
void Init(BaseFloat learning_rate, int32 input_dim, int32 output_dim, BaseFloat param_stddev, BaseFloat bias_stddev)
virtual void Read(std::istream &is, bool binary)
virtual void InitFromString(std::string args)
Initialize, typically from a line of a config file.
virtual Component * Copy() const
Copy component (deep copy).
void Check() const
Checks that the data in the ChunkInfo is valid, and die if not.
virtual void Propagate(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out) const
Perform forward pass propagation Input->Output.
virtual int32 OutputDim() const
Get size of output vectors.
void GroupMax(const CuMatrixBase< Real > &src)
Apply the function y(i) = (max_{j = i*G}^{(i+1)*G-1} x_j where G = x.NumCols() / y.NumCols() must be an integer.
virtual void Propagate(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out) const
Perform forward pass propagation Input->Output.
virtual void InitFromString(std::string args)
Initialize, typically from a line of a config file.
virtual void InitFromString(std::string args)
Initialize, typically from a line of a config file.
static void ReverseIndexes(const std::vector< int32 > &forward_indexes, int32 input_dim, std::vector< std::vector< int32 > > *backward_indexes)
BaseFloat max_change_per_sample_
virtual void Resize(int32 input_dim, int32 output_dim)
virtual void InitFromString(std::string args)
We implement InitFromString at this level.
virtual void Write(std::ostream &os, bool binary) const
Write component to stream.
Component * CollapseWithPrevious(const FixedAffineComponent &prev) const
virtual std::string Info() const
float RandUniform(struct RandomState *state=NULL)
Returns a random number strictly between 0 and 1.
virtual void InitFromString(std::string args)
Initialize, typically from a line of a config file.
void Init(const CuVectorBase< BaseFloat > &scales)
virtual void Backprop(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_value, const CuMatrixBase< BaseFloat > &out_deriv, Component *to_update, CuMatrix< BaseFloat > *in_deriv) const
Perform backward pass propagation of the derivative, and also either update the model (if to_update =...
CuMatrix< BaseFloat > filter_params_
virtual void UnVectorize(const VectorBase< BaseFloat > ¶ms)
Converts the parameters from vector form.
virtual Component * Copy() const
Copy component (deep copy).
virtual void PerturbParams(BaseFloat stddev)
We introduce a new virtual function that only applies to class UpdatableComponent.
BaseFloat GetScalingFactor(const CuVectorBase< BaseFloat > &in_products, BaseFloat gamma_prod, CuVectorBase< BaseFloat > *out_products)
The following function is only called if max_change_per_sample_ > 0, it returns a scaling factor alph...
virtual void UpdateSimple(const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_deriv)
CuArray< Int32Pair > indexes_
static Component * NewFromString(const std::string &initializer_line)
Initialize the Component from one line that will contain first the type, e.g.
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
void Init(int32 input_dim, int32 output_dim, int32 pool_size, int32 pool_stride)
Base class which provides matrix operations not involving resizing or allocation. ...
virtual void Update(const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_deriv)
void CopyColFromMat(const CuMatrixBase< Real > &mat, MatrixIndexT col)
void ReadBasicType(std::istream &is, bool binary, T *t)
ReadBasicType is the name of the read function for bool, integer types, and floating-point types...
void Init(const CuMatrixBase< BaseFloat > &matrix)
std::vector< int32 > context_
virtual Component * Copy() const
Copy component (deep copy).
virtual void Propagate(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out) const
Perform forward pass propagation Input->Output.
virtual void Write(std::ostream &os, bool binary) const
Write component to stream.
bool SplitStringToIntegers(const std::string &full, const char *delim, bool omit_empty_strings, std::vector< I > *out)
Split a string (e.g.
virtual void Add(BaseFloat alpha, const UpdatableComponent &other)
This new virtual function adds the parameters of another updatable component, times some constant...
Abstract class, basic element of the network, it is a box with defined inputs, outputs, and tranformation functions interface.
void Update(const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_deriv)
virtual void Backprop(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &, const CuMatrixBase< BaseFloat > &out_deriv, Component *to_update, CuMatrix< BaseFloat > *in_deriv) const
Perform backward pass propagation of the derivative, and also either update the model (if to_update =...
void Init(int32 dim, int32 dct_dim, bool reorder, int32 keep_dct_dim=0)
void ComputeDctMatrix(Matrix< Real > *M)
ComputeDctMatrix computes a matrix corresponding to the DCT, such that M * v equals the DCT of vector...
void Init(int32 input_dim, std::vector< int32 > context, int32 const_component_dim=0)
virtual void Read(std::istream &is, bool binary)
virtual void SetZero(bool treat_as_gradient)
Set parameters to zero, and if treat_as_gradient is true, we'll be treating this as a gradient so set...
virtual Component * Copy() const
Copy component (deep copy).
virtual void InitFromString(std::string args)
Initialize, typically from a line of a config file.
virtual int32 OutputDim() const =0
Get size of output vectors.
void Add(BaseFloat alpha, const NonlinearComponent &other)
CuSubMatrix< Real > Range(const MatrixIndexT row_offset, const MatrixIndexT num_rows, const MatrixIndexT col_offset, const MatrixIndexT num_cols) const
void Write(std::ostream &os, bool binary) const
Write component to stream.
void ApplyFloor(Real floor_val)
void AddDiagMat2(Real alpha, const CuMatrixBase< Real > &M, MatrixTransposeType trans, Real beta)
Add the diagonal of a matrix times itself: *this = diag(M M^T) + beta * *this (if trans == kNoTrans)...
virtual void SetZero(bool treat_as_gradient)
Set parameters to zero, and if treat_as_gradient is true, we'll be treating this as a gradient so set...
virtual void Backprop(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &, const CuMatrixBase< BaseFloat > &out_deriv, Component *to_update, CuMatrix< BaseFloat > *in_deriv) const
Perform backward pass propagation of the derivative, and also either update the model (if to_update =...
virtual void Read(std::istream &is, bool binary)
void swap(basic_filebuf< CharT, Traits > &x, basic_filebuf< CharT, Traits > &y)
virtual void Backprop(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_value, const CuMatrixBase< BaseFloat > &out_deriv, Component *to_update, CuMatrix< BaseFloat > *in_deriv) const
Perform backward pass propagation of the derivative, and also either update the model (if to_update =...
void ReadToken(std::istream &is, bool binary, std::string *str)
ReadToken gets the next token and puts it in str (exception on failure).
virtual void Backprop(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_value, const CuMatrixBase< BaseFloat > &out_deriv, Component *to_update_in, CuMatrix< BaseFloat > *in_deriv) const
Perform backward pass propagation of the derivative, and also either update the model (if to_update =...
void SetParams(const VectorBase< BaseFloat > &bias, const MatrixBase< BaseFloat > &filter)
void AddMat(Real alpha, const CuMatrixBase< Real > &A, MatrixTransposeType trans=kNoTrans)
*this += alpha * A
virtual int32 GetParameterDim() const
The following new virtual function returns the total dimension of the parameters in this class...
virtual void Write(std::ostream &os, bool binary) const
Write component to stream.
virtual void InitFromString(std::string args)
Initialize, typically from a line of a config file.
This class represents a matrix that's stored on the GPU if we have one, and in memory if not...
PermuteComponent does a permutation of the dimensions (by default, a fixed random permutation...
virtual void Scale(BaseFloat scale)
This new virtual function scales the parameters by this amount.
void Resize(MatrixIndexT length, MatrixResizeType resize_type=kSetZero)
Set vector to a specified size (can be zero).
void CopyRowsFromVec(const CuVectorBase< Real > &v)
This function has two modes of operation.
This is a bit similar to dropout but adding (not multiplying) Gaussian noise with a given standard de...
CuVector< double > deriv_sum_
virtual void Backprop(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_value, const CuMatrixBase< BaseFloat > &out_deriv, Component *to_update, CuMatrix< BaseFloat > *in_deriv) const
Perform backward pass propagation of the derivative, and also either update the model (if to_update =...
virtual void Update(const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_deriv)
CuVector< BaseFloat > bias_
virtual void Write(std::ostream &os, bool binary) const
Write component to stream.
virtual void Read(std::istream &is, bool binary)
virtual void Write(std::ostream &os, bool binary) const
Write component to stream.
virtual void Write(std::ostream &os, bool binary) const
Write component to stream.
virtual void InitFromString(std::string args)
Initialize, typically from a line of a config file.
virtual void Backprop(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_value, const CuMatrixBase< BaseFloat > &out_deriv, Component *to_update, CuMatrix< BaseFloat > *in_deriv) const
Perform backward pass propagation of the derivative, and also either update the model (if to_update =...
int32 GetParameterDim() const
The following new virtual function returns the total dimension of the parameters in this class...
virtual void Backprop(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_value, const CuMatrixBase< BaseFloat > &out_deriv, Component *to_update, CuMatrix< BaseFloat > *in_deriv) const
Perform backward pass propagation of the derivative, and also either update the model (if to_update =...
void Init(BaseFloat learning_rate, int32 input_dim, int32 output_dim, BaseFloat param_stddev, BaseFloat bias_stddev, int32 num_blocks, BaseFloat alpha)
virtual void Propagate(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out) const
Perform forward pass propagation Input->Output.
virtual void InitFromString(std::string args)
Initialize, typically from a line of a config file.
virtual void Propagate(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out) const
Perform forward pass propagation Input->Output.
void AddCols(const CuMatrixBase< Real > &src, const CuArrayBase< MatrixIndexT > &indices)
Add column indices[r] of src to column r.
virtual void Read(std::istream &is, bool binary)
virtual Component * Copy() const
Copy component (deep copy).
virtual void Write(std::ostream &os, bool binary) const
Write component to stream.
virtual void Write(std::ostream &os, bool binary) const
Write component to stream.
virtual void Write(std::ostream &os, bool binary) const
Write component to stream.
void SetZero(bool treat_as_gradient)
Set parameters to zero, and if treat_as_gradient is true, we'll be treating this as a gradient so set...
virtual void Backprop(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_value, const CuMatrixBase< BaseFloat > &out_deriv, Component *to_update, CuMatrix< BaseFloat > *in_deriv) const
Perform backward pass propagation of the derivative, and also either update the model (if to_update =...
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
virtual void Update(const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_deriv)
void Scale(BaseFloat scale)
This new virtual function scales the parameters by this amount.
virtual void Read(std::istream &is, bool binary)
static const BaseFloat kNormFloor
void Init(BaseFloat learning_rate, int32 input_dim, int32 output_dim, BaseFloat param_stddev, BaseFloat bias_stddev, int32 num_blocks)
virtual std::string Info() const
virtual void Propagate(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out) const
Perform forward pass propagation Input->Output.
Discrete cosine transform.
void DestructiveSvd(VectorBase< Real > *s, MatrixBase< Real > *U, MatrixBase< Real > *Vt)
Singular value decomposition Major limitations: For nonsquare matrices, we assume m>=n (NumRows >= Nu...
bool SameDim(const MatrixBase< Real > &M, const MatrixBase< Real > &N)
OnlinePreconditioner preconditioner_in_
void GetSizes(std::vector< int32 > *sizes) const
void UpdateStats(const CuMatrixBase< BaseFloat > &out_value, const CuMatrixBase< BaseFloat > *deriv=NULL)
void PreconditionDirections(CuMatrixBase< BaseFloat > *R, CuVectorBase< BaseFloat > *row_prod, BaseFloat *scale)
virtual void InitFromString(std::string args)
We implement InitFromString at this level.
void AddMatBlocks(Real alpha, const CuMatrixBase< Real > &A, MatrixTransposeType trans=kNoTrans)
This function is like AddMat (it does *this += alpha * src), except that it supports cases where *thi...
FixedScaleComponent applies a fixed per-element scale; it's similar to the Rescale component in the n...
virtual void Read(std::istream &is, bool binary)=0
virtual void Backprop(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_value, const CuMatrixBase< BaseFloat > &out_deriv, Component *to_update, CuMatrix< BaseFloat > *in_deriv) const
Perform backward pass propagation of the derivative, and also either update the model (if to_update =...
bool ParseFromString(const std::string &name, std::string *string, int32 *param)
Functions used in Init routines.
void Init(int32 dim, std::vector< int32 > context)
void Init(BaseFloat learning_rate, int32 input_dim, int32 output_dim, BaseFloat param_stddev, BaseFloat bias_stddev, int32 rank_in, int32 rank_out, int32 update_period, BaseFloat num_samples_history, BaseFloat alpha, BaseFloat max_change_per_sample)
virtual std::string Info() const
virtual void Backprop(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_value, const CuMatrixBase< BaseFloat > &out_deriv, Component *to_update, CuMatrix< BaseFloat > *in_deriv) const
Perform backward pass propagation of the derivative, and also either update the model (if to_update =...
virtual std::string Info() const
int32 GetOffset(int32 index) const
virtual void Propagate(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out) const
Perform forward pass propagation Input->Output.
void CopyFromVec(const CuVectorBase< Real > &src)
Copy functions; these will crash if the dimension do not match.
void Reorder(CuMatrixBase< BaseFloat > *mat, bool reverse) const
void Init(int32 input_dim, int32 output_dim, BaseFloat p)
virtual void Backprop(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_value, const CuMatrixBase< BaseFloat > &out_deriv, Component *to_update, CuMatrix< BaseFloat > *in_deriv) const
Perform backward pass propagation of the derivative, and also either update the model (if to_update =...
void DiffNormalizePerRow(const CuMatrixBase< Real > &in_value, const CuMatrixBase< Real > &out_deriv, const Real target_rms, const bool add_log_stddev, CuMatrixBase< Real > *in_deriv)
void Max(const CuMatrixBase< Real > &A)
Do, elementwise, *this = max(*this, A).
void ReadIntegerVector(std::istream &is, bool binary, std::vector< T > *v)
Function for reading STL vector of integer types.
void AddVecToRows(Real alpha, const CuVectorBase< Real > &row, Real beta=1.0)
(for each row r of *this), r = alpha * row + beta * r
void Init(BaseFloat learning_rate)
void ApplyPowAbs(Real power, bool include_sign=false)
virtual void Write(std::ostream &os, bool binary) const
Write component to stream.
virtual void Write(std::ostream &os, bool binary) const
Write component to stream.
virtual void Write(std::ostream &os, bool binary) const
Write component to stream.
void Init(const CuMatrixBase< BaseFloat > &matrix)
matrix should be of size input-dim+1 to output-dim, last col is offset
void Sigmoid(const CuMatrixBase< Real > &src)
Set each element to the sigmoid of the corresponding element of "src": element by element...
BaseFloat num_samples_history_
virtual Component * Copy() const
Copy component (deep copy).
CuMatrix< BaseFloat > dct_mat_
virtual std::string Info() const
Take the absoute values of an input vector to a power.
virtual int32 InputDim() const
Get size of input vectors.
void ExpectToken(std::istream &is, bool binary, const char *token)
ExpectToken tries to read in the given token, and throws an exception on failure. ...
virtual std::string Info() const
void SoftMaxPerRow(const CuMatrixBase< Real > &src)
Softmax nonlinearity Y = Softmax(X) : Yij = e^Xij / sum_k(e^Xik), done to each row, with attention to avoiding overflow or underflow.
static Component * ReadNew(std::istream &is, bool binary)
Read component from stream.
BaseFloat learning_rate_
learning rate (0.0..0.01)
void SplitStringToVector(const std::string &full, const char *delim, bool omit_empty_strings, std::vector< std::string > *out)
Split a string using any of the single character delimiters.
virtual void Backprop(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_value, const CuMatrixBase< BaseFloat > &out_deriv, Component *to_update, CuMatrix< BaseFloat > *in_deriv) const
Perform backward pass propagation of the derivative, and also either update the model (if to_update =...
void MulElements(const CuMatrixBase< Real > &A)
Multiply two matrices elementwise: C = C .* A.
This Component, if present, randomly zeroes half of the inputs and multiplies the other half by two...
void CopyRows(const CuMatrixBase< Real > &src, const CuArrayBase< MatrixIndexT > &indexes)
Copies row r from row indexes[r] of src.
virtual void InitFromString(std::string args)
Initialize, typically from a line of a config file.
CuVector< BaseFloat > bias_params_
ChunkInfo is a class whose purpose is to describe the structure of matrices holding features...
virtual void Backprop(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_value, const CuMatrixBase< BaseFloat > &out_deriv, Component *to_update, CuMatrix< BaseFloat > *in_deriv) const
Perform backward pass propagation of the derivative, and also either update the model (if to_update =...
virtual void Propagate(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out) const
Perform forward pass propagation Input->Output.
virtual void Write(std::ostream &os, bool binary) const
Write component to stream.
virtual void Read(std::istream &is, bool binary)
OnlinePreconditioner preconditioner_out_
virtual void Backprop(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_value, const CuMatrixBase< BaseFloat > &out_deriv, Component *to_update, CuMatrix< BaseFloat > *in_deriv) const
Perform backward pass propagation of the derivative, and also either update the model (if to_update =...
virtual void Add(BaseFloat alpha, const UpdatableComponent &other)
This new virtual function adds the parameters of another updatable component, times some constant...
Convolutional1dComponent()
CuVector< double > value_sum_
void Resize(MatrixIndexT dim, MatrixResizeType t=kSetZero)
Allocate the memory.
This is as SpliceComponent but outputs the max of any of the inputs (taking the max across time)...
virtual void InitFromString(std::string args)
Initialize, typically from a line of a config file.
virtual void SetZero(bool treat_as_gradient)
Set parameters to zero, and if treat_as_gradient is true, we'll be treating this as a gradient so set...
bool ConvertStringToReal(const std::string &str, T *out)
ConvertStringToReal converts a string into either float or double and returns false if there was any ...
virtual void Update(const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_deriv)
void GroupPnorm(const CuMatrixBase< Real > &src, Real pow)
Apply the function y(i) = (sum_{j = i*G}^{(i+1)*G-1} x_j ^ (power)) ^ (1 / p) where G = x...
void ApplyPow(Real power)
virtual int32 OutputDim() const
Get size of output vectors.
void AddMatMat(Real alpha, const CuMatrixBase< Real > &A, MatrixTransposeType transA, const CuMatrixBase< Real > &B, MatrixTransposeType transB, Real beta)
C = alpha * A(^T)*B(^T) + beta * C.
virtual void Backprop(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_value, const CuMatrixBase< BaseFloat > &out_deriv, Component *to_update, CuMatrix< BaseFloat > *in_deriv) const
Perform backward pass propagation of the derivative, and also either update the model (if to_update =...
virtual void Propagate(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out) const
Perform forward pass propagation Input->Output.
int32 const_component_dim_
virtual void Read(std::istream &is, bool binary)
virtual void Propagate(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out) const
Perform forward pass propagation Input->Output.
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.
virtual Component * Copy() const
Copy component (deep copy).
This class is used for a piece of a CuMatrix.
virtual std::string Info() const
virtual void Read(std::istream &is, bool binary)
virtual void Backprop(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_value, const CuMatrixBase< BaseFloat > &out_deriv, Component *to_update, CuMatrix< BaseFloat > *in_deriv) const
Perform backward pass propagation of the derivative, and also either update the model (if to_update =...
void WriteToken(std::ostream &os, bool binary, const char *token)
The WriteToken functions are for writing nonempty sequences of non-space characters.
void DiffSoftmaxPerRow(const CuMatrixBase< Real > &value, const CuMatrixBase< Real > &diff)
Differentiate backward through the softmax function.
virtual void Propagate(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out) const
Perform forward pass propagation Input->Output.
void GroupMaxDeriv(const CuMatrixBase< Real > &input, const CuMatrixBase< Real > &output)
Calculate derivatives for the GroupMax function above, where "input" is the input to the GroupMax fun...
virtual void Propagate(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out) const
Perform forward pass propagation Input->Output.
virtual std::string Info() const
virtual BaseFloat DotProduct(const UpdatableComponent &other) const
Here, "other" is a component of the same specific type.
virtual void InitFromString(std::string args)
Initialize, typically from a line of a config file.
virtual void Propagate(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out) const
Perform forward pass propagation Input->Output.
virtual void Backprop(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_value, const CuMatrixBase< BaseFloat > &out_deriv, Component *to_update, CuMatrix< BaseFloat > *in_deriv) const
Perform backward pass propagation of the derivative, and also either update the model (if to_update =...
virtual void Write(std::ostream &os, bool binary) const
Write component to stream.
int32 InputDim() const
Get size of input vectors.
virtual void Write(std::ostream &os, bool binary) const
Write component to stream.
virtual void PerturbParams(BaseFloat stddev)
We introduce a new virtual function that only applies to class UpdatableComponent.
virtual void InitFromString(std::string args)
Initialize, typically from a line of a config file.
FixedBiasComponent applies a fixed per-element bias; it's similar to the AddShift component in the nn...
virtual void InitFromString(std::string args)
Initialize, typically from a line of a config file.
virtual Component * Copy() const
Copy component (deep copy).
virtual int32 Index() const
Returns the index in the sequence of layers in the neural net; intended only to be used in debugging ...
virtual void UpdateSimple(const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_deriv)
virtual void Read(std::istream &is, bool binary)
CuVector< BaseFloat > scales_
virtual void Backprop(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &, const CuMatrixBase< BaseFloat > &out_deriv, Component *to_update, CuMatrix< BaseFloat > *in_deriv) const
Perform backward pass propagation of the derivative, and also either update the model (if to_update =...
virtual void Backprop(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_value, const CuMatrixBase< BaseFloat > &out_deriv, Component *to_update, CuMatrix< BaseFloat > *in_deriv) const
Perform backward pass propagation of the derivative, and also either update the model (if to_update =...
Real Sum() const
Returns sum of the elements.
void Init(int32 dim, BaseFloat noise_stddev)
virtual void Scale(BaseFloat scale)
This new virtual function scales the parameters by this amount.
int32 NumRows() const
Returns the number of rows that we expect the feature matrix to have.
virtual void Backprop(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_value, const CuMatrixBase< BaseFloat > &out_deriv, Component *to_update, CuMatrix< BaseFloat > *in_deriv) const
Perform backward pass propagation of the derivative, and also either update the model (if to_update =...
int32 GetIndex(int32 offset) const
void PerturbParams(BaseFloat stddev)
We introduce a new virtual function that only applies to class UpdatableComponent.
virtual std::string Info() const
virtual void InitFromString(std::string args)
Initialize, typically from a line of a config file.
virtual Component * Copy() const
Copy component (deep copy).
virtual void Write(std::ostream &os, bool binary) const
Write component to stream.
virtual Component * Copy() const
Copy component (deep copy).
virtual std::string Info() const
virtual void Propagate(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out) const
Perform forward pass propagation Input->Output.
virtual int32 GetParameterDim() const
The following new virtual function returns the total dimension of the parameters in this class...
CuMatrix< BaseFloat > linear_params_
void Read(std::istream &is, bool binary)
void CheckSize(const CuMatrixBase< BaseFloat > &mat) const
Checks that the matrix has the size we expect, and die if not.
void SetPreconditionerConfigs()
MaxPoolingComponent : Maxpooling component was firstly used in ConvNet for selecting an representativ...
virtual void Write(std::ostream &os, bool binary) const
Write component to stream.
virtual Component * Copy() const
Copy component (deep copy).
void MulRowsVec(const VectorBase< Real > &scale)
Equivalent to (*this) = diag(scale) * (*this).
virtual void InitFromString(std::string args)
Initialize, typically from a line of a config file.
void MulColsVec(const CuVectorBase< Real > &scale)
scale i'th column by scale[i]
void SumColumnRanges(const CuMatrixBase< Real > &src, const CuArrayBase< Int32Pair > &indexes)
For each row r of this and for each column c, sets (*this)(r, c) to the sum src(r, j), where j ranges from indexes[c].first through indexes[c].second - 1.
virtual void Write(std::ostream &os, bool binary) const
Write component to stream.
CuSubMatrix< Real > ColRange(const MatrixIndexT col_offset, const MatrixIndexT num_cols) const
virtual Component * Copy() const
Copy component (deep copy).
void Propagate(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out) const
Perform forward pass propagation Input->Output.
virtual void InitFromString(std::string args)
Initialize, typically from a line of a config file.
FixedLinearComponent is a linear transform that is supplied at network initialization time and is not...
virtual void Propagate(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out) const
Perform forward pass propagation Input->Output.
Matrix for CUDA computing.
MatrixIndexT NumCols() const
virtual void Propagate(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out) const
Perform forward pass propagation Input->Output.
void Scale(BaseFloat scale)
void DiffLogSoftmaxPerRow(const CuMatrixBase< Real > &out_value, const CuMatrixBase< Real > &out_deriv)
Differentiate backward through the log softmax function.
void DiffGroupPnorm(const CuMatrixBase< Real > &in_value, const CuMatrixBase< Real > &out_value, const CuMatrixBase< Real > &out_deriv, Real power)
Differentiate backward through the GroupPnorm function.
virtual std::string Type() const
virtual std::string Info() const
static Component * NewComponentOfType(const std::string &type)
Return a new Component of the given type e.g.
A class representing a vector.
virtual void InitFromString(std::string args)
Initialize, typically from a line of a config file.
virtual void Propagate(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out) const
Perform forward pass propagation Input->Output.
virtual void Backprop(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_value, const CuMatrixBase< BaseFloat > &out_deriv, Component *to_update, CuMatrix< BaseFloat > *in_deriv) const
Perform backward pass propagation of the derivative, and also either update the model (if to_update =...
virtual void Vectorize(VectorBase< BaseFloat > *params) const
Turns the parameters into vector form.
virtual std::string Info() const
#define KALDI_ASSERT(cond)
virtual std::string Type() const =0
void Read(std::istream &is, bool binary)
I/O functions.
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
BaseFloat GetScalingFactor(const CuMatrix< BaseFloat > &in_value_precon, const CuMatrix< BaseFloat > &out_deriv_precon)
The following function is only called if max_change_ > 0.
static void ExpectOneOrTwoTokens(std::istream &is, bool binary, const std::string &token1, const std::string &token2)
std::vector< int32 > reorder_
static void RearrangeIndexes(const std::vector< std::vector< int32 > > &in, std::vector< std::vector< int32 > > *out)
virtual void Propagate(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out) const
Perform forward pass propagation Input->Output.
virtual void Propagate(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out) const
Perform forward pass propagation Input->Output.
void Read(std::istream &is, bool binary)
I/O.
Keywords: natural gradient descent, NG-SGD, naturalgradient.
virtual void Read(std::istream &is, bool binary)
AffineComponentPreconditionedOnline()
void Init(BaseFloat learning_rate, int32 input_dim, int32 output_dim, int32 patch_dim, int32 patch_step, int32 patch_stride, BaseFloat param_stddev, BaseFloat bias_stddev, bool appended_conv)
virtual std::string Info() const
virtual void Resize(int32 input_dim, int32 output_dim)
CuVector< BaseFloat > bias_params_
virtual void Read(std::istream &is, bool binary)
virtual int32 InputDim() const
Get size of input vectors.
void MulRowsGroupMat(const CuMatrixBase< Real > &src)
divide each row into src.NumCols() groups, and then scale i'th row's jth group of elements by src[i...
virtual BaseFloat DotProduct(const UpdatableComponent &other) const
Here, "other" is a component of the same specific type.
virtual void InitFromString(std::string args)
Initialize, typically from a line of a config file.
Component * Copy() const
Copy component (deep copy).
void WriteIntegerVector(std::ostream &os, bool binary, const std::vector< T > &v)
Function for writing STL vectors of integer types.
void Init(int32 input_dim, int32 output_dim)
virtual void InitFromString(std::string args)=0
Initialize, typically from a line of a config file.
Component * CollapseWithNext(const AffineComponent &next) const
void Init(BaseFloat learning_rate, int32 input_dim, int32 output_dim, BaseFloat param_stddev, BaseFloat bias_stddev, BaseFloat alpha, BaseFloat max_change)
void WriteBasicType(std::ostream &os, bool binary, T t)
WriteBasicType is the name of the write function for bool, integer types, and floating-point types...
void CopyCols(const CuMatrixBase< Real > &src, const CuArrayBase< MatrixIndexT > &indexes)
Copies column r from column indexes[r] of src.
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).
virtual void Read(std::istream &is, bool binary)
void Init(int32 dim, BaseFloat scale)
void Init(const CuVectorBase< BaseFloat > &scales)
virtual void Propagate(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out) const
Perform forward pass propagation Input->Output.
virtual void Read(std::istream &is, bool binary)
We implement Read at this level as it just needs the Type().
virtual std::string Info() const
void Init(const std::vector< int32 > &sizes)
virtual void Write(std::ostream &os, bool binary) const
Write component to stream.
virtual void Backprop(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_value, const CuMatrixBase< BaseFloat > &out_deriv, Component *to_update, CuMatrix< BaseFloat > *in_deriv) const
Perform backward pass propagation of the derivative, and also either update the model (if to_update =...
virtual void Add(BaseFloat alpha, const UpdatableComponent &other)
This new virtual function adds the parameters of another updatable component, times some constant...
CuMatrix< BaseFloat > linear_params_
virtual void Propagate(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out) const
Perform forward pass propagation Input->Output.
Keywords for search: natural gradient, naturalgradient, NG-SGD.
void LogSoftMaxPerRow(const CuMatrixBase< Real > &src)
LogSoftmax nonlinearity Y = LogSoftmax(X) : Yij = Xij - log(sum_k(e^Xik)), done to each row...
void SetLearningRate(BaseFloat lrate)
Sets the learning rate of gradient descent.
virtual std::string Type() const
void InitFromString(std::string args)
Initialize, typically from a line of a config file.
Convolutional1dComponent implements convolution over frequency axis.
MatrixIndexT NumRows() const
Dimensions.
Provides a vector abstraction class.
virtual Component * Copy() const
Copy component (deep copy).
void NormalizePerRow(const CuMatrixBase< Real > &in, const Real target_rms, const bool add_log_stddev, CuMatrixBase< Real > *out)
Normalize nonlinearity modifies the vector of activations by scaling it so that the root-mean-square ...
virtual void Read(std::istream &is, bool binary)
void Init(int32 dim, BaseFloat power=2)
Splices a context window of frames together [over time].
void SetMatMatDivMat(const CuMatrixBase< Real > &A, const CuMatrixBase< Real > &B, const CuMatrixBase< Real > &C)
*this = a * b / c (by element; when c = 0, *this = a) *this can be an alias of a, b or c safely and g...
virtual Component * Copy() const
Copy component (deep copy).
void Resize(int32 input_dim, int32 output_dim)
void Tanh(const CuMatrixBase< Real > &src)
Compute the hyperbolic tangent (tanh) function; element by element, *this = tanh(src).
virtual void Backprop(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_value, const CuMatrixBase< BaseFloat > &out_deriv, Component *to_update, CuMatrix< BaseFloat > *in_deriv) const
Perform backward pass propagation of the derivative, and also either update the model (if to_update =...
CuVector< BaseFloat > bias_params_
bool IsSortedAndUniq(const std::vector< T > &vec)
Returns true if the vector is sorted and contains each element only once.
Real VecVec(const VectorBase< Real > &a, const VectorBase< Real > &b)
Returns dot product between v1 and v2.
CuMatrix< BaseFloat > linear_params_
virtual void Read(std::istream &is, bool binary)
virtual void Write(std::ostream &os, bool binary) const
Write component to stream.
virtual std::string Info() const
virtual void Read(std::istream &is, bool binary)
virtual void Update(const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_deriv)
void PreconditionDirectionsAlphaRescaled(const CuMatrixBase< BaseFloat > &R, double alpha, CuMatrixBase< BaseFloat > *P)
This wrapper for PreconditionDirections computes lambda using = /(N D) trace(R^T, R), and calls PreconditionDirections.
void EqualElementMask(const CuMatrixBase< Real > &mat, CuMatrix< Real > *mask) const
virtual std::string Info() const
BaseFloat LearningRate() const
Gets the learning rate of gradient descent.
virtual BaseFloat DotProduct(const UpdatableComponent &other) const
Here, "other" is a component of the same specific type.
virtual std::string Info() const
int32 NumCols() const
Returns the number of columns that we expect the feature matrix to have.
void Resize(MatrixIndexT rows, MatrixIndexT cols, MatrixResizeType resize_type=kSetZero, MatrixStrideType stride_type=kDefaultStride)
Allocate the memory.
virtual void Read(std::istream &is, bool binary)
virtual void Propagate(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out) const
Perform forward pass propagation Input->Output.
virtual void Propagate(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out) const
Perform forward pass propagation Input->Output.
void AddRowSumMat(Real alpha, const CuMatrixBase< Real > &mat, Real beta=1.0)
Sum the rows of the matrix, add to vector.
void SortSvd(VectorBase< Real > *s, MatrixBase< Real > *U, MatrixBase< Real > *Vt, bool sort_on_absolute_value)
Function to ensure that SVD is sorted.
void Init(int32 dim, BaseFloat dropout_proportion=0.5, BaseFloat dropout_scale=0.0)
dropout-proportion is the proportion that is dropped out, e.g.
MatrixIndexT Dim() const
Dimensions.
virtual void Backprop(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_value, const CuMatrixBase< BaseFloat > &out_deriv, Component *to_update, CuMatrix< BaseFloat > *in_deriv) const
Perform backward pass propagation of the derivative, and also either update the model (if to_update =...
virtual void Read(std::istream &is, bool binary)
Vector for CUDA computing.
virtual void InitFromString(std::string args)
Initialize, typically from a line of a config file.
virtual void Write(std::ostream &os, bool binary) const
Write component to stream.
virtual void LimitRank(int32 dimension, AffineComponent **a, AffineComponent **b) const
This function is for getting a low-rank approximations of this AffineComponent by two AffineComponent...
CuArray< int32 > reverse_indexes_
virtual int32 OutputDim() const
Get size of output vectors.
Class UpdatableComponent is a Component which has trainable parameters and contains some global param...
virtual void Propagate(const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out) const
Perform forward pass propagation Input->Output.
SubVector< Real > Range(const MatrixIndexT o, const MatrixIndexT l)
Returns a sub-vector of a vector (a range of elements).