-
-
Notifications
You must be signed in to change notification settings - Fork 193
Changed calculation of log_sum_exp(x1, x2) #1772
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5fd8e2b
0869a16
2f5432f
8aa6a50
b513358
3a03bd9
088ee0c
817dec8
67a1b13
829be7f
c261931
197831a
62a3c1f
a793873
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -3,11 +3,11 @@ | |||
|
||||
#include <stan/math/rev/meta.hpp> | ||||
#include <stan/math/rev/core.hpp> | ||||
#include <stan/math/rev/fun/calculate_chain.hpp> | ||||
#include <stan/math/rev/fun/typedefs.hpp> | ||||
#include <stan/math/prim/meta.hpp> | ||||
#include <stan/math/prim/fun/constants.hpp> | ||||
#include <stan/math/prim/fun/Eigen.hpp> | ||||
#include <stan/math/prim/fun/inv_logit.hpp> | ||||
#include <stan/math/prim/fun/log_sum_exp.hpp> | ||||
#include <cmath> | ||||
#include <vector> | ||||
|
@@ -22,8 +22,8 @@ class log_sum_exp_vv_vari : public op_vv_vari { | |||
log_sum_exp_vv_vari(vari* avi, vari* bvi) | ||||
: op_vv_vari(log_sum_exp(avi->val_, bvi->val_), avi, bvi) {} | ||||
void chain() { | ||||
avi_->adj_ += adj_ * calculate_chain(avi_->val_, val_); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be possible to get rid of calculate_chain everywhere? It seems like a strange function. Also used here (which should be fixed as part of this pull): math/stan/math/rev/fun/log_sum_exp.hpp Line 37 in b513358
In stan/math/rev/fun/log_diff_exp.hpp and in stan/math/rev/fun/log1p_exp.hpp. |
||||
bvi_->adj_ += adj_ * calculate_chain(bvi_->val_, val_); | ||||
avi_->adj_ += adj_ * inv_logit(avi_->val_ - bvi_->val_); | ||||
bvi_->adj_ += adj_ * inv_logit(bvi_->val_ - avi_->val_); | ||||
} | ||||
}; | ||||
class log_sum_exp_vd_vari : public op_vd_vari { | ||||
|
@@ -34,7 +34,7 @@ class log_sum_exp_vd_vari : public op_vd_vari { | |||
if (val_ == NEGATIVE_INFTY) { | ||||
avi_->adj_ += adj_; | ||||
} else { | ||||
avi_->adj_ += adj_ * calculate_chain(avi_->val_, val_); | ||||
avi_->adj_ += adj_ * inv_logit(avi_->val_ - bd_); | ||||
} | ||||
} | ||||
}; | ||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#include <stan/math/rev.hpp> | ||
#include <gtest/gtest.h> | ||
#include <test/unit/math/rev/fun/util.hpp> | ||
#include <test/unit/math/rev/util.hpp> | ||
|
||
TEST(log_sum_exp_tests, large_values) { | ||
using stan::math::var; | ||
|
||
// check autodiffing works with var types with large values | ||
var a = 1e50; | ||
var output = stan::math::log_sum_exp(a, a); | ||
output.grad(); | ||
EXPECT_FLOAT_EQ(output.val(), log(2.0) + value_of(a)); | ||
EXPECT_FLOAT_EQ(a.adj(), 1.0); | ||
|
||
var a2 = 1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Once you remove the other calculate_chain thing we'll need some tests where a1 is a var and a2 is a double. |
||
var a3 = 1e50; | ||
var output2 = stan::math::log_sum_exp(a2, a3); | ||
output2.grad(); | ||
EXPECT_FLOAT_EQ(a2.adj(), 0.0); | ||
EXPECT_FLOAT_EQ(a3.adj(), 1.0); | ||
|
||
var a4 = 1e50; | ||
var a5 = 1; | ||
var output3 = stan::math::log_sum_exp(a4, a5); | ||
output3.grad(); | ||
EXPECT_FLOAT_EQ(a4.adj(), 1.0); | ||
EXPECT_FLOAT_EQ(a5.adj(), 0.0); | ||
|
||
// check autodiffing works with var types with large values | ||
var b = 1e20; | ||
var output6 = stan::math::log_sum_exp(b, b); | ||
output6.grad(); | ||
EXPECT_FLOAT_EQ(output6.val(), log(2.0) + value_of(b)); | ||
EXPECT_FLOAT_EQ(b.adj(), 1.0); | ||
|
||
var b2 = -2; | ||
var b3 = 1e20; | ||
var output7 = stan::math::log_sum_exp(b2, b3); | ||
output7.grad(); | ||
EXPECT_FLOAT_EQ(b2.adj(), 0.0); | ||
EXPECT_FLOAT_EQ(b3.adj(), 1.0); | ||
|
||
var b4 = 1e20; | ||
var b5 = -2; | ||
var output8 = stan::math::log_sum_exp(b4, b5); | ||
output8.grad(); | ||
EXPECT_FLOAT_EQ(b4.adj(), 1.0); | ||
EXPECT_FLOAT_EQ(b5.adj(), 0.0); | ||
|
||
// check arguement combinations of vars and doubles | ||
var a6 = 1e50; | ||
double a7 = 1; | ||
var output4 = stan::math::log_sum_exp(a6, a7); | ||
output4.grad(); | ||
EXPECT_FLOAT_EQ(a6.adj(), 1.0); | ||
|
||
var a8 = 1; | ||
double a9 = 1e50; | ||
var output5 = stan::math::log_sum_exp(a8, a9); | ||
output5.grad(); | ||
EXPECT_FLOAT_EQ(a8.adj(), 0.0); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no way to use inv_logit here? If so, that's fine, just checking.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see how you could because one has the extra 1 term inside and the other outside.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh good point. I was too lazy to actually think through the math at all.