armed.models package#

Submodules#

armed.models.autoencoder_classifier module#

Autoencoder-classifiers, including domain adversarial and mixed effects variations.

class armed.models.autoencoder_classifier.AdversarialClassifier(*args: Any, **kwargs: Any)#

Bases: Layer

call(inputs)#
get_config()#
class armed.models.autoencoder_classifier.AuxClassifier(*args: Any, **kwargs: Any)#

Bases: Layer

call(inputs)#
get_config()#
class armed.models.autoencoder_classifier.BaseAutoencoderClassifier(*args: Any, **kwargs: Any)#

Bases: Model

call(inputs, training=None)#
class armed.models.autoencoder_classifier.Decoder(*args: Any, **kwargs: Any)#

Bases: Layer

call(inputs, training=None)#
get_config()#
class armed.models.autoencoder_classifier.DomainAdversarialAEC(*args: Any, **kwargs: Any)#

Bases: BaseAutoencoderClassifier

call(inputs, training=None)#
compile(loss_recon=tensorflow.keras.losses.MeanSquaredError, loss_class=tensorflow.keras.losses.BinaryCrossentropy, loss_adv=tensorflow.keras.losses.CategoricalCrossentropy, metric_class=tensorflow.keras.metrics.AUC, metric_adv=tensorflow.keras.metrics.CategoricalAccuracy, opt_autoencoder=tensorflow.keras.optimizers.Adam, opt_adversary=tensorflow.keras.optimizers.Adam, loss_recon_weight=1.0, loss_class_weight=0.01, loss_gen_weight=0.05)#

Compile model with given losses, metrics, and optimizers.

The main autoencoder-classifier is trained with this loss function: loss_recon_weight * loss_recon (image reconstruction loss) + loss_class_weight * loss_class (phenotype classification loss) - loss_gen_weight * loss_adv (generalization (adversarial) loss)

While the adversarial classifier is trained with loss_adv.

Parameters:
  • loss_recon (loss, optional) – Image reconstruction loss. Defaults to tf.keras.losses.MeanSquaredError().

  • loss_class (loss, optional) – Auxiliary classification loss. Defaults to tf.keras.losses.BinaryCrossentropy().

  • loss_adv (loss, optional) – Adversarial classification loss. Defaults to tf.keras.losses.CategoricalCrossentropy().

  • metric_class (metric, optional) – Auxiliary classification metric. Defaults to tf.keras.metrics.AUC(name=’auroc’).

  • metric_adv (metric, optional) – Adversarial classification metric. Defaults to tf.keras.metrics.CategoricalAccuracy(name=’acc’).

  • opt_autoencoder (optimizer, optional) – Optimizer for the main model. Defaults to tf.keras.optimizers.Adam(lr=0.0001).

  • opt_adversary (optimizer, optional) – Optimizer for the adversarial classifier. Defaults to tf.keras.optimizers.Adam(lr=0.0001).

  • loss_recon_weight (float, optional) – Weight for reconstruction loss. Defaults to 1.0.

  • loss_class_weight (float, optional) – Weight for auxiliary classification loss. Defaults to 0.01.

  • loss_gen_weight (float, optional) – Weight for generalization loss. Defaults to 0.05.

property metrics#
test_step(data)#
train_step(data)#
class armed.models.autoencoder_classifier.DomainEnhancingAutoencoderClassifier(*args: Any, **kwargs: Any)#

Bases: Model

call(inputs, training=None)#
compile(loss_recon=tensorflow.keras.losses.MeanSquaredError, loss_class=tensorflow.keras.losses.BinaryCrossentropy, loss_cluster=tensorflow.keras.losses.CategoricalCrossentropy, metric_class=tensorflow.keras.metrics.AUC, optimizer=tensorflow.keras.optimizers.Adam, loss_class_weight=0.01, loss_latent_cluster_weight=0.001, loss_image_cluster_weight=0.001)#

Compile model with given losses, metrics, and optimizer.

The autoencoder-classifier is trained with this loss function: loss_recon (image reconstruction loss) + loss_class_weight * loss_class (phenotype classification loss) + loss_latent_cluster_weight * loss_cluster(latent classifier) (cluster predictiveness of latents) + loss_image_cluster_weight * loss_cluster(recon classifier) (cluster predictiveness of reconstructions)

Parameters:
  • loss_recon (loss, optional) – Image reconstruction loss. Defaults to tf.keras.losses.MeanSquaredError().

  • loss_class (loss, optional) – Auxiliary classification loss. Defaults to tf.keras.losses.BinaryCrossentropy().

  • loss_cluster (loss, optional) – Cluster classification loss. Defaults to tf.keras.losses.CategoricalCrossentropy().

  • metric_class (metric, optional) – Auxiliary classification metric. Defaults to tf.keras.metrics.AUC(name=’auroc’).

  • optimizer (optimizer, optional) – Optimizer. Defaults to tf.keras.optimizers.Adam(lr=0.0001).

  • loss_class_weight (float, optional) – Weight for auxiliary classification loss. Defaults to 0.01.

  • loss_latent_cluster_weight (float, optional) – Weight for cluster prediction loss for latents. Defaults to 0.001.

  • loss_image_cluster_weight (float, optional) – Weight for cluster prediction loss for recons. Defaults to 0.001.

property metrics#
test_step(data)#
train_step(data)#
class armed.models.autoencoder_classifier.Encoder(*args: Any, **kwargs: Any)#

Bases: Layer

call(inputs, training=None)#
get_config()#
class armed.models.autoencoder_classifier.ImageClassifier(*args: Any, **kwargs: Any)#

Bases: Layer

call(inputs, training=None)#
get_config()#
class armed.models.autoencoder_classifier.LatentClassifier(*args: Any, **kwargs: Any)#

Bases: Layer

call(inputs, training=None)#
get_config()#
class armed.models.autoencoder_classifier.MixedEffectAuxClassifier(*args: Any, **kwargs: Any)#

Bases: Layer

call(inputs, training=None)#
get_config()#
class armed.models.autoencoder_classifier.MixedEffectsAEC(*args: Any, **kwargs: Any)#

Bases: DomainAdversarialAEC

Still under development!

call(inputs, training=None)#
compile(loss_recon=tensorflow.keras.losses.MeanSquaredError, loss_class=tensorflow.keras.losses.BinaryCrossentropy, loss_adv=tensorflow.keras.losses.CategoricalCrossentropy, metric_class=tensorflow.keras.metrics.AUC, metric_adv=tensorflow.keras.metrics.CategoricalAccuracy, opt_autoencoder=tensorflow.keras.optimizers.Adam, opt_adversary=tensorflow.keras.optimizers.Adam, opt_recon_classifier=tensorflow.keras.optimizers.Adam, loss_recon_weight=1, loss_recon_fe_weight=1, loss_class_weight=0.01, loss_gen_weight=0.2, loss_recon_cluster_weight=0.01)#

Compile model with given losses, metrics, and optimizers.

The main autoencoder-classifier is trained with this loss function: loss_recon_weight * loss_recon (image reconstruction loss) + loss_class_weight * loss_class (phenotype classification loss) - loss_gen_weight * loss_adv (generalization (adversarial) loss)

While the adversarial classifier is trained with loss_adv.

Parameters:
  • loss_recon (loss, optional) – Image reconstruction loss. Defaults to tf.keras.losses.MeanSquaredError().

  • loss_class (loss, optional) – Auxiliary classification loss. Defaults to tf.keras.losses.BinaryCrossentropy().

  • loss_adv (loss, optional) – Adversarial classification loss. Defaults to tf.keras.losses.CategoricalCrossentropy().

  • metric_class (metric, optional) – Auxiliary classification metric. Defaults to tf.keras.metrics.AUC(name=’auroc’).

  • metric_adv (metric, optional) – Adversarial classification metric. Defaults to tf.keras.metrics.CategoricalAccuracy(name=’acc’).

  • opt_autoencoder (optimizer, optional) – Optimizer for the main model. Defaults to tf.keras.optimizers.Adam(lr=0.0001).

  • opt_adversary (optimizer, optional) – Optimizer for the adversarial classifier. Defaults to tf.keras.optimizers.Adam(lr=0.0001).

  • loss_recon_weight (float, optional) – Weight for reconstruction loss. Defaults to 1.0.

  • loss_class_weight (float, optional) – Weight for auxiliary classification loss. Defaults to 0.01.

  • loss_gen_weight (float, optional) – Weight for generalization loss. Defaults to 0.05.

property metrics#
test_step(data)#
train_step(data)#
class armed.models.autoencoder_classifier.RandomEffectDecoder(*args: Any, **kwargs: Any)#

Bases: Decoder

call(inputs, training=None)#
class armed.models.autoencoder_classifier.RandomEffectEncoder(*args: Any, **kwargs: Any)#

Bases: Encoder

call(inputs, training=None)#
class armed.models.autoencoder_classifier.TiedConv2DTranspose(*args: Any, **kwargs: Any)#

Bases: Conv2DTranspose

build(input_shape)#
class armed.models.autoencoder_classifier.TiedDecoder(*args: Any, **kwargs: Any)#

Bases: Decoder

armed.models.autoencoder_classifier.load_weights_base_aec(model, weights_path: str)#

Loads weights into BaseAutoencoderClassifier. When using the native Keras model.load_weights(), it fails to match up the saved weights with the model layers. This workaround manually matches them up by name.

Parameters:
  • model (tf.keras.Model) – model

  • weights_path (str) – path to weights

armed.models.cnn_classifier module#

Simple 2D CNN classifiers, including domain adversarial and mixed effects extensions.

class armed.models.cnn_classifier.AdversarialClassifier(*args: Any, **kwargs: Any)#

Bases: Model

Domain adversarial classifier for the ImageClassifier. Receives the layer activations from the ImageClassifier as inputs and predicts cluster membership.

call(inputs)#
class armed.models.cnn_classifier.ClusterInputImageClassifier(*args: Any, **kwargs: Any)#

Bases: ImageClassifier

Simple 2D image classifier that takes an additional input containing the one-hot encoded cluster membership of each sample. This input is concatenated to the flattened output of the last convolution layer, before the first dense layer.

call(inputs, return_layer_activations=False)#
class armed.models.cnn_classifier.DomainAdversarialImageClassifier(*args: Any, **kwargs: Any)#

Bases: Model

Domain adversarial 2D image classifier which learns the classification task while competing with an adversary, which learns to predict cluster membership from the classifier’s layer activations.

call(inputs)#
compile(loss_classifier=tensorflow.keras.losses.BinaryCrossentropy, loss_adversary=tensorflow.keras.losses.CategoricalCrossentropy, loss_classifier_weight=1.0, loss_gen_weight=0.1, metric_classifier=tensorflow.keras.metrics.AUC, opt_classifier=tensorflow.keras.optimizers.Nadam, opt_adversary=tensorflow.keras.optimizers.Nadam)#

Compile model. Must be called before training or loading weights.

Parameters:
  • loss_classifier (loss, optional) – Main classification loss. Defaults to tf.keras.losses.BinaryCrossentropy().

  • loss_adversary (loss, optional) – Adversary classification loss. Defaults to tf.keras.losses.CategoricalCrossentropy().

  • loss_classifier_weight (float, optional) – Main classification loss weight. Defaults to 1.0.

  • loss_gen_weight (float, optional) – Generalization loss weight. Defaults to 0.01.

  • metric_classifier (metric, optional) – Main classification metric. Defaults to tf.keras.metrics.AUC().

  • opt_classifier (optimizer, optional) – Optimizer for main classifier. Defaults to tf.keras.optimizers.Nadam(lr=0.0001).

  • opt_adversary (optimizer, optional) – Optimizer for adversary. Defaults to tf.keras.optimizers.Nadam(lr=0.0001).

property metrics#
test_step(data)#
train_step(data)#
class armed.models.cnn_classifier.ImageClassifier(*args: Any, **kwargs: Any)#

Bases: Model

Simple 2D image binary classifier with 7 convolution blocks and 2 final dense layers.

call(inputs, return_layer_activations=False)#
class armed.models.cnn_classifier.MixedEffectsClassifier(*args: Any, **kwargs: Any)#

Bases: DomainAdversarialImageClassifier

Random linear slope and intercept introduced before last dense layer

call(inputs, return_layer_activations=False)#
compile(loss_classifier=tensorflow.keras.losses.BinaryCrossentropy, loss_adversary=tensorflow.keras.losses.CategoricalCrossentropy, loss_classifier_weight=1.0, loss_gen_weight=0.1, metric_classifier=tensorflow.keras.metrics.AUC, opt_classifier=tensorflow.keras.optimizers.Nadam, opt_adversary=tensorflow.keras.optimizers.Nadam)#

Compile model. Must be called before training or loading weights.

Parameters:
  • loss_classifier (loss, optional) – Main classification loss. Defaults to tf.keras.losses.BinaryCrossentropy().

  • loss_adversary (loss, optional) – Adversary classification loss. Defaults to tf.keras.losses.CategoricalCrossentropy().

  • loss_classifier_weight (float, optional) – Main classification loss weight. Defaults to 1.0.

  • loss_gen_weight (float, optional) – Generalization loss weight. Defaults to 0.01.

  • metric_classifier (metric, optional) – Main classification metric. Defaults to tf.keras.metrics.AUC().

  • opt_classifier (optimizer, optional) – Optimizer for main classifier. Defaults to tf.keras.optimizers.Nadam(lr=0.0001).

  • opt_adversary (optimizer, optional) – Optimizer for adversary. Defaults to tf.keras.optimizers.Nadam(lr=0.0001).

property metrics#
test_step(data)#
train_step(data)#
class armed.models.cnn_classifier.RandomEffectsClassifier(*args: Any, **kwargs: Any)#

Bases: ImageClassifier

call(inputs, return_layer_activations=False)#

armed.models.lme module#

Logistic regression and logistic mixed effects models built on statsmodels.

class armed.models.lme.LogisticGLM(formula: str)#

Bases: object

fit(dataframe: pandas.DataFrame)#

Fit model

Parameters:

dataframe (pd.DataFrame) – contains columns for each feature and label

predict(dataframe: pandas.DataFrame)#

Predict on data

Parameters:

dataframe (pd.DataFrame) – contains same columns as training data

Raises:

UserWarning – .fit() has not been called

Returns:

predictions

Return type:

pd.DataFrame

class armed.models.lme.MixedLogisticGLM(formula: str, re_dict: dict, cluster_name: str)#

Bases: object

fit(dataframe: pandas.DataFrame)#

Fit model

Parameters:

dataframe (pd.DataFrame) – contains columns for each feature and label

predict(dataframe: pandas.DataFrame)#

Predict on data. Random effects are applied if the cluster has been seen during training.

Parameters:

dataframe (pd.DataFrame) – contains same columns as training data

Raises:

UserWarning – .fit() has not been called

Returns:

predictions

Return type:

pd.DataFrame

armed.models.metalearning module#

Meta-learning wrapper function to train a conventional model to be domain-agnostic.

armed.models.metalearning.mldg(X: numpy.ndarray, Y: numpy.ndarray, Z: numpy.ndarray, model: tensorflow.keras.Model, outer_lr: float = 0.001, inner_lr: float = 0.001, epochs: int = 40, cluster_batch_size: int = 4, loss_fn=tensorflow.keras.losses.categorical_crossentropy, meta_test_weight: float = 1.0, verbose: bool = False)#

Implementation of metalearning domain generalization by Li 2018 https://arxiv.org/abs/1710.03463

Trains a conventional model using MLDG.

Parameters:
  • X (np.ndarray) – data

  • Y (np.ndarray) – labels

  • Z (np.ndarray) – one-hot encoded cluster (domain) membership

  • model (tf.keras.Model) – model

  • outer_lr (float, optional) – Outer learning rate. Defaults to 0.001.

  • inner_lr (float, optional) – Inner learning rate. Defaults to 0.001.

  • epochs (int, optional) – Training duration. Defaults to 40.

  • cluster_batch_size (int, optional) – Per-domain batch size. Defaults to 4.

  • loss_fn (optional) – Loss function. Defaults to categorical_crossentropy.

  • meta_test_weight (float, optional) – Weighting of meta-test gradient step relative to meta-training gradient step. Defaults to 1.

  • verbose (bool, optional) – Defaults to False.

Returns:

trained model

Return type:

tf.keras.Model

armed.models.mlp_classifiers module#

Simple neural networks for classification

class armed.models.mlp_classifiers.Adversary(*args: Any, **kwargs: Any)#

Bases: Layer

call(inputs)#
get_config()#
class armed.models.mlp_classifiers.BaseMLP(*args: Any, **kwargs: Any)#

Bases: Model

call(inputs)#
class armed.models.mlp_classifiers.ClusterCovariateMLP(*args: Any, **kwargs: Any)#

Bases: BaseMLP

Basic MLP that concatenates the site membership design matrix to the data.

call(inputs)#
class armed.models.mlp_classifiers.DomainAdversarialMLP(*args: Any, **kwargs: Any)#

Bases: Model

call(inputs)#
compile(loss_class=tensorflow.keras.losses.BinaryCrossentropy, loss_adv=tensorflow.keras.losses.CategoricalCrossentropy, metric_class=tensorflow.keras.metrics.AUC, metric_adv=tensorflow.keras.metrics.CategoricalAccuracy, opt_main=tensorflow.keras.optimizers.Adam, opt_adversary=tensorflow.keras.optimizers.Adam, loss_class_weight=1.0, loss_gen_weight=1.0)#

Compile model with selected losses and metrics. Must be called before training.

Loss weights apply to the main model: total_loss = loss_class_weight * loss_class - loss_gen_weight * loss_adv

Parameters:
  • loss_class (loss, optional) – Main classification loss. Defaults to tf.keras.losses.BinaryCrossentropy().

  • loss_adv (loss, optional) – Adversary classification loss. Defaults to tf.keras.losses.CategoricalCrossentropy().

  • metric_class (metric, optional) – Main classification metric. Defaults to tf.keras.metrics.AUC(curve=’PR’, name=’auprc’).

  • metric_adv (metric, optional) – Adversary classification metric. Defaults to tf.keras.metrics.CategoricalAccuracy(name=’acc’).

  • opt_main (optimizer, optional) – Main optimizer. Defaults to tf.keras.optimizers.Adam(lr=0.001).

  • opt_adversary (optimizer, optional) – Adversary optimizer. Defaults to tf.keras.optimizers.Adam(lr=0.001).

  • loss_class_weight (float, optional) – Classification loss weight. Defaults to 1.0.

  • loss_gen_weight (float, optional) – Generalization loss weight. Defaults to 1.0.

property metrics#
test_step(data)#
train_step(data)#
class armed.models.mlp_classifiers.MLPActivations(*args: Any, **kwargs: Any)#

Bases: Layer

call(inputs)#
get_config()#
class armed.models.mlp_classifiers.MixedEffectsMLP(*args: Any, **kwargs: Any)#

Bases: DomainAdversarialMLP

call(inputs, training=None)#
compile(loss_class=tensorflow.keras.losses.BinaryCrossentropy, loss_adv=tensorflow.keras.losses.CategoricalCrossentropy, metric_class_me=tensorflow.keras.metrics.AUC, metric_class_fe=tensorflow.keras.metrics.AUC, metric_adv=tensorflow.keras.metrics.CategoricalAccuracy, opt_main=tensorflow.keras.optimizers.Adam, opt_adversary=tensorflow.keras.optimizers.Adam, loss_class_me_weight=1.0, loss_class_fe_weight=1.0, loss_gen_weight=1.0)#

Compile model with selected losses and metrics. Must be called before training.

Loss weights apply to the main model: total_loss = loss_class_me_weight * loss_class_me + loss_class_fe_weight * loss_class_fe

  • loss_gen_weight * loss_adv

Parameters:
  • loss_class (loss, optional) – Main classification loss. This applies to both the mixed and fixed effects-based classifications. Defaults to tf.keras.losses.BinaryCrossentropy().

  • loss_adv (loss, optional) – Adversary classification loss. Defaults to tf.keras.losses.CategoricalCrossentropy().

  • metric_class_me (metric, optional) – Metric for classification using mixed effects. Defaults to tf.keras.metrics.AUC(curve=’PR’, name=’auprc’).

  • metric_class_fe (metric, optional) – Metric for classification using fixed effects. Defaults to tf.keras.metrics.AUC(curve=’PR’, name=’auprc_fe’).

  • metric_adv (metric, optional) – Adversary classification metric. Defaults to tf.keras.metrics.CategoricalAccuracy(name=’acc’).

  • opt_main (optimizer, optional) – Main optimizer. Defaults to tf.keras.optimizers.Adam(lr=0.001).

  • opt_adversary (optimizer, optional) – Adversary optimizer. Defaults to tf.keras.optimizers.Adam(lr=0.001).

  • loss_class_me_weight (float, optional) – Weight for classification using mixed effects. Defaults to 1.0.

  • loss_class_fe_weight (float, optional) – Weight for classification using fixed effects. Defaults to 1.0.

  • loss_gen_weight (float, optional) – Generalization loss weight. Defaults to 1.0.

property metrics#
test_step(data)#
train_step(data)#
class armed.models.mlp_classifiers.MixedEffectsMLPNonlinearSlope(*args: Any, **kwargs: Any)#

Bases: MixedEffectsMLP

call(inputs, training=None)#
class armed.models.mlp_classifiers.RandomEffectsLinearSlopeIntercept(*args: Any, **kwargs: Any)#

Bases: Layer

call(inputs, training=None)#
get_config()#

armed.models.random_effects module#

Core random effects Bayesian layers

class armed.models.random_effects.ClusterScaleBiasBlock(*args: Any, **kwargs: Any)#

Bases: Layer

call(inputs, training=None)#
get_config()#
class armed.models.random_effects.GammaRandomEffects(*args: Any, **kwargs: Any)#

Bases: RandomEffects

call(inputs, training=None)#
get_config()#
class armed.models.random_effects.NamedVariableLayer(*args: Any, **kwargs: Any)#

Bases: VariableLayer

get_config()#
class armed.models.random_effects.RandomEffects(*args: Any, **kwargs: Any)#

Bases: DenseVariational

call(inputs, training=None)#
armed.models.random_effects.make_deterministic_posterior_fn()#
armed.models.random_effects.make_fixed_prior_fn(prior_scale)#
armed.models.random_effects.make_gamma_prior_fn()#
armed.models.random_effects.make_posterior_fn(post_loc_init_scale, post_scale_init_min, post_scale_init_range)#
armed.models.random_effects.make_trainable_prior_fn(prior_scale)#

Module contents#