Created
August 12, 2018 16:19
-
-
Save mrm1001/713bb3e18fbdeaa0e1f889798b2bbd91 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::computeOutputSoftmax(Vector& hidden, Vector& output) const { | |
if (quant_ && args_->qout) { | |
output.mul(*qwo_, hidden); | |
} else { | |
output.mul(*wo_, hidden); | |
} | |
real max = output[0], z = 0.0; | |
for (int32_t i = 0; i < osz_; i++) { | |
max = std::max(output[i], max); | |
} | |
for (int32_t i = 0; i < osz_; i++) { | |
output[i] = exp(output[i] - max); | |
z += output[i]; | |
} | |
for (int32_t i = 0; i < osz_; i++) { | |
output[i] /= z; | |
} | |
} | |
void Model::computeOutputSoftmax() { | |
computeOutputSoftmax(hidden_, output_); | |
} | |
real Model::softmax(int32_t target, real lr) { | |
grad_.zero(); | |
computeOutputSoftmax(); | |
for (int32_t i = 0; i < osz_; i++) { | |
real label = (i == target) ? 1.0 : 0.0; | |
real alpha = lr * (label - output_[i]); | |
grad_.addRow(*wo_, i, alpha); | |
wo_->addRow(hidden_, i, alpha); | |
} | |
return -log(output_[target]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment