# How to manipulate gradients in Lux.jl

**URL:** <https://discourse.julialang.org/t/how-to-manipulate-gradients-in-lux-jl/133102>\
**Category:** Machine Learning\
**Tags:** question\
**Created:** [October 13, 2025, 7:20am UTC](https://discourse.julialang.org/t/how-to-manipulate-gradients-in-lux-jl/133102 "2025-10-13T07:20:22Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![IBArbitrary](https://avatars.discourse-cdn.com/v4/letter/i/9de053/32.png) [@IBArbitrary](https://discourse.julialang.org/u/IBArbitrary)\
**Post date:** [October 13, 2025, 7:20am UTC](https://discourse.julialang.org/t/how-to-manipulate-gradients-in-lux-jl/133102/1 "2025-10-13T07:20:22Z")

</div>

I am using `Lux.jl`, and I want to do algebra on the gradients before applying. Currently I convert them to `ComponentArray` and convert them back to `NamedTuple` after I do the necessary algebra. But this requires me to move my `ps` to CPU and back. Is there any way to manipulate where I don’t need to offload to CPU?

A follow up doubt is, how to keep track of the `ps` even after updates, so that I can reset the system back after any updates? I currently use a deepcopied ComponentArray for that, but I believe that is not the efficient way to go about.
