Esempio n. 1
0
File: dbn.py Progetto: ysmiraak/lgm
 def __init__(self, dim, samples
              , init_w= tf.random_uniform_initializer(minval= -0.01, maxval= 0.01)
              , ftype= tf.float32, scope= 'dbn'):
     self.dim, self.ftype = dim, ftype
     with tf.variable_scope(scope):
         self.rbm = tuple(
             Rbm(scope= "rbm{}".format(i)
                 , dim_v= dim_v
                 , dim_h= dim_h
                 , samples= samples
                 , init_w= init_w
                 , ftype= self.ftype)
             for i, (dim_v, dim_h) in enumerate(zip(dim, dim[1:]), 1))
         self.w = tuple(rbm.w for rbm in self.rbm[::-1])
         self.wg = tuple(tf.transpose(w) for w in self.w)
         self.wr = tuple(
             tf.get_variable(name= "wr{}".format(i), shape= (dim_d, dim_a), initializer= init_w)
             for i, (dim_d, dim_a) in enumerate(zip(self.dim, self.dim[1:]), 1))
         self.lr_ = tf.placeholder(name= 'lr_', dtype= self.ftype, shape= ())
         # wake
         self.v_ = self.rbm[0].v_
         with tf.name_scope('wake'):
             recogn = [self.v_]
             for w in self.wr: recogn.append(binary(tf.matmul(recogn[-1], w)))
             self.recogn = tuple(recogn)
             recogn = recogn[::-1]
             eps = self.lr_ / tf.cast(tf.shape(self.v_)[0], dtype= self.ftype)
             self.wake = tuple(
                 w.assign_add(tf.matmul((sj - pj), sk, transpose_a= True) * eps).op
                 for w, sk, sj, pj in zip(
                         self.w, recogn, recogn[1:]
                         , (tf.sigmoid(tf.matmul(s, w))
                            for w, s in zip(self.wg, recogn))))
         # sleep
         top = self.rbm[-1]
         self.k_, (self.v, self.a) = top.k_, top.gibbs
         with tf.name_scope('sleep'):
             recons = [self.a, self.v]
             for w in self.wg[1::]: recons.append(binary(tf.matmul(recons[-1], w)))
             self.recons = tuple(recons)
             recons = recons[::-1]
             eps = self.lr_ / tf.cast(tf.shape(self.a)[0], dtype= self.ftype)
             self.sleep = tuple(
                 w.assign_add(tf.matmul(sj, (sk - qk), transpose_a= True) * eps).op
                 for w, sj, sk, qk in zip(
                         self.wr, recons, recons[1:]
                         , (tf.sigmoid(tf.matmul(s, w))
                            for w, s in zip(self.wr, recons))))
         # the waking world is the amnesia of dream.
         self.v = self.recons[-1]
         with tf.name_scope('ances'):
             self.a = self.rbm[-1].h
             ances = [self.a]
             for w in self.wg: ances.append(binary(tf.matmul(ances[-1], w)))
             self.ances = ances[-1]
         self.step = 0
def get_mask_zero_embedded(var_em, var_ph):
    mask = tf.equal(var_ph, 0)
    mask2 = tf.concat(
        [tf.expand_dims(~mask, -1) for i in range(EMBEDDING_DIM)], -1)

    rst = tf.where(
        mask2, tf.nn.embedding_lookup(var_em, var_ph),
        tf.zeros([tf.shape(var_ph)[0],
                  tf.shape(var_ph)[1], EMBEDDING_DIM]))
    return rst
Esempio n. 3
0
File: rbm.py Progetto: ysmiraak/lgm
    def __init__(self, dim_v, dim_h, samples
                 , init_w= tf.random_uniform_initializer(minval= -0.01, maxval= 0.01)
                 , ftype= tf.float32
                 , scope= 'rbm'):
        self.dim_v, self.dim_h, self.ftype, self.scope = dim_v, dim_h, ftype, scope
        with tf.variable_scope(scope):
            # todo add bias
            self.w = tf.get_variable(name= 'w', shape= (self.dim_v, self.dim_h), initializer= init_w)
            # positive stage: inference
            self.v_ = tf.placeholder(name= 'v_', dtype= self.ftype, shape= (None, self.dim_v))
            with tf.name_scope('hgv'):
                self.hgv = tf.sigmoid(tf.matmul(self.v_, self.w))
            # self.act_h = binary(self.hgv, transform= False, threshold= None)
            # self.h_ = tf.placeholder(name= 'h_', dtype= self.ftype, shape= (None, self.dim_h))
            # self.vgh = tf.matmul(self.h_, self.w, transpose_b= True)
            # self.act_v = binary(self.vgh, transform= False, threshold= None)

            with tf.name_scope('pos'):
                self.pos = tf.matmul(self.v_, self.hgv, transpose_a= True)
                self.pos /= tf.cast(tf.shape(self.v_)[0], dtype= self.ftype)
            # negative stage: stochastic approximation
            self.v = binary_variable(name= 'v', shape= (samples, self.dim_v), dtype= self.ftype)
            self.h = binary_variable(name= 'h', shape= (samples, self.dim_h), dtype= self.ftype)
            self.k_ = tf.placeholder(name= 'k_', dtype= tf.int32, shape= ())

            def gibbs(v, _h):
                h = binary(tf.matmul(v, self.w))
                v = binary(tf.matmul(h, self.w, transpose_b= True))
                # todo real valued v
                # v = tf.sigmoid(tf.matmul(h, self.w, transpose_b= True))
                return v, h

            with tf.name_scope('gibbs'):
                vh = self.v, self.h
                v, h = self.gibbs = tuple(
                    tf.assign(x, x2, validate_shape= False) for x, x2 in zip(
                        vh, tf.while_loop(
                            loop_vars= (self.k_, vh)
                            , cond= lambda k, vh: (0 < k)
                            , body= lambda k, vh: (k - 1, gibbs(*vh)))[1]))

            with tf.name_scope('neg'):
                # todo update with real probabilities instead of binaries
                h = tf.sigmoid(tf.matmul(v, self.w))
                v = tf.sigmoid(tf.matmul(h, self.w, transpose_b= True))
                self.neg = tf.matmul(v, h, transpose_a= True)
                self.neg /= tf.cast(tf.shape(self.v)[0], dtype= self.ftype)
            self.lr_ = tf.placeholder(name= 'lr_', dtype= self.ftype, shape= ())
            with tf.name_scope('up'):
                self.up = self.w.assign_add((self.pos - self.neg) * self.lr_).op
            self.step = 0
Esempio n. 4
0
File: sbn.py Progetto: ysmiraak/lgm
 def __init__(self, dim, samples
              , init_w= tf.random_uniform_initializer(minval= -0.01, maxval= 0.01)
              , ftype= tf.float32, scope= 'sbn'):
     self.dim, self.ftype, self.scope = dim, ftype, scope
     with tf.variable_scope(scope):
         self.wr = tuple(
             tf.get_variable(name= "wr{}".format(i), shape= (dim_d, dim_a), initializer= init_w)
             for i, (dim_d, dim_a) in enumerate(zip(self.dim, self.dim[1:]), 1))
         self.wg = tuple(
             tf.get_variable(name= "wg{}".format(i), shape= (dim_a, dim_d), initializer= init_w)
             for i, (dim_d, dim_a) in enumerate(zip(self.dim, self.dim[1:]), 1))[::-1]
         self.lr_ = tf.placeholder(name= 'lr_', dtype= self.ftype, shape= ())
         # wake
         self.v_ = tf.placeholder(name= 'v_', dtype= self.ftype, shape= (None, self.dim[0]))
         with tf.name_scope('wake'):
             recogn = [self.v_]
             for w in self.wr: recogn.append(binary(tf.matmul(recogn[-1], w)))
             self.recogn = tuple(recogn)
             recogn = recogn[::-1]
             eps = self.lr_ / tf.cast(tf.shape(self.v_)[0], dtype= self.ftype)
             self.wake = tuple(
                 w.assign_add(tf.matmul(sk, (sj - pj), transpose_a= True) * eps).op
                 for w, sk, sj, pj in zip(
                         self.wg, recogn, recogn[1:]
                         , (tf.sigmoid(tf.matmul(s, w))
                            for w, s in zip(self.wg, recogn))))
         # sleep
         with tf.name_scope('a'):
             self.a = tf.round(tf.random_uniform(shape= (samples, self.dim[-1])))
         with tf.name_scope('sleep'):
             recons = [self.a]
             for w in self.wg: recons.append(binary(tf.matmul(recons[-1], w)))
             self.recons = tuple(recons)
             recons = recons[::-1]
             eps = self.lr_ / tf.cast(tf.shape(self.a)[0], dtype= self.ftype)
             self.sleep = tuple(
                 w.assign_add(tf.matmul(sj, (sk - qk), transpose_a= True) * eps).op
                 for w, sj, sk, qk in zip(
                         self.wr, recons, recons[1:]
                         , (tf.sigmoid(tf.matmul(s, w))
                            for w, s in zip(self.wr, recons))))
         # the waking world is the amnesia of dream.
         self.v = self.recons[-1]
         self.step = 0
Esempio n. 5
0
    def __init__(self, dim, samples
                 , init_w= tf.random_uniform_initializer(minval= -0.01, maxval= 0.01)
                 , ftype= tf.float32, scope= 'dbm'):
        self.dim, self.ftype = dim, ftype
        # todo pretraining
        with tf.variable_scope(scope):
            self.rbm = tuple(
                Rbm(scope= "rbm{}".format(i)
                    , dim_v= dim_v
                    , dim_h= dim_h
                    , samples= samples
                    , init_w= init_w
                    , ftype= self.ftype)
                for i, (dim_v, dim_h) in enumerate(zip(dim, dim[1:]), 1))
            self.w = tuple(rbm.w for rbm in self.rbm)
            # positive stage: variational inference
            self.m = tuple(rbm.h for rbm in self.rbm)
            self.v_ = self.rbm[0].v_
            self.k_meanf_ = tf.placeholder(name= 'k_meanf_', dtype= tf.int32, shape= ())

            def meanf(m):
                mf, ml = [], self.v_
                for wl, wr, mr in zip(self.w, self.w[1:], m[1:]):
                    mf.append(tf.sigmoid(tf.matmul(ml, wl) + tf.matmul(mr, wr, transpose_b= True)))
                    ml = mf[-1]
                mf.append(tf.sigmoid(tf.matmul(ml, wr)))
                return tuple(mf)

            with tf.name_scope('meanf'):
                self.meanf = tuple(
                    tf.assign(m, mf, validate_shape= False) for m, mf in zip(
                        self.m, tf.while_loop(
                            loop_vars= (self.k_meanf_, self.m)
                            , cond= lambda k, _: (0 < k)
                            , body= lambda k, m: (k - 1, meanf(m)))[1]))

            with tf.name_scope('pos'):
                bs = tf.cast(tf.shape(self.v_)[0], dtype= self.ftype)
                vm = (self.v_,) + self.meanf
                self.pos = tuple((tf.matmul(ml, mr, transpose_a= True) / bs) for ml, mr in zip(vm, vm[1:]))
            # negative stage: stochastic approximation
            self.x = tuple(rbm.v for rbm in self.rbm)
            self.x += (binary_variable(name= 'x', shape= (samples, self.dim[-1]), dtype= self.ftype),)
            self.v = self.x[0]
            self.k_gibbs_ = tf.placeholder(name= 'k_gibbs_', dtype= tf.int32, shape= ())

            def gibbs(x):
                x = list(x)
                # update odd layers
                for i, (xl, xr, wl, wr) in enumerate(zip(x[::2], x[2::2], self.w, self.w[1:])):
                    x[1+(2*i)] = binary(tf.matmul(xl, wl) + tf.matmul(xr, wr, transpose_b= True))
                # update first layer
                x[0] = binary(tf.matmul(x[1], self.w[0], transpose_b= True))
                # update even layers
                for i, (xl, xr, wl, wr) in enumerate(zip(x[1::2], x[3::2], self.w[1:], self.w[2:])):
                    x[2+(2*i)] = binary(tf.matmul(xl, wl) + tf.matmul(xr, wr, transpose_b= True))
                # update last layer
                x[-1] = binary(tf.matmul(x[-2], self.w[-1]))
                return tuple(x)

            with tf.name_scope('gibbs'):
                x = self.gibbs = tuple(
                    tf.assign(x, xg, validate_shape= False) for x, xg in zip(
                        self.x, tf.while_loop(
                            loop_vars= (self.k_gibbs_, self.x)
                            , cond= lambda k, x: (0 < k)
                            , body= lambda k, x: (k - 1, gibbs(x)))[1]))

            with tf.name_scope('neg'):
                bs = tf.cast(tf.shape(self.v)[0], dtype= self.ftype)
                self.neg = tuple((tf.matmul(xl, xr, transpose_a= True) / bs) for xl, xr in zip(x, x[1:]))
            # parameter update
            self.lr_ = tf.placeholder(name= 'lr_', dtype= self.ftype, shape= ())
            with tf.name_scope('up'):
                self.up = tuple(
                    w.assign_add((pos - neg) * self.lr_).op
                    for w, pos, neg in zip(self.w, self.pos, self.neg))
            self.step = 0
Esempio n. 6
0
 def __init__(self, dat, dim_rec, dim_z, dim_gen, scope='vae'):
     assert 2 == dat.ndim
     assert isinstance(dim_rec, tuple)
     assert isinstance(dim_z, int)
     assert isinstance(dim_gen, tuple)
     init_w = tf.variance_scaling_initializer(scale=2.0,
                                              mode='fan_in',
                                              distribution='uniform')
     init_b = tf.constant_initializer(0.01)
     init_z = tf.zeros_initializer()
     with tf.variable_scope(scope):
         dat = self.dat = tf.constant(name='dat', value=dat)
         bs_ = self.bs_ = tf.placeholder(name='bs_',
                                         dtype=tf.int32,
                                         shape=())
         bat = self.bat = tf.random_uniform(name='bat',
                                            shape=(bs_, ),
                                            minval=0,
                                            maxval=dat.shape[0],
                                            dtype=tf.int32)
         h = x = self.x = tf.nn.embedding_lookup(name='x',
                                                 params=dat,
                                                 ids=bat)
         for i, dim in enumerate(dim_rec, 1):
             name = "hr{}".format(i)
             h = tf.layers.dense(name=name,
                                 inputs=h,
                                 units=dim,
                                 activation=tf.nn.relu,
                                 kernel_initializer=init_w,
                                 bias_initializer=init_b)
             setattr(self, name, h)
         mu = self.mu = tf.layers.dense(name='mu',
                                        inputs=h,
                                        units=dim_z,
                                        kernel_initializer=init_w,
                                        bias_initializer=init_z)
         lv = self.lv = tf.layers.dense(name='lv',
                                        inputs=h,
                                        units=dim_z,
                                        kernel_initializer=init_w,
                                        bias_initializer=init_z)
         with tf.name_scope('z'):
             h = z = self.z = mu + tf.exp(
                 0.5 * lv) * tf.random_normal(shape=tf.shape(lv))
         for i, dim in enumerate(dim_gen, 1):
             name = "hg{}".format(i)
             h = tf.layers.dense(name=name,
                                 inputs=h,
                                 units=dim,
                                 activation=tf.nn.relu,
                                 kernel_initializer=init_w,
                                 bias_initializer=init_b)
             setattr(self, name, h)
         logits = tf.layers.dense(
             name='logits',
             inputs=h,
             units=dat.shape[1]
             # , activation= tf.nn.sigmoid
             ,
             kernel_initializer=init_w,
             bias_initializer=init_z)
         g = self.g = tf.sigmoid(logits)
         with tf.name_scope('loss_recons'):
             # loss_recons = self.loss_recons = tf.reduce_mean(
             #     tf.reduce_sum(tf.nn.sigmoid_cross_entropy_with_logits(labels= x, logits= logits), axis= 1))
             loss_recons = self.loss_recons = tf.reduce_mean(
                 tf.reduce_sum(tf.square(x - g), axis=1))
         with tf.name_scope('loss_relent'):
             # loss_relent = self.loss_relent = tf.reduce_mean(
             #     0.5 * tf.reduce_sum((- 1.0 - lv + tf.exp(lv) + tf.square(mu)), axis= 1))
             loss_relent = self.loss_relent = tf.reduce_mean(
                 tf.reduce_sum((-1.0 - lv + tf.exp(lv) + tf.square(mu)),
                               axis=1))
         with tf.name_scope('loss'):
             loss = self.loss = loss_relent + loss_recons
         up = self.up = tf.train.AdamOptimizer().minimize(loss)
         self.step = 0