Skip to content

Commit

Permalink
style updates
Browse files Browse the repository at this point in the history
  • Loading branch information
PGimenez committed Nov 11, 2024
1 parent 4062cf6 commit a47e69b
Show file tree
Hide file tree
Showing 7 changed files with 1,003 additions and 73 deletions.
1 change: 1 addition & 0 deletions .theme
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
usertheme
28 changes: 16 additions & 12 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
name = "SeriesForecast"
imgname = "forecast"
version = "0.0.2"

[deps]
BSON = "fbb218c0-5317-5bc6-957e-2ee96dd4b1f0"
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
Observables = "510215fc-4207-5dde-b226-833fc4488ee2"
BSON = "fbb218c0-5317-5bc6-957e-2ee96dd4b1f0"
Optimization = "7f7a1694-90dd-40f0-9382-eb1efda571ba"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
ComponentArrays = "b0b7db55-cfe3-40fc-9ded-d10e2dbeff66"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Interpolations = "a98d9a8b-a2ab-59e6-89dd-64a1c18fca59"
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
PlotlyBase = "a03496cd-edff-5a9b-9e67-9cda94a718b5"
Lux = "b2108857-7c20-44ae-9111-449ecde12c47"
DiffEqFlux = "aae7a2af-3d4f-5e19-a356-7da93b79d9d0"
DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa"
Flux = "587475ba-b771-5e3f-ad9e-33799f191a9c"
GenieFramework = "a59fdf5c-6bf0-4f5d-949c-a137c9e2f353"
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
Interpolations = "a98d9a8b-a2ab-59e6-89dd-64a1c18fca59"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
Lux = "b2108857-7c20-44ae-9111-449ecde12c47"
Observables = "510215fc-4207-5dde-b226-833fc4488ee2"
Optimization = "7f7a1694-90dd-40f0-9382-eb1efda571ba"
DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa"
OptimizationOptimisers = "42dfb2eb-d2b4-4451-abcd-913932933ac1"
PlotlyBase = "a03496cd-edff-5a9b-9e67-9cda94a718b5"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"

[Genie]
instantiated_pwd = "/Users/pere/genie/genieawesome/SeriesForecast"
11 changes: 6 additions & 5 deletions app.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const obs_grid = 4:4:20 # we train on an increasing amount of the first k obs
const maxiters = 150
const lr = 5e-3
const N_steps = 100 # number of points in prediction over the full time range
_, _, init_state = neural_ode(train_df.t, length(features))
_, θ, init_state = neural_ode(train_df.t, length(features))
t_grid = range(minimum(data.t), maximum(data.t), length=N_steps) |> collect
# We can cache the predictions for the full time range to avoid recomputing them
#= const ŷ_cached = predict(Vector(train_df[1,features]), t_grid, θ, init_state) =#
Expand All @@ -64,16 +64,17 @@ t_grid = range(minimum(data.t), maximum(data.t), length=N_steps) |> collect
@out mse = [0.0,0.0,0.0,0.0]
@private k = 20

@private state = init_state
@private state::Any = init_state
@onbutton start begin
println("Training")
# change k and r to display the correct number of training points and the prediction over the entire range
k=1; r[!] = 100;
@show size(Matrix(train_df[!,features]))
# We pass the Observable version of θ to `train` , which will update its value during training.
# When the training is finished we store the final value in θ
θ, state = train(Vector(train_df[!,:t]), Matrix(train_df[!,features]), obs_grid, maxiters, lr, rng, __model__.θ; progress=true);
@save "params.jld2" θ
θ, state = train(Vector(train_df[!,:t]), Matrix(train_df[!,features]), obs_grid, lr, rng, __model__.θ; maxiters=maxiters);

JLD2.save("params.jld2"; θ)
end
@onchange r begin
k = 20
Expand All @@ -90,7 +91,7 @@ t_grid = range(minimum(data.t), maximum(data.t), length=N_steps) |> collect
= predict(Vector(train_df[1,features]), t_predict, θ, state)
predict_df = DataFrame(t = t_predict, meantemp = ŷ[1,:], humidity = ŷ[2,:], wind_speed = ŷ[3,:], meanpressure = ŷ[4,:])
temp_pdata, hum_pdata, wind_pdata, press_pdata = get_traces(data[1:k,:], data[k+1:end,:], predict_df, scaling)

mse = [calc_mse(t_predict, ŷ[i,:], interpolators[i]) for i in 1:4]
end

Expand Down
100 changes: 51 additions & 49 deletions app.jl.html
Original file line number Diff line number Diff line change
@@ -1,69 +1,71 @@
<header class="st-header q-pa-sm" style="text-align:center">
<header id="ixsk" class="st-header q-pa-sm">
<h1 class="st-header__title text-h3">Weather data forecast</h1>
</header>

<div class="row st-module" style="padding:15px">
<div class=" col-6">
<h4 style="text-align:center;margin:0px">Mean-squared error</h4>
<div class="row">
<st-big-number :number="mse[0]" title="Temperature" ></st-big-number>
<st-big-number :number="mse[1]" title="Humidity" ></st-big-number>
<st-big-number :number="mse[2]" title="Wind" ></st-big-number>
<st-big-number :number="mse[3]" title="Pressure" ></st-big-number>
<div class="row">
<div id="iivt7" class="st-module">
<h6 id="iqanm"><span id="imrvv">Mean-squared error</span></h6>
<div id="i2x43">
<st-big-number id="ilrgj" :number="mse[0]" title="Temperature"></st-big-number>
<st-big-number id="i8o8q" :number="mse[3]" title="Pressure"></st-big-number>
<st-big-number id="i1dwj" :number="mse[2]" title="Wind"></st-big-number>
<st-big-number id="iki2r" :number="mse[1]" title="Humidity"></st-big-number>
</div>
</div>
<q-separator vertical />

<div class="col-4 col-sm" style="padding-left:50px;padding-top:15px">
<q-badge color="secondary">
<div class="col-12 col-sm st-module">
<div id="iaizc" class="col-4 col-sm">
<q-badge id="inm59" color="primary">
Prediction horizon (samples)
</q-badge>
<q-slider :min=30 v-model="r" :max=100 :step=1 label-always></q-slider>
<q-badge color="secondary">
<q-slider id="in2wj" :min="30" v-model="r" :max="100" :step="1" :label-always="true"></q-slider>
<q-badge id="i48xk" color="primary">
Prediction step size (samples)
</q-badge>
<div class="q-gutter-sm">
<q-radio val=1 label="1" v-model="pstep"></q-radio>
<q-radio val=2 label="2" v-model="pstep"></q-radio>
<q-radio val=5 label="5" v-model="pstep"></q-radio>
<div id="ihdmj" class="q-gutter-sm">
<q-radio id="im7zq" val="1" label="1" v-model="pstep"></q-radio>
<q-radio id="iamcg" val="2" label="2" v-model="pstep"></q-radio>
<q-radio id="izhd9" val="5" label="5" v-model="pstep"></q-radio>
</div>
</div>
<div class="col-2 col-sm" style="text-align:center">
<q-btn style="width:87px" :loading="start" label="Train" v-on:click="start = true" color="$button_color" :disable="$prod_mode">
<q-tooltip>
</div>
<div id="i0k0w" class="st-module">
<div id="ikvoi" class="container">
<q-btn id="ifwxl" :loading="start" label="Train" v-on:click="start = true" color="$button_color" :disable="$prod_mode">
<q-tooltip id="i80mg">
$button_tooltip
</q-tooltip>
</q-btn><br>
<q-btn style="margin-top:15px" :loading="animate" label="Animate" v-on:click="animate = true" color="primary"></q-btn><br>
<q-btn style="margin-top:15px" color="secondary" label="App info">
<q-popup-proxy>
</q-btn>
<q-btn id="i9sqz" :loading="animate" label="Animate" v-on:click="animate = true" color="primary"></q-btn>
<q-btn id="il288" color="primary" label="App info">
<q-popup-proxy id="i14xm">
<q-banner>
This app uses a neural ordinary differential equation (NODE) to forecast weather data from Delhi. The forecast is implemented with the DiffeqFlux, DifferentialEquations, Optimization and Lux packages, and the code is based on the <a href="https://sebastiancallh.github.io/post/neural-ode-weather-forecast/">blog post</a> by Sebastian Callh.
</q-banner>
</q-popup-proxy>
</q-btn>
</div></div>
</div>
</div>
</div>
<div class="row">
<div class="st-col col-12 st-module">
<div class="row">
<div class="st-col col-12 st-module">
<div class="row">
<div class="st-col col-12 col-sm "style="padding-right:10px">
<h4 style="text-align:center;margin-bottom:10px">Temperature</h4>
<plotly :data="temp_pdata" :layout="temp_layout" :displaylogo="false"></plotly>
</div>
<div class="st-col col-12 col-sm "style="padding-left:10px">
<h4 style="text-align:center;margin-bottom:10px">Humidity</h4>
<plotly :data="hum_pdata" :layout="hum_layout" :displaylogo="false"></plotly>
</div>
</div>
<div class="row">
<div class="st-col col-12 col-sm " style="padding-right:10px">
<h4 style="text-align:center;margin:0px;margin-bottom:10px">Wind</h4>
<plotly :data="wind_pdata" :layout="wind_layout" :displaylogo="false"></plotly>
</div>
<div class="st-col col-12 col-sm " style="padding-left:10px">
<h4 style="text-align:center;margin:0px;margin-bottom:10px">Pressure</h4>
<plotly :data="press_pdata" :layout="press_layout" :displaylogo="false"></plotly>
</div>
</div>
<div id="i5f5e" class="st-col col-12 col-sm">
<h4 id="ixb7p">Temperature</h4>
<plotly :displaylogo="false" id="ichdu" :data="temp_pdata" :layout="temp_layout"></plotly>
</div>
<div id="ia4uv" class="st-col col-12 col-sm">
<h4 id="is1ih">Humidity</h4>
<plotly :displaylogo="false" id="ipa5w" :data="hum_pdata" :layout="hum_layout"></plotly>
</div>
</div>
<div class="row">
<div id="ie07i" class="st-col col-12 col-sm">
<h4 id="iohzq">Wind</h4>
<plotly :displaylogo="false" id="inplg" :data="wind_pdata" :layout="wind_layout"></plotly>
</div>
<div id="iljh4" class="st-col col-12 col-sm">
<h4 id="ivy2n">Pressure</h4>
<plotly :displaylogo="false" id="ijinh" :data="press_pdata" :layout="press_layout"></plotly>
</div>
</div>
</div>
</div>
9 changes: 4 additions & 5 deletions lib/NodeUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,15 @@ Perform one round of training for the neural ODE model.
Updates the model's parameters by minimizing the loss function
using the specified optimizer. Returns the updated parameters and state.
"""
function train_one_round(node, θ, state, y, opt, maxiters, rng, y0=y[1, :]; kwargs...)
@show "innn"
function train_one_round(node, θ, state, y, opt, rng, y0=y[1, :]; kwargs...)
predict(θ) = Array(node(y0, θ, state)[1])
loss(θ) = sum(abs2, predict(θ)' .- y)
@show size(y), size(y0), size(predict(θ))

adtype = Optimization.AutoZygote()
optf = OptimizationFunction((θ, p) -> loss(θ), adtype)
optprob = OptimizationProblem(optf, θ)
res = solve(optprob, opt, maxiters=maxiters; kwargs...)
res = solve(optprob, opt; kwargs...)
res.minimizer, state
end

Expand All @@ -69,7 +68,7 @@ Iteratively updates the model parameters and logs the results.
Returns the collection of parameter updates, final state, and losses.
θs is an Observable that is udpdated after every training step
"""
function train(t, y, obs_grid, maxiters, lr, rng, θs; kwargs...)
function train(t, y, obs_grid, lr, rng, θs; kwargs...)
θ=nothing
state=nothing

Expand All @@ -78,7 +77,7 @@ function train(t, y, obs_grid, maxiters, lr, rng, θs; kwargs...)
if θ === nothing θ = θ_new end
if state === nothing state = state_new end

θ, state = train_one_round( node, θ, state, y[1:k,:], Optimisers.ADAMW(lr), maxiters, rng; kwargs...)
θ, state = train_one_round( node, θ, state, y[1:k,:], Optimisers.ADAMW(lr),rng; kwargs...)
@show k, size(y)
θs[] = θ
end
Expand Down
Loading

0 comments on commit a47e69b

Please sign in to comment.