pycsamt.ai.inversion.inv1d#
End-to-end 1-D EM inversion workflow.
EMInverter1D is the primary user-facing estimator for 1-D
neural-network inversion of MT, CSAMT, and TEM data. It:
Loads a
ForwardDataset(or a.npzpath to one).Normalises features and targets with z-score normalisation (log₁₀ thickness transform applied automatically).
Instantiates the requested architecture (
'cnn1d','resnet','fcn').Trains with
EMTrainer(early stopping, LR scheduling, masked MSE loss).Predicts on new data as: * numpy arrays * lists of
Zobjects *ForwardResponseobjects
The sklearn-compatible interface (fit / predict / score)
from BaseEMNet is preserved.
Quick start#
>>> import numpy as np
>>> from pycsamt.forward.batch import generate_dataset
>>> from pycsamt.ai.inversion.inv1d import EMInverter1D
# Generate 2 000 training samples >>> ds = generate_dataset(n_samples=2_000, seed=0, n_layers=5)
# Train >>> inv = EMInverter1D(arch=”resnet”, n_layers=5, solver=”mt1d”) >>> inv.fit(ds, epochs=30, batch_size=128, verbose=True)
# Predict on a new forward response >>> from pycsamt.forward import MT1DForward, LayeredModel >>> m_new = LayeredModel.random(n_layers=5, seed=99) >>> resp = MT1DForward(np.logspace(-3, 4, 30)).run(m_new) >>> y_pred = inv.predict_response(resp) # → LayeredModel
Classes
|
1-D EM neural-network inverter. |
- class pycsamt.ai.inversion.inv1d.EMInverter1D(arch='resnet', n_layers=5, solver='mt1d', *, device=None, log_thickness=True, include_phase=True, augment_noise=0.02, **net_kwargs)[source]#
Bases:
BaseEMNet1-D EM neural-network inverter.
Supports MT, CSAMT (far-field), and TEM step-off data.
- Parameters:
arch ({'resnet', 'cnn1d', 'fcn'}) – Network architecture.
'resnet'(Liu 2021 style) gives the best accuracy on typical MT datasets.'cnn1d'(Puzyrev style) is faster to train.'fcn'(Moghadas style) handles variable input length.n_layers (int) – Number of earth layers to invert for.
solver ({'mt1d', 'csamt1d', 'tem1d'}) – EM method this inverter targets (determines default frequency grid for Z-object input coercion).
device (str or None) – Compute device. Auto-detects CUDA/MPS/CPU if
None.log_thickness (bool) – Apply log₁₀ to thickness in training targets. Strongly recommended when thicknesses span > 2 orders of magnitude.
include_phase (bool) – Include impedance phase in the input feature vector for MT/CSAMT. Setting to
Falsehalves the input size.augment_noise (float) – On-the-fly noise level added to training inputs each epoch.
net_kwargs (dict) – Extra keyword arguments forwarded to the network constructor (e.g.
channels=(64, 128, 256)for ResNet).
- fit(X, y=None, *, epochs=100, batch_size=256, lr=0.001, patience=20, val_frac=0.1, grad_clip=1.0, seed=None, verbose=True)[source]#
Train the inverter on a
ForwardDatasetor a.npzfile path.- Parameters:
X (ForwardDataset or str or Path) – Training data. If y is also given, X and y are treated as raw feature / target numpy arrays.
y (ndarray or None) – Raw targets (only when X is an ndarray).
epochs (int) – Maximum training epochs.
batch_size (int)
lr (float) – Initial learning rate.
patience (int) – Early-stopping patience.
val_frac (float) – Fraction of training data used for validation.
grad_clip (float or None) – Gradient-norm clipping threshold.
seed (int or None) – Random seed for train/val split.
verbose (bool) – Print per-epoch summary.
- Return type:
self
- predict(X, *, as_log_rho=True)[source]#
Predict model parameters for new data.
- Parameters:
X (ndarray (n_samples, n_features) | Z | list of Z | ForwardResponse) – Input data.
as_log_rho (bool) – If
True(default), the returned resistivity values are in log₁₀(Ω·m) space matching the training targets. IfFalse, resistivities are back-transformed to Ω·m.
- Returns:
y_pred – Parameter vector:
[log10(ρ), log10(h)](or linear ifas_log_rho=False).- Return type:
- predict_models(X)[source]#
Predict and return a list of
LayeredModel.Resistivity and thickness are back-transformed from log space.
- Return type:
- predict_response(response)[source]#
Convenience: invert a single
ForwardResponseand return the predictedLayeredModel.- Return type:
- classmethod from_pretrained(name, *, cache_dir=None)[source]#
Load a pre-trained model from the pycsamt model zoo.
Pre-trained weights are hosted at earthai-tech/pycsamt-models and are downloaded to
~/.pycsamt/model_zoo/on first call.- Parameters:
- Return type:
- Raises:
KeyError – If name is not in the model zoo registry.
RuntimeError – If the download fails (weights not yet publicly available).
Examples
>>> from pycsamt.ai.inversion import EMInverter1D >>> inv = EMInverter1D.from_pretrained("mt1d-resnet-5layer-v1")