Reputation: 129
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?
Upvotes: 2
Views: 835
Reputation: 117
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)
>>>
Upvotes: 2
Reputation: 96181
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)
>>>
Upvotes: 7
Reputation: 3130
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.
Upvotes: 3
Reputation: 60997
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.
Upvotes: 1