Frontmatter

If you are publishing this notebook on the web, you can set the parameters below to provide HTML metadata. This is useful for search engines and social media.

using DifferentialEquations, Plots, PlutoUI
48.4 s

Initial Condition

md"
Initial Condition
"
192 μs
1.0
@bind u0 Slider(0:.1:2; default=1, show_value=true)
617 ms

Parameter

md"
Parameter
"
198 μs
1.0
@bind p Slider(-2:.1:2; default=1, show_value=true)
1.2 ms
f (generic function with 1 method)
f(u,p,t) = 10*sin(t*30) + p*u
543 μs
1.0
11.2 μs
begin
tspan = (0.0,1.0)
prob = ODEProblem(f,u0,tspan,p)
sol = solve(prob, Tsit5(), reltol=1e-8, abstol=1e-8)
nothing
end
1.8 s
begin
begin
plot(sol,linewidth=5,title="Solution to u'=f(u,t)",
xaxis="Time (t)",yaxis="u(t)",label="solution", ylims=(0,3))
end
end
9.5 s

A Simple scalar Neural Network

md"
A Simple scalar Neural Network
"
299 μs
simple (generic function with 1 method)
begin
W1 = rand(n)
W2 = rand(1,n)
simple(x) = (W2 *tanh.(x*W1))[1]
end
35.1 ms
3.0831909010016787
217 ms
using Flux
26.5 s
n
10
n = 10
15.1 μs
NNODE
Chain(
  Main.var"#1#2"{DataType}(Float32),
  Dense(1 => 10, tanh),                 # 20 parameters
  Dense(10 => 1),                       # 11 parameters
  first,
)                   # Total: 4 arrays, 31 parameters, 404 bytes.
NNODE = Chain(x -> [Float32(x)], # Take in a scalar and transform it into an array
Dense(1 => n,tanh),
Dense(n => 1),
first) # Take first value, i.e. return a scalar
141 ms
NNODE.layers
---
10×1 Matrix{Float32}:
  0.3943369
  0.018199405
 -0.0168786
 -0.38904595
  0.44548574
 -0.2726122
  0.5850382
 -0.6337153
 -0.4097277
  0.34402916
NNODE[2].weight
16.8 μs
1×10 Matrix{Float32}:
 -0.0337934  0.635426  0.498114  0.141992  …  0.253938  -0.705633  -0.137575
NNODE[3].weight
18.2 μs
Flux.params(NNODE).params
13.7 ms

Let's solve u'=sin(2πt), u(0)=1

md"
Let's solve u'=sin(2πt), u(0)=1
"
248 μs
g (generic function with 1 method)
g(t) = t*NNODE(t) + 1f0
476 μs
loss (generic function with 1 method)
begin
using Statistics # for the loss
ϵ = sqrt(eps(Float32))
loss() = mean(abs2(((g(t+ϵ)-g(t))/ϵ) - sin(2π*t)) for t in 0:1f-2:1f0)
end
1.5 ms
begin
opt = Flux.Descent(0.01)
data = Iterators.repeated((), 5000)
iter = 0
cb = function () #callback function to observe training
global iter += 1
if iter % 500 == 0
display(loss())
end
end
display(loss())
Flux.train!(loss, Flux.params(NNODE), data, opt; cb=cb)
end
100% 97.2 s
begin
t = 0:0.001:1.0
plot(t,g.(t),label="NN")
plot!(t,1.0 +1/(2π) .- cos.(2π.*t)/2π, label = "True Solution")
end
653 ms