Hi,

I've been trying to modify deeplearning.net's tutorial on RBM to create a 
RSM layer but i am struggling with theano's scan function to simulate 
multinomial random variables.

I find theano's scan function difficult to master. Was hoping that someone 
can help identify where I went wrong with the self-contained gibbs sampling 
code i've modified below (from: http://deeplearning.net/tutorial/rbm.html). 

The error i'm currently facing is at the scan function which claims that 
i'm trying to fit 4 inputs to a 2 input function which does not appear to 
be the case to me. Obviously i'm wrong. Hoping someone can help!

The error returned is: as follows:

---------------------------------------------------------------------------TypeError
                                 Traceback (most recent call 
last)<ipython-input-299-0e41a0430dfb> in <module>()      9                      
           sequences = [ipt,ipt_rSum],     10                                 
n_steps=1,---> 11                                 name="gibbs_hvh" 
)/home/ekhongl/.conda/envs/py3/lib/python3.5/site-packages/theano/scan_module/scan.py
 in scan(fn, sequences, outputs_info, non_sequences, n_steps, 
truncate_gradient, go_backwards, mode, name, profile, allow_gc, strict)    743  
   # and outputs that needs to be separated    744 --> 745     condition, 
outputs, updates = scan_utils.get_updates_and_outputs(fn(*args))    746     if 
condition is not None:    747         as_while = True
TypeError: gibbs_hvh() takes 2 positional arguments but 3 were given









#----------------------------------------------------------------------------------------------------------------
#---------------------------------------------------CODE----------------------------------------------------
#----------------------------------------------------------------------------------------------------------------
import numpy as np
import theano
from theano import tensor as T
theano_rng = T.shared_randomstreams.RandomStreams(1234)


# ----------- parameters construction ----------------- 
W_values = np.array([[1,1],[1,1]], dtype=theano.config.floatX)
bvis_values = np.array([[0],[0]], dtype=theano.config.floatX)
bhid_values = np.array([[0],[0]], dtype=theano.config.floatX)
W = theano.shared(W_values)
vbias = theano.shared(bvis_values)
hbias = theano.shared(bhid_values)



#-------------functions used ----------------- 
def propup(vis, v0_doc_len):
        pre_sigmoid_activation = T.dot(vis, W) + hbias*v0_doc_len
        return [pre_sigmoid_activation, 
T.nnet.sigmoid(pre_sigmoid_activation)]

def sample_h_given_v(v0_sample, v0_doc_len):
    pre_sigmoid_h1, h1_mean = propup(v0_sample, v0_doc_len)
    h1_sample = theano_rng.binomial(size=h1_mean.shape[1:],
                                         n=1, 
p=h1_mean.reshape(h1_mean.shape[1:]).shape,
                                         dtype=theano.config.floatX)
    return [pre_sigmoid_h1, h1_mean, h1_sample]

def propdown(hid):
    pre_softmax_activation = T.dot(hid, W.T) + vbias
    return [pre_softmax_activation, T.nnet.softmax(pre_softmax_activation)]

def sample_v_given_h(h0_sample, v0_doc_len):
    pre_softmax_v1, v1_mean = propdown(h0_sample)
    v1_sample = theano_rng.multinomial(size=None,
                                         n=v0_doc_len, pvals=v1_mean,
                                         dtype=theano.config.floatX)
    return [pre_softmax_v1, v1_mean, v1_sample]

def gibbs_hvh(h0_sample, v0_doc_len):
    pre_softmax_v1, v1_mean, v1_sample = sample_v_given_h(h0_sample, 
v0_doc_len)
    pre_sigmoid_h1, h1_mean, h1_sample = sample_h_given_v(v1_sample, 
v0_doc_len)
    return [pre_softmax_v1, v1_mean, v1_sample,
            pre_sigmoid_h1, h1_mean, h1_sample] 

def gibbs_vhv(v0_sample, v0_doc_len):
    pre_sigmoid_h1, h1_mean, h1_sample = sample_h_given_v(v0_sample, 
v0_doc_len)
    pre_softmax_v1, v1_mean, v1_sample = sample_v_given_h(h1_sample, 
v0_doc_len)
    return [pre_sigmoid_h1, h1_mean, h1_sample,
            softmax_v1, v1_mean, v1_sample]

#-------------symbolic input declaration-------------
ipt = T.matrix()
ipt_rSum = ipt.sum(axis=1)

#-------------initialize sample-------------
pre_sigmoid_ph, ph_mean, ph_sample = sample_h_given_v(ipt, ipt_rSum)
chain_start = ph_sample

#-------------theano scan ------------- #                                 
<<<----------------------------------------------------------[ point where 
error occured :( ]
([pre_sigmoid_nvs,
  nv_means,
  nv_samples,
  pre_sigmoid_nhs,
  nh_means,
  nh_samples],   updates) = theano.scan(
                                gibbs_hvh,
                                outputs_info=[None, None, None, None, None, 
chain_start],
                                sequences = [ipt,ipt_rSum],
                                n_steps=1,
                                name="gibbs_hvh" )

#-------------theano function-------------
gibbs = theano.function( [ipt], outputs=[pre_sigmoid_nvs,
                                          nv_means,
                                          nv_samples,
                                          pre_sigmoid_nhs,
                                          nh_means,
                                          nh_samples], updates=updates)

#-------------running the function------------
b = np.array([[2,0,],[0,2],[1,1]], dtype = theano.config.floatX)
[pre_sigmoid_h1, h1_mean, h1_sample, softmax_v1, v1_mean, v1_sample]= 
gibbs(b)

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"theano-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to