Skip to content

Commit 24ee460

Browse files
committed
Preserve cost names through xtensor gradient collapse
1 parent e9f4c7e commit 24ee460

2 files changed

Lines changed: 8 additions & 0 deletions

File tree

pytensor/xtensor/gradient.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ def visit_input(inp: Variable) -> None:
141141
return lowered
142142
unit = OpFromGraph(dummies, [lowered], inline=True)
143143
[new_exit] = unit(*outer_inputs, return_list=True)
144+
new_exit.name = exit_var.name
144145
return cast(Variable, new_exit)
145146

146147

tests/xtensor/test_grad.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ def test_grad_repeated_input():
7979
np.testing.assert_allclose(g, power * x_test ** (power - 1))
8080

8181

82+
def test_grad_preserves_cost_name():
83+
xt = pt.vector("x", shape=(3,))
84+
loss = (as_xtensor(xt, dims=("a",)) ** 2).sum().values
85+
loss.name = "loss"
86+
assert pt.grad(loss, xt).name == "(dloss/dx)"
87+
88+
8289
def test_grad_second_order():
8390
W = pytensor.shared(np.ones((3, 2)), name="W")
8491
xt = pt.vector("x", shape=(3,))

0 commit comments

Comments
 (0)