Repository navigation
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 |
Answered by
awni
Jan 29, 2024
Replies: 2 comments 5 replies
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 |
0 replies
Answer selected by
RahulBhalley
|
@awni It may be helpful to have a no_grad decorator to simplify the following: import functools
import mlx.core as mx
def no_grad(fn):
@functools.wraps(fn)
def wrapper(*args, **kwargs):
out = fn(*args, **kwargs)
if isinstance(out, mx.array):
return mx.stop_gradient(out)
elif isinstance(out, (tuple, list)):
return type(out)(
mx.stop_gradient(x) if isinstance(x, mx.array) else x for x in out
)
else:
return out
return wrapper
@no_grad
def fun(x):
return mx.exp(x) + mx.exp(x)
print(mx.grad(fun)(mx.array(1.0)))
# array(0, dtype=float32) |
5 replies
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).