Exemplo n.º 1
0
    def animate_training_comparison(self, gridworld, learner_1, learner_2,
                                    episode):
        # make local copies of input
        grid = gridworld
        training_episodes_history_v1 = learner_1.training_episodes_history
        training_episodes_history_v2 = learner_2.training_episodes_history

        # initialize figure
        fig = plt.figure(figsize=(10, 3))
        axs = []
        for i in range(2):
            ax = fig.add_subplot(1, 2, i + 1, aspect='equal')
            axs.append(ax)

        # compute maximum length of episodes animated
        max_len = 0
        key = episode
        L1 = len(training_episodes_history_v1[str(key)])
        L2 = len(training_episodes_history_v2[str(key)])
        max_len = max(L1, L2)

        # loop over the episode histories and plot the results
        rewards = np.zeros((2, 1))

        def show_episode(step):
            # loop over subplots and plot current step of each episode history
            artist = fig
            for k in range(len(axs)):
                ax = axs[k]

                # take correct episode
                current_episode = 0
                if k == 0:
                    current_episode = training_episodes_history_v1[str(key)]
                else:
                    current_episode = training_episodes_history_v2[str(key)]

                # define new location of agent
                grid.agent = current_episode[min(step,
                                                 len(current_episode) - 1)]

                # color gridworld for this episode and step
                grid.color_gridworld(ax=ax)
            return artist,

        # create animation object
        anim = animation.FuncAnimation(fig,
                                       show_episode,
                                       frames=min(100, max_len),
                                       interval=min(100, max_len),
                                       blit=True)

        # set frames per second in animation
        IPython_display.anim_to_html(anim, fps=min(100, max_len) / float(5))

        return (anim)
Exemplo n.º 2
0
    def animate_training_runs(self, gridworld, learner, episodes):
        # make local copies of input
        grid = gridworld
        training_episodes_history = learner.training_episodes_history

        # initialize figure
        fig = plt.figure(figsize=(10, 3))
        axs = []
        for i in range(len(episodes)):
            ax = fig.add_subplot(1, len(episodes), i + 1, aspect='equal')
            axs.append(ax)

        if len(episodes) == 1:
            axs = np.array(axs)

        # compute maximum length of episodes animated
        max_len = 0
        for key in episodes:
            l = len(training_episodes_history[str(key)])
            if l > max_len:
                max_len = l

        # loop over the episode histories and plot the results
        def show_episode(step):
            # loop over subplots and plot current step of each episode history
            artist = fig

            for k in range(len(axs)):
                ax = axs[k]

                # take correct episode
                episode_num = episodes[k]
                current_episode = training_episodes_history[str(episode_num)]

                # define new location of agent
                grid.agent = current_episode[min(step,
                                                 len(current_episode) - 1)]

                # color gridworld for this episode and step
                grid.color_gridworld(ax=ax)
                ax.set_title('episode = ' + str(episode_num + 1))

            return artist,

        # create animation object
        anim = animation.FuncAnimation(fig,
                                       show_episode,
                                       frames=min(100, max_len),
                                       interval=min(100, max_len),
                                       blit=True)

        # set frames per second in animation
        IPython_display.anim_to_html(anim, fps=min(100, max_len) / float(5))

        return (anim)
Exemplo n.º 3
0
def plot_movie_js2(enc_array, image_array, save=None):
    #Shows encoding and frames
    fig = plt.figure(figsize=(10, 3), dpi=72)
    ax1 = fig.add_subplot(2, 2, 1)
    plt.title('Visual Encoding', fontsize=15)
    plt.axis('off')
    im = plt.imshow(enc_array[0][:8, :], vmin=-1, vmax=25)
    ax2 = fig.add_subplot(2, 2, 3)
    plt.title('Vector Encoding', fontsize=15)
    plt.axis('off')
    im2 = plt.imshow(enc_array[0][8:, :], vmin=-1, vmax=25)
    ax3 = fig.add_subplot(1, 2, 2)
    im3 = plt.imshow(image_array[0])
    plt.axis('off')

    Writer = animation.writers['ffmpeg']
    writer = Writer(fps=10, metadata=dict(artist='Me'), bitrate=1800)

    def animate(i):
        im.set_array(enc_array[i][:8, :])
        im2.set_array(enc_array[i][8:, :])
        im3.set_array(image_array[i])
        return (im, )

    anim = animation.FuncAnimation(fig, animate, frames=len(image_array))

    display(IPython_display.display_animation(anim))
    if save != None:
        anim.save(save, writer=writer)
Exemplo n.º 4
0
def plot_movie_js3(enc_array, image_array, cluster, save=None):
    #Plot Encodings and frames + information about which cluster the frame is in
    fig = plt.figure(figsize=(10, 3), dpi=72)
    ax1 = fig.add_subplot(2, 2, 1)
    plt.axis('off')
    im = plt.imshow(enc_array[0][:8, :], vmin=-1, vmax=25)
    plt.title('Visual Encoding', fontsize=15)
    ax2 = fig.add_subplot(2, 2, 3)
    plt.axis('off')
    im2 = plt.imshow(enc_array[0][8:, :], vmin=-1, vmax=25)
    plt.title('Vector Encoding', fontsize=15)
    ax4 = fig.add_subplot(1, 2, 2)
    plt.axis('off')
    im4 = plt.imshow(image_array[0])
    ax3 = fig.add_subplot(6, 2, 2)
    im3 = plt.text(0.3,
                   0.1,
                   'Cluster ' + str(cluster[0]),
                   fontsize=20,
                   color='white',
                   bbox=dict(facecolor='blue', alpha=0.5))
    plt.axis('off')

    Writer = animation.writers['ffmpeg']
    writer = Writer(fps=10, metadata=dict(artist='Me'), bitrate=1800)

    def animate(i):
        im.set_array(enc_array[i][:8, :])
        im2.set_array(enc_array[i][8:, :])
        im3.set_text('Cluster ' + str(cluster[i]))
        im4.set_array(image_array[i])
        return (im, )

    anim = animation.FuncAnimation(fig, animate, frames=len(image_array))
    display(IPython_display.display_animation(anim))
    if save != None:
        anim.save(save, writer=writer)
Exemplo n.º 5
0
    def classification_slider(self, **args):
        # run logistic regression
        x = self.x_orig
        y = self.data[:, 1]

        ##### precomputations #####
        # precompute fits input
        x_fit = np.linspace(np.min(x) - 1, np.max(x) + 1, 100)

        # precompute surface
        costs1 = [v[0] for v in self.cost_history]
        costs2 = [v[1] for v in self.cost_history]

        a = np.linspace(min(costs1), max(costs1))
        b = np.linspace(min(costs2), max(costs2))

        s, t = np.meshgrid(a, b)
        shape = np.shape(s)
        s = np.reshape(s, (np.size(s), 1))
        t = np.reshape(t, (np.size(t), 1))

        # generate surface based on given data - done very lazily - recomputed each time
        g = 0
        P = len(y)
        for p in range(0, P):
            g += np.log(1 + np.exp(-y[p] * (s + t * x[p])))

        # reshape and plot the surface, as well as where the zero-plane is
        s.shape = (shape)
        t.shape = (shape)
        g.shape = (shape)

        ##### start plotting #####
        fig = plt.figure(num=None,
                         figsize=(12, 4),
                         dpi=80,
                         facecolor='w',
                         edgecolor='k')
        ax1 = plt.subplot(131)
        ax2 = plt.subplot(132)
        ax3 = plt.subplot(133, projection='3d')

        def show_fit(step):
            ### initialize plot data points and fit
            # initialize fit
            vals = self.cost_history[step]
            b = vals[0]
            w = vals[1]

            #### print left panel ####
            # transform input if needed for plotting
            y_fit = np.tanh(b + x_fit * w)

            # plot fit to data
            ax1.cla()
            ax1.plot(x_fit, y_fit, '-k', linewidth=3, zorder=0)

            # initialize points
            pos_inds = np.argwhere(y > 0)
            pos_inds = [v[0] for v in pos_inds]
            ax1.scatter(x[pos_inds],
                        y[pos_inds],
                        color='salmon',
                        linewidth=1,
                        marker='o',
                        edgecolor='k',
                        s=80)

            neg_inds = np.argwhere(y < 0)
            neg_inds = [v[0] for v in neg_inds]
            ax1.scatter(x[neg_inds],
                        y[neg_inds],
                        color='cornflowerblue',
                        linewidth=1,
                        marker='o',
                        edgecolor='k',
                        s=80)

            # clean up panel
            xgap = float(max(x) - min(x)) / float(10)
            ax1.set_xlim([min(x) - xgap, max(x) + xgap])
            ygap = float(max(y) - min(y)) / float(10)
            ax1.set_ylim([min(y) - ygap, max(y) + ygap])
            ax1.set_xticks([])
            ax1.set_yticks([])
            ax1.set_title('logistic fit')

            #### print middle panel ####
            # plot fit to data
            ax2.cla()
            y_fit = np.sign(b + x_fit * w)
            ax2.plot(x_fit, y_fit, '-k', linewidth=3, zorder=0)

            # initialize points
            pos_inds = np.argwhere(y > 0)
            pos_inds = [v[0] for v in pos_inds]
            ax2.scatter(x[pos_inds],
                        y[pos_inds],
                        color='salmon',
                        linewidth=1,
                        marker='o',
                        edgecolor='k',
                        s=80)

            neg_inds = np.argwhere(y < 0)
            neg_inds = [v[0] for v in neg_inds]
            ax2.scatter(x[neg_inds],
                        y[neg_inds],
                        color='cornflowerblue',
                        linewidth=1,
                        marker='o',
                        edgecolor='k',
                        s=80)

            # clean up panel
            xgap = float(max(x) - min(x)) / float(10)
            ax2.set_xlim([min(x) - xgap, max(x) + xgap])
            ygap = float(max(y) - min(y)) / float(10)
            ax2.set_ylim([min(y) - ygap, max(y) + ygap])
            ax2.set_xticks([])
            ax2.set_yticks([])
            ax2.set_title('final classifier')

            #### print right panel ####
            ax3.cla()
            ax3.plot_surface(s, t, g, alpha=0.15)
            ax3.plot_surface(s, t, g * 0, alpha=0.1)

            # plot all gradient descent steps faintly for visualization purposes
            bs = []
            ws = []
            costs = []
            for i in range(len(self.cost_history)):
                bwg = self.cost_history[i]
                b = bwg[0]
                w = bwg[1]
                cost = bwg[2]
                bs.append(b)
                ws.append(w)
                costs.append(cost)
            ax3.scatter(bs,
                        ws,
                        costs,
                        color='m',
                        marker='x',
                        linewidth=3,
                        alpha=0.1)

            # plot current gradient descent step in bright red
            b = vals[0]
            w = vals[1]
            cost = vals[2]
            artist = ax3.scatter(b,
                                 w,
                                 cost,
                                 marker='o',
                                 color='r',
                                 s=60,
                                 edgecolor='k',
                                 linewidth=4)

            # clean up panel
            ax3.view_init(args['view'][0], args['view'][1])
            ax3.set_xticks([])
            ax3.set_yticks([])
            ax3.set_zticks([])
            ax3.set_title('cost function')

            ax3.set_xlabel(args['xlabel'], fontsize=14, labelpad=-5)
            ax3.set_ylabel(args['ylabel'], fontsize=14, labelpad=-5)

            ax3.zaxis.set_rotate_label(False)  # disable automatic rotation
            ax3.set_zlabel('cost  ', fontsize=14, rotation=0, labelpad=1)

            return artist,

        anim = animation.FuncAnimation(fig,
                                       show_fit,
                                       frames=len(self.cost_history),
                                       interval=len(self.cost_history),
                                       blit=True)

        # set frames per second in animation
        IPython_display.anim_to_html(anim,
                                     fps=int(
                                         len(self.cost_history) / float(3)))

        return (anim)
Exemplo n.º 6
0
    def fitting_slider(self, **args):
        # pull out coordinates
        x_orig = self.x_orig
        x_tran = self.data[:, 0]
        y = self.data[:, 1]

        ##### precomputations #####
        # precompute fits input
        x_fit = np.linspace(np.min(x_orig) - 1, np.max(x_orig) + 1, 100)

        # precompute surface
        xs = max([abs(v[0]) for v in self.cost_history])
        ys = max([abs(v[1]) for v in self.cost_history])
        minval = min(-xs, -ys)
        maxval = max(xs, ys)
        gap = (maxval - minval) * 0.2
        r = np.linspace(minval - gap, maxval + gap)
        s, t = np.meshgrid(r, r)
        s = np.reshape(s, (np.size(s), 1))
        t = np.reshape(t, (np.size(t), 1))

        # generate surface based on given data - done very lazily - recomputed each time
        g = 0
        P = len(y)
        if args['fit_type'] == 'line fit' or args['fit_type'] == 'sine fit':
            for p in range(0, P):
                g += (s + t * x_tran[p] - y[p])**2
        if args['fit_type'] == 'logistic fit':
            for p in range(0, P):
                g += np.log(1 + np.exp(-y[p] * (s + t * x_tran[p])))

        # reshape and plot the surface, as well as where the zero-plane is
        s.shape = (np.size(r), np.size(r))
        t.shape = (np.size(r), np.size(r))
        g.shape = (np.size(r), np.size(r))

        # setup figure to plot
        fig = plt.figure(num=None,
                         figsize=(12, 4),
                         dpi=80,
                         facecolor='w',
                         edgecolor='k')
        ax1 = plt.subplot(121)
        ax2 = plt.subplot(122, projection='3d')

        # slider mechanism
        def show_fit(step):
            ### initialize plot data points and fit
            # initialize fit
            vals = self.cost_history[step]
            b = vals[0]
            w = vals[1]
            yfit = 0
            # transform input if needed for plotting
            if args['fit_type'] == 'line fit':
                y_fit = b + x_fit * w
            if args['fit_type'] == 'sine fit':
                y_fit = b + np.sin(2 * np.pi * x_fit) * w
            if args['fit_type'] == 'logistic fit':
                y_fit = np.tanh(b + x_fit * w)

            # plot fit to data
            ax1.cla()
            ax1.plot(x_fit, y_fit, '-r', linewidth=3)

            # initialize points
            ax1.scatter(x_orig, y)

            # clean up panel
            xgap = float(max(x_orig) - min(x_orig)) / float(10)
            ax1.set_xlim([min(x_orig) - xgap, max(x_orig) + xgap])
            ygap = float(max(y) - min(y)) / float(10)
            ax1.set_ylim([min(y) - ygap, max(y) + ygap])
            ax1.set_xticks([])
            ax1.set_yticks([])

            ### plot surface
            ax2.cla()
            artist = ax2.plot_surface(s, t, g, alpha=0.15)
            ax2.plot_surface(s, t, g * 0, alpha=0.1)

            # plot all gradient descent steps faintly for visualization purposes
            bs = []
            ws = []
            costs = []
            for i in range(len(self.cost_history)):
                bwg = self.cost_history[i]
                b = bwg[0]
                w = bwg[1]
                cost = bwg[2]
                bs.append(b)
                ws.append(w)
                costs.append(cost)
            ax2.scatter(bs,
                        ws,
                        costs,
                        color='m',
                        marker='x',
                        linewidth=3,
                        alpha=0.1)

            # plot current gradient descent step in bright red
            b = vals[0]
            w = vals[1]
            cost = vals[2]
            ax2.scatter(b,
                        w,
                        cost,
                        marker='o',
                        color='r',
                        s=50,
                        edgecolor='k',
                        linewidth=1)

            # clean up panel
            ax2.view_init(args['view'][0], args['view'][1])
            ax2.set_xticks([])
            ax2.set_yticks([])
            ax2.set_zticks([])

            ax2.set_xlabel(args['xlabel'], fontsize=14, labelpad=-5)
            ax2.set_ylabel(args['ylabel'], fontsize=14, labelpad=-5)

            ax2.zaxis.set_rotate_label(False)  # disable automatic rotation
            ax2.set_zlabel('cost  ', fontsize=14, rotation=0, labelpad=1)

            return artist,

        anim = animation.FuncAnimation(fig,
                                       show_fit,
                                       frames=len(self.cost_history),
                                       interval=len(self.cost_history),
                                       blit=True)

        # set frames per second in animation
        IPython_display.anim_to_html(anim,
                                     fps=int(
                                         len(self.cost_history) / float(3)))

        return (anim)
Exemplo n.º 7
0
    def animate_training_runs(self, **args):
        grid = args['gridworld']
        episodes = args['episodes']
        learner = args['learner']

        # make local copies of input
        training_episodes_history = learner.training_episodes_history

        # initialize figure
        fsize = 3
        if grid.width:
            fsize = 5
        fig = plt.figure(figsize=(12, fsize))
        axs = []
        for i in range(len(episodes)):
            ax = fig.add_subplot(1, len(episodes), i + 1, aspect='equal')
            axs.append(ax)

        if len(episodes) == 1:
            axs = np.array(axs)

        # compute maximum length of episodes animated
        max_len = 0
        for key in episodes:
            l = len(training_episodes_history[str(key)])
            if l > max_len:
                max_len = l

        # loop over the episode histories and plot the results
        print 'animating run...'

        def show_episode(step):
            # loop over subplots and plot current step of each episode history
            artist = fig

            for k in range(len(axs)):
                ax = axs[k]

                # take correct episode
                episode_num = episodes[k]
                current_episode = training_episodes_history[str(episode_num)]

                # define new location of agent
                grid.agent = current_episode[min(step,
                                                 len(current_episode) - 1)]

                # color gridworld for this episode and step
                if 'lights' not in args:
                    grid.color_gridworld(ax=ax)
                else:
                    grid.color_gridworld(ax=ax, lights=args['lights'])

                ax.set_title('episode = ' + str(episode_num + 1))

            return artist,

        # create animation object
        anim = animation.FuncAnimation(fig,
                                       show_episode,
                                       frames=min(100, max_len),
                                       interval=min(100, max_len),
                                       blit=True)

        # set frames per second in animation
        IPython_display.anim_to_html(anim, fps=min(100, max_len) / float(10))

        print '...done!'
        time.sleep(1)
        clear_output()

        return (anim)
Exemplo n.º 8
0
    def animate_validation_runs(self, **args):
        gridworld = args['gridworld']
        learner = args['learner']
        starting_locations = args['starting_locations']

        # make local copies of input
        grid = gridworld
        Q = learner.Q
        starting_locs = starting_locations

        # initialize figure
        fsize = 3
        if grid.width:
            fsize = 5
        fig = plt.figure(figsize=(12, fsize))
        axs = []
        for i in range(len(starting_locs)):
            ax = fig.add_subplot(1, len(starting_locs), i + 1, aspect='equal')
            axs.append(ax)

        # only one added subplot, axs must be in array format
        if len(starting_locs) == 1:
            axs = np.array(axs)

        ### produce validation runs ###
        print 'animating run...'
        validation_run_history = []
        for i in range(len(starting_locs)):
            # take random starting point - for short just from validation schedule
            grid.agent = starting_locs[i]

            # loop over max number of steps and try reach goal
            episode_path = []
            for j in range(grid.max_steps):
                # store current location
                episode_path.append(grid.agent)

                ### if you reach the goal end current episode immediately
                if grid.agent == grid.goal:
                    break

                # translate current agent location tuple into index
                s_k_1 = grid.state_tuple_to_index(grid.agent)

                # get action
                a_k = grid.get_action(method='optimal', Q=Q)

                # move based on this action - if move takes you out of gridworld don't move and instead move randomly
                s_k = grid.get_movin(action=a_k,
                                     illegal_move_response='random')

                # record next step in path
                grid.agent = grid.state_index_to_tuple(state_index=s_k)

            # record this episode's path
            validation_run_history.append(episode_path)

        ### compute maximum length of episodes animated ###
        max_len = 0
        for i in range(len(starting_locs)):
            l = len(validation_run_history[i])
            if l > max_len:
                max_len = l

        ### loop over the episode histories and plot the results ###
        def show_episode(step):
            # loop over subplots and plot current step of each episode history
            artist = fig

            for k in range(len(axs)):
                ax = axs[k]

                # take correct episode
                current_episode = validation_run_history[k]

                # define new location of agent
                loc = current_episode[min(step, len(current_episode) - 1)]
                grid.agent = loc

                # color gridworld for this episode and step
                if 'lights' not in args:
                    grid.color_gridworld(ax=ax)
                else:
                    grid.color_gridworld(ax=ax, lights=args['lights'])

                ax.set_title('fully trained run ' + str(k + 1))
                # fig.subplots_adjust(left=0,right=1,bottom=0,top=1)  ## gets rid of the white space around image

            return artist,

        # create animation object
        anim = animation.FuncAnimation(fig,
                                       show_episode,
                                       frames=min(100, max_len),
                                       interval=min(100, max_len),
                                       blit=True)

        # set frames per second in animation
        IPython_display.anim_to_html(anim, fps=min(100, max_len) / float(10))

        print '...done!'
        time.sleep(1)
        clear_output()

        return (anim)
Exemplo n.º 9
0
    def animate_training_comparison(self, **args):
        grid = args['gridworld']
        learner_1 = args['learner_1']
        learner_2 = args['learner_2']
        episode = args['episode']

        # make local copies of input
        training_episodes_history_v1 = learner_1.training_episodes_history
        training_episodes_history_v2 = learner_2.training_episodes_history

        # initialize figure
        fsize = 3
        if grid.width > 10:
            fsize = 5
        fig = plt.figure(figsize=(12, fsize))
        axs = []
        for i in range(2):
            ax = fig.add_subplot(1, 2, i + 1, aspect='equal')
            axs.append(ax)

        # compute maximum length of episodes animated
        max_len = 0
        key = episode
        L1 = len(training_episodes_history_v1[str(key)])
        L2 = len(training_episodes_history_v2[str(key)])
        max_len = max(L1, L2)

        # loop over the episode histories and plot the results
        print 'animating run...'
        rewards = np.zeros((2, 1))

        def show_episode(step):
            # loop over subplots and plot current step of each episode history
            artist = fig
            for k in range(len(axs)):
                ax = axs[k]

                # take correct episode
                current_episode = 0
                if k == 0:
                    current_episode = training_episodes_history_v1[str(key)]
                else:
                    current_episode = training_episodes_history_v2[str(key)]

                # define new location of agent
                grid.agent = current_episode[min(step,
                                                 len(current_episode) - 1)]

                # color gridworld for this episode and step
                if 'lights' not in args:
                    grid.color_gridworld(ax=ax)
                else:
                    grid.color_gridworld(ax=ax, lights=args['lights'])

                # set title
                if k == 0:
                    ax.set_title('random')
                else:
                    ax.set_title('exploration/exploitation')
            return artist,

        # create animation object
        anim = animation.FuncAnimation(fig,
                                       show_episode,
                                       frames=min(100, max_len),
                                       interval=min(100, max_len),
                                       blit=True)

        # set frames per second in animation
        IPython_display.anim_to_html(anim, fps=min(100, max_len) / float(10))

        print '...done!'
        time.sleep(1)
        clear_output()

        return (anim)
Exemplo n.º 10
0
def plot_movie_jsInfo(enc_array, image_array, acts, vals, rews, save=None):
    def getImage(act, num):
        if act == 0:
            return stand
        if num == 0:
            if act == 1:
                return up
            if act == 2:
                return down
        if num == 1:
            if act == 1:
                return turn_l
            if act == 2:
                return turn_r
        if num == 2 and act == 1:
            return jump
        if num == 3:
            if act == 1:
                return right
            if act == 2:
                return left

    jump = imageio.imread('./symbols/jump.png')
    left = imageio.imread('./symbols/arrow-left.png')
    right = imageio.imread('./symbols/arrow_right.png')
    down = imageio.imread('./symbols/down-arrow.png')
    up = imageio.imread('./symbols/up-arrow.png')
    turn_l = imageio.imread('./symbols/turn-left.png')
    turn_r = imageio.imread('./symbols/turn-right.png')
    stand = imageio.imread('./symbols/Stand.png')

    fig = plt.figure(figsize=(10, 3), dpi=72)
    ax1 = fig.add_subplot(2, 2, 1)
    plt.axis('off')
    if not isinstance(enc_array, list):
        im = plt.imshow(enc_array[0][:8, :], vmin=-1, vmax=25)
        plt.title('Visual Encoding', fontsize=15)
    else:
        icaLen = enc_array[0][0].shape[0]
        im = plt.imshow(enc_array[0][0].reshape(1, icaLen), vmin=-1, vmax=25)
        plt.title('Visual Encoding - ICs', fontsize=15)
    ax2 = fig.add_subplot(2, 2, 3)
    plt.axis('off')
    if not isinstance(enc_array, list):
        im2 = plt.imshow(enc_array[0][8:, :], vmin=-1, vmax=25)
        plt.title('Vector Encoding', fontsize=15)
    else:
        im2 = plt.imshow(enc_array[1][0].reshape(1, icaLen), vmin=-1, vmax=25)
        plt.title('Vector Encoding - ICs', fontsize=15)
    ax4 = fig.add_subplot(1, 2, 2)
    im4 = plt.imshow(image_array[0])
    plt.axis('off')
    ax3 = fig.add_subplot(6, 2, 2)
    im3 = plt.text(0.2,
                   0.1,
                   "R: " + str(rews[0]) + ' V: ' + str(vals[0]),
                   fontsize=15,
                   color='white',
                   bbox=dict(facecolor='blue', alpha=0.5))
    plt.axis('off')

    ax5 = fig.add_subplot(4, 10, 10)
    im5 = plt.imshow(getImage(acts[0][0][0], 0))
    plt.axis('off')
    ax6 = fig.add_subplot(4, 10, 20)
    im6 = plt.imshow(getImage(acts[0][0][1], 1))
    plt.axis('off')
    ax7 = fig.add_subplot(4, 10, 30)
    im7 = plt.imshow(getImage(acts[0][0][2], 2))
    plt.axis('off')
    ax8 = fig.add_subplot(4, 10, 40)
    im8 = plt.imshow(getImage(acts[0][0][3], 3))
    plt.axis('off')

    Writer = animation.writers['ffmpeg']
    writer = Writer(fps=10, metadata=dict(artist='Me'), bitrate=1800)

    def animate(i):
        if not isinstance(enc_array, list):
            im.set_array(enc_array[i][:8, :])
            im2.set_array(enc_array[i][8:, :])
        else:
            im.set_array(enc_array[0][i].reshape(1, icaLen))
            im2.set_array(enc_array[1][i].reshape(1, icaLen))
        im3.set_text("R: " + str(rews[i])[:3] + ' V: ' +
                     str(vals[i][0][0])[:4])
        im4.set_array(image_array[i])
        im5.set_array(getImage(acts[i][0][0], 0))
        im6.set_array(getImage(acts[i][0][1], 1))
        im7.set_array(getImage(acts[i][0][2], 2))
        im8.set_array(getImage(acts[i][0][3], 3))
        return (im, )

    anim = animation.FuncAnimation(fig, animate, frames=len(image_array))
    display(IPython_display.display_animation(anim))
    if save != None:
        anim.save(save, writer=writer)
Exemplo n.º 11
0
def plot_movie_semantic2(semantic, image_array, acts, vals, rews, save=None):
    label_dict = {
        "Unknown": 0,
        "Agent": 1,
        "Level Door": 2,
        "Regular Door": 3,
        "Key Door": 4,
        "Entry Door": 5,
        "Puzzle Door": 6,
        "Key": 7,
        "Time Orb": 8,
        "Wall": 9,
        "Floor": 10
    }
    inv_map = {v: k for k, v in label_dict.items()}

    def getImage(act, num):
        if act == 0:
            return stand
        if num == 0:
            if act == 1:
                return up
            if act == 2:
                return down
        if num == 1:
            if act == 1:
                return turn_l
            if act == 2:
                return turn_r
        if num == 2 and act == 1:
            return jump
        if num == 3:
            if act == 1:
                return right
            if act == 2:
                return left

    jump = imageio.imread('./symbols/jump.png')
    left = imageio.imread('./symbols/arrow-left.png')
    right = imageio.imread('./symbols/arrow_right.png')
    down = imageio.imread('./symbols/down-arrow.png')
    up = imageio.imread('./symbols/up-arrow.png')
    turn_l = imageio.imread('./symbols/turn-left.png')
    turn_r = imageio.imread('./symbols/turn-right.png')
    stand = imageio.imread('./symbols/Stand.png')

    fig = plt.figure(figsize=(10, 3), dpi=72)
    ax1 = fig.add_subplot(1, 2, 1)
    plt.axis('off')
    im1 = plt.imshow(rgb2L(semantic[0]), vmin=0, vmax=11, cmap='tab20')
    values = np.linspace(0, 10, 11)
    colors = [im1.cmap(im1.norm(value)) for value in values]
    patches = [
        mpatches.Patch(color=colors[i], label=inv_map[values[i]])
        for i in range(len(values))
    ]
    plt.legend(handles=patches,
               bbox_to_anchor=(1.05, 1),
               loc=2,
               borderaxespad=0.)

    ax4 = fig.add_subplot(1, 2, 2)
    im4 = plt.imshow(image_array[0])
    plt.axis('off')
    ax3 = fig.add_subplot(6, 2, 2)
    im3 = plt.text(0.2,
                   0.1,
                   "0 R: " + str(rews[0]) + ' V: ' + str(vals[0]),
                   fontsize=15,
                   color='white',
                   bbox=dict(facecolor='blue', alpha=0.5))
    plt.axis('off')

    ax5 = fig.add_subplot(4, 10, 10)
    im5 = plt.imshow(getImage(acts[0][0][0], 0))
    plt.axis('off')
    ax6 = fig.add_subplot(4, 10, 20)
    im6 = plt.imshow(getImage(acts[0][0][1], 1))
    plt.axis('off')
    ax7 = fig.add_subplot(4, 10, 30)
    im7 = plt.imshow(getImage(acts[0][0][2], 2))
    plt.axis('off')
    ax8 = fig.add_subplot(4, 10, 40)
    im8 = plt.imshow(getImage(acts[0][0][3], 3))
    plt.axis('off')

    Writer = animation.writers['ffmpeg']
    writer = Writer(fps=10, metadata=dict(artist='Me'), bitrate=1800)

    def animate(i):
        try:
            im1.set_array(rgb2L(semantic[i]))
        except:
            print(str(i) + " - " + str(broken.shape))

        im3.set_text(
            str(i) + " R: " + str(rews[i])[:3] + ' V: ' +
            str(vals[i][0][0])[:4])
        im4.set_array(image_array[i])
        im5.set_array(getImage(acts[i][0][0], 0))
        im6.set_array(getImage(acts[i][0][1], 1))
        im7.set_array(getImage(acts[i][0][2], 2))
        im8.set_array(getImage(acts[i][0][3], 3))
        return (im1, )

    anim = animation.FuncAnimation(fig, animate, frames=len(image_array))
    display(IPython_display.display_animation(anim))
    if save != None:
        anim.save(save, writer=writer)
Exemplo n.º 12
0
def plot_movie_semantic(semantic, image_array, acts, vals, rews, save=None):
    def getImage(act, num):
        if act == 0:
            return stand
        if num == 0:
            if act == 1:
                return up
            if act == 2:
                return down
        if num == 1:
            if act == 1:
                return turn_l
            if act == 2:
                return turn_r
        if num == 2 and act == 1:
            return jump
        if num == 3:
            if act == 1:
                return right
            if act == 2:
                return left

    jump = imageio.imread('./symbols/jump.png')
    left = imageio.imread('./symbols/arrow-left.png')
    right = imageio.imread('./symbols/arrow_right.png')
    down = imageio.imread('./symbols/down-arrow.png')
    up = imageio.imread('./symbols/up-arrow.png')
    turn_l = imageio.imread('./symbols/turn-left.png')
    turn_r = imageio.imread('./symbols/turn-right.png')
    stand = imageio.imread('./symbols/Stand.png')

    fig = plt.figure(figsize=(10, 3), dpi=72)
    ax1 = fig.add_subplot(1, 2, 1)
    plt.axis('off')
    lbls = np.rot90(
        np.array([int(n) for n in semantic[0][1:-4].split(",")]).reshape(
            (128, 128)))
    im1 = plt.imshow(lbls, vmin=-1, vmax=10, cmap='tab20')
    values = np.linspace(-1, 10, 12)
    colors = [im1.cmap(im1.norm(value)) for value in values]
    patches = [
        mpatches.Patch(color=colors[i], label=inv_map[values[i]])
        for i in range(len(values))
    ]
    plt.legend(handles=patches,
               bbox_to_anchor=(1.05, 1),
               loc=2,
               borderaxespad=0.)

    ax4 = fig.add_subplot(1, 2, 2)
    im4 = plt.imshow(image_array[0])
    plt.axis('off')
    ax3 = fig.add_subplot(6, 2, 2)
    im3 = plt.text(0.2,
                   0.1,
                   "R: " + str(rews[0]) + ' V: ' + str(vals[0]),
                   fontsize=15,
                   color='white',
                   bbox=dict(facecolor='blue', alpha=0.5))
    plt.axis('off')

    ax5 = fig.add_subplot(4, 10, 10)
    im5 = plt.imshow(getImage(acts[0][0][0], 0))
    plt.axis('off')
    ax6 = fig.add_subplot(4, 10, 20)
    im6 = plt.imshow(getImage(acts[0][0][1], 1))
    plt.axis('off')
    ax7 = fig.add_subplot(4, 10, 30)
    im7 = plt.imshow(getImage(acts[0][0][2], 2))
    plt.axis('off')
    ax8 = fig.add_subplot(4, 10, 40)
    im8 = plt.imshow(getImage(acts[0][0][3], 3))
    plt.axis('off')

    Writer = animation.writers['ffmpeg']
    writer = Writer(fps=10, metadata=dict(artist='Me'), bitrate=1800)

    def animate(i):
        try:
            lbls = np.rot90(
                np.array([int(n)
                          for n in semantic[i][1:-4].split(",")]).reshape(
                              (128, 128)))
            im1.set_array(lbls)
        except:
            broken = np.array([int(n) for n in data[i][1:-4].split(",")])
            lbls = np.rot90(
                np.append(broken,
                          np.zeros((128 * 128) - broken.shape[0])).reshape(
                              (128, 128)))
            im1.set_array(lbls)
            print(str(i) + " - " + str(broken.shape))

        im3.set_text("R: " + str(rews[i])[:3] + ' V: ' +
                     str(vals[i][0][0])[:4])
        im4.set_array(image_array[i])
        im5.set_array(getImage(acts[i][0][0], 0))
        im6.set_array(getImage(acts[i][0][1], 1))
        im7.set_array(getImage(acts[i][0][2], 2))
        im8.set_array(getImage(acts[i][0][3], 3))
        return (im1, )

    anim = animation.FuncAnimation(fig, animate, frames=len(image_array))
    display(IPython_display.display_animation(anim))
    if save != None:
        anim.save(save, writer=writer)
Exemplo n.º 13
0
def plot_movie_curInfo(enc_array,
                       image_array,
                       pred_s,
                       enc_cur,
                       acts,
                       vals,
                       rews,
                       range_vis,
                       range_vec,
                       save=None):

    fig = plt.figure(figsize=(10, 3), dpi=72)
    ax1 = fig.add_subplot(3, 3, 1)
    plt.axis('off')
    if not isinstance(enc_array, list):
        im = plt.imshow(enc_array[0][:4, :],
                        vmin=range_vis[0],
                        vmax=range_vis[1])
        plt.title('Visual Encoding', fontsize=15)
    else:
        icaLen = enc_array[0][0].shape[0]
        im = plt.imshow(enc_array[0][0].reshape(1, icaLen))
        plt.title('Visual Encoding - ICs', fontsize=15)
    ax2 = fig.add_subplot(3, 3, 2)
    plt.axis('off')
    if not isinstance(enc_array, list):
        im2 = plt.imshow(enc_array[0][4:, :],
                         vmin=range_vec[0],
                         vmax=range_vec[1])
        plt.title('Vector Encoding', fontsize=15)
    else:
        im2 = plt.imshow(enc_array[1][0].reshape(1, icaLen))
        plt.title('Vector Encoding - ICs', fontsize=15)

    ax5 = fig.add_subplot(3, 3, 4)
    plt.axis('off')
    im5 = plt.imshow(pred_s[0][:4, :], vmin=range_vis[0], vmax=range_vis[1])
    plt.title('Predicted')

    ax6 = fig.add_subplot(3, 3, 5)
    plt.axis('off')
    im6 = plt.imshow(pred_s[0][4:, :], vmin=range_vec[0], vmax=range_vec[1])
    plt.title('Predicted')

    ax7 = fig.add_subplot(3, 3, 7)
    plt.axis('off')
    im7 = plt.imshow(enc_cur[0][:4, :], vmin=range_vis[0], vmax=range_vis[1])
    plt.title('Actual')

    ax8 = fig.add_subplot(3, 3, 8)
    plt.axis('off')
    im8 = plt.imshow(enc_cur[0][4:, :], vmin=range_vec[0], vmax=range_vec[1])
    plt.title('Actual')

    ax4 = fig.add_subplot(1, 3, 3)
    im4 = plt.imshow(image_array[0])
    plt.axis('off')
    ax3 = fig.add_subplot(6, 3, 3)
    im3 = plt.text(0.2,
                   0.1,
                   "R: " + str(rews[0]) + ' V: ' + str(vals[0]),
                   fontsize=15,
                   color='white',
                   bbox=dict(facecolor='blue', alpha=0.5))
    plt.axis('off')

    Writer = animation.writers['ffmpeg']
    writer = Writer(fps=10, metadata=dict(artist='Me'), bitrate=1800)

    def animate(i):
        if not isinstance(enc_array, list):
            im.set_array(enc_array[i][:4, :])
            im2.set_array(enc_array[i][4:, :])
        else:
            im.set_array(enc_array[0][i].reshape(1, icaLen))
            im2.set_array(enc_array[1][i].reshape(1, icaLen))
        im3.set_text("R: " + str(rews[i])[:3] + ' V: ' +
                     str(vals[i][0][0])[:4])
        im4.set_array(image_array[i])

        im5.set_array(pred_s[i][:4, :])
        im6.set_array(pred_s[i][4:, :])
        im7.set_array(enc_cur[i][:4, :])
        im8.set_array(enc_cur[i][4:, :])
        return (im, )

    anim = animation.FuncAnimation(fig, animate, frames=len(image_array))
    display(IPython_display.display_animation(anim))
    if save != None:
        anim.save(save, writer=writer)
Exemplo n.º 14
0
    def transformation_slider(self):  
        
        print "rendering JS animation, this can take several minutes..."
        
        #### make points start and end
        X1 = self.orig_data
        X2 = self.transformed_data
        
        ### make separator start and end
        r = np.linspace(-10.1,10.1,2000)
        a,b = np.meshgrid(r,r)
        a = np.reshape(a,(np.size(a),1))
        b = np.reshape(b,(np.size(b),1))
        h = np.concatenate((a,b),axis = 1)

        ### use rule to partition the input space
        f1,f2 = self.make_rule(h)
        z = f1 + f2
        z = (np.sign(z))
        a.shape = (np.size(r),np.size(r))
        b.shape = (np.size(r),np.size(r))
        z.shape = (np.size(r),np.size(r))
        f1.shape = (np.size(r),np.size(r))
        f2.shape = (np.size(r),np.size(r))
        
        #### make grid start and end 
        grid1 = self.grid
        grida,gridb = self.make_rule(self.grid)
        grid2 = np.concatenate((grida,gridb),axis = 1)
           
        # make figure
        fig = plt.figure(figsize = (5,5))
        ax1 = fig.add_subplot(111)          # panel for original space
        def show_fit(p):
            ax1.cla()
            
            ## make alpha
            alpha = p/float(10)

            #### setup current points and print
            T = (1-alpha)*X1 + alpha*X2
            
            # custom colors
            red = 'salmon'    # custom color for plotting purposes
            blue = 'cornflowerblue'  # custom color for plotting purposes

            # plot points on desired panel
            ax1.scatter(T[self.ind0,0],T[self.ind0,1],s = 60, color = blue, edgecolor = 'k')
            artist = ax1.scatter(T[self.ind1,0],T[self.ind1,1],s = 60, color = red, edgecolor = 'k')
            
            #### setup separator and print
            sep1 = (1-alpha)*a + alpha*f1
            sep2 = (1-alpha)*b + alpha*f2
            
            # the cntr command grabs a contour without plotting it
            c = cntr.Cntr(sep1, sep2, z)
            res = c.trace(0)         # here trace grabs a contour at slice z = value

            # plot points
            for k in range(len(res) - 1):
                data = res[k]            # extract the right array from the trace object
                ax1.plot(data[:,0],data[:,1],'k', linewidth = 3)
            
            ### setup current grid and print
            grid = (1-alpha)*grid1 + alpha*grid2
            
            # plot points
            for i in range(80):
                ax1.plot(grid[200*i:(i+1)*200,0],grid[200*i:(i+1)*200,1],color = [0.75,0.75,0.75],linewidth = 1,zorder = 0)   
                    
            ### set axis limits for current dataset
            hgap = (max(T[:,0]) - min(T[:,0]))*0.05
            vgap = (max(T[:,1]) - min(T[:,1]))*0.05
            ax1.set_xlim([min(T[:,0])-hgap,max(T[:,0])+hgap])
            ax1.set_ylim([min(T[:,1])-vgap,max(T[:,1])+vgap])
            ax1.axis('off')
            fig.subplots_adjust(left=0,right=1,bottom=0,top=1)  ## gets rid of the white space around image
            return artist,  
        
        anim = animation.FuncAnimation(fig, show_fit,frames=10, interval=5, blit=True)
        
        # set frames per second in animation
        IPython_display.anim_to_html(anim,fps = 2)
        
        return(anim)
Exemplo n.º 15
0
    def animate_fit(self, **args):
        # run logistic regression
        classifier = demo_logistic_regression()
        self.w, self.cost_history = classifier.fit(self.X, self.y, **args)

        # setup range for plots
        costs = [v[-1] for v in self.cost_history]
        X = self.X
        y = self.y
        xgap = float(max(X[1, :]) - min(X[1, :])) / float(10)
        ygap = float(max(X[2, :]) - min(X[2, :])) / float(10)
        cgap = float(max(costs) - min(costs)) / float(10)

        # take positive and negative label indicies
        pos_inds = np.argwhere(y > 0)
        pos_inds = [v[0] for v in pos_inds]

        neg_inds = np.argwhere(y < 0)
        neg_inds = [v[0] for v in neg_inds]

        # setup surface data for left and middle plot
        colors = ['salmon', 'cornflowerblue']
        a = np.linspace(np.min(X[:, 0]) - xgap, np.max(X[:, 0]) + xgap, 200)
        b = np.linspace(np.min(X[:, 1]) - ygap, np.max(X[:, 1]) + ygap, 200)

        s, t = np.meshgrid(a, b)
        shape = np.shape(s)

        s = np.reshape(s, (np.size(s), 1))
        t = np.reshape(t, (np.size(t), 1))
        h = np.concatenate((s, t), 1)

        s.shape = (shape)
        t.shape = (shape)

        # get kernelized data
        if "kernel" in args:
            h = classifier.kernelize_test(h)
        else:
            o = np.ones((np.shape(h)[0], 1))
            h = np.concatenate((o, h), 1)

        # plotting
        fig = plt.figure(num=None,
                         figsize=(15, 5),
                         dpi=80,
                         facecolor='w',
                         edgecolor='k')
        ax1 = plt.subplot(131)
        ax2 = plt.subplot(132, projection='3d')
        ax3 = plt.subplot(133)

        # slider mechanism
        def show_fit(step):
            #### print left panel ####
            ax1.cla()
            ax1.scatter(X[pos_inds, 0],
                        X[pos_inds, 1],
                        color=colors[0],
                        linewidth=1,
                        marker='o',
                        edgecolor='k',
                        s=80)
            ax1.scatter(X[neg_inds, 0],
                        X[neg_inds, 1],
                        color=colors[1],
                        linewidth=1,
                        marker='o',
                        edgecolor='k',
                        s=80)

            # print approximation
            w_now = self.cost_history[step][:-1]
            w_now = np.asarray([u[0] for u in w_now])
            w_now.shape = (len(w_now), 1)

            z = np.tanh(np.dot(h, w_now))
            z2 = np.sign(np.copy(z))
            z2.shape = (shape)

            unique_labels = np.unique(y)
            levels = unique_labels
            ax1.contourf(s, t, z2, colors=colors, alpha=0.2)

            # show the classification boundary if it exists
            if len(np.unique(z2)) > 1:
                ax1.contour(s,
                            t,
                            z2,
                            colors='k',
                            linewidths=2.5,
                            levels=unique_labels)

            # clean up panel
            ax1.set_xlim([min(X[:, 0]) - xgap, max(X[:, 0]) + xgap])
            ax1.set_ylim([min(X[:, 1]) - ygap, max(X[:, 1]) + ygap])
            ax1.set_xticks([])
            ax1.set_yticks([])

            #### print middle panel ####
            ax2.cla()

            # compute surface - due to bug in matplotlib surface will sometimes appear in front of points, sometimes behind, depending on angle and regardless of zorder setting
            z.shape = (shape)
            ax2.plot_surface(s, t, z, alpha=0.1, color='k', zorder=0)

            # plot points
            ax2.scatter(X[pos_inds, 0],
                        X[pos_inds, 1],
                        y[pos_inds],
                        color=colors[0],
                        linewidth=1,
                        marker='o',
                        edgecolor='k',
                        s=80,
                        zorder=1)
            ax2.scatter(X[neg_inds, 0],
                        X[neg_inds, 1],
                        y[neg_inds],
                        color=colors[1],
                        linewidth=1,
                        marker='o',
                        edgecolor='k',
                        s=80,
                        zorder=1)

            ## clean up panel
            # set viewing angle
            ax2.view_init(args['view'][0], args['view'][1])

            # set viewing limits
            ax2.set_xlim([min(X[:, 0]) - xgap, max(X[:, 0]) + xgap])
            ax2.set_ylim([min(X[:, 1]) - ygap, max(X[:, 1]) + ygap])

            # turn off tick labels
            ax2.set_xticklabels([])
            ax2.set_yticklabels([])
            ax2.set_zticklabels([])

            # Get rid of the spines on the 3d plot
            ax2.w_xaxis.line.set_color((1.0, 1.0, 1.0, 0.0))
            ax2.w_yaxis.line.set_color((1.0, 1.0, 1.0, 0.0))
            ax2.w_zaxis.line.set_color((1.0, 1.0, 1.0, 0.0))

            # turn off tick marks
            ax2.xaxis.set_tick_params(size=0, color='w')
            ax2.yaxis.set_tick_params(size=0, color='w')
            ax2.zaxis.set_tick_params(size=0, color='w')

            #### print right panel ####
            # print all of the step cost function values lightly
            ax3.cla()
            ax3.plot(costs, color='m', marker='o', linewidth=3, alpha=0.05)

            # print current value
            artist = ax3.scatter(step,
                                 costs[step],
                                 marker='o',
                                 color='r',
                                 s=60,
                                 edgecolor='k',
                                 linewidth=2)

            # dress up plot
            ax3.set_xlabel('step', fontsize=15, labelpad=5)
            ax3.set_ylabel('cost function value',
                           fontsize=12,
                           rotation=90,
                           labelpad=5)
            ax3.set_ylim(min(costs) - cgap, max(costs) + cgap)
            #             ax3.set_xticks([])
            #             ax3.set_yticks([])

            return artist,

        anim = animation.FuncAnimation(fig,
                                       show_fit,
                                       frames=len(self.cost_history),
                                       interval=len(self.cost_history),
                                       blit=True)

        # set frames per second in animation
        IPython_display.anim_to_html(anim,
                                     fps=int(
                                         len(self.cost_history) / float(3)))

        return (anim)