Code example for stop_gradient()
#580
Answered
by
awni
RahulBhalley
asked this question in
Q&A
-
Could someone please help me with a code example how to use |
Beta Was this translation helpful? Give feedback.
Answered by
awni
Jan 29, 2024
Replies: 1 comment
-
def fun(x):
return mx.exp(x) + mx.stop_gradient(mx.exp(x))
print(mx.grad(fun)(mx.array(1.0))) Gives So there you would only get the gradient through the first Compare to: def fun(x):
return mx.exp(x) + mx.exp(x)
print(mx.grad(fun)(mx.array(1.0))) Which gives you twice the result of the first (grad through both paths). Gives |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
RahulBhalley
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Gives
array(2.71828, dtype=float32)
.So there you would only get the gradient through the first
mx.exp
.Compare to:
Which gives you twice the result of the first (grad through both paths). Gives
array(5.43656, dtype=float32)
.