Пример #1
0
    def _plot_model(self, model, title):
        x_plt = np.arange(0, 1, 1e-2)
        color = Colors()
        contexts = self._data.raw_test_samples[0][:10]
        for i in range(9):
            plt.subplot(3, 3, i + 1)
            context = contexts[i:i + 1]
            plt.imshow(self._data.img_from_context(context[0]))
            lines = []
            for k, c in enumerate(model.components):
                m = (c.mean(context)[0] + 1) / 2
                cov = c.covar(context)[0]
                mx, my = m[::2], m[1::2]
                plt.scatter(200 * mx, 100 * my, c=color(k))
                for j in range(mx.shape[0]):
                    mean = np.array([mx[j], my[j]])
                    cov_j = cov[2 * j: 2 * (j + 1), 2 * j: 2 * (j + 1)]
                    plt_cx, plt_cy = self._draw_2d_covariance(mean, cov_j, 1, return_raw=True)
                    plt.plot(200 * plt_cx, 100 * plt_cy, c=color(k), linestyle="dotted", linewidth=2)
                for j in range(10):
                    s = np.array(c.sample(contexts[i:i + 1]))
                    spline = self._data.get_spline(s[0])
                    l, = plt.plot(200 * x_plt, 100 * spline(x_plt), c=color(k), linewidth=1)
                lines.append(l)
            for j in range(10):
                s = self._data.raw_test_samples[1][i, j]
                spline = self._data.get_spline(s)
                plt.plot(200 * x_plt, 100 * spline(x_plt), c=color(model.num_components), linewidth=1, linestyle="dashed")

            weights = model.gating_distribution.probabilities(context)[0]
            strs = ["{:.3f}".format(weights[i]) for i in range(model.num_components)]
            plt.legend(lines, strs, loc=1)
            plt.gca().set_axis_off()
            plt.gca().set_xlim(0, 200)
            plt.gca().set_ylim(0, 100)
Пример #2
0
 def _subplot(self, i, title, data_list, data_list2=None, y_lim=None):
     plt.subplot(5 if self._target_ld is not None else 4, 1, i)
     plt.title(title)
     plt.plot(np.array(data_list))
     if data_list2 is not None:
         plt.plot(np.array(data_list2))
     plt.xlim(0, self._num_iters)
     if y_lim is not None:
         plt.ylim(y_lim[0], y_lim[1])
 def _plot_fn(self):
     plt.subplot(2, 1, 1)
     plt.title("Expected KL")
     plt.plot(self._kls)
     plt.xlim(0, self._num_iters)
     plt.subplot(2, 1, 2)
     plt.title("Expected Entropy")
     plt.plot(self._entropies)
     plt.xlim(0, self._num_iters)
     plt.tight_layout()
Пример #4
0
    def _plot(self):
        plt.subplot(2 if self._true_log_density is not None else 1, 1, 1)
        plt.title("Train Log Likelihood")

        plt.plot(np.arange(0, len(self._train_ll_list)), np.array(self._train_ll_list))
        plt.xlim(0, self._num_iters)
        if self._true_log_density is not None:
            plt.subplot(2, 1, 2)
            plt.title("I-Projection KL (MC-Estimate)")
            plt.plot(np.arange(0, len(self._train_kl_list)), np.array(self._train_kl_list))
            plt.xlim((0, self._num_iters))
        plt.tight_layout()
 def _plot_fn(self):
     plt.subplot(2, 1, 1)
     plt.title("Expected KL")
     for i in range(self._num_components):
         plt.plot(self._kls[i], c=self._c(i))
     plt.legend([
         "Component {:d}".format(i + 1) for i in range(self._num_components)
     ])
     plt.xlim(0, self._num_iters)
     plt.subplot(2, 1, 2)
     plt.title("Expected Entropy")
     for i in range(self._num_components):
         plt.plot(self._entropies[i], c=self._c(i))
     plt.xlim(0, self._num_iters)
     plt.tight_layout()
Пример #6
0
    def _plot_model(self, emm, title):
        plt.subplot(3, 1, 1)
        plt.title(title)
        plt.scatter(np.squeeze(self._train_samples[0]), np.squeeze(self._train_samples[1]))
        axis = plt.gca()
        axis.set_xlim([self._x_lim[0], self._x_lim[1]])
        y_lim = axis.get_ylim()

        plt.subplot(3, 1, 2)
        for i in range(self._data_obj.num_modes):
            mean, cov = self._data_obj.get_conditional_parameters(self._plt_x, i)
            std = np.sqrt(cov[..., 0])
            plt.plot(self._plt_x, mean, c=self._colors(0))
            plt.fill_between(np.squeeze(self._plt_x), np.squeeze(mean) - 2 * std, np.squeeze(mean) + 2 * std,
                             alpha=0.5, edgecolor=self._colors(0), facecolor=self._colors(0))

        for i, c in enumerate(emm.components):
            mean = c.mean(self._plt_x)
            if callable(c.covar):
                std = np.sqrt(np.squeeze(c.covar(self._plt_x)))
            else:
                std = np.sqrt(np.squeeze(c.covar))
            plt.plot(self._plt_x, mean, c=self._colors(i+1))
            plt.fill_between(np.squeeze(self._plt_x), np.squeeze(mean) - 2 * std, np.squeeze(mean) + 2 * std,
                             alpha=0.5, edgecolor=self._colors(i+1), facecolor=self._colors(i+1))
        plt.gca().set_xlim([self._x_lim[0], self._x_lim[1]])
        plt.gca().set_ylim(y_lim)
        plt.grid(True)

        #plt.subplot(3, 1, 3)
        #weights = emm.weights(self._plt_x)
        #for i in range(len(emm.components)):
        #    plt.plot(self._plt_x, weights[:, i], c=self._colors(i+1))
        #plt.gca().set_xlim([self._x_lim[0], self._x_lim[1]])
        #plt.gca().set_ylim(-0.1, 1.1)
        plt.grid(True)
Пример #7
0
    def _draw_2d_covariance(mean, covmatrix, chisquare_val=2.4477, return_raw=False, *args, **kwargs):
        (largest_eigval, smallest_eigval), eigvec = np.linalg.eig(covmatrix)
        phi = -np.arctan2(eigvec[0, 1], eigvec[0, 0])

        a = chisquare_val * np.sqrt(largest_eigval)
        b = chisquare_val * np.sqrt(smallest_eigval)

        ellipse_x_r = a * np.cos(np.linspace(0, 2 * np.pi))
        ellipse_y_r = b * np.sin(np.linspace(0, 2 * np.pi))

        R = np.array([[np.cos(phi), np.sin(phi)], [-np.sin(phi), np.cos(phi)]])
        r_ellipse = np.array([ellipse_x_r, ellipse_y_r]).T @ R
        if return_raw:
            return mean[0] + r_ellipse[:, 0], mean[1] + r_ellipse[:, 1]
        else:
            return plt.plot(mean[0] + r_ellipse[:, 0], mean[1] + r_ellipse[:, 1], *args, **kwargs)