def plot_corr(model, X, absolute_value, threshold, figure_name='pcr'):
    activations, _ = get_activations(model, X)
    activations, _ = np.unique(activations, return_inverse=True, axis=0)

    min_activations = 10

    # partition that are not on the domain are removed.

    for i, v in enumerate(np.all(activations == activations[0, :], axis=0)):
        if v:
            activations[:, i] = 0
    # unique after corrections
    activations, _ = np.unique(activations, return_inverse=True, axis=0)

    nb_activations = activations.shape[0]
    nb_c_activations = min(nb_activations, min_activations)

    nb_params = -1
    for n, p in model.named_parameters():
        if len(p.shape) == 2:
            nb_params += 1
    plt(figure_name, figsize=(nb_c_activations * 6.4, nb_params * 4.8))

    vmin = 0. if absolute_value else -1.
    vmax = 1.
    print_info(str(nb_activations) + ' affine spaces used')
    for idx, a in enumerate(range(nb_c_activations)):
        tc = 0
        c = 0
        for name, params in model.named_parameters():

            if len(params.shape) == 2:

                A = params.detach().numpy()
                B = np.zeros((A.shape[0], A.shape[0]))
                plt(figure_name).subplot(nb_params, nb_c_activations,
                                         1 + c * nb_c_activations + idx)
                for i in range(A.shape[0]):

                    for j in range(A.shape[0]):
                        if activations[idx,
                                       tc + i] != 0 and activations[idx, tc +
                                                                    j] != 0:
                            B[i,
                              j] = np.dot(A[i, :], A[j, :]) / np.linalg.norm(
                                  A[i, :]) / np.linalg.norm(A[j, :])
                        else:
                            B[i, j] = None
                if absolute_value:
                    B = np.abs(B)
                if type(threshold) is not bool:
                    B = (B > threshold).astype(int)
                plt(figure_name).imshow(B, vmin=vmin, vmax=vmax)
                plt(figure_name).title(name)
                plt(figure_name).colorbar()
                tc += A.shape[0]
                c += 1
            if tc >= activations.shape[1]:
                break
def plot_gradient_field(X,
                        y,
                        model,
                        ax=None,
                        figure_name='pfg',
                        normalized=True):
    from torch.autograd import Variable
    if ax is None:
        ax = plt(figure_name).gca()

    # disabling grad
    grads = []
    model.eval()
    for param in model.parameters():
        grads.append(param.requires_grad)
        param.requires_grad = False

    num = 20
    x_min, x_max = ax.get_xlim()
    y_min, y_max = ax.get_ylim()
    xx, yy = np.meshgrid(np.linspace(x_min, x_max, num),
                         np.linspace(y_min, y_max, num))

    _X = np.c_[xx.ravel(), yy.ravel()].astype(np.float32)

    data = torch.from_numpy(_X).float()

    d = Variable(data, requires_grad=True)
    output = model(d)

    for i in range(d.shape[0]):
        output[i][0].backward(retain_graph=True)

    # plotting
    U = d.grad[:, 0]
    V = d.grad[:, 1]
    U = U.reshape(xx.shape)
    V = V.reshape(yy.shape)
    E_norm = 2 * np.sqrt(U**2 + V**2)
    if normalized:
        U = U / E_norm
        V = V / E_norm
        ax.quiver(xx,
                  yy,
                  U,
                  V,
                  E_norm,
                  cmap=plt().cm.coolwarm,
                  units='xy',
                  scale=5.)
    else:
        ax.quiver(xx, yy, U, V, color='darkred', units='xy', scale=5.)
    # reestablishing grad
    for grad, param in zip(grads, model.parameters()):
        param.requires_grad = grad
    return ax
def plot_partition(X, y, model, figure_name='pp', ax=None):
    if ax is None:
        ax = plt(figure_name).gca()

    plot_dataset(X, y, ax=ax, alpha=.25)

    num = 500
    x_min, x_max = ax.get_xlim()
    y_min, y_max = ax.get_ylim()
    xx, yy = np.meshgrid(np.linspace(x_min, x_max, num),
                         np.linspace(y_min, y_max, num))
    _X = np.c_[xx.ravel(), yy.ravel()].astype(np.float32)
    _, Z = get_activations(model, _X)
    Z = Z.reshape(xx.shape)

    levels = len(np.unique(Z))

    im = Image.fromarray(((Z / levels) * 255.).astype(np.uint8), mode='L')
    im = im.filter(ImageFilter.FIND_EDGES)
    im = im.point(lambda p: p > 0 and 255)
    ax.imshow(im,
              origin='lower',
              extent=(x_min, x_max, y_min, y_max),
              cmap='gray',
              aspect='auto')

    ax.imshow(Z / levels,
              origin='lower',
              extent=(x_min, x_max, y_min, y_max),
              alpha=.75,
              cmap='gray',
              aspect='auto')
    return ax
Exemplo n.º 4
0
    def print_mask(self,
                   save=False,
                   show=True,
                   color=None,
                   figure_name='map_mask',
                   ax=None):
        """
        Plot mask (original 1st read layer)
        """
        if ax is None:
            ax = plt(figure_name,
                     figsize=special_parameters.plt_default_size).gca()

        reverse_mask = (self.mask == 0).astype(np.int)

        if color is not None:
            reverse_mask[color[0], color[1]] = -15

        reverse_mask = np.ma.masked_where(reverse_mask == 1, reverse_mask)

        # Plot
        ax.imshow(reverse_mask,
                  cmap=matplotlib.pyplot.get_cmap('gray'),
                  vmin=-15,
                  vmax=5)
        ax.set_xlabel('Longitude')
        ax.set_ylabel('Latitude')
        ax.set_title('Land-sea mask')
def plot_activation_rate(X,
                         y,
                         model,
                         ax=None,
                         layer=None,
                         figure_name='par',
                         colorbar=True):
    if ax is None:
        ax = plt(figure_name).gca()

    # plot_dataset(X, y, ax=ax, alpha=.25)

    num = 500
    x_min, x_max = ax.get_xlim()
    y_min, y_max = ax.get_ylim()
    xx, yy = np.meshgrid(np.linspace(x_min, x_max, num),
                         np.linspace(y_min, y_max, num))
    _X = np.c_[xx.ravel(), yy.ravel()].astype(np.float32)
    Z, _ = get_activations(model, _X, layer=layer)
    Z = np.average(Z, axis=1)

    Z = Z.reshape(xx.shape)
    c = ax.contourf(xx,
                    yy,
                    Z,
                    cmap='gray',
                    vmin=Z.min(),
                    vmax=Z.max(),
                    alpha=.5)
    if colorbar:
        ax.figure.colorbar(c, ax=ax, alpha=.75)
    return ax
def plot_decision_boundary(model, figure_name='pdb', ax=None):
    """
    Plot the decision boundary
    :param model: the model
    :param figure_name: figure name, by default pdb
    :param ax: an axis if needs to be plotted on an other figure
    :return: the axis of the current figure
    """
    pred_func = make_pred_func(model)
    if ax is None:
        ax = plt(figure_name).gca()
    if not callable(pred_func) and hasattr(pred_func, 'predict'):
        pred_func = pred_func.predict

    num = 1000
    x_min, x_max = ax.get_xlim()
    y_min, y_max = ax.get_ylim()
    xx, yy = np.meshgrid(np.linspace(x_min, x_max, num),
                         np.linspace(y_min, y_max, num))

    zz = pred_func(np.c_[xx.ravel(), yy.ravel()]).cpu()

    zz = zz.reshape(xx.shape)

    ax.contourf(xx, yy, zz, alpha=.4, zorder=-1)
    return ax
Exemplo n.º 7
0
    def show_view(self, figure_name='regatta_view', show=False):
        view = self.get_view()

        uwind = view[0, :, :]
        vwind = view[1, :, :]
        mask = view[2, :, :]

        print(view.shape)

        fig = plt(figure_name)

        # ax = fig.gca()

        fig.subplot(1, 3, 1)
        fig.imshow(np.squeeze(uwind))
        fig.title('u wind (m.s-1)')

        fig.subplot(1, 3, 2)
        fig.imshow(np.squeeze(vwind))
        fig.title('v wind (m.s-1)')

        fig.subplot(1, 3, 3)
        fig.imshow(np.squeeze(mask))
        fig.title('mask')

        save_fig(figure_name=figure_name)
Exemplo n.º 8
0
 def __call__(self, validation_id):
     ax = plt('VCallback').subplot(self.nb_calls + 2, 1, validation_id + 1)
     plot_dataset(self.dataset.dataset, self.dataset.labels, ax=ax, figure_name='VCallback')
     plot_decision_boundary(self.model, ax=ax, figure_name='VCallback')
     plot_activation_rate(self.dataset.dataset, self.dataset.labels, self.model, ax=ax,
                          figure_name='VCallback')
     plot_gradient_field(self.dataset.dataset, self.dataset.labels, self.model, ax=ax, normalized=True,
                         figure_name='VCallback')
Exemplo n.º 9
0
    def print_wind(self,
                   idx,
                   save=False,
                   show=True,
                   figure_name='map_wind',
                   ax=None):
        """
        Plot the u_v_wind components as quiver (basemap) from the 1st u, v components read
        """
        if ax is None:
            ax = plt(figure_name,
                     figsize=special_parameters.plt_default_size).gca()

        self.print_mask(show=False, save=False, ax=ax)

        x_grid = np.arange(0, self.mask.shape[1])
        y_grid = np.arange(0, self.mask.shape[0])
        x_mesh, y_mesh = np.meshgrid(x_grid, y_grid)

        intensity = np.sqrt(
            np.power(np.flip(self.gribs[idx, 0, :, :], axis=0), 2) +
            np.power(np.flip(self.gribs[idx, 1, :, :], axis=0), 2))

        u10 = np.flip(self.gribs[idx, 0, :, :], axis=0).flatten()
        v10 = np.flip(self.gribs[idx, 1, :, :], axis=0).flatten()

        wind_speed = np.sqrt((u10**2 + v10**2))

        # Create colour bar
        norm = matplotlib.colors.Normalize()
        norm.autoscale(wind_speed)
        cm = matplotlib.cm.CMRmap  # selecting the colourmap

        sm = matplotlib.cm.ScalarMappable(cmap=cm, norm=norm)
        sm.set_array([])

        ax.pcolormesh(x_mesh, y_mesh, intensity, alpha=0.5, cmap=cm)

        # Plot
        Y, X = np.mgrid[0:self.gribs[idx, 0].shape[0]:self.granularity,
                        0:self.gribs[idx, 0].shape[1]:self.granularity]
        q = ax.barbs(
            X,
            Y,
            np.flip(self.gribs[idx, 0, ::self.granularity, ::self.granularity],
                    axis=0),
            np.flip(self.gribs[idx, 1, ::self.granularity, ::self.granularity],
                    axis=0),
            length=3,
            linewidths=0.5)

        ax.set_xlabel('Longitude')
        ax.set_ylabel('Latitude')

        cbar = ax.figure.colorbar(sm)
        cbar.set_label('velocity (m.s-1)')

        ax.set_title('Wind velocity  ' + str(self.time_refs[idx, 3]))
def plot_representation(fig_name='representation_tsne', file_name=None):
    file_name = fig_name if file_name is None else file_name
    ax = plt(fig_name, figsize=(8, 10)).gca()
    path = os.path.join(last_experiment_path(experiment_name),
                        file_name + '.dump')
    with open(path, 'rb') as f:
        artists = pickle.load(f)
    colors = cm.tab20b(np.linspace(0, 1, len(artists)))

    for row in artists:
        ax.scatter(row[0][:, 0],
                   row[0][:, 1],
                   c=colors[row[1]],
                   label=clean_labels(row[2]))

    ax.legend(bbox_to_anchor=(0, 0, 1, 1),
              bbox_transform=plt(fig_name).gcf().transFigure,
              prop={'size': 6},
              ncol=4,
              loc=3)
    ax.set_title('Painting representation')
def plot_layer_output_corr_interspace(model,
                                      X,
                                      absolute_value,
                                      threshold,
                                      min_activations=10000,
                                      figure_name='ploci'):
    #Compute neural directions first
    vectors, vmin, vmax = compute_neural_directions(model, X, absolute_value,
                                                    threshold, min_activations)
    plt(figure_name, figsize=(6.4, len(vectors) * 4.8))

    # compute interspace co-linearity
    for li, l in enumerate(vectors):

        cos = np.zeros((len(l), len(l)))

        for r in range(len(l)):
            for c in range(len(l)):
                if np.count_nonzero(l[r]) > 0 and np.count_nonzero(l[c]) > 0:
                    cos[r, c] = np.dot(l[r], l[c]) / np.linalg.norm(
                        l[r]) / np.linalg.norm(l[c])
                else:
                    cos[r, c] = None
            if absolute_value:
                cos = np.abs(cos)
            if type(threshold) is not bool:
                cos = (cos > threshold).astype(int)

        plt(figure_name).subplot(len(vectors), 1, 1 + li)
        plt(figure_name).imshow(cos, vmin=vmin, vmax=vmax)
        plt(figure_name).colorbar()
        print('colorbar')
        plt(figure_name).title('Layer: ' + str(li + 1))

        # plt(figure_name).tight_layout()

    return vectors
def plot_partition_distribution(model,
                                X,
                                figure_name='plot_pat_dist',
                                ax=None):
    if ax is None:
        fig = plt(figure_name).figure()
        ax = fig.gca()

    _, activations = get_activations(model, X)
    unique, counts = np.unique(activations, return_counts=True, axis=0)
    print(len(unique))

    sorted_counts = np.sort(counts)[::-1]
    cumulative_sum = sorted_counts.cumsum() / sorted_counts.sum()

    ax.plot(cumulative_sum)
    ax.grid()
Exemplo n.º 13
0
    def plot(self, plot_weather=False, save=False, show=True, track=None, figure_name='regatta_plot'):
        if track is not None:
            self.track = track

        ax = plt(figure_name, figsize=special_parameters.plt_default_size).gca()
        # draw land
        if plot_weather:
            # current simulation timestamp
            t_idx = self.numpy_grib.physical_time_to_idx(self.start_timestamp + self.timedelta)
            self.numpy_grib.print_wind(show=False, save=False, idx=t_idx, ax=ax)
        else:
            self.numpy_grib.print_mask(show=False, save=False, figure_name=figure_name, ax=ax)

        # current position
        y_position, x_position = project(latitude=self.position[1], longitude=self.position[0], grib=self.numpy_grib)

        y_position = self.numpy_grib.latitudes.shape[0] - y_position

        ax.plot(x_position, y_position, 's', markersize=4, color="blue")

        # target
        y_target, x_target = project(latitude=self.target[1], longitude=self.target[0], grib=self.numpy_grib)

        y_target = self.numpy_grib.latitudes.shape[0] - y_target
        ax.plot(x_target, y_target, 'bo', markersize=6, color="green")

        # track
        X, Y = [], []
        for lat, lon in zip(self.track[:, 1], self.track[:, 0]):
            y, x = project(lat, lon, self.numpy_grib)
            X.append(x)
            Y.append(self.numpy_grib.latitudes.shape[0] - y)

        ax.plot(X, Y, '-', color="red")
        ax.set_xlabel('Longitude')
        ax.set_ylabel('Latitude')

        ax.set_title('Offshore Regatta (at ' + str(self.start_timestamp + self.timedelta) + ')', y=1, fontsize=12)

        save_fig(figure_name=figure_name)
def plot_dataset(dataset, y, ax=None, figure_name='plot_dataset', **kwargs):
    """
    Plot the dataset
    :param dataset: the dataset, must be 2D
    :param y: the label
    :param ax: the axis if needs to be plotted on an other figure
    :param figure_name: plot_dataset by default
    :param kwargs:
    :return:
    """
    locals().update(kwargs)

    if ax is None:
        ax = plt(figure_name).gca()

    n_classes = len(np.unique(y))
    colors = ['red', 'cyan', 'orange']
    for k in range(n_classes):
        dataset_k = dataset[y == k]
        ax.scatter(dataset_k[:, 0],
                   dataset_k[:, 1],
                   color=colors[k],
                   alpha=0.5)
    return ax
Exemplo n.º 15
0
from datascience.visu.util import plt, save_fig

noise = [i/10 for i in range(1, 11)]

OU = lambda x:  x - eta * x - n * normal(0, 1, 1)[0]
SGD = lambda x: x - eta * (x - n * x * np.abs(normal(0, 1, 1)[0]))

print(normal(0, 1, 1))

nb_iterations = 300
all_positions = []
for n in noise:
    pos = 150
    positions = [pos]
    eta = 0.05
    all_positions.append(positions)

    for _ in range(nb_iterations):
        pos = SGD(pos)
        positions.append(pos)


ax = plt('OU').gca()

for i, j in zip(noise, all_positions):
    print(i,j)
    ax.plot(j, label='noise: '+str(i))
ax.legend()
save_fig()
Exemplo n.º 16
0
 def initial_call(self, modulo, nb_calls, dataset, model):
     super().initial_call(modulo, nb_calls, dataset, model)
     self.fig = plt('VCallback', figsize=(6.4, (nb_calls + 2) * 4.8))
     self.__call__(0)
def plot_separator(separator, figure_name='separator', ax=None, **kwargs):
    if ax is None:
        ax = plt(figure_name).gca()

    ax.plot(separator[0], separator[1], **kwargs)
Exemplo n.º 18
0
def pplot_patch(patch, resolution=1., return_fig=True, header=None):
    """
    plot a patch that has already been extracted
    :param header:
    :param patch:
    :param resolution:
    :param return_fig:
    :return:
    """
    if header is None:
        header = ['' for _ in range(len(patch))]

    # computing number of rows and columns...
    nb_rows = (len(patch) + 4) // 5
    nb_cols = 5

    plt('patch_ext',
        figsize=(nb_cols * 6.4 * resolution, nb_rows * 4.8 * resolution))
    fig = get_figure('patch_ext')
    for i, (t, p) in enumerate(zip(header, patch)):
        plt('patch_ext').subplot(nb_rows, nb_cols, i + 1)
        plt('patch_ext').title(t, fontsize=20)
        plt('patch_ext').imshow(p, aspect='auto')
        if len(p.shape) < 3:
            plt('patch_ext').colorbar()
    fig.tight_layout()
    if return_fig:
        return fig
    else:
        fig.show()
        plt('patch_ext').close(fig)
def plot_on_map(activations,
                map_ids,
                n_cols=1,
                n_rows=1,
                figsize=4,
                log_scale=False,
                mean_size=1,
                selected=tuple(),
                legend=None,
                output="activations",
                style="grey",
                exp_scale=False,
                cmap=None,
                alpha=None,
                bad_alpha=1.,
                font_size=12,
                color_text='black',
                color_tick='black'):
    if log_scale:
        print_info("apply log...")
        activations = activations + 1.0
        activations = np.log(activations)
    elif exp_scale:
        print_info("apply exp...")
        p = np.full(activations.shape, 1.2)
        activations = np.power(p, activations)

    print_info("construct array activation map...")
    pos = []
    max_x = 0
    max_y = 0
    for id_ in map_ids:
        x, y = id_.split("_")
        x, y = int(x), int(y)
        pos.append((x, y))
        if x > max_x:
            max_x = x
        if y > max_y:
            max_y = y
    size = max(max_x + 1, max_y + 1)
    while size % mean_size != 0:
        size += 1
    nb = n_cols * n_rows
    act_map = np.ndarray((nb, size, size))
    act_map[:] = np.nan

    print_info("select neurons to print...")
    if len(selected) > 0:
        list_select = selected
    else:
        list_select = random.sample(list(range(activations.shape[1])), nb)

    print_info("fill activation map array...")
    for k, act in enumerate(activations):
        for idx, j in enumerate(list_select):
            x, y = pos[k][0], pos[k][1]
            act_map[idx, x, y] = act[j]
    """
    fig, axs = plt.subplots(n_rows, n_cols, sharex='col', sharey='row',
                            figsize=(n_cols*figsize*1.2, n_rows*figsize))
    fig.subplots_adjust(wspace=0.5)
    plt.tight_layout(pad=1.5)

    print_info("make figure...")
    for j in range(nb):
        if mean_size != 1:
            height, width = act_map[j].shape
            act_map_j = np.ma.average(np.split(np.ma.average(np.split(act_map[j], width // mean_size, axis=1),
                                               axis=2), height // mean_size, axis=1), axis=2)
        else:
            act_map_j = act_map[j]

        masked_array = np.ma.array(act_map_j, mask=np.isnan(act_map_j))
        cmap = matplotlib.cm.inferno
        cmap.set_bad('grey', 1.)
        im = axs[j // n_cols, j % n_cols].imshow(masked_array, cmap=cmap, interpolation='none')
        axs[j // n_cols, j % n_cols].set_title(str(list_select[j]))
        divider = make_axes_locatable(axs[j // n_cols, j % n_cols])
        cax = divider.append_axes("right", size="5%", pad=0.05)
        fig.colorbar(im, cax=cax)
    """

    font = {'family': 'normal', 'weight': 'bold', 'size': font_size}

    matplotlib.rc('font', **font)

    if legend is None:
        legend = list(map(str, list_select))

    mplt.rcParams['text.color'] = color_text
    mplt.rcParams['axes.labelcolor'] = color_tick
    mplt.rcParams['xtick.color'] = color_tick
    mplt.rcParams['ytick.color'] = color_tick

    plt(output, figsize=(n_cols * figsize * 1.2, n_rows * figsize))
    fig = get_figure(output)
    fig.subplots_adjust(wspace=0.05)

    print_info("make figure...")
    for j in range(nb):
        if mean_size != 1:
            height, width = act_map[j].shape
            act_map_j = np.nanmean(np.split(np.nanmean(np.split(act_map[j],
                                                                width //
                                                                mean_size,
                                                                axis=1),
                                                       axis=2),
                                            height // mean_size,
                                            axis=1),
                                   axis=2)
        else:
            act_map_j = act_map[j]
        print(act_map_j[2, 572])
        masked_array = np.ma.array(act_map_j, mask=np.isnan(act_map_j))
        if cmap is None:
            if style == "grey":
                cmap = matplotlib.cm.inferno
                cmap.set_bad('grey', bad_alpha)
            elif style == "white":
                cmap = matplotlib.cm.inferno
                cmap.set_bad('white', bad_alpha)
        ax = plt(output).subplot(n_rows, n_cols, j + 1)
        ax.set_facecolor((0, 0, 0, 0))
        im = plt(output).imshow(masked_array,
                                cmap=cmap,
                                interpolation='none',
                                alpha=alpha)
        plt(output).title(legend[j])
        divider = make_axes_locatable(ax)
        cax = divider.append_axes("right", size="5%", pad=0.05)
        plt(output).colorbar(im, cax=cax)

    fig.tight_layout(pad=0.05)
    fig.patch.set_alpha(0.0)

    save_fig(figure_name=output, extension='png')
def plot_layer_output_corr(model,
                           X,
                           absolute_value,
                           threshold,
                           figure_name='ploc'):
    # this method only works on fully connected models
    if type(model) is not fully_connected.Net:
        print_errors(str(type(model)) + ' must be of type ' +
                     str(fully_connected.Net) + '.',
                     do_exit=True)

    layers = [m for m in model.modules()
              if type(m) in (BatchNorm1d, Linear)][:-1]
    final_layers = []
    it = 0

    while it < len(layers):
        # linear layer
        M = layers[it]
        it += 1

        linear_app = M.weight.detach().numpy()

        if it < len(layers) and type(layers[it]) is BatchNorm1d:
            A = layers[it]
            var = np.diag(A.running_var.numpy())

            gamma = np.diag(A.weight.detach().numpy())
            bn = np.matmul(gamma, np.linalg.inv(var))

            linear_app = np.matmul(bn, linear_app)
            it += 1
        final_layers.append(linear_app)

    # get activations
    activations, _ = get_activations(model, X)
    activations, _ = np.unique(activations, return_inverse=True, axis=0)

    # partition that are not on the domain are removed.

    for i, v in enumerate(np.all(activations == activations[0, :], axis=0)):
        if v:
            activations[:, i] = 0
    # unique after corrections
    activations, _ = np.unique(activations, return_inverse=True, axis=0)

    min_activations = 10

    nb_c_activations = min(activations.shape[0], min_activations)

    plt(figure_name, figsize=(nb_c_activations * 6.4, len(final_layers) * 4.8))
    vmin = 0. if absolute_value else -1.
    vmax = 1.
    for i in range(min_activations):
        la = None
        for li, l in enumerate(final_layers):
            cos = np.zeros((l.shape[0], l.shape[0]))
            activated = activations[i][li * l.shape[0]:(li + 1) * l.shape[0]]

            if la is None:
                la = final_layers[li] * activated[:, np.newaxis]
            else:
                la = np.matmul(final_layers[li], la) * activated[:, np.newaxis]
            for r in range(l.shape[0]):
                for c in range(l.shape[0]):
                    if activations[i, li * l.shape[0] +
                                   r] != 0 and activations[i, li * l.shape[0] +
                                                           c] != 0:
                        cos[r,
                            c] = np.dot(la[r, :], la[c, :]) / np.linalg.norm(
                                la[r, :]) / np.linalg.norm(la[c, :])
                    else:
                        cos[r, c] = None
            if absolute_value:
                cos = np.abs(cos)
            if type(threshold) is not bool:
                cos = (cos > threshold).astype(int)
            plt(figure_name).subplot(len(final_layers), nb_c_activations,
                                     1 + li * nb_c_activations + i)
            plt(figure_name).imshow(cos, vmin=vmin, vmax=vmax)
            plt(figure_name).title('Layer: ' + str(li + 1))
            plt(figure_name).colorbar()
Exemplo n.º 21
0
import numpy as np
import os

from engine.parameters.special_parameters import get_parameters
from engine.path import last_experiment_path

experiment_name = get_parameters('roc_experiment', 'country')
path = os.path.join(last_experiment_path(experiment_name), 'results.csv')

df = pd.read_csv(path, header='infer', sep=';')

print(df)

fpr, tpr, thresholds = roc_curve(df.true_label, df.prediction, pos_label=1)

ax = plt('roc_curve').gca()

ax.set_xlim([-0.007, 1.0])
ax.set_ylim([0.0, 1.01])
ax.set_xlabel('False Positive Rate')
ax.set_ylabel('True Positive Rate')
ax.set_title('Receiver operating characteristic (AUC: %.3f)' % auc(fpr, tpr))

ax.plot([0, 1], [0, 1], color='red', linestyle='--', label='Random model')
ax.plot(fpr, tpr, color='yellow', label='IArt')
ax.plot([0, 0, 1], [0, 1, 1],
        color='green',
        linestyle='--',
        label='Perfect model')

ax.legend(loc="lower right")
Exemplo n.º 22
0
from datascience.visu.util import plt, save_fig
from numpy import linspace, power

ax = plt('x_square').gca()

x = linspace(-2, 2, 100)
x_small = linspace(0, 2, 100)
ax.set_title('$x^2$')

ax.plot(x, power(x, 2), label='$x^2+0x^3$')
ax.plot(x, power(2 * x, 2), label='$4x^2+0x^3$')
ax.plot(x, power(2 * x, 2) + power(x, 3) * 2, label='$4x^2+2x^3$')
ax.plot(x, power(2 * x, 2) + power(x, 3) * 3, label='$4x^2+3x^3$')
ax.plot(x_small, 8 * x_small - 4, label='tangent: $8x-4$')

ax.legend()
save_fig()
Exemplo n.º 23
0
from datascience.visu.util import plt, save_fig
import numpy as np

ax = plt('sde_omega').gca()

x = np.linspace(0, 3500, 5000)


def f(t, omega=32, eta=0.1, b=10.):
    kappa = 0.015
    kappa_k = kappa

    var_x_delta = 1.
    lambda_jk = 1.
    w_0 = 1.
    result = eta / (b * 2) * (var_x_delta / lambda_jk)
    w_t = np.log(kappa * omega * kappa_k * t + np.exp(kappa * omega * w_0))
    w_t /= (kappa * omega + (kappa**2) * kappa_k * (omega**2) * t +
            np.exp(kappa * omega * w_0))
    result *= w_t**2
    result *= (1 - np.exp(-2 * lambda_jk * eta * t))
    return result


for width in [16 * i for i in range(2, 5)]:
    ax.plot(x, f(x, width), label='$\\omega$=%ld' % width)
ax.legend()
save_fig('sde_omega')

ax = plt('sde_eta_b').gca()