Created
December 3, 2015 07:26
-
-
Save AlexanderSavochkin/45969f71b6c73d74644c to your computer and use it in GitHub Desktop.
Piece of code for calculation of likehood gradient in multinomial logistic regression for one training sample
This file contains hidden or 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 MultinomialLogRelModel::gradientForSample(const valarray<double>& sample_features, int label, valarray<double>& gradient) const | |
| { | |
| double sum_exp = 0.0; | |
| valarray<double> features(0.0, (num_sample_features + 1) * num_classes); | |
| valarray<double> gradient_add(0.0, (num_sample_features + 1) * num_classes); | |
| for (int k = 0; k < num_classes; ++k) | |
| { | |
| featurize(sample_features, k, features); | |
| double wf = inner_product(begin(model_weights), end(model_weights), begin(features), 0.0); | |
| double exponent = exp(wf); | |
| gradient_add -= features * exponent; | |
| sum_exp += exponent; | |
| } | |
| gradient_add /= sum_exp; | |
| featurize(sample_features, label, features); | |
| gradient_add += features; | |
| gradient += gradient_add; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment