Training NN with Julia's Flux - Loss function with derivative of output and functions of output

Viewed 144

I want to run this NN in which input is time over some interval. There's no label, and the loss function requires the derivative of the outputs and a specified function (H in my code), which is also a function of the outputs. I believe my loss function is not properly set yet. I also would like to see how the loss function decreases, to see how close to the actual function I am getting, but I don't seem to find a away to see how the loss function progresses.

Here is my new code:

using Flux, Zygote, ForwardDiff

##Data
t=vcat(0:0.1:4)

##Problem parameters
α = 2; C = 1; β = 0.5; P = 1; π₀ = 0.5

#Initial and final conditions
x₀ = 0.5
p₄ = 1
t₀ = 0
t = 4

#Hidden layer length 
len_hidden=5

X = Chain(Dense(1,len_hidden),Dense(len_hidden,1,relu))
x(t) = (t - t₀)*X([t])[1] + x₀
dxdt(t) = ForwardDiff.derivative(x,t)

Ρ = Chain(Dense(1,len_hidden),Dense(len_hidden,1,relu))
p(t) = p₄ + (t - t)*Ρ([t])[1]
dpdt(t) = ForwardDiff.derivative(p,t)

U = Chain(Dense(1,len_hidden),Dense(len_hidden,1,relu))
u(t) = U([t])[1] 

Θ = Flux.params(X,Ρ,U)

H(x,p,u) = α*u*x - C*u^2 + p*β*x*(1 - x)*(P*u - π₀)

#Partials
dHdx(t) = α*u(t) + p(t)*(1 - x(t))*β*(P*u(t) - π₀) - p(t)*x(t)*(P*u(t) - π₀)
dHdp(t) = (1 - x(t))*x(t)*β*(P*u(t) - π₀)
dHdu(t) = α*x(t) - 2*C*u(t) + P*p(t)*β*x(t)*(1 - x(t))

#Loss function
function loss(t)
    return (-dxdt(t) + dHdp(t))^2 + (dpdt(t) + dHdx(t))^2 + (dHdu(t))^2 
end

opt=Descent()
parameters=Θ
data=t

Flux.train!(loss, parameters, data, opt, cb = () -> println("Training"))

Is the way I wrote the loss function correct? For each time instant (which is my data vector), am I computing the loss with the updated value of each function in loss()? So far, x(0) becomes the imposed initial condition, however it stays constant for all other time instants, which makes me think the loss is not being evaluated and minimized over time taking in considerations the evolution of all other functions.

0 Answers
Related