Ejemplo n.º 1
0
    def _prediction_layer(self, inputs, task_idx, no_samples):
        K = no_samples
        act = tf.tile(tf.expand_dims(inputs, 0), [K, 1, 1])
        weight_share = self.weights[0]
        weight_head = self.weights[1]
        for i in range(self.no_layers - 1):
            din = self.size[i]
            dout = self.size[i + 1]
            #get w
            list_mean_w = []
            list_variance_w = []
            list_coff = []
            for mixture in range(self.gauss_mixture):
                list_mean_w.append(weight_share[mixture][0][0][i])
                list_variance_w.append(
                    tf.exp(
                        self.compute_weightfromfactorize_variance(
                            weight_share[mixture][1][0][i])))
                list_coff.append(weight_share[mixture][2][0][i])
            weights = sample_gumbel_trick.sample_from_gumbel_softmax_trick(
                list_mean_w, list_variance_w, list_coff, self.tau,
                self.gauss_mixture, K, din, dout, False)
            list_mean_w = []
            list_variance_w = []
            list_coff = []
            for mixture in range(self.gauss_mixture):
                list_mean_w.append(weight_share[mixture][0][1][i])
                list_variance_w.append(tf.exp(weight_share[mixture][1][1][i]))
                list_coff.append(weight_share[mixture][2][1][i])
            biases = sample_gumbel_trick.sample_from_gumbel_softmax_trick(
                list_mean_w, list_variance_w, list_coff, self.tau,
                self.gauss_mixture, K, din, dout, True)
            #feed vao mang
            pre = tf.add(tf.einsum('mni,mio->mno', act, weights), biases)
            act = tf.nn.relu(pre)
        #tai layer cuoi cung
        din = self.size[-2]
        dout = self.size[-1]

        eps_w = tf.random_normal((K, din, dout), 0, 1, dtype=tf.float32)
        eps_b = tf.random_normal((K, 1, dout), 0, 1, dtype=tf.float32)

        Wtask_m = tf.gather(weight_head[0][0], task_idx)
        Wtask_v = tf.gather(self.Wlast_v, task_idx)
        btask_m = tf.gather(weight_head[0][1], task_idx)
        btask_v = tf.gather(weight_head[1][0], task_idx)
        weights = tf.add(tf.multiply(eps_w, tf.exp(0.5 * Wtask_v)), Wtask_m)
        biases = tf.add(tf.multiply(eps_b, tf.exp(0.5 * btask_v)), btask_m)

        act = tf.expand_dims(act, 3)
        weights = tf.expand_dims(weights, 1)
        pre = tf.add(tf.reduce_sum(act * weights, 2), biases)
        return pre
 def _prediction_layer(self, inputs, task_idx, no_samples):
     K = no_samples
     act = tf.tile(tf.expand_dims(inputs, 0), [K, 1, 1])
     for i in range(self.no_layers - 1):
         din = self.size[i]
         dout = self.size[i + 1]
         #get w
         list_mean_w = []
         list_variance_w = []
         list_coff = []
         for mixture in range(self.gauss_mixture):
             list_mean_w.append(self.weights[mixture][0][0][i])
             list_variance_w.append(tf.exp(self.weights[mixture][1][0][i]))
             list_coff.append(self.weights[mixture][2][0][i])
         weights = sample_gumbel_trick.sample_from_gumbel_softmax_trick(
             list_mean_w, list_variance_w, list_coff, self.tau,
             self.gauss_mixture, K, din, dout, False)
         list_mean_w = []
         list_variance_w = []
         list_coff = []
         for mixture in range(self.gauss_mixture):
             list_mean_w.append(self.weights[mixture][0][1][i])
             list_variance_w.append(tf.exp(self.weights[mixture][1][1][i]))
             list_coff.append(self.weights[mixture][2][1][i])
         biases = sample_gumbel_trick.sample_from_gumbel_softmax_trick(
             list_mean_w, list_variance_w, list_coff, self.tau,
             self.gauss_mixture, K, din, dout, True)
         #feed vao mang
         pre = tf.add(tf.einsum('mni,mio->mno', act, weights), biases)
         act = tf.nn.relu(pre)
     #tai layer cuoi cung
     din = self.size[-2]
     dout = self.size[-1]
     Wtask_m , Wtask_v , btask_m , btask_v , coff_m , coff_b  = [] , [] , [] , [] , [] , []
     for mixture in range(self.gauss_mixture):
         Wtask_m.append(self.weights[mixture][0][2][0])
         Wtask_v.append(tf.exp(self.weights[mixture][1][2][0]))
         btask_m.append(self.weights[mixture][0][3][0])
         btask_v.append(tf.exp(self.weights[mixture][1][3][0]))
         coff_m.append(self.weights[mixture][2][2][0])
         coff_b.append(self.weights[mixture][2][3][0])
     weights = sample_gumbel_trick.sample_from_gumbel_softmax_trick(
         Wtask_m, Wtask_v, coff_m, self.tau, self.gauss_mixture, K, din,
         dout, False)
     biases = sample_gumbel_trick.sample_from_gumbel_softmax_trick(
         btask_m, btask_v, coff_b, self.tau, self.gauss_mixture, K, din,
         dout, True)
     act = tf.expand_dims(act, 3)
     weights = tf.expand_dims(weights, 1)
     pre = tf.add(tf.reduce_sum(act * weights, 2), biases)
     return pre