Skip to content

Commit

Permalink
Merge branch 'v0.13.1' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
0815Creeper authored Dec 24, 2024
2 parents 7119c13 + 03dd11e commit bb69d50
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 32 deletions.
2 changes: 1 addition & 1 deletion examples/jupyter-src/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.ipynb_checkpoints/
params/
*.png
./*.gif
*.gif
17 changes: 6 additions & 11 deletions examples/pluto-src/SciMLUsingFMUs/SciMLUsingFMUs.jl
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
### A Pluto.jl notebook ###
# v0.19.46
# v0.20.3

using Markdown
using InteractiveUtils

# This Pluto notebook uses @bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of @bind gives bound variables a default value (instead of an error).
macro bind(def, element)
#! format: off
quote
local iv = try
Base.loaded_modules[Base.PkgId(
Base.UUID("6e696c72-6542-2067-7265-42206c756150"),
"AbstractPlutoDingetjes",
)].Bonds.initial_value
catch
b -> missing
end
local iv = try Base.loaded_modules[Base.PkgId(Base.UUID("6e696c72-6542-2067-7265-42206c756150"), "AbstractPlutoDingetjes")].Bonds.initial_value catch; b -> missing; end
local el = $(esc(element))
global $(esc(def)) = Core.applicable(Base.get, el) ? Base.get(el) : iv(el)
el
end
#! format: on
end

# ╔═╡ a1ee798d-c57b-4cc3-9e19-fb607f3e1e43
Expand Down Expand Up @@ -1941,7 +1936,7 @@ ProgressLogging = "~0.1.4"
PLUTO_MANIFEST_TOML_CONTENTS = """
# This file is machine-generated - editing it directly is not advised
julia_version = "1.10.3"
julia_version = "1.10.7"
manifest_format = "2.0"
project_hash = "79772b37e2cae2421c7159b63f3cbe881b42eaeb"
Expand Down Expand Up @@ -4744,7 +4739,7 @@ version = "0.15.1+0"
[[deps.libblastrampoline_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "8e850b90-86db-534c-a0d3-1478176c7d93"
version = "5.8.0+1"
version = "5.11.0+0"
[[deps.libevdev_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
Expand Down
39 changes: 24 additions & 15 deletions src/layers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -142,48 +142,57 @@ Flux.@functor SimultaniousZeroCrossing (m,)
### SHIFTSCALE ###

"""
ToDo.
ToDo.
Shifts and scales input data to a given distribution
"""
struct ShiftScale{T}
struct ShiftScale{T, A}
shift::AbstractArray{T}
scale::AbstractArray{T}
activation::A

function ShiftScale{T}(shift::AbstractArray{T}, scale::AbstractArray{T}) where {T}
inst = new(shift, scale)
function ShiftScale{T, A}(shift::AbstractArray{T}, scale::AbstractArray{T}, activation::A=Flux.identity) where {T, A}
inst = new(shift, scale, activation)
return inst
end

function ShiftScale(shift::AbstractArray{T}, scale::AbstractArray{T}) where {T}
return ShiftScale{T}(shift, scale)
function ShiftScale(shift::AbstractArray{T}, scale::AbstractArray{T}, activation::A=Flux.identity) where {T, A}
return ShiftScale{T, A}(shift, scale, activation)
end

# initialize for data array
function ShiftScale(
data::AbstractArray{<:AbstractArray{T}};
range::Union{Symbol,UnitRange{<:Integer}} = -1:1,
) where {T}
data::AbstractArray{<:AbstractArray{T}},
activation::A=Flux.identity;
range::Union{Symbol,UnitRange{<:Integer}} = :Normalize,
) where {T, A}
shift = -mean.(data)
scale = nothing

if range == :NormalDistribution
if range == :Normalize
range = 0:1
end

if range == :Standardize # NormalDistribution
scale = 1.0 ./ std.(data)
elseif isa(range, UnitRange{<:Integer})
scale =
1.0 ./
(collect(max(d...) for d in data) - collect(min(d...) for d in data)) .*
(range[end] - range[1])
else
@assert false "Unsupported scaleMode, supported is `:NormalDistribution` or `UnitRange{<:Integer}`"
@assert false "Unsupported range `$(range)`, supported is `:Standardize`, `:Normalize` or `UnitRange{<:Integer}`"
end

return ShiftScale{T}(shift, scale)
return ShiftScale{T, A}(shift, scale, activation)
end
end
export ShiftScale

function (l::ShiftScale)(x)

x_proc = (x .+ l.shift) .* l.scale
x_proc = l.activation((x .+ l.shift) .* l.scale)

return x_proc
end
Expand All @@ -209,14 +218,14 @@ struct ScaleShift{T}
end

# init ScaleShift with inverse transformation of a given ShiftScale
function ScaleShift(l::ShiftScale{T}; indices = 1:length(l.scale)) where {T}
function ScaleShift(l::ShiftScale{T, A}; indices = 1:length(l.scale)) where {T, A}
return ScaleShift{T}(1.0 ./ l.scale[indices], -1.0 .* l.shift[indices])
end

function ScaleShift(data::AbstractArray{<:AbstractArray{T}}) where {T}
shift = mean.(data)
scale = std.(data)
return ShiftScale{T}(scale, shift)
return ShiftScale(scale, shift)
end
end
export ScaleShift
Expand Down
4 changes: 2 additions & 2 deletions test/layers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ s = ShiftScale(shift, scale)
@test s(input) == [6.0, 16.0, 24.0]

s = ShiftScale(inputArray)
@test s(input) == [2.5, -0.8, -1.0]
@test s(input) == [1.25, -0.4, -0.5]

s = ShiftScale(inputArray; range = -1:1)
for i = 1:length(inputArray)
Expand All @@ -32,7 +32,7 @@ for i = 1:length(inputArray)
@test min(res...) >= -2
end

s = ShiftScale(inputArray; range = :NormalDistribution)
s = ShiftScale(inputArray; range = :Normalize)
# ToDo: Test for :NormalDistribution

# ScaleShift
Expand Down
1 change: 0 additions & 1 deletion test/solution_gradients.jl
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ stepCb = FunctionCallingCallback(stepCompleted; func_everystep = true, func_star
#fmu_params = nothing
fmu = loadFMU("BouncingBall1D", "Dymola", "2023x"; type = :ME)
fmu_params = Dict("damping" => 0.7, "mass_radius" => 0.0, "mass_s_min" => DBL_MIN)
fmu.executionConfig.isolatedStateDependency = true

net = Chain(#Dense(W1, b1, identity),
x -> fmu(; x = x, dx_refs = :all),
Expand Down
3 changes: 2 additions & 1 deletion test/supported_sensitivities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#

using Flux
using DifferentialEquations
using DifferentialEquations, Sundials

import Random
Random.seed!(5678);
Expand Down Expand Up @@ -58,6 +58,7 @@ for fmu in fmus
@info "##### CHECKING FMU WITH $(fmu.modelDescription.numberOfEventIndicators) SIMULTANEOUS EVENT INDICATOR(S) #####"
solvers = [
Tsit5(),
CVODE_BDF(),
Rosenbrock23(autodiff = false),
Rodas5(autodiff = false),
FBDF(autodiff = false),
Expand Down
1 change: 0 additions & 1 deletion test/time_solution_gradients.jl
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ fmu_params = Dict(
"mass_m" => MASS,
"mass_s_min" => DBL_MIN,
)
fmu.executionConfig.isolatedStateDependency = true

net = Chain(#Dense(W1, b1, identity),
x -> fmu(; x = x, dx_refs = :all),
Expand Down

0 comments on commit bb69d50

Please sign in to comment.