def get_hidden_values(self, input): """ Computes the values of the hidden layer """ return af(T.dot(input, self.W) + self.b)
def get_reconstructed_input(self, hidden): """Computes the reconstructed input given the values of the hidden layer """ return af(T.dot(hidden, self.W_prime) + self.b_prime)
def __init__(self, numpy_rng, theano_rng=None, input1=None, input2=None, cor_reg=None, n_visible1=784 / 2, n_visible2=784 / 2, n_hidden=500, W1=None, bhid1=None, bvis1=None, W2=None, bhid2=None, bvis2=None): self.n_visible1 = n_visible1 self.n_visible2 = n_visible2 self.n_hidden = n_hidden # create a Theano random generator that gives symbolic random values if not theano_rng: theano_rng = RandomStreams(numpy_rng.randint(2**30)) # note : W' was written as `W_prime` and b' as `b_prime` if not W1: # W is initialized with `initial_W` which is uniformely sampled # from -4*sqrt(6./(n_visible+n_hidden)) and # 4*sqrt(6./(n_hidden+n_visible))the output of uniform if # converted using asarray to dtype # theano.config.floatX so that the code is runable on GPU initial_W1 = numpy.asarray(numpy_rng.uniform( low=-4 * numpy.sqrt(6. / (n_hidden + n_visible1)), high=4 * numpy.sqrt(6. / (n_hidden + n_visible1)), size=(n_visible1, n_hidden)), dtype=theano.config.floatX) W1 = theano.shared(value=initial_W1, name='W1', borrow=True) if not W2: # W is initialized with `initial_W` which is uniformely sampled # from -4*sqrt(6./(n_visible+n_hidden)) and # 4*sqrt(6./(n_hidden+n_visible))the output of uniform if # converted using asarray to dtype # theano.config.floatX so that the code is runable on GPU initial_W2 = numpy.asarray(numpy_rng.uniform( low=-4 * numpy.sqrt(6. / (n_hidden + n_visible2)), high=4 * numpy.sqrt(6. / (n_hidden + n_visible2)), size=(n_visible2, n_hidden)), dtype=theano.config.floatX) W2 = theano.shared(value=initial_W2, name='W2', borrow=True) if not bvis1: bvis1 = theano.shared(value=numpy.zeros( n_visible1, dtype=theano.config.floatX), name='b1p', borrow=True) if not bvis2: bvis2 = theano.shared(value=numpy.zeros( n_visible2, dtype=theano.config.floatX), name='b2p', borrow=True) if not bhid1: bhid1 = theano.shared(value=numpy.zeros( n_hidden, dtype=theano.config.floatX), name='b1', borrow=True) if not bhid2: bhid2 = theano.shared(value=numpy.zeros( n_hidden, dtype=theano.config.floatX), name='b2', borrow=True) self.W1 = W1 self.W2 = W2 # b corresponds to the bias of the hidden self.b1 = bhid1 self.b2 = bhid2 # b_prime corresponds to the bias of the visible self.b1_prime = bvis1 self.b2_prime = bvis2 # tied weights, therefore W_prime is W transpose self.W1_prime = self.W1.T self.W2_prime = self.W2.T self.theano_rng = theano_rng self.L1 = ( abs(self.W1).sum() + abs(self.W2).sum( ) #+abs(self.b1).sum()+abs(self.b2).sum()+abs(self.b1_prime).sum()+abs(self.b2_prime).sum() ) self.L2_sqr = ((self.W1**2).sum( ) #+(self.W2**2).sum()#+abs(self.b1**2).sum()+abs(self.b2**2).sum()+abs(self.b1_prime**2).sum()+abs(self.b2_prime**2).sum() ) # if no input is given, generate a variable representing the input if input1 is None: # we use a matrix because we expect a minibatch of several # examples, each example being a row self.x1 = T.dmatrix(name='input1') self.x2 = T.dmatrix(name='input2') else: self.x1 = input1 self.x2 = input2 self.params = [ self.W1, self.b1, self.b1_prime, self.W2, self.b2, self.b2_prime ] # end-snippet-1 self.output1 = af(T.dot(self.x1, self.W1) + self.b1) self.output2 = af(T.dot(self.x2, self.W2) + self.b2) self.rec1 = (T.dot(self.output1, self.W1_prime) + self.b1_prime) self.rec2 = (T.dot(self.output2, self.W2_prime) + self.b2_prime) self.reg = (T.dot(self.output1, self.W2_prime) + self.b2_prime) self.cor_reg = theano.shared(numpy.float32(1.0), name='reg')
def get_hidden_values(self, input1, input2): """ Computes the values of the hidden layer """ return af(T.dot(input1, self.W1) + self.b1), af(T.dot(input2, self.W2) + self.b2)
def __init__( self, numpy_rng, theano_rng=None, input1=None, input2=None, cor_reg=None, n_visible1=784/2, n_visible2=784/2, n_hidden=500, W1=None, bhid1=None, bvis1=None, W2=None, bhid2=None, bvis2=None ): self.n_visible1 = n_visible1 self.n_visible2 = n_visible2 self.n_hidden = n_hidden # create a Theano random generator that gives symbolic random values if not theano_rng: theano_rng = RandomStreams(numpy_rng.randint(2 ** 30)) # note : W' was written as `W_prime` and b' as `b_prime` if not W1: # W is initialized with `initial_W` which is uniformely sampled # from -4*sqrt(6./(n_visible+n_hidden)) and # 4*sqrt(6./(n_hidden+n_visible))the output of uniform if # converted using asarray to dtype # theano.config.floatX so that the code is runable on GPU initial_W1 = numpy.asarray( numpy_rng.uniform( low=-4 * numpy.sqrt(6. / (n_hidden + n_visible1)), high=4 * numpy.sqrt(6. / (n_hidden + n_visible1)), size=(n_visible1, n_hidden) ), dtype=theano.config.floatX ) W1 = theano.shared(value=initial_W1, name='W1', borrow=True) if not W2: # W is initialized with `initial_W` which is uniformely sampled # from -4*sqrt(6./(n_visible+n_hidden)) and # 4*sqrt(6./(n_hidden+n_visible))the output of uniform if # converted using asarray to dtype # theano.config.floatX so that the code is runable on GPU initial_W2 = numpy.asarray( numpy_rng.uniform( low=-4 * numpy.sqrt(6. / (n_hidden + n_visible2)), high=4 * numpy.sqrt(6. / (n_hidden + n_visible2)), size=(n_visible2, n_hidden) ), dtype=theano.config.floatX ) W2 = theano.shared(value=initial_W2, name='W2', borrow=True) if not bvis1: bvis1 = theano.shared( value=numpy.zeros( n_visible1, dtype=theano.config.floatX ), name='b1p', borrow=True ) if not bvis2: bvis2 = theano.shared( value=numpy.zeros( n_visible2, dtype=theano.config.floatX ), name='b2p', borrow=True ) if not bhid1: bhid1 = theano.shared( value=numpy.zeros( n_hidden, dtype=theano.config.floatX ), name='b1', borrow=True ) if not bhid2: bhid2 = theano.shared( value=numpy.zeros( n_hidden, dtype=theano.config.floatX ), name='b2', borrow=True ) self.W1 = W1 self.W2 = W2 # b corresponds to the bias of the hidden self.b1 = bhid1 self.b2 = bhid2 # b_prime corresponds to the bias of the visible self.b1_prime = bvis1 self.b2_prime = bvis2 # tied weights, therefore W_prime is W transpose self.W1_prime = self.W1.T self.W2_prime = self.W2.T self.theano_rng = theano_rng self.L1 = ( abs(self.W1).sum()+abs(self.W2).sum()#+abs(self.b1).sum()+abs(self.b2).sum()+abs(self.b1_prime).sum()+abs(self.b2_prime).sum() ) self.L2_sqr = ( (self.W1**2).sum()#+(self.W2**2).sum()#+abs(self.b1**2).sum()+abs(self.b2**2).sum()+abs(self.b1_prime**2).sum()+abs(self.b2_prime**2).sum() ) # if no input is given, generate a variable representing the input if input1 is None: # we use a matrix because we expect a minibatch of several # examples, each example being a row self.x1 = T.dmatrix(name='input1') self.x2 = T.dmatrix(name='input2') else: self.x1 = input1 self.x2 = input2 self.params = [self.W1, self.b1, self.b1_prime, self.W2, self.b2, self.b2_prime ] # end-snippet-1 self.output1 = af(T.dot(self.x1, self.W1) + self.b1) self.output2 = af(T.dot(self.x2, self.W2) + self.b2) self.rec1 = (T.dot(self.output1, self.W1_prime) + self.b1_prime) self.rec2 = (T.dot(self.output2, self.W2_prime) + self.b2_prime) self.reg = (T.dot(self.output1, self.W2_prime) + self.b2_prime) self.cor_reg = theano.shared(numpy.float32(1.0),name='reg')