In [98]:
import numpy as np
import theano
from theano import tensor as T
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) # we assume that ``W_values`` contains the
# initial values of your weight matrix
bvis = theano.shared(bvis_values)
bhid = theano.shared(bhid_values)
trng = T.shared_randomstreams.RandomStreams(1234)
def OneStep(vsample, W, bhid, bvis) :
hmean = T.nnet.sigmoid(theano.dot(vsample, W) + bhid)
hsample = trng.binomial(size=hmean.shape, n=1, p=hmean)
vmean = T.nnet.sigmoid(theano.dot(hsample, W.T) + bvis)
return trng.binomial(size=vsample.shape, n=1, p=vmean,
dtype=theano.config.floatX)
sample = T.matrix()
values, updates = theano.scan(OneStep, sequences=sample, non_sequences = [W, bhid, bvis], n_steps=sample.shape[0])
gibbs10 = theano.function([sample], values, updates=updates)
In [99]:
tmp = np.array([[10,10],[-10,-10]], dtype = theano.config.floatX)
In [100]:
gibbs10(tmp)
Out[100]: