-
Notifications
You must be signed in to change notification settings - Fork 145
Add rewrite for softplus(log(x)) -> log1p(x)
#1452
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 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2010,6 +2010,27 @@ def test_exp_softplus(self, exp_op): | |
decimal=6, | ||
) | ||
|
||
def test_softplus_log(self): | ||
# softplus(log(x)) -> log1p(x) | ||
data_valid = np.random.random((4, 3)).astype("float32") * 2 | ||
data_valid[0, 0] = 0 # edge case | ||
data_invalid = data_valid - 2 | ||
|
||
x = fmatrix() | ||
f = function([x], softplus(log(x)), mode=self.mode) | ||
|
||
graph = f.maker.fgraph.toposort() | ||
ops_graph = [ | ||
node | ||
for node in graph | ||
if isinstance(node.op, Elemwise) | ||
and isinstance(node.op.scalar_op, ps.Log | ps.Exp | ps.Softplus) | ||
] | ||
assert len(ops_graph) == 0 | ||
|
||
expected = np.log1p(data_valid) | ||
np.testing.assert_almost_equal(f(data_valid), expected) | ||
assert np.all(np.isnan(f(data_invalid))) | ||
|
||
@pytest.mark.parametrize( | ||
["nested_expression", "expected_switches"], | ||
[ | ||
|
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.
Nitpick I prefer to refer to it by
log1pexp
, which we have as an alias to softplus:pytensor/pytensor/tensor/math.py
Line 2474 in ff98ab8
Also we can add a similar case for
log1mexp
?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 tested this (with the code below) and it works fine in its domain [0, 1]. I will add it too.