class_docstr: Boolean/Bernoulli Restricted Boltzmann Machine (RBM).
  Algorithm assumes that inputs are either binary
  values or values between 0 and 1.
  
  Parameters
  ----------
  n_visible : int
      Number of visible units. Number of features (columns)
      in the input data.
  
  n_hidden : int
      Number of hidden units. The large the number the more
      information network can capture from the data, but it
      also mean that network is more likely to overfit.
  
  batch_size : int
      Size of the mini-batch. Defaults to ``10``.
  
  weight : array-like, Tensorfow variable, Initializer or scalar
      Default initialization methods
      you can find :ref:`here <init-methods>`.
      Defaults to :class:`Normal <neupy.init.Normal>`.
  
  hidden_bias : array-like, Tensorfow variable, Initializer or scalar
      Default initialization methods
      you can find :ref:`here <init-methods>`.
      Defaults to :class:`Constant(value=0) <neupy.init.Constant>`.
  
  visible_bias : array-like, Tensorfow variable, Initializer or scalar
      Default initialization methods
      you can find :ref:`here <init-methods>`.
      Defaults to :class:`Constant(value=0) <neupy.init.Constant>`.
  
  {BaseNetwork.Parameters}
  
  Methods
  -------
  train(X_train, epochs=100)
      Trains network.
  
  predict(X)
      Alias to the ``visible_to_hidden`` method.
  
  visible_to_hidden(visible_input)
      Propagates data through the network and returns output
      from the hidden layer.
  
  hidden_to_visible(hidden_input)
      Propagates output from the hidden layer backward
      to the visible.
  
  gibbs_sampling(visible_input, n_iter=1)
      Makes Gibbs sampling ``n`` times using visible input.
  
  {BaseSkeleton.fit}
  
  Examples
  --------
  >>> import numpy as np
  >>> from neupy import algorithms
  >>>
  >>> data = np.array([
  ...     [1, 0, 1, 0],
  ...     [1, 0, 1, 0],
  ...     [1, 0, 0, 0],  # incomplete sample
  ...     [1, 0, 1, 0],
  ...
  ...     [0, 1, 0, 1],
  ...     [0, 0, 0, 1],  # incomplete sample
  ...     [0, 1, 0, 1],
  ...     [0, 1, 0, 1],
  ...     [0, 1, 0, 1],
  ...     [0, 1, 0, 1],
  ... ])
  >>>
  >>> rbm = algorithms.RBM(n_visible=4, n_hidden=1)
  >>> rbm.train(data, epochs=100)
  >>>
  >>> hidden_states = rbm.visible_to_hidden(data)
  >>> hidden_states.round(2)
  array([[ 0.99],
         [ 0.99],
         [ 0.95],
         [ 0.99],
         [ 0.  ],
         [ 0.01],
         [ 0.  ],
         [ 0.  ],
         [ 0.  ],
         [ 0.  ]])
  
  References
  ----------
  [1] G. Hinton, A Practical Guide to Training Restricted
      Boltzmann Machines, 2010.
      http://www.cs.toronto.edu/~hinton/absps/guideTR.pdf
