コード例 #1
0
ファイル: mdn.py プロジェクト: uscresl/bayes_sim
    def predict_mog(self, x_test):
        tfd = tfp.distributions
        x_test = x_test.T
        ntest = x_test.shape[1]
        start_time = datetime.now()
        # Prepares the data for LSTMs

        x_test_fixed = np.zeros([self.nsteps, self.inputd, ntest])
        for i in range(ntest):
            x_test_fixed[:, :,
                         i] = x_test[:, i].reshape([self.nsteps, self.inputd])
        x_test_fixed = np.swapaxes(x_test_fixed, 0, 1)
        x_test_fixed = np.swapaxes(x_test_fixed, 0, 2)
        y_pred = self.model.predict(x_test_fixed)
        end_time = datetime.now()
        print('\n')
        print(
            "*********************************  Prediction ends  *********************************"
        )
        print('\n')
        print('Duration: {}'.format(end_time - start_time))

        # Builds the MoG
        # Parameters of the mixture
        # ntest, dim = x_test.shape  # test dimensionality and number of queries
        comp = np.reshape(y_pred, [
            -1, self.outputd + int(0.5 * (self.outputd + 1) * self.outputd) +
            1, self.ncomp
        ])

        mu_pred = comp[:, :self.outputd, :]
        sigma_pred = comp[:, self.outputd:self.outputd +
                          int(0.5 * (self.outputd + 1) * self.outputd), :]

        mu_pred = np.reshape(mu_pred, [-1, self.ncomp, self.outputd])
        sigma_pred = np.reshape(
            sigma_pred,
            [-1, self.ncomp,
             int(0.5 * (self.outputd + 1) * self.outputd)])
        alpha_pred = comp[:, -1, :]

        mog = []
        for pt in range(ntest):
            a = alpha_pred[pt, :]
            ms = [mu_pred[pt, i, :] for i in range(self.ncomp)]
            Ss = []
            for comp_idx in range(self.ncomp):
                sess = tf.Session()
                with sess.as_default():
                    m = tfd.fill_triangular(sigma_pred[pt, comp_idx, :])
                    tf.matrix_set_diag(m, tf.exp(tf.matrix_diag_part(m)))
                    L = m.eval()

                Ss.append(np.matmul(L, L.T))
            mog.append(pdf.MoG(a=a, ms=ms, Ss=Ss))
        return mog
コード例 #2
0
ファイル: mdn.py プロジェクト: uscresl/bayes_sim
    def predict_mog(self, x_test):
        tfd = tfp.distributions
        start_time = datetime.now()
        x_testS = self.scaler.transform(x_test)
        x_feat = self.rff.toFeatures(x_testS)
        y_pred = self.model.predict(x_feat)
        end_time = datetime.now()
        print('\n')
        print(
            "*********************************  Prediction ends  *********************************"
        )
        print('\n')
        print('Duration: {}'.format(end_time - start_time))

        # Builds the MoG
        # Parameters of the mixture
        ntest, dim = x_test.shape  # test dimensionality and number of queries
        comp = np.reshape(y_pred, [
            -1, self.outputd + int(0.5 * (self.outputd + 1) * self.outputd) +
            1, self.ncomp
        ])

        mu_pred = comp[:, :self.outputd, :]
        sigma_pred = comp[:, self.outputd:self.outputd +
                          int(0.5 * (self.outputd + 1) * self.outputd), :]

        mu_pred = np.reshape(mu_pred, [-1, self.ncomp, self.outputd])
        sigma_pred = np.reshape(
            sigma_pred,
            [-1, self.ncomp,
             int(0.5 * (self.outputd + 1) * self.outputd)])
        alpha_pred = comp[:, -1, :]

        mog = []
        for pt in range(ntest):
            a = alpha_pred[pt, :]
            ms = [mu_pred[pt, i, :] for i in range(self.ncomp)]
            Ss = []
            for comp_idx in range(self.ncomp):
                sess = tf.Session()
                with sess.as_default():
                    m = tfd.fill_triangular(sigma_pred[pt, comp_idx, :])
                    tf.matrix_set_diag(m, tf.exp(tf.matrix_diag_part(m)))
                    L = m.eval()

                Ss.append(np.matmul(L, L.T))
            mog.append(pdf.MoG(a=a, ms=ms, Ss=Ss))
        return mog
コード例 #3
0
ファイル: mdn.py プロジェクト: uscresl/bayes_sim
 def predict_mog_from_stats(self,
                            ntest=1,
                            alpha_pred=None,
                            mu_pred=None,
                            sigma_pred=None):
     alpha_pred = np.array(alpha_pred).reshape(-1, self.ncomp)
     mu_pred = np.array(mu_pred).reshape(-1, self.ncomp, self.outputd)
     sigma_pred = np.array(sigma_pred).reshape(-11, self.ncomp,
                                               self.outputd)
     mog = []
     for pt in range(ntest):
         a = alpha_pred[pt, :]
         ms = [mu_pred[pt, i, :] for i in range(self.ncomp)]
         Ss = []
         di = np.diag_indices(self.outputd)  # diagonal indices
         for i in range(self.ncomp):
             tmp = np.zeros((self.outputd, self.outputd))
             tmp[di] = sigma_pred[pt, i, :]**2
             Ss.append(tmp)
         mog.append(pdf.MoG(a=a, ms=ms, Ss=Ss))
     return mog[0]