This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set.seed(123) | |
nba_trim <- dplyr::sample_n(NBA_Draft_Data, size=100) | |
nba_trim_Lottery <- ifelse(nba_trim$Pick.Number<=14,1,0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Flux, Plots | |
using Base.Iterators: repeated | |
using Flux: @epochs | |
using LinearAlgebra | |
gridsize = 150; | |
tstart=0.0 | |
tend=20.0 | |
myfunc(x) = sin(x); | |
x = collect(range(tstart,stop=tend,length=gridsize)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using DifferentialEquations, Flux, Optim, DiffEqFlux, Plots | |
function lotka_volterra(du,u,p,t) | |
x, y = u | |
α, β, δ, γ = p | |
du[1] = dx = α*x - β*x*y | |
du[2] = dy = -δ*y + γ*x*y | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using DifferentialEquations, Flux, Optim, DiffEqFlux, Plots | |
u0 = [1.0,1.0] | |
tstart=0.0 | |
tend=10.0 | |
sampling=0.1 | |
model_params= [1.5,1.0,3.0,1.0] | |
function model(du,u,p,t) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function predict_adjoint(param) # Our 1-layer neural network | |
prob=ODEProblem(model,[1.0,0.0],(tstart,tend), model_params) | |
Array(concrete_solve(prob,Tsit5(),param[1:2],param[3:end],saveat=tstart:sampling:tend, abstol=1e-8,reltol=1e-6)) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Generate some data to fit, and add some noise to it | |
data=predict_adjoint([1.0,1.0,1.5,1.0,3.0,1.0]) | |
σN=0.1 | |
data+=σN*randn(size(data)) | |
data=abs.(data) #Keep measurements positive | |
# Returning more than just the loss function breaks the Flux optim | |
function loss_adjoint(param) | |
prediction = predict_adjoint(param) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function train_model(;pguess=[0.8,1.2,1.2,1.0,2.9,1.1]) | |
println("The initial loss function is $(loss_adjoint(pguess)[1])") | |
#Train the ODE | |
resinit=DiffEqFlux.sciml_train(loss_adjoint,pguess,ADAM(), maxiters=3000) | |
res = DiffEqFlux.sciml_train(loss_adjoint,resinit.minimizer,BFGS(initial_stepnorm = 1e-5)) | |
println("The parameters are $(res.minimizer) with final loss value $(res.minimum)") | |
return(res) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using DifferentialEquations, Flux, Optim, DiffEqFlux, Plots | |
model_params=[10.0,28.0,8.0/3] | |
all_ic=[[1.0,0.0,0.0],[0.5,1.0,0.0], [3,0.2,0.1]]; | |
tend=10.0 | |
tstart=0.0 | |
sampling=0.05 | |
function model(du,u,p,t) | |
x,y,z = u |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function predict_adjoint(param) # Our 1-layer neural network | |
prob=ODEProblem(model,[1.0,0.0,0.0],(tstart,tend), model_params) | |
Array(concrete_solve(prob,Tsit5(),param[1:3],param[4:end],saveat=tstart:sampling:tend,abstol=1e-8,reltol=1e-6)) | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Generate some data to fit, and add some noise to it | |
data=predict_adjoint([1.0,0.0,0.0,10.0,28.0,8.0/3]) | |
σN=0.05 | |
data+=σN*randn(size(data)) | |
function loss_adjoint(param) | |
prediction = predict_adjoint(param) | |
loss = sum(abs2,prediction - data) | |
loss | |
end |
OlderNewer