def predict_mog(self, data_test, sigma=0.01):
        # Builds a MoG representation to be compatible with MDN
        # Parameters of the mixture
        ntest, _ = data_test.shape  # test dimensionality and number of queries

        mog = []
        for pt in range(ntest):
            start_time = datetime.now()
            #x_testS = self.scaler.transform(x_test) no scaling implemented
            y_pred = self.predict(data_test)
            end_time = datetime.now()
            print('\n')
            print(
                "*********************************  Prediction ends  *********************************"
            )
            print('\n')
            print('Duration: {}'.format(end_time - start_time))

            a = [1. / self.n_particles for i in range(self.n_particles)]

            # Assuming output from BlackBoxOptimizer
            ms = [y_pred[i, :] for i in range(self.n_particles)]
            p_std = 0.005 * np.ones(y_pred.shape[1])

            Ss = [np.diag(p_std) for i in range(self.n_particles)]
            mog.append(pdf.MoG(a=a, ms=ms, Ss=Ss))
        #self.plot_objective([-2, 0])
        return mog
    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(np.array(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 = []
        config = tf.ConfigProto(device_count={'CPU': 12, 'GPU': 0})
        config.gpu_options.allow_growth = True
        sess = tf.Session(config=config)
        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):
                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
    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 = []

        config = tf.ConfigProto(device_count={'CPU': 12, 'GPU': 0})
        config.gpu_options.allow_growth = True
        sess = tf.Session(config=config)

        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):
                with sess.as_default():
                    m = tfd.fill_triangular(sigma_pred[pt, comp_idx, :])
                    tf.matrix_set_diag(m,
                                       1.e-4 + 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
Example #4
0
    def get_mog(self, x):
        """
        Return the conditional mog at location x.
        :param network: an MDN network
        :param x: single input location
        :return: conditional mog at x
        """
        # gather mog parameters
        pi, sigma, mean = self(x)
        pi = pi.data.numpy().transpose()
        mean = mean.data.numpy().transpose()
        sigma = sigma.data.numpy().transpose()

        # return mog
        return pdf.MoG(a=pi, ms=mean, Ss=sigma)
Example #5
0
    def predict_mog(self, x_test):
        tfd = tfp.distributions
        start_time = datetime.now()
        y_pred = self.model.predict(x_test)
        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
 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]