2

I am trying to implement a restricted boltzmann machine in C++. I am using this Python code as a guide: https://github.com/echen/restricted-boltzmann-machines/blob/master/rbm.py

This is Line 37:

pos_hidden_states = pos_hidden_probs > np.random.rand(num_examples, self.num_hidden + 1)

pos_hidden_states and pos_hidden_probs are both 2D matrices, of type vector<vector<double>> in C++, and num_examples and num_hidden are both integers.

Could anyone explain what the greater-than symbol means here?

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
user3047661
  • 119
  • 2
  • 8
  • 1
    refer here https://docs.scipy.org/doc/numpy/reference/generated/numpy.greater.html#numpy.greater – JkShaw May 11 '17 at 04:31
  • http://stackoverflow.com/questions/1061283/lt-instead-of-cmp & https://docs.python.org/3/reference/datamodel.html#object.__lt__ – Stephen Rauch May 11 '17 at 04:34

4 Answers4

7

Probably not easy to translate numpy into C++, lot's of abstraction in numpy. Anyway, it's acting as a vectorized comparison, because np.random.rand(...) returns a np.ndarray, which if pos_hidden_probs is either a scalar or a np.ndarray it will behave in a vectorized (i.e. elementwise) manner:

>>> rand_array = np.random.rand(2, 2)
>>> rand_array
array([[ 0.1807726 ,  0.67617382],
       [ 0.84396805,  0.04450794]])
>>> 0.5 > rand_array
array([[ True, False],
       [False,  True]], dtype=bool)
>>>

If pos_hidden_probs is some sort of np.ndarray, the behavior might be influenced by broadcasting, a feature of numpy:

>>> np.array([0.5, 0.5]) > rand_array
array([[ True, False],
       [False,  True]], dtype=bool)
>>> np.array([0.5, .9]) > rand_array
array([[ True,  True],
       [False,  True]], dtype=bool)
>>>
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
3

The > operator works element-wise in NumPy, e.g.

np.array([[1,2],[3,4]]) > np.array([[2,2],[2,2]])

gives you np.array([[False,False],[True,True]])

NumPy also does broadcasting, which gives meaning to comparison between arrays of different dimensions.

Ken Wei
  • 3,020
  • 1
  • 10
  • 30
2

The > is comparing the hidden prob (a float in this case) against each item in a 2d numpy array and returning a 2d array of boolean values:

>>> import numpy as np
>>> np.random.randn(3,2)array([[-0.74615339, -1.22667606],
       [ 0.22729787,  0.72070398],
       [-1.06876014,  0.06367189]])
>>> 5.  >  np.random.randn(3,2)
array([[ True,  True],
       [ True,  True],
       [ True,  True]], dtype=bool)
>>> 
Aaron Paul
  • 117
  • 3
  • 14
1

Due to operator overloading, the > operator can do practically anything -- it simply invokes the __gt__ special method on the object. But absent any other information, I would expect it to simply evaluate "greater than" and return a bool value.

Daniel Pryden
  • 59,486
  • 16
  • 97
  • 135