soft.computing.wrappers package

Submodules

soft.computing.wrappers.clustering module

Implements helpful ‘wrapper’ methods that are primarily used for clustering algorithms in a way that is compatible with soft.computing.organize.SelfOrganize.

soft.computing.wrappers.clustering.g_means_wrapper(dataset: SupervisedDataset) LabeledClusters

Given the input data, discover clusters using the G-means algorithm and return their centers.

Parameters:

dataset – The dataset.

Returns:

The clusters’ centers discovered by the G-means algorithm.

soft.computing.wrappers.clustering.hdbscan_wrapper(dataset: SupervisedDataset) LabeledClusters

Apply the Hierarchical Density-Based Spatial Clustering of Applications with Noise algorithm and return the approximate centers of the discovered clusters.

Parameters:

dataset – The dataset.

Returns:

The approximate centers of the discovered clusters.

soft.computing.wrappers.clustering.persistable_wrapper(dataset: SupervisedDataset) LabeledClusters

Cluster the data using the Persistable Python library.

Parameters:

dataset – The dataset.

Returns:

The approximate centers of the discovered clusters.

soft.computing.wrappers.clustering.two_threshold_sequential_algorithmic_scheme_wrapper(dataset: SupervisedDataset) LabeledClusters

Given the dataset, discover clusters using the TTSAS algorithm and return their centers.

Parameters:

dataset – The dataset.

Returns:

The clusters’ centers discovered by the TTSAS algorithm.

soft.computing.wrappers.d3rlpy module

Implements helpful ‘wrapper’ methods that are primarily used with algorithms from the d3rlpy library in a way that is compatible with soft.computing.organize.SelfOrganize.

class soft.computing.wrappers.d3rlpy.CustomDiscreteMeanQFunction(encoder: Encoder, hidden_size: int, action_size: int)

Bases: DiscreteMeanQFunction

A custom discrete mean Q-function. This is to simply use the provided model (i.e., encoder/fuzzy logic controller) rather than non-interpretable neural network operations.

property encoder: Encoder
forward(x: Tensor)

Forward pass of the Q-function.

class soft.computing.wrappers.d3rlpy.CustomEncoder(*args, knowledge_base: KnowledgeBase, specifications: Specifications, consequences: Tensor, disabled_parameters: Sequence[str] | None = None, **kwargs)

Bases: Module

A custom encoder wrapper for use with the d3rlpy library.

forward(observations: Tensor)

Conduct fuzzy inference on the input.

Parameters:

observations – The input to the fuzzy logic controller.

Returns:

The output of the fuzzy logic controller.

get_feature_size() int

Get the feature size of the output space; this is required by d3rlpy.

Returns:

The dimensionality of the output space.

class soft.computing.wrappers.d3rlpy.CustomEncoderFactory(knowledge_base: KnowledgeBase, specifications: Specifications, consequences: Tensor | None = None, disabled_parameters: Sequence[str] | None = None)

Bases: EncoderFactory

A custom encoder factory wrapper for use with the d3rlpy library.

create(observation_shape: tuple) CustomEncoder

Returns PyTorch’s state enocder module.

Parameters:

observation_shape – observation shape.

Returns:

an enocder object.

create_with_action(observation_shape: Sequence[int], action_size: int, discrete_action: bool = False) EncoderWithAction

Returns PyTorch’s state-action enocder module.

Parameters:
  • observation_shape – observation shape.

  • action_size – action size. If None, the encoder does not take action as input.

  • discrete_action – flag if action-space is discrete.

Returns:

an enocder object.

get_params(deep=False) Dict[str, Any]

Get the parameters of the encoder factory.

static get_type() str
class soft.computing.wrappers.d3rlpy.CustomMeanQFunctionFactory(share_encoder: bool = False)

Bases: QFunctionFactory

Standard Q function factory class.

This is the standard Q function factory class.

References

create_continuous(encoder: EncoderWithAction, hidden_size: int) NoReturn

Create a continuous Q-function.

create_discrete(encoder: Encoder, hidden_size: int, action_size: int)

Create a discrete Q-function. This is required by d3rlpy.

get_params(deep: bool = False) Dict[str, Any]

Get the parameters of the Q-function factory.

static get_type() str

Get the type of the Q-function factory. This is required by d3rlpy.

class soft.computing.wrappers.d3rlpy.CustomPixelEncoder(observation_shape: Sequence[int], action_size: int, filters: List[Sequence[int]] | None = None, feature_size: int = 512, use_batch_norm: bool = False, dropout_rate: float | None = False, activation: Module = ReLU(), consequences: Tensor | None = None)

Bases: Module

A custom pixel encoder wrapper for use with the d3rlpy library.

forward(input_tensor: Tensor) Tensor

Forward pass of the encoder.

get_feature_size() int

Get the feature size of the input space; this is required by d3rlpy.

property last_layer: Linear

Get the last layer of the encoder; this is required by d3rlpy.

property observation_shape: Sequence[int]

Get the observation shape; this is required by d3rlpy.

class soft.computing.wrappers.d3rlpy.CustomPixelEncoderFactory(action_size: int, filters: List[Sequence[int]] | None = None, feature_size: int = 512, activation: str = 'relu', use_batch_norm: bool = False, dropout_rate: float | None = None)

Bases: EncoderFactory

A custom pixel encoder factory wrapper for use with the d3rlpy library.

create(observation_shape: Sequence[int]) CustomPixelEncoder

Create a custom pixel encoder.

create_with_action(observation_shape: Sequence[int], action_size: int, discrete_action: bool = False) NoReturn

Create a custom pixel encoder with action.

get_params(deep: bool = False) Dict[str, Any]

Get the parameters of the encoder factory.

static get_type() str

Get the type of the encoder factory. This is required by d3rlpy.

soft.computing.wrappers.fetchers module

Implements helpful ‘wrapper’ methods primarily used by soft.computing.organize.SelfOrganize.

soft.computing.wrappers.fetchers.fetch_model_from_train_results(train_results: Tuple[Module, Dict[str, List[float]]]) Module

Conveniently fetches the torch.nn.Module from the results of the function ‘train’.

Parameters:
  • train_results – A 2-tuple where the first element is a torch.nn.Module and the second

  • keys (element is a dictionary with two) – ‘train’ and ‘val’.

Returns:

torch.nn.Module

soft.computing.wrappers.fetchers.fetch_supervised_dataset(labeled_clusters: LabeledClusters) SupervisedDataset

Conveniently returns the centers of a sequence of fuzzy sets. This is useful for some self-organizing neuro-fuzzy networks, such as those that use the CLIP and ECM algorithms.

Parameters:
  • labeled_clusters – The labeled clusters, where each cluster is a ContinuousFuzzySet

  • type. (and the label is a torch.Tensor)

Returns:

A dataset that has fuzzy set centers as the inputs, with their corresponding labels as targets.

soft.computing.wrappers.sb3 module

This file contains classes and functions that are used to collect data with the stable-baselines3 library.

class soft.computing.wrappers.sb3.ExportExperienceReplayBuffer(verbose=0, path=None)

Bases: BaseCallback

A custom callback class for use with the stable-baselines3 library that will export the experience replay into a d3rlpy MDPDataset.

soft.computing.wrappers.sb3.save_model_and_data(path: Path, model, num_of_timesteps)
Parameters:
  • path

  • model

  • num_of_timesteps

soft.computing.wrappers.skorch module

This file provides wrapper or extension classes for the skorch library.

class soft.computing.wrappers.skorch.SoftRegressor(module, *args, criterion=<class 'torch.nn.modules.loss.MSELoss'>, **kwargs)

Bases: NeuralNetRegressor

A wrapper around skorch’s NeuralNetRegressor that allows for the use of soft computing techniques, such as neuro-fuzzy systems and network morphisms.

infer(x: Tensor, **fit_params)

Infer the output of the module.

load_params(f_params=None, f_optimizer=None, f_criterion=None, f_history=None, checkpoint=None, use_safetensors=False, **kwargs)

Loads the module’s parameters, history, and optimizer, not the whole object.

To save and load the whole object, use pickle.

f_params, f_optimizer, etc. uses PyTorch’s load().

If you’ve created a custom module, e.g. net.mymodule_, you can save that as well by passing f_mymodule.

Parameters:
  • f_params (file-like object, str, None (default=None)) – Path of module parameters. Pass None to not load.

  • f_optimizer (file-like object, str, None (default=None)) – Path of optimizer. Pass None to not load.

  • f_criterion (file-like object, str, None (default=None)) – Path of criterion. Pass None to not save

  • f_history (file-like object, str, None (default=None)) – Path to history. Pass None to not load.

  • checkpoint (Checkpoint, None (default=None)) – Checkpoint to load params from. If a checkpoint and a f_* path is passed in, the f_* will be loaded. Pass None to not load.

  • use_safetensors (bool (default=False)) – Whether to use the safetensors library to load the state. By default, PyTorch is used, which in turn uses pickle under the hood. When the state was saved with safetensors=True when skorch.net.NeuralNet.save_params() was called, it should be set to True here as well.

Examples

>>> before = NeuralNetClassifier(mymodule)
>>> before.save_params(f_params='model.pkl',
>>>                    f_optimizer='optimizer.pkl',
>>>                    f_history='history.json')
>>> after = NeuralNetClassifier(mymodule).initialize()
>>> after.load_params(f_params='model.pkl',
>>>                   f_optimizer='optimizer.pkl',
>>>                   f_history='history.json')
module_parameters() Dict[str, Any]

Get the parameters of the module.

class soft.computing.wrappers.skorch.WandbEpochScoring(*args, **kwargs)

Bases: EpochScoring

Overrides the behavior of recording the score to also record it to wandb.

soft.computing.wrappers.temporal module

Classes and functions that assist with temporal operations, such as TimeDistributed class, or a wrapper to construct KnowledgeBase from the Fuzzy Temporal Association Rule Mining algorithm.

class soft.computing.wrappers.temporal.TimeDistributed(module: Module, batch_first: bool = False)

Bases: Module

A wrapper class for PyTorch modules that allows them to operate on a sequence of data.

forward(input_data: Tensor) Tensor

Forward pass of the TimeDistributed wrapper class.

Parameters:

input_data – The input data.

Returns:

The output of the module on the input sequence of data.

soft.computing.wrappers.temporal.fuzzy_temporal_association_rule_mining_wrapper(dataset: SupervisedDataset, linguistic_variables: LinguisticVariables, config: Config) KnowledgeBase

Convenient wrapper method for Fuzzy Temporal Association Rule Mining to be used by soft.computing.organize.SelfOrganize with data that is not temporal. This is done by placing a temporary ‘date’ attribute that the algorithm relies on.

Parameters:
  • dataset – Non-temporal dataset.

  • linguistic_variables – Linguistic variables.

  • config – A configuration dictionary.

Returns:

soft.computing.knowledge.KnowledgeBase

Module contents