xRBM icon indicating copy to clipboard operation
xRBM copied to clipboard

Dimension Error for Binary Visible Data in CRBM

Open tianc2014 opened this issue 8 years ago • 1 comments
trafficstars

Hi Omid,

I followed your Tutorial 3: Training a Conditional RBM on Timeseries Data and was trying to replace the vis_type to 'binary' in:

crbm = xrbm.models.CRBM(num_vis=num_vis,
                        num_cond=num_cond,
                        num_hid=num_hid,
                        vis_type='gaussian',
                        initializer=tf.contrib.layers.xavier_initializer(),
                        name='crbm')

Then I got a dimension error:

ValueError                                Traceback (most recent call last)
<ipython-input-7-205d82de8a49> in <module>()
     25                                            momentum=momentum,
     26                                            k=1)
---> 27 train_op           = cdapproximator.train(crbm, vis_data=batch_vis_data, in_data=[batch_cond_data])

~/anaconda3/lib/python3.6/site-packages/xrbm/train/cdk.py in train(self, model, vis_data, in_data, global_step, var_list, name)
     74 
     75         # Get the model's cost function for the training data and the reconstructed data (chain_end)
---> 76         cost = model.get_cost(vis_data, chain_end, in_data)
     77 
     78         # We a regularizer is set, then add the regularization terms to the cost function

~/anaconda3/lib/python3.6/site-packages/xrbm/models/crbm.py in get_cost(self, v_sample, chain_end, in_data)
    298 
    299         with tf.variable_scope('fe_cost'):
--> 300             cost = tf.reduce_mean(self.free_energy(v_sample, cond)
    301                     - self.free_energy(chain_end, cond), reduction_indices=0)
    302         return cost

~/anaconda3/lib/python3.6/site-packages/xrbm/models/crbm.py in free_energy(self, v_sample, cond)
    327 
    328             if self.vis_type == 'binary':
--> 329                 v = - tf.matmul(v_sample, tf.expand_dims(vbias_n_cond,1), name='bin_visible_term')
    330             elif self.vis_type == 'gaussian':
    331                 v = tf.reduce_sum(0.5 * tf.square(v_sample - vbias_n_cond), reduction_indices=1, name='gauss_visible_term')
...
...
ValueError: Shape must be rank 2 but is rank 3 for 'fe_cost/free_energy/bin_visible_term' (op: 'MatMul') with input shapes: [?,4], [?,1,4].

If we check xrbm.models.crbm.free_energy (shown below), we have cond.shape to be [None, num_cond], which makes the shape of vbias_n_cond = self.vbias + tf.matmul(cond, self.A) to be [None, num_vis].

When we calculate v = - tf.matmul(v_sample, tf.expand_dims(vbias_n_cond,1), name='bin_visible_term'), v_sample.shape is [None, num_vis] and the shape of tf.expand_dims(vbias_n_cond,1) is [None, 1, num_vis], which causes the ValueError: Shape must be rank 2 but is rank 3 for 'fe_cost/free_energy/bin_visible_term' (op: 'MatMul') with input shapes: [?,4], [?,1,4].

Is it because for binary visible data, I should not define the shape of batch_cond_data = tf.placeholder(tf.float32, shape=(None, num_cond), name='cond_data')? Or any other possible reasons?

def free_energy(self, v_sample, cond):
        """
        Calcuates the free-energy of a given visible tensor

        Parameters
        ----------
        v_sample:   tensor
            the visible units tensor
        cond:       tensor
            the condition units tensor

        Returns
        -------
        e:  float
            the free energy
        """
        with tf.variable_scope('free_energy'):
            bottom_up = (tf.matmul(v_sample, self.W) + # visible to hidden 
                         tf.matmul(cond, self.B) + # condition to hidden
                         self.hbias) # static hidden biases
            
            vbias_n_cond = self.vbias + tf.matmul(cond, self.A)

            if self.vis_type == 'binary':
                v = - tf.matmul(v_sample, tf.expand_dims(vbias_n_cond,1), name='bin_visible_term')
            elif self.vis_type == 'gaussian':
                v = tf.reduce_sum(0.5 * tf.square(v_sample - vbias_n_cond), reduction_indices=1, name='gauss_visible_term')

Thanks, Tian

tianc2014 avatar Nov 11 '17 00:11 tianc2014

Hi Tian,

Thanks for pointing this out. It looks like it was a bug. I've updated code in 191a2f00c75b517682f4ac1be9e38812eaa36063. Can you please check it out and confirm that it solves your issue?

Cheers, Omid

omimo avatar Nov 19 '17 07:11 omimo