class_docstr_tok: ["Boolean\/Bernoulli","Restricted","Boltzmann","Machine","(","RBM",")",".","Algorithm","assumes","that","inputs","are","either","binary","values","or","values","between","0","and","1",".","Parameters","--","--","--","--","--","n_visible",":","int","Number","of","visible","units",".","Number","of","features","(","columns",")","in","the","input","data",".","n_hidden",":","int","Number","of","hidden","units",".","The","large","the","number","the","more","information","network","can","capture","from","the","data",",","but","it","also","mean","that","network","is","more","likely","to","overfit",".","batch_size",":","int","Size","of","the","mini-batch",".","Defaults","to","``","10","``",".","weight",":","array-like",",","Tensorfow","variable",",","Initializer","or","scalar","Default","initialization","methods","you","can","find",":","ref",":","`","here","<","init-methods",">","`",".","Defaults","to",":","class",":","`","Normal","<","neupy.init.Normal",">","`",".","hidden_bias",":","array-like",",","Tensorfow","variable",",","Initializer","or","scalar","Default","initialization","methods","you","can","find",":","ref",":","`","here","<","init-methods",">","`",".","Defaults","to",":","class",":","`","Constant","(","value=0",")","<","neupy.init.Constant",">","`",".","visible_bias",":","array-like",",","Tensorfow","variable",",","Initializer","or","scalar","Default","initialization","methods","you","can","find",":","ref",":","`","here","<","init-methods",">","`",".","Defaults","to",":","class",":","`","Constant","(","value=0",")","<","neupy.init.Constant",">","`",".","{","BaseNetwork.Parameters","}","Methods","--","--","--","-","train","(","X_train",",","epochs=100",")","Trains","network",".","predict","(","X",")","Alias","to","the","``","visible_to_hidden","``","method",".","visible_to_hidden","(","visible_input",")","Propagates","data","through","the","network","and","returns","output","from","the","hidden","layer",".","hidden_to_visible","(","hidden_input",")","Propagates","output","from","the","hidden","layer","backward","to","the","visible",".","gibbs_sampling","(","visible_input",",","n_iter=1",")","Makes","Gibbs","sampling","``","n","``","times","using","visible","input",".","{","BaseSkeleton.fit","}","Examples","--","--","--","--",">",">",">","import","numpy","as","np",">",">",">","from","neupy","import","algorithms",">",">",">",">",">",">","data","=","np.array","(","[","...","[","1",",","0",",","1",",","0","]",",","...","[","1",",","0",",","1",",","0","]",",","...","[","1",",","0",",","0",",","0","]",",","#","incomplete","sample","...","[","1",",","0",",","1",",","0","]",",","...","...","[","0",",","1",",","0",",","1","]",",","...","[","0",",","0",",","0",",","1","]",",","#","incomplete","sample","...","[","0",",","1",",","0",",","1","]",",","...","[","0",",","1",",","0",",","1","]",",","...","[","0",",","1",",","0",",","1","]",",","...","[","0",",","1",",","0",",","1","]",",","...","]",")",">",">",">",">",">",">","rbm","=","algorithms.RBM","(","n_visible=4",",","n_hidden=1",")",">",">",">","rbm.train","(","data",",","epochs=100",")",">",">",">",">",">",">","hidden_states","=","rbm.visible_to_hidden","(","data",")",">",">",">","hidden_states.round","(","2",")","array","(","[","[","0.99","]",",","[","0.99","]",",","[","0.95","]",",","[","0.99","]",",","[","0",".","]",",","[","0.01","]",",","[","0",".","]",",","[","0",".","]",",","[","0",".","]",",","[","0",".","]","]",")","References","--","--","--","--","--","[","1","]","G.","Hinton",",","A","Practical","Guide","to","Training","Restricted","Boltzmann","Machines",",","2010.","http",":","\/\/www.cs.toronto.edu\/~hinton\/absps\/guideTR.pdf"]
code: def init_methods(self):
      def free_energy(visible_sample):
          with tf.name_scope("free-energy"):
              wx = tf.matmul(visible_sample, self.weight)
              wx_b = wx + self.hidden_bias
  
              visible_bias_term = dot(visible_sample, self.visible_bias)
  
              # We can get infinity when wx_b is a relatively large number
              # (maybe 100). Taking exponent makes it even larger and
              # for with float32 it can convert it to infinity. But because
              # number is so large we don't care about +1 value before taking
              # logarithms and therefore we can just pick value as it is
              # since our operation won't change anything.
              hidden_terms = tf.where(
                  # exp(30) is such a big number that +1 won't
                  # make any difference in the outcome.
                  tf.greater(wx_b, 30),
                  wx_b,
                  tf.log1p(tf.exp(wx_b)),
              )
  
              hidden_term = tf.reduce_sum(hidden_terms, axis=1)
              return -(visible_bias_term + hidden_term)
  
      def visible_to_hidden(visible_sample):
          with tf.name_scope("visible-to-hidden"):
              wx = tf.matmul(visible_sample, self.weight)
              wx_b = wx + self.hidden_bias
              return tf.nn.sigmoid(wx_b)
  
      def hidden_to_visible(hidden_sample):
          with tf.name_scope("hidden-to-visible"):
              wx = tf.matmul(
                  hidden_sample, self.weight, transpose_b=True
              )
              wx_b = wx + self.visible_bias
              return tf.nn.sigmoid(wx_b)
  
      def sample_hidden_from_visible(visible_sample):
          with tf.name_scope("sample-hidden-to-visible"):
              hidden_prob = visible_to_hidden(visible_sample)
              hidden_sample = random_binomial(hidden_prob)
              return hidden_sample
  
      def sample_visible_from_hidden(hidden_sample):
          with tf.name_scope("sample-visible-to-hidden"):
              visible_prob = hidden_to_visible(hidden_sample)
              visible_sample = random_binomial(visible_prob)
              return visible_sample
  
      network_input = self.network_input
      network_hidden_input = self.network_hidden_input
      input_shape = tf.shape(network_input)
      n_samples = input_shape[0]
  
      weight = self.weight
      h_bias = self.hidden_bias
      v_bias = self.visible_bias
      h_samples = self.h_samples
      step = asfloat(self.step)
  
      with tf.name_scope("positive-values"):
          # We have to use `cond` instead of `where`, because
          # different if-else cases might have different shapes
          # and it triggers exception in tensorflow.
          v_pos = tf.cond(
              tf.equal(n_samples, self.batch_size),
              lambda: network_input,
              lambda: random_sample(network_input, self.batch_size),
          )
          h_pos = visible_to_hidden(v_pos)
  
      with tf.name_scope("negative-values"):
          v_neg = sample_visible_from_hidden(h_samples)
          h_neg = visible_to_hidden(v_neg)
  
      with tf.name_scope("weight-update"):
          weight_update = (
              tf.matmul(v_pos, h_pos, transpose_a=True)
              - tf.matmul(v_neg, h_neg, transpose_a=True)
          ) / asfloat(n_samples)
  
      with tf.name_scope("hidden-bias-update"):
          h_bias_update = tf.reduce_mean(h_pos - h_neg, axis=0)
  
      with tf.name_scope("visible-bias-update"):
          v_bias_update = tf.reduce_mean(v_pos - v_neg, axis=0)
  
      with tf.name_scope("flipped-input-features"):
          # Each row will have random feature marked with number 1
          # Other values will be equal to 0
          possible_feature_corruptions = tf.eye(self.n_visible)
          corrupted_features = random_sample(
              possible_feature_corruptions, n_samples
          )
  
          rounded_input = tf.round(network_input)
          # If we scale input values from [0, 1] range to [-1, 1]
          # than it will be easier to flip feature values with simple
          # multiplication.
          scaled_rounded_input = 2 * rounded_input - 1
          scaled_flipped_rounded_input = (
              # for corrupted_features we convert 0 to 1 and 1 to -1
              # in this way after multiplication we will flip all
              # signs where -1 in the transformed corrupted_features
              (-2 * corrupted_features + 1)
              * scaled_rounded_input
          )
          # Scale it back to the [0, 1] range
          flipped_rounded_input = (scaled_flipped_rounded_input + 1) / 2
  
      with tf.name_scope("pseudo-likelihood-loss"):
          # Stochastic pseudo-likelihood
          error = tf.reduce_mean(
              self.n_visible
              * tf.log_sigmoid(
                  free_energy(flipped_rounded_input)
                  - free_energy(rounded_input)
              )
          )
  
      with tf.name_scope("gibbs-sampling"):
          gibbs_sampling = sample_visible_from_hidden(
              sample_hidden_from_visible(network_input)
          )
  
      tf_utils.initialize_uninitialized_variables()
      self.weight_update_one_step = tf_utils.function(
          [network_input],
          error,
          name="rbm/train-epoch",
          updates=[
              (weight, weight + step * weight_update),
              (h_bias, h_bias + step * h_bias_update),
              (v_bias, v_bias + step * v_bias_update),
              (h_samples, random_binomial(p=h_neg)),
          ],
      )
      self.score_func = tf_utils.function(
          [network_input],
          error,
          name="rbm/prediction-error",
      )
      self.visible_to_hidden_one_step = tf_utils.function(
          [network_input],
          visible_to_hidden(network_input),
          name="rbm/visible-to-hidden",
      )
      self.hidden_to_visible_one_step = tf_utils.function(
          [network_hidden_input],
          hidden_to_visible(network_hidden_input),
          name="rbm/hidden-to-visible",
      )
      self.gibbs_sampling_one_step = tf_utils.function(
          [network_input],
          gibbs_sampling,
          name="rbm/gibbs-sampling",
      )
  
imports: import numpy
  import tensorflow
  from neupy.core.config import DumpableObject
  from neupy.core.properties import IntProperty, ParameterProperty
  from neupy.algorithms.base import BaseNetwork
  from neupy.utils import asfloat, format_data, dot, iters, tf_utils
  from neupy import init
label_desc: Use image node_id 3 for calling the RBM obj's underlying member method code with example usage: obj.init_methods() and returns: unknown, tf, tf, hidden_sample, visible_sample