Skip to content

Commit

Permalink
add softmax function
Browse files Browse the repository at this point in the history
  • Loading branch information
suzusuzu committed Mar 22, 2018
1 parent 0032fbf commit b351385
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
33 changes: 15 additions & 18 deletions src/nupic/algorithms/SDRClassifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,7 @@ void SDRClassifier::infer_(const vector<UInt> &patternNZ,
add(likelihoods->begin(), likelihoods->end(), weights.begin(bit),
weights.begin(bit + 1));
}
Real64 maxLikelihoods = *max_element(likelihoods->begin(), likelihoods->end());
for (auto likelihood : *likelihoods) {
likelihood -= maxLikelihoods;
}
range_exp(1.0, *likelihoods);
Real64 sumLikelihoods = accumulate(likelihoods->begin(), likelihoods->end(), 0);
for (auto likelihood : *likelihoods) {
likelihood /= sumLikelihoods;
}
softmax_(likelihoods->begin(), likelihoods->end());
}
}

Expand All @@ -226,15 +218,7 @@ vector<Real64> SDRClassifier::calculateError_(const vector<UInt> &bucketIdxList,
add(likelihoods.begin(), likelihoods.end(), weights.begin(bit),
weights.begin(bit + 1));
}
Real64 maxLikelihoods = *max_element(likelihoods.begin(), likelihoods.end());
for (auto likelihood : likelihoods) {
likelihood -= maxLikelihoods;
}
range_exp(1.0, likelihoods);
Real64 sumLikelihoods = accumulate(likelihoods.begin(), likelihoods.end(), 0);
for (auto likelihood : likelihoods) {
likelihood /= sumLikelihoods;
}
softmax_(likelihoods.begin(), likelihoods.end());

// compute target likelihoods
vector<Real64> targetDistribution(maxBucketIdx_ + 1, 0.0);
Expand All @@ -246,6 +230,19 @@ vector<Real64> SDRClassifier::calculateError_(const vector<UInt> &bucketIdxList,
return likelihoods;
}

template <typename Iterator>
void SDRClassifier::softmax_(Iterator begin, Iterator end) {
Iterator maxItr= max_element(begin, end);
for (auto itr = begin; itr != end; ++itr) {
*itr -= *maxItr;
}
range_exp(1.0, begin, end);
typename std::iterator_traits<Iterator>::value_type sum = accumulate(begin, end, 0);
for (auto itr = begin; itr != end; ++itr) {
*itr /= sum;
}
}

UInt SDRClassifier::version() const { return version_; }

UInt SDRClassifier::getVerbosity() const { return verbosity_; }
Expand Down
4 changes: 4 additions & 0 deletions src/nupic/algorithms/SDRClassifier.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ class SDRClassifier : public Serializable<SdrClassifierProto> {
vector<Real64> calculateError_(const vector<UInt> &bucketIdxList,
const vector<UInt> patternNZ, UInt step);

// softmax function
template <typename Iterator>
void softmax_(Iterator begin, Iterator end);

// The list of prediction steps to learn and infer.
vector<UInt> steps_;

Expand Down

0 comments on commit b351385

Please sign in to comment.