-
Notifications
You must be signed in to change notification settings - Fork 6
[wip] KGE and fix for build_chains #206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
21160c9
for list comprehension
BernhardAhrens ddb6091
alpha beta of kge
BernhardAhrens 5e61d0a
show parameters
BernhardAhrens 2843561
helpers for extraction
BernhardAhrens 3276489
to allow Dropout in custom Chain
BernhardAhrens 523b47d
extracts into DataFrames
BernhardAhrens e675422
Update condition for constructing neural network in GenericHybridMode…
BernhardAhrens b401241
rm unnecessary
BernhardAhrens 3526bf6
Merge remote-tracking branch 'origin/main' into ba/kge_stuff
BernhardAhrens 1ce93c8
runic
BernhardAhrens 5d0811a
codecov
BernhardAhrens File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| export extract_histories, extract_parameters, stack_extracts, wide_params, wide_histories | ||
|
|
||
| """ | ||
| extract_histories(obj; metric::Union{Symbol,AbstractVector{Symbol}}, | ||
| target::Union{Symbol,AbstractVector{Symbol}}) | ||
|
|
||
| Return both train and validation histories for the given `metric`(s) and `target`(s). | ||
|
|
||
| `obj` is assumed to have fields `train_history` and `val_history`. | ||
| Returns a `NamedTuple`: `(; train, val)`, where each entry is a `DataFrame`. | ||
|
|
||
| - If `metric` is a single `Symbol`, columns are named by `target`s. | ||
| - If `metric` is a vector, columns are named `:<metric>_<target>`. | ||
| """ | ||
| function extract_histories(obj; | ||
| metric::Union{Symbol,AbstractVector{Symbol}}, | ||
| target::Union{Symbol,AbstractVector{Symbol}}, | ||
| ) | ||
| metrics = _asvec(metric) | ||
| targets = _asvec(target) | ||
|
|
||
| train = _history_df(obj.train_history, metrics, targets) | ||
| val = _history_df(obj.val_history, metrics, targets) | ||
| (; train, val) | ||
| end | ||
|
|
||
| # history: whatever your history container is | ||
| # metrics, targets: AbstractVector{Symbol} | ||
| function _history_df(history, metrics::AbstractVector{Symbol}, targets::AbstractVector{Symbol}) | ||
| cols = Any[] | ||
| names = Symbol[] | ||
|
|
||
| single_metric = (length(metrics) == 1) | ||
|
|
||
| for m in metrics, t in targets | ||
| push!(cols, metric_target_history(history, m, t)) | ||
| # if there is only one metric, keep the old behaviour: | ||
| # column names are just the targets | ||
| name = single_metric ? t : Symbol(string(m), "_", string(t)) | ||
| push!(names, name) | ||
| end | ||
|
|
||
| DataFrame(cols, names) | ||
| end | ||
|
|
||
|
|
||
| """ | ||
| extract_parameters(nt::NamedTuple) | ||
|
|
||
| Convert a `NamedTuple` of vectors and scalars to a `DataFrame`. | ||
|
|
||
| - Vector fields with length `N` become columns of length `N`. | ||
| - Scalars and length-1 vectors are repeated to length `N`. | ||
| - Throws an error if vector lengths are incompatible. | ||
| """ | ||
| function extract_parameters(nt::NamedTuple) | ||
| vals = values(nt) | ||
|
|
||
| # Determine number of rows: maximum length of all vector-like entries | ||
| lengths = map(vals) do v | ||
| v isa AbstractVector ? length(v) : 1 | ||
| end | ||
| nrows = maximum(lengths) | ||
|
|
||
| cols = map(vals) do v | ||
| if v isa AbstractVector | ||
| if length(v) == nrows | ||
| v | ||
| elseif length(v) == 1 | ||
| fill(v[1], nrows) | ||
| else | ||
| throw(ArgumentError("Incompatible vector length $(length(v)); expected 1 or $nrows")) | ||
| end | ||
| else | ||
| fill(v, nrows) | ||
| end | ||
| end | ||
|
|
||
| # Build a DataFrame with the same field names as the NamedTuple | ||
| return DataFrame((; zip(keys(nt), cols)...)) | ||
| end | ||
|
|
||
| function extract_parameters(tr::TrainResults) | ||
| train = extract_parameters(tr.train_diffs.parameters) | ||
| val = extract_parameters(tr.val_diffs.parameters) | ||
| (; train, val) | ||
| end | ||
|
|
||
| """ | ||
| stack_extracts(h; col = :set) | ||
|
|
||
| Given a NamedTuple `h` with fields `:train` and `:val` (both `DataFrame`s), | ||
| return a single `DataFrame` with an extra column `col` indicating `"train"` vs `"val"`. | ||
| """ | ||
| function stack_extracts(h::NamedTuple{(:train, :val)}; col::Symbol = :set) | ||
| train = copy(h.train) | ||
| val = copy(h.val) | ||
|
|
||
| train[!, col] = fill("train", nrow(train)) | ||
| val[!, col] = fill("val", nrow(val)) | ||
|
|
||
| vcat(train, val) | ||
| end | ||
|
|
||
| function wide_params(tr::TrainResults) | ||
| params = extract_parameters(tr) | ||
| stack_extracts(params) | ||
| end | ||
|
|
||
| function wide_histories(tr::TrainResults; | ||
| metrics::Union{Symbol,AbstractVector{Symbol}} = :all, | ||
| targets::Union{Symbol,AbstractVector{Symbol}} = :all, | ||
| ) | ||
| ms = metrics | ||
| ts = targets | ||
|
|
||
| if ms == :all | ||
| ms = collect(keys(tr.train_history[1])) | ||
| end | ||
| if ts == :all | ||
| ts = collect(keys(tr.train_history[1][1])) | ||
| end | ||
|
|
||
| histories = extract_histories(tr, metric = ms, target = ts) | ||
| stack_extracts(histories) | ||
| end | ||
|
|
||
| _asvec(x::AbstractVector) = x | ||
| _asvec(x::Symbol) = [x] | ||
|
|
||
| """ | ||
| metric_target_history(history, metric, target) -> Vector | ||
|
|
||
| Return the time series (over epochs) for a given `metric` and `target`. | ||
|
|
||
| Assumes `history` is an indexable collection where, for each entry `h`, | ||
| `h[metric][target]` is a scalar. | ||
| """ | ||
| function metric_target_history(history, metric::Symbol, target::Symbol) | ||
| [h[metric][target] for h in history] | ||
| end | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note that the collection (a DataFrame) is being done from here on:
EasyHybrid.jl/src/train.jl
Line 338 in 1736d03
filterfor aDataFrame.