MCPcopy
hub / github.com/ddbourgin/numpy-ml / reconstruct

Method reconstruct

numpy_ml/neural_nets/layers/layers.py:587–625  ·  view source on GitHub ↗

Reconstruct an input `X` by running the trained Gibbs sampler for `n_steps`-worth of CD-`k`. Parameters ---------- X : :py:class:`ndarray ` of shape `(n_ex, n_in)` Layer input, representing the `n_in`-dimensional features for a

(self, X, n_steps=10, return_prob=False)

Source from the content-addressed store, hash-verified

585 self.gradients["W"] = positive_grad - negative_grad
586
587 def reconstruct(self, X, n_steps=10, return_prob=False):
588 """
589 Reconstruct an input `X` by running the trained Gibbs sampler for
590 `n_steps`-worth of CD-`k`.
591
592 Parameters
593 ----------
594 X : :py:class:`ndarray <numpy.ndarray>` of shape `(n_ex, n_in)`
595 Layer input, representing the `n_in`-dimensional features for a
596 minibatch of `n_ex` examples. Each feature in `X` should ideally be
597 binary-valued, although it is possible to also train on real-valued
598 features ranging between (0, 1) (e.g., grayscale images). If `X` has
599 missing values, it may be sufficient to mark them with random
600 entries and allow the reconstruction to impute them.
601 n_steps : int
602 The number of Gibbs sampling steps to perform when generating the
603 reconstruction. Default is 10.
604 return_prob : bool
605 Whether to return the real-valued feature probabilities for the
606 reconstruction or the binary samples. Default is False.
607
608 Returns
609 -------
610 V : :py:class:`ndarray <numpy.ndarray>` of shape `(n_ex, in_ch)`
611 The reconstruction (or feature probabilities if `return_prob` is
612 true) of the visual input `X` after running the Gibbs sampler for
613 `n_steps`.
614 """
615 self.forward(X, K=n_steps)
616 p_V_prime = self.derived_variables["p_V_prime"]
617
618 # ignore the gradients produced during this reconstruction
619 self.flush_gradients()
620
621 # sample V_prime reconstruction if return_prob is False
622 V = p_V_prime
623 if not return_prob:
624 V = (np.random.rand(*p_V_prime.shape) <= p_V_prime).astype(float)
625 return V
626
627
628#######################################################################

Callers

nothing calls this directly

Calls 2

forwardMethod · 0.95
flush_gradientsMethod · 0.45

Tested by

no test coverage detected