def build_fcn_net(self, inp, use_dice=False):
        with self.graph.as_default():
            self.saver = tf.train.Saver(max_to_keep=1)


            with tf.name_scope("Out"):
                bn1 = tf.layers.batch_normalization(inputs=inp, name='bn1')
                dnn1 = tf.layers.dense(bn1, 200, activation=None, name='f1')
                if use_dice:
                    dnn1 = dice(dnn1, name='dice_1')
                else:
                    dnn1 = prelu(dnn1, 'prelu1')

                dnn2 = tf.layers.dense(dnn1, 80, activation=None, name='f2')
                if use_dice:
                    dnn2 = dice(dnn2, name='dice_2')
                else:
                    dnn2 = prelu(dnn2, 'prelu2')
                dnn3 = tf.layers.dense(dnn2, 2, activation=None, name='f3')
                self.y_hat = tf.nn.softmax(dnn3) + 0.00000001

            with tf.name_scope('Metrics'):
                # Cross-entropy loss and optimizer initialization
                # 'core_type_ph': [1, 1, 0,..],

                ctr_loss = - tf.reduce_mean(tf.log(self.y_hat) * self.target_ph)
                self.loss = ctr_loss
                # tf.summary.scalar('loss', self.loss)
                self.optimizer = tf.train.AdamOptimizer(learning_rate=self.lr_ph).minimize(self.loss)
                # self.optimizer = tf.train.GradientDescentOptimizer(learning_rate=self.lr_ph).minimize(self.loss)
                # Accuracy metric
                self.accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.round(self.y_hat), self.target_ph), tf.float32))
                # tf.summary.scalar('accuracy', self.accuracy)

            self.merged = tf.summary.merge_all()
Exemplo n.º 2
0
Arquivo: sbn.py Projeto: 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