HybridQRBM package

Submodules

HybridQRBM.callbacks module

HybridQRBM.optimizers module

Optimizer to update the weights of the RBM.

class HybridQRBM.optimizers.Parameters(weights, biases_visible, biases_hidden)

Bases: tuple

biases_hidden

Alias for field number 2

biases_visible

Alias for field number 1

weights

Alias for field number 0

class HybridQRBM.optimizers.RBMOptimizer(learning_rate: float = 0.05, momentum: float = 0.9, decay_factor: float = 1.0005, regularizers: tuple = ())[source]

Bases: object

Optimizer to update the weights of the RBM.

calculate_update(positive_sample, negative_sample)[source]

Calculate the update for the weights and biases.

Parameters
  • positive_sample (tuple) – The values and probabilities of the visible and hidden layers produced in the positive phase of the training. The tuple should contain four elements: (visible_values, visible_probabilities, hidden_values, hidden_probabilities).

  • negative_sample (tuple) – The values and probabilities of the visible and hidden layers produced in the negative phase of the training. The tuple should contain four elements: (visible_values, visible_probabilities, hidden_values, hidden_probabilities).

Returns

The delta values for the update of the weights and biases.

Return type

namedtuple

HybridQRBM.pytorchdnx module

HybridQRBM.rbm module

HybridQRBM.samplers module

Samplers for the RBM training.

class HybridQRBM.samplers.ContrastiveDivergenceSampler(num_gibbs_updates: int)[source]

Bases: HybridQRBM.samplers.Sampler

Implements sampling for contrastive divergence training.

sample(visible: numpy.ndarray[Any, numpy.dtype[numpy._typing._array_like._ScalarType_co]], **kwargs) tuple[source]

Implements sampling for contrastive divergence training.

The expected use is to start with feature values for the visible layer and then perform a number (defined in the constructor) of Gibbs updates. If the number of Gibbs updates is 1, this implements the standard CD-1 training.

Parameters

visible (numpy.ndarray) – The visible layer values. Shape (num_samples, num_visible).

Returns

visible, prob_visible, hidden, prob_hidden – See gibbs_updates() method for details.

Return type

tuple

class HybridQRBM.samplers.DWaveInferenceSampler(dwave_sampler, num_spin_reversal_transforms, temp=1.0, num_gibbs_updates=0, chain_strength=None, dwave_params=None, **kwargs)[source]

Bases: HybridQRBM.samplers.DWaveSampler

Extends DWaveSampler with inference / prediction.

predict(features: numpy.ndarray[Any, numpy.dtype[numpy._typing._array_like._ScalarType_co]], num_particles: int = 100, num_gibbs_updates: int = None, use_cache=False, **kwargs) numpy.ndarray[Any, numpy.dtype[numpy._typing._array_like._ScalarType_co]][source]

Predict the labels for the given feature values.

Parameters
  • features (numpy.ndarray[np.int0]) – The feature values to predict the labels for. Shape (num_samples, num_features).

  • num_particles (int) – Number of particles to use for the sampling, i.e. how may times to run the label sampling process for each sample.

  • num_gibbs_updates (int, optional) – Number of Gibbs updates to perform for each particle. If not provided, self.num_gibbs_updates will be used.

  • use_cache (bool) – If set to True, and a cached response is available, it will be used.

Returns

labels – The predicted labels. Shape (num_samples, num_label_classes).

Return type

numpy.ndarray[np.float]

class HybridQRBM.samplers.DWaveSampler(dwave_sampler, num_spin_reversal_transforms, temp=1.0, num_gibbs_updates=0, chain_strength=None, dwave_params=None, **kwargs)[source]

Bases: HybridQRBM.samplers.Sampler

Implements sampling for the D-Wave machine.

sample(visible, **kwargs)[source]

Implements sampling for the D-Wave machine.

Parameters

visible (numpy.ndarray) – The visible layer values are always passed from the RBM. When sampling from the model distribution, it is only used to determine the shape of the sample.

Returns

visible, prob_visible, hidden, prob_hidden – visible and hidden are the samples returned by the DWave sampler. prob_visible and prob_hidden are calculated based on the DWave sample values.

Return type

tuple

class HybridQRBM.samplers.DynexSampler(num_reads=100, annealing_time=300, mainnet=False, clones=1, minimum_stepsize=6e-08, logging=True, debugging=False, num_gibbs_updates=0, **kwargs)[source]

Bases: HybridQRBM.samplers.Sampler

sample(visible, **kwargs)[source]

Implements sampling for the Dynex Neuromorphic Platform.

Parameters

visible (numpy.ndarray) – The visible layer values are always passed from the RBM. When sampling from the model distribution, it is only used to determine the shape of the sample.

Returns

visible, prob_visible, hidden, prob_hidden – visible and hidden are the samples returned by the Dynex sampler. prob_visible and prob_hidden are calculated based on the Dynex sample values.

Return type

tuple

class HybridQRBM.samplers.NaiveSampler(num_gibbs_updates: int)[source]

Bases: HybridQRBM.samplers.Sampler

Implements sampling for naive training.

sample(visible, **kwargs)[source]

Implements sampling for naive training.

The particles (Markov chains) for the negative phase are initialized to random values at the beginning of each batch update. If the Markov chains are properly burnt in, the sampled particles represent the model distribution better than those from either contrastive divergence or persistent contrastive divergence; howevere the required number of Gibbs updates to burn in the Markov chains is unknown and likely high.

Parameters

visible (numpy.ndarray) – The visible layer values are always passed from the RBM. For this sampler it is only used to determine the shape of the sample.

Returns

visible, prob_visible, hidden, prob_hidden – See gibbs_updates() method for details.

Return type

tuple

class HybridQRBM.samplers.PersistentContrastiveDivergenceSampler(*args, **kwargs)[source]

Bases: HybridQRBM.samplers.Sampler

Implements sampling for persistent contrastive divergence training.

sample(visible, **kwargs) tuple[source]

Performs a number of Gibbs updates, starting from the state of the visible layer after the previous update.

Parameters

visible (numpy.ndarray) – The visible layer values are always passed from the RBM. For this sampler it is only used to determine the shape of the sample.

Returns

visible, prob_visible, hidden, prob_hidden – See gibbs_updates() method for details.

Return type

tuple

class HybridQRBM.samplers.Sampler(num_gibbs_updates: int)[source]

Bases: object

Base class for samplers.

generate(hidden: numpy.ndarray[Any, numpy.dtype[numpy._typing._array_like._ScalarType_co]])[source]

Generate the visible layer given the hidden layer.

Parameters

hidden (numpy.ndarray) – The hidden layer. Shape (num_samples, num_hidden).

Returns

  • visible (numpy.ndarray) – Binary visible layer values sampled from their probability distribution. Shape (num_samples, num_visible).

  • prob_visible (numpy.ndarray) – The probability for each unit of the visible layer to be 1. Shape (num_samples, num_visible).

gibbs_updates(visible: numpy.ndarray[Any, numpy.dtype[numpy._typing._array_like._ScalarType_co]], num_gibbs_updates=None)[source]

Perform Gibbs sampling starting from some visible layer values.

Parameters
  • visible (numpy.ndarray) – The initial visible layer values. Shape (num_samples, num_visible).

  • num_gibbs_updates (int) – The number of full updates to perform. If None, the default for the sampler is performed.

Returns

  • visible (numpy.ndarray) – The visible layer after the Gibbs sampling. Shape (num_samples, num_visible).

  • prob_visible (numpy.ndarray) – The probability for each unit of the visible layer to be 1. Shape (num_samples, num_visible).

  • hidden (numpy.ndarray) – Binary hidden layer values sampled from their probability distribution. Shape (num_samples, num_hidden).

  • prob_hidden (numpy.ndarray) – The probability for each unit of the hidden layer to be 1. Shape (num_samples, num_hidden).

infer(visible: numpy.ndarray[Any, numpy.dtype[numpy._typing._array_like._ScalarType_co]])[source]

Infer the hidden layer given the visible layer.

Parameters

visible (numpy.ndarray) – The visible layer. Shape (num_samples, num_visible).

Returns

  • hidden (numpy.ndarray) – Binary hidden layer values sampled from their probability distribution. Shape (num_samples, num_hidden).

  • prob_hidden (numpy.ndarray) – The probability for each unit of the hidden layer to be 1. Shape (num_samples, num_hidden).

predict(features: numpy.ndarray[Any, numpy.dtype[numpy._typing._array_like._ScalarType_co]], num_particles: int = 10, num_gibbs_updates: int = None, **kwargs) numpy.ndarray[Any, numpy.dtype[numpy._typing._array_like._ScalarType_co]][source]

Predict the features and labels for the given feature values.

Parameters
  • features (numpy.ndarray[np.int0]) – The feature values to predict the labels for. Shape (num_samples, num_features).

  • num_particles (int) – Number of particles to use for the sampling, i.e. how may times to run the label sampling process for each sample.

  • num_gibbs_updates (int, optional) – Number of Gibbs updates to perform for each particle. If not provided, self.num_gibbs_updates will be used.

Returns

  • labels (numpy.ndarray[np.float]) – The predicted labels. Shape (num_samples, num_labels).

  • features (numpy.ndarray[np.float]) – The predicted features. Shape (num_samples, num_features).

sample(visible: numpy.ndarray[Any, numpy.dtype[numpy._typing._array_like._ScalarType_co]], **kwargs)[source]

Abstract method for sampling from the model distribution. Possibly with some clamped visible layer values.

HybridQRBM.utils module

Module contents