Skip to content

Commit

Permalink
Add back the missing logsumexp overload
Browse files Browse the repository at this point in the history
  • Loading branch information
HanatoK committed Aug 2, 2023
1 parent 3dec411 commit 51d6226
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/colvar_arithmeticpath.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ T logsumexp(const vector<T>& a, const vector<T>& b) {
return max_a + cvm::logn(sum);
}

template <typename T>
vector<T> softmax(const vector<T>& a) {
const auto max_a = *std::max_element(a.begin(), a.end());
T sum = T();
vector<T> out(a.size());
for (size_t i = 0; i < a.size(); ++i) {
sum += cvm::exp(a[i] - max_a);
}
for (size_t i = 0; i < a.size(); ++i) {
out[i] = cvm::exp(a[i] - max_a) / sum;
}
return out;
}

template <typename T>
T logsumexp(const vector<T>& a) {
const auto max_a = *std::max_element(a.begin(), a.end());
Expand Down

0 comments on commit 51d6226

Please sign in to comment.