Blame view
src/nnet3/am-nnet-simple.cc
3.62 KB
8dcb6dfcb first commit |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
// nnet3/am-nnet-simple.cc // Copyright 2012-2015 Johns Hopkins University (author: Daniel Povey) // See ../../COPYING for clarification regarding multiple authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, // MERCHANTABLITY OR NON-INFRINGEMENT. // See the Apache 2 License for the specific language governing permissions and // limitations under the License. #include "nnet3/am-nnet-simple.h" #include "nnet3/nnet-utils.h" namespace kaldi { namespace nnet3 { int32 AmNnetSimple::NumPdfs() const { int32 ans = nnet_.OutputDim("output"); KALDI_ASSERT(ans > 0); return ans; } void AmNnetSimple::Write(std::ostream &os, bool binary) const { // We don't write any header or footer like <AmNnetSimple> and </AmNnetSimple> -- we just // write the neural net and then the priors. Who knows, there might be some // situation where we want to just read the neural net. nnet_.Write(os, binary); WriteToken(os, binary, "<LeftContext>"); WriteBasicType(os, binary, left_context_); WriteToken(os, binary, "<RightContext>"); WriteBasicType(os, binary, right_context_); WriteToken(os, binary, "<Priors>"); priors_.Write(os, binary); } void AmNnetSimple::Read(std::istream &is, bool binary) { nnet_.Read(is, binary); ExpectToken(is, binary, "<LeftContext>"); ReadBasicType(is, binary, &left_context_); ExpectToken(is, binary, "<RightContext>"); ReadBasicType(is, binary, &right_context_); SetContext(); // temporarily, I'm not trusting the written ones (there was // briefly a bug) ExpectToken(is, binary, "<Priors>"); priors_.Read(is, binary); } void AmNnetSimple::SetNnet(const Nnet &nnet) { nnet_ = nnet; SetContext(); if (priors_.Dim() != 0 && priors_.Dim() != nnet_.OutputDim("output")) { KALDI_WARN << "Removing priors since there is a dimension mismatch after " << "changing the nnet: " << priors_.Dim() << " vs. " << nnet_.OutputDim("output"); priors_.Resize(0); } } void AmNnetSimple::SetPriors(const VectorBase<BaseFloat> &priors) { priors_ = priors; if (priors_.Dim() != nnet_.OutputDim("output") && priors_.Dim() != 0) { KALDI_ERR << "Dimension mismatch when setting priors: priors have dim " << priors.Dim() << ", model expects " << nnet_.OutputDim("output"); } } std::string AmNnetSimple::Info() const { std::ostringstream ostr; ostr << "input-dim: " << nnet_.InputDim("input") << " "; ostr << "ivector-dim: " << nnet_.InputDim("ivector") << " "; ostr << "num-pdfs: " << nnet_.OutputDim("output") << " "; ostr << "prior-dimension: " << priors_.Dim() << " "; if (priors_.Dim() != 0) { ostr << "prior-sum: " << priors_.Sum() << " "; ostr << "prior-min: " << priors_.Min() << " "; ostr << "prior-max: " << priors_.Max() << " "; } ostr << "# Nnet info follows. "; return ostr.str() + nnet_.Info(); } void AmNnetSimple::SetContext() { if (!IsSimpleNnet(nnet_)) { KALDI_ERR << "Class AmNnetSimple is only intended for a restricted type of " << "nnet, and this one does not meet the conditions."; } ComputeSimpleNnetContext(nnet_, &left_context_, &right_context_); } } // namespace nnet3 } // namespace kaldi |