Created
August 12, 2018 16:11
-
-
Save mrm1001/46904b5d6a1611c5026e2e9bfb21135f to your computer and use it in GitHub Desktop.
FastText
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Model::computeHidden(const std::vector<int32_t>& input, Vector& hidden) const { | |
assert(hidden.size() == hsz_); | |
hidden.zero(); | |
for (auto it = input.cbegin(); it != input.cend(); ++it) { | |
if(quant_) { | |
hidden.addRow(*qwi_, *it); | |
} else { | |
hidden.addRow(*wi_, *it); | |
} | |
} | |
hidden.mul(1.0 / input.size()); | |
} | |
void Model::update(const std::vector<int32_t>& input, int32_t target, real lr) { | |
assert(target >= 0); | |
assert(target < osz_); | |
if (input.size() == 0) return; | |
computeHidden(input, hidden_); | |
if (args_->loss == loss_name::ns) { | |
loss_ += negativeSampling(target, lr); | |
} else if (args_->loss == loss_name::hs) { | |
loss_ += hierarchicalSoftmax(target, lr); | |
} else { | |
loss_ += softmax(target, lr); | |
} | |
nexamples_ += 1; | |
if (args_->model == model_name::sup) { | |
grad_.mul(1.0 / input.size()); | |
} | |
for (auto it = input.cbegin(); it != input.cend(); ++it) { | |
wi_->addRow(grad_, *it, 1.0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment