Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions configs/mlp_bootstrap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Example configuration for MLP bootstrap ensemble training with toy data.

seed: 2025
device: cpu

datasource:
datasource_type: pmgjson
json_path: ./data/toy_data/
spectrum_key: "XANES"

dataset:
# general:
dataset_type: descriptor
root: ./data/processed/toy_data_mlp_bootstrap/ # Where should the processed data be stored
preload: True # Preload dataset into RAM if True; otherwise load on-the-fly
skip_prepare: False
split_ratios: [0.8, 0.2]
# params:
# descriptors:
descriptors:
- descriptor_type: wacsf
r_min: 1.0
r_max: 6.0
n_g2: 16
n_g4: 32

encodings:
- encoding_type: identity

model:
# general:
model_type: mlp
# params:
in_size: auto
out_size: auto
hidden_size: 256
dropout: 0.1
num_hidden_layers: 3
shrink_rate: 0.5
activation: prelu

trainer:
# general:
trainer_type: basic
batch_size: 4
shuffle: True
drop_last: False
num_workers: 0
# params:
epochs: 20
learning_rate: 0.001
optimizer: Adam
max_norm: null
validation_interval: 10
lr_warmup: True
warmup_steps: 500
loss:
- loss_type: mse
regularizer:
regularizer_type: none
lr_scheduler:
lr_scheduler_type: linear
start_factor: 1.0
end_factor: 0.1
total_iters: 40
early_stopper:
early_stopper_type: basic
patience: 25
min_delta: 0.001
restore_best: True

strategy:
# general:
strategy_type: bootstrap
weight_init: xavier_uniform
bias_init: zeros
checkpoint_interval: 25
# params:
n_models: 5
sample_fraction: 1.0
seeds: [101, 202, 303, 404, 505]
23 changes: 23 additions & 0 deletions configs/mlp_bootstrap_infer.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Example configuration for MLP bootstrap ensemble inference with toy data.

seed: 2025
device: cuda

datasource:
datasource_type: pmgjson
json_path: ./data/toy_data/
spectrum_key: "XANES"

dataset:
root: ./data/processed/toy_data_mlp_bootstrap/ # Where should the processed data be stored
preload: True # Preload dataset into RAM if True; otherwise load on-the-fly
skip_prepare: False

inferencer:
inferencer_type: ensemble
batch_size: 4
shuffle: False
drop_last: False
num_workers: 0
buffer_size: 1000
model_device_policy: sequential
80 changes: 80 additions & 0 deletions configs/mlp_kfold.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Example configuration for MLP k-fold cross-validation training with toy data.

seed: 2025
device: cpu

datasource:
datasource_type: pmgjson
json_path: ./data/toy_data/
spectrum_key: "XANES"

dataset:
# general:
dataset_type: descriptor
root: ./data/processed/toy_data_mlp_kfold/ # Where should the processed data be stored
preload: True # Preload dataset into RAM if True; otherwise load on-the-fly
skip_prepare: False
# params:
# descriptors:
descriptors:
- descriptor_type: wacsf
r_min: 1.0
r_max: 6.0
n_g2: 16
n_g4: 32

encodings:
- encoding_type: identity

model:
# general:
model_type: mlp
# params:
in_size: auto
out_size: auto
hidden_size: 256
dropout: 0.1
num_hidden_layers: 3
shrink_rate: 0.5
activation: prelu

trainer:
# general:
trainer_type: basic
batch_size: 4
shuffle: True
drop_last: False
num_workers: 0
# params:
epochs: 20
learning_rate: 0.001
optimizer: Adam
max_norm: null
validation_interval: 10
lr_warmup: True
warmup_steps: 500
loss:
- loss_type: mse
regularizer:
regularizer_type: none
lr_scheduler:
lr_scheduler_type: linear
start_factor: 1.0
end_factor: 0.1
total_iters: 40
early_stopper:
early_stopper_type: basic
patience: 25
min_delta: 0.001
restore_best: True

strategy:
# general:
strategy_type: kfold
weight_init: xavier_uniform
bias_init: zeros
checkpoint_interval: 25
# params:
n_splits: 3
n_repeats: 1
seed: 2025
18 changes: 16 additions & 2 deletions xanesnet/schemas/strategies/bootstrap.schema.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
$schema: https://json-schema.org/draft/2020-12/schema
$id: bootstrap.schema.yaml
title: 'XANESNET strategy schema: bootstrap'
$comment: Registered in the strategy schemas, but the implementation currently raises NotImplementedError.
$defs:
strategyBootstrap:
type: object
Expand Down Expand Up @@ -33,5 +32,20 @@ $defs:
strategy_type:
const: bootstrap
description: Strategy identifier.
description: Bootstrap ensemble strategy.
n_models:
type: integer
minimum: 1
default: 5
description: Number of bootstrap ensemble members.
sample_fraction:
type: number
exclusiveMinimum: 0
default: 1.0
description: Fraction of the training subset resampled for each bootstrap member.
seeds:
type: array
items:
type: integer
Comment thread
Junkiii marked this conversation as resolved.
description: Optional per-model random seeds for resampling and weight initialization.
description: Sequential bootstrap-ensemble training and aggregate inference strategy.
description: Strategy schemas backed by ``StrategyRegistry`` classes.
49 changes: 49 additions & 0 deletions xanesnet/schemas/strategies/kfold.schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
$schema: https://json-schema.org/draft/2020-12/schema
$id: kfold.schema.yaml
title: 'XANESNET strategy schema: kfold'
$defs:
strategyKFold:
type: object
additionalProperties: false
required:
- strategy_type
properties:
weight_init:
$ref: ../components/component_types.schema.yaml#/$defs/weightInitName
default: default
description: Weight initialization scheme name.
weight_init_params:
type: object
default: {}
additionalProperties: true
$comment: Forwarded as kwargs to the selected torch.nn.init weight initializer. Examples include {a,b} for uniform_,
{mean,std} for normal_, {gain} for xavier_*, and {a,mode,nonlinearity} for kaiming_*.
description: Additional weight-initializer parameters.
bias_init:
$ref: ../components/component_types.schema.yaml#/$defs/biasInitName
default: zeros
description: Bias initialization scheme name.
checkpoint_interval:
type:
- integer
- 'null'
default: null
description: Epoch interval between checkpoints, or ``None``.
strategy_type:
const: kfold
description: Strategy identifier.
n_splits:
type: integer
minimum: 2
default: 3
description: Number of folds per repeat.
n_repeats:
type: integer
minimum: 1
default: 1
description: Number of repeated k-fold shuffles.
seed:
type: integer
description: Random seed used to shuffle samples before splitting.
Comment thread
Junkiii marked this conversation as resolved.
description: Repeated k-fold cross-validation strategy returning the best fold model.
description: Strategy schemas backed by ``StrategyRegistry`` classes.
1 change: 1 addition & 0 deletions xanesnet/schemas/strategies/strategies.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ $defs:
- $ref: single.schema.yaml#/$defs/strategySingle
- $ref: deep_ensemble.schema.yaml#/$defs/strategyDeepEnsemble
- $ref: bootstrap.schema.yaml#/$defs/strategyBootstrap
- $ref: kfold.schema.yaml#/$defs/strategyKFold
- $ref: snapshot_ensemble.schema.yaml#/$defs/strategySnapshotEnsemble
description: Union of strategy configuration objects instantiated by ``StrategyRegistry``.
description: Strategy schemas backed by ``StrategyRegistry`` classes.
1 change: 1 addition & 0 deletions xanesnet/schemas/strategies/strategy_types.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ $defs:
- single
- deep_ensemble
- bootstrap
- kfold
- snapshot_ensemble
description: Strategy registry key accepted by ``StrategyRegistry.get``.
description: Strategy schemas backed by ``StrategyRegistry`` classes.
12 changes: 6 additions & 6 deletions xanesnet/serialization/runtime_contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,20 +110,20 @@ def _require_concrete_inference_encodings(config: ConfigRaw) -> None:
)


def _require_ensemble_inferencer_for_deep_ensemble(config: ConfigRaw) -> None:
"""Validate deep-ensemble inference runner selection.
def _require_ensemble_inferencer_for_ensemble_strategies(config: ConfigRaw) -> None:
"""Validate ensemble-strategy inference runner selection.

Args:
config: Schema-valid merged inference configuration.

Raises:
ConfigError: If a deep-ensemble strategy is paired with a non-ensemble
ConfigError: If an ensemble strategy is paired with a non-ensemble
inferencer.
"""
strategy_type = _section_value(config, "strategy", "strategy_type")
inferencer_type = _section_value(config, "inferencer", "inferencer_type")
if strategy_type == "deep_ensemble" and inferencer_type != "ensemble":
raise ConfigError("Inference strategy 'deep_ensemble' requires inferencer 'ensemble'.")
if strategy_type in {"deep_ensemble", "bootstrap"} and inferencer_type != "ensemble":
raise ConfigError(f"Inference strategy '{strategy_type}' requires inferencer 'ensemble'.")


def _section_value(config: ConfigRaw, section: str, key: str) -> Any:
Expand Down Expand Up @@ -169,7 +169,7 @@ def _auto_token_paths(value: Any, path: tuple[str, ...]) -> Iterator[str]:
_require_registered_batch_processor,
_require_concrete_inference_model,
_require_concrete_inference_encodings,
_require_ensemble_inferencer_for_deep_ensemble,
_require_ensemble_inferencer_for_ensemble_strategies,
),
"analyze": (),
}
2 changes: 2 additions & 0 deletions xanesnet/strategies/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from .base import Strategy
from .bootstrap import Bootstrap
from .deep_ensemble import DeepEnsemble
from .kfold import KFold
from .registry import StrategyRegistry
from .single import Single
from .snapshot_ensemble import SnapshotEnsemble
Expand All @@ -31,6 +32,7 @@
"Strategy",
"Bootstrap",
"DeepEnsemble",
"KFold",
"SnapshotEnsemble",
"Single",
"StrategyRegistry",
Expand Down
Loading
Loading