# Training Neural SDEs with Mutating Arrays

**URL:** <https://discourse.julialang.org/t/training-neural-sdes-with-mutating-arrays/91877>\
**Category:** Machine Learning\
**Created:** [December 19, 2022, 11:57pm UTC](https://discourse.julialang.org/t/training-neural-sdes-with-mutating-arrays/91877 "2022-12-19T23:57:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ChrisVL1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisvl1/32/45240_2.png) [@ChrisVL1](https://discourse.julialang.org/u/ChrisVL1)\
**Post date:** [December 19, 2022, 11:57pm UTC](https://discourse.julialang.org/t/training-neural-sdes-with-mutating-arrays/91877/1 "2022-12-19T23:57:48Z")

</div>

I am writing a training function that requires a loop as my loss is the difference between call prices which I need to calculate for various strikes using a loop. However, this results in a mutating array, how do I get around this?

```julia
function predict_neuralsde(p, u = S₀) 
  return Array(Pre_NSDE(u, p)) # Returns an array of stock prices/SDE solutions at each time point. 
end

function loss_neuralsde(p; n = 10000)
u = repeat(reshape(S₀, :, 1), 1, n)
stock = predict_neuralsde(p,u)[1,:,end]
    for i = 1:length(K)
NSDE_call[i] = exp(-r*tspan[2]).*mean(maximum([terminal_values .- K[i] zeros(size(terminal_values))], dims = 2))
end 
NSDE_call
    
loss = sum(abs2, NSDE_call - BS_Price')  
 return loss , NSDE_call  
end  

opt = ADAM(0.025)

adtype = Optimization.AutoZygote()
optf = Optimization.OptimizationFunction((x,p) -> loss_neuralsde(x, n=10000), adtype)
optprob = Optimization.OptimizationProblem(optf, Pre_NSDE.p)
result1 = Optimization.solve(optprob, opt, callback = callback, maxiters = 200) # 200 iterations of training.

```

The error message is as follows:

```julia
Mutating arrays is not supported -- called setindex!(Matrix{Float64}, ...)
This error occurs when you ask Zygote to differentiate operations that change
the elements of arrays in place (e.g. setting values with x .= ...)

```

---

<div class="post-metadata">

**Author:** ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)\
**Post date:** [December 20, 2022, 12:16am UTC](https://discourse.julialang.org/t/training-neural-sdes-with-mutating-arrays/91877/2 "2022-12-20T00:16:51Z")

</div>

Unfortunately it’s difficult to help without a MWE, see [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757) for more.

One thing to test while you’re working on that MWE: instead of using a loop here and assigning to some array, just use `map` or an array comprehension over the range. It should be just as fast and much more AD-friendly to boot.

> [@ChrisVL1](#):
>
> ```julia
> for i = 1:length(K)
> NSDE_call[i] = exp(-r*tspan[2]).*mean(maximum([terminal_values .- K[i] zeros(size(terminal_values))], dims = 2))
> end 
> 
> ```

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [December 20, 2022, 7:46am UTC](https://discourse.julialang.org/t/training-neural-sdes-with-mutating-arrays/91877/3 "2022-12-20T07:46:51Z")

</div>

Quadruplicate of [Training with Mutating Arrays - #2 by ChrisRackauckas](https://discourse.julialang.org/t/training-with-mutating-arrays/91876/2), answered there. Please don’t quadruple post ([Training Neural SDEs with Mutating Arrays · Issue #785 · SciML/DiffEqFlux.jl · GitHub](https://github.com/SciML/DiffEqFlux.jl/issues/785)).
