# Testing your DP model after training

**URL:** <https://discourse.julialang.org/t/testing-your-dp-model-after-training/62268>\
**Category:** Machine Learning\
**Created:** [June 2, 2021, 11:57am UTC](https://discourse.julialang.org/t/testing-your-dp-model-after-training/62268 "2021-06-02T11:57:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Linda\_tK](https://avatars.discourse-cdn.com/v4/letter/l/f17d59/32.png) [@Linda\_tK](https://discourse.julialang.org/u/Linda_tK)\
**Post date:** [June 2, 2021, 11:57am UTC](https://discourse.julialang.org/t/testing-your-dp-model-after-training/62268/1 "2021-06-02T11:57:26Z")

</div>

Hi there, I have trained a model to approximate a system of differential equations using certain initial conditions and parameters. I would now like to see how my models performs if I use different starting conditions. I thus want to test it, because I’ve already trained my model, but I have no clue how to do it. Can somebody help me?

Here is my programme:  
#Lotka-Volterra  
using DiffEqFlux, OrdinaryDiffEq, Flux, Optim, Plots

u0 = Float32[2.; 3.] #initial conditions  
datasize = 50 #timesteps  
tspan = (0.0f0,2.5f0) #timespan  
#System of differential equations  
function trueODEfunc(du,u,p,t)  
x, y = u  
a, b, c, d = p  
du[1] = dx = a_x - b_x_y  
du[2] = dy = -c_y + d_x_y  
end  
p = [1.5,1.0,3.0,1.0] #initial parameters  
t = range(tspan[1],tspan[2],length=datasize) #timespan + steps  
prob = ODEProblem(trueODEfunc,u0,tspan,p) #makes the ODE problem  
ode\_data = Array(solve(prob,Tsit5(),saveat=t)) #generates the data

dudt2 = Chain(Dense(2,50,relu), Dense(50,2)) #define a multilayer perceptron with 1 hidden layer and a relu activation function  
p,re = Flux.destructure(dudt2) # use this p as the initial condition!  
dudt(u,p,t) = re(p)(u) # need to restrcture for backprop!  
prob2 = ODEProblem(dudt,u0,tspan)

#prediction function  
function predict\_n\_ode()  
Array(solve(prob2,Tsit5(),u0=u0,p=p,saveat=t))  
end  
#loss function  
function loss\_n\_ode()  
pred = predict\_n\_ode()  
loss = sum(abs2,ode\_data .- pred)  
loss  
end

loss\_n\_ode() # n\_ode.p stores the initial parameters of the neural ODE  
#makes the graphs and displays the loss for every cycle through the dataset  
cb = function (;doplot=false) #callback function to observe training  
pred = predict\_n\_ode()  
display(sum(abs2,ode\_data .- pred))  
display(sum(abs2,ode\_data[1,:] .- pred[1,:]))  
display(sum(abs2,ode\_data[2,:] .- pred[2,:]))

# plot current prediction against data

pl = Plots.scatter(t,ode\_data[1,:],label=“data prey”)  
Plots.scatter!(pl,t,ode\_data[2,:],label=“data predator”)  
Plots.scatter!(pl,t,pred[1,:],label=“prediction prey”)  
Plots.scatter!(pl,t,pred[2,:],label=“prediction predator”)  
display(plot(pl))  
return false  
end

# Display the ODE with the initial parameter values.

epochs = 300  
learningrate = 0.01  
data = Iterators.repeated((), epochs)

Flux.train!(loss\_n\_ode, Flux.params(u0,p), data, ADAM(learningrate), cb = cb)

---

<div class="post-metadata">

**Author:** ![frankschae](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frankschae/32/8382_2.png) [@frankschae](https://discourse.julialang.org/u/frankschae)\
**Post date:** [June 3, 2021, 4:48pm UTC](https://discourse.julialang.org/t/testing-your-dp-model-after-training/62268/2 "2021-06-03T16:48:14Z")

</div>

You could wrap your `prob2` in an ensemble problem:  
[https://diffeq.sciml.ai/stable/features/ensemble/#Random-Initial-Conditions](https://diffeq.sciml.ai/stable/features/ensemble/#Random-Initial-Conditions)

```julia
function prob_func(prob,i,repeat)
  remake(prob,u0=rand()*prob.u0)
end

```

then allows you to define different initial conditions.